Skip to content

Commit d50a42d

Browse files
committed
Merge branch 'main' of https://github.com/JohnnyMorganz/StyLua into prefer-hang-over-expand
2 parents dcdc524 + 760737f commit d50a42d

File tree

4 files changed

+60
-3
lines changed

4 files changed

+60
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Fixed leading comments lost from an expression when excessive parentheses are removed from it ([#530](https://github.com/JohnnyMorganz/StyLua/issues/530))
1414
- Fixed comments present in a complex expression not forcing multiline hanging leading to a syntax error ([#524](https://github.com/JohnnyMorganz/StyLua/issues/524))
1515
- Fixed unnecessary break on `else` in an if-expression when the expression contains a comment ([#520](https://github.com/JohnnyMorganz/StyLua/issues/520))
16-
- 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))
16+
- 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))
1717
- Fixed comments added to a newly created trailing comment not being formatted ([#547](https://github.com/JohnnyMorganz/StyLua/issues/547))
1818
- Fixed call chain with a small prefix not being kept inlined causing unstable formatting ([#514](https://github.com/JohnnyMorganz/StyLua/issues/514))
19-
- Fix shape computation for table fields causing unnecessary expansion ([#551](https://github.com/JohnnyMorganz/StyLua/issues/551))
19+
- Fixed shape computation for table fields causing unnecessary expansion ([#551](https://github.com/JohnnyMorganz/StyLua/issues/551))
20+
- Fixed hanging the prefix string in `("str"):call` unnecessarily when it provides no benefit ([#508](https://github.com/JohnnyMorganz/StyLua/issues/508))
2021
- Fixed table field value being expanded when it could be hanged instead ([#541](https://github.com/JohnnyMorganz/StyLua/issues/541))
2122

2223
## [0.14.2] - 2022-07-27

src/formatters/expression.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,15 @@ pub fn format_index(ctx: &Context, index: &Index, shape: Shape) -> Index {
368368
}
369369
}
370370

371+
// Checks if this is a string (allows strings wrapped in parentheses)
372+
fn is_string(expression: &Expression) -> bool {
373+
match expression {
374+
Expression::Value { value, .. } => matches!(&**value, Value::String(_)),
375+
Expression::Parentheses { expression, .. } => is_string(expression),
376+
_ => false,
377+
}
378+
}
379+
371380
/// Formats a Prefix Node
372381
pub fn format_prefix(ctx: &Context, prefix: &Prefix, shape: Shape) -> Prefix {
373382
match prefix {
@@ -376,7 +385,7 @@ pub fn format_prefix(ctx: &Context, prefix: &Prefix, shape: Shape) -> Prefix {
376385
format_expression_internal(ctx, expression, ExpressionContext::Prefix, shape);
377386
let singeline_shape = shape.take_first_line(&strip_trivia(&singleline_format));
378387

379-
if singeline_shape.over_budget() {
388+
if singeline_shape.over_budget() && !is_string(expression) {
380389
Prefix::Expression(format_hanging_expression_(
381390
ctx,
382391
expression,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
-- https://github.com/JohnnyMorganz/StyLua/issues/508
2+
exports.ScalarLeafsRule = function(context)
3+
return {
4+
Field = function(_self, node)
5+
if type_ then
6+
if not selectionSet then
7+
context:reportError(
8+
GraphQLError.new(
9+
('Field "%s" of type "%s" must have a selection of subfields. Did you mean "%s { ... }"?'):format(
10+
fieldName,
11+
typeStr,
12+
fieldName
13+
),
14+
node
15+
)
16+
)
17+
end
18+
end
19+
end,
20+
}
21+
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
source: tests/tests.rs
3+
expression: format(&contents)
4+
---
5+
-- https://github.com/JohnnyMorganz/StyLua/issues/508
6+
exports.ScalarLeafsRule = function(context)
7+
return {
8+
Field = function(_self, node)
9+
if type_ then
10+
if not selectionSet then
11+
context:reportError(
12+
GraphQLError.new(
13+
('Field "%s" of type "%s" must have a selection of subfields. Did you mean "%s { ... }"?'):format(
14+
fieldName,
15+
typeStr,
16+
fieldName
17+
),
18+
node
19+
)
20+
)
21+
end
22+
end
23+
end,
24+
}
25+
end
26+

0 commit comments

Comments
 (0)