Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed leading comments lost from an expression when excessive parentheses are removed from it ([#530](https://github.com/JohnnyMorganz/StyLua/issues/530))
- Fixed comments present in a complex expression not forcing multiline hanging leading to a syntax error ([#524](https://github.com/JohnnyMorganz/StyLua/issues/524))
- Fixed unnecessary break on `else` in an if-expression when the expression contains a comment ([#520](https://github.com/JohnnyMorganz/StyLua/issues/520))
- Take into account the extra line created when hanging at equals token in an assignment. This should prevent unnecessary hanging ([#542](https://github.com/JohnnyMorganz/StyLua/issues/542))
- Take into account the extra line created when hanging at equals token in an assignment. This should prevent unnecessary hanging ([#542](https://github.com/JohnnyMorganz/StyLua/issues/542))
- Fixed comments added to a newly created trailing comment not being formatted ([#547](https://github.com/JohnnyMorganz/StyLua/issues/547))
- Fixed call chain with a small prefix not being kept inlined causing unstable formatting ([#514](https://github.com/JohnnyMorganz/StyLua/issues/514))
- Fix shape computation for table fields causing unnecessary expansion ([#551](https://github.com/JohnnyMorganz/StyLua/issues/551))
- Fixed shape computation for table fields causing unnecessary expansion ([#551](https://github.com/JohnnyMorganz/StyLua/issues/551))
- Fixed hanging the prefix string in `("str"):call` unnecessarily when it provides no benefit ([#508](https://github.com/JohnnyMorganz/StyLua/issues/508))

## [0.14.2] - 2022-07-27

Expand Down
11 changes: 10 additions & 1 deletion src/formatters/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,15 @@ pub fn format_index(ctx: &Context, index: &Index, shape: Shape) -> Index {
}
}

// Checks if this is a string (allows strings wrapped in parentheses)
fn is_string(expression: &Expression) -> bool {
match expression {
Expression::Value { value, .. } => matches!(&**value, Value::String(_)),
Expression::Parentheses { expression, .. } => is_string(expression),
_ => false,
}
}

/// Formats a Prefix Node
pub fn format_prefix(ctx: &Context, prefix: &Prefix, shape: Shape) -> Prefix {
match prefix {
Expand All @@ -376,7 +385,7 @@ pub fn format_prefix(ctx: &Context, prefix: &Prefix, shape: Shape) -> Prefix {
format_expression_internal(ctx, expression, ExpressionContext::Prefix, shape);
let singeline_shape = shape.take_first_line(&strip_trivia(&singleline_format));

if singeline_shape.over_budget() {
if singeline_shape.over_budget() && !is_string(expression) {
Prefix::Expression(format_hanging_expression_(
ctx,
expression,
Expand Down
21 changes: 21 additions & 0 deletions tests/inputs/hang-prefix-ignore-string.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- https://github.com/JohnnyMorganz/StyLua/issues/508
exports.ScalarLeafsRule = function(context)
return {
Field = function(_self, node)
if type_ then
if not selectionSet then
context:reportError(
GraphQLError.new(
('Field "%s" of type "%s" must have a selection of subfields. Did you mean "%s { ... }"?'):format(
fieldName,
typeStr,
fieldName
),
node
)
)
end
end
end,
}
end
26 changes: 26 additions & 0 deletions tests/snapshots/tests__standard@hang-prefix-ignore-string.lua.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
source: tests/tests.rs
expression: format(&contents)
---
-- https://github.com/JohnnyMorganz/StyLua/issues/508
exports.ScalarLeafsRule = function(context)
return {
Field = function(_self, node)
if type_ then
if not selectionSet then
context:reportError(
GraphQLError.new(
('Field "%s" of type "%s" must have a selection of subfields. Did you mean "%s { ... }"?'):format(
fieldName,
typeStr,
fieldName
),
node
)
)
end
end
end,
}
end