Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle comments inlined in assignment expression better #341

Merged
merged 4 commits into from
Jan 20, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Specify whether to apply parentheses on function calls with single string or tab

### Fixed
- Fixed generic variadics not being handled under the `luau` feature flag. ([#333](https://github.com/JohnnyMorganz/StyLua/issues/333))
- Fixed issue with comments within an assignment not being correctly handled, leading to a syntax error. ([#340](https://github.com/JohnnyMorganz/StyLua/issues/340))

### Deprecated
- Option `no_call_parentheses` has been deprecated. Use `call_parentheses = "None"` instead.
Expand Down
10 changes: 9 additions & 1 deletion src/formatters/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,15 @@ fn attempt_assignment_tactics(
let shape = shape.reset().increment_additional_indent();

// As we know that there is only a single element in the list, we can extract it to work with it
let expression = format_expression(ctx, expression, shape);
// Format the expression given - if it contains comments, make sure to hang the expression
// Ignore the leading comments though (as they are solved by hanging at the equals), and the
// trailing comments, as they don't affect anything
let expression =
if trivia_util::expression_contains_inline_comments(&strip_trivia(expression)) {
hang_expression(ctx, expression, shape, None)
} else {
format_expression(ctx, expression, shape)
};

// We need to take all the leading trivia from the expr_list
let (expression, leading_comments) =
Expand Down
15 changes: 14 additions & 1 deletion tests/inputs/assignment-comments.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,17 @@ local isValid = -- test comment
-- Allow nil for conditional declaration
contextType == nil or
(contextType["$$typeof"] == REACT_CONTEXT_TYPE and
contextType._context == nil) -- Not a <Context.Consumer>
contextType._context == nil) -- Not a <Context.Consumer>

-- https://github.com/JohnnyMorganz/StyLua/issues/340
local useDisposableConcast =
-- * Refetching uses a disposable Concast to allow refetches using different
-- options/variables, without permanently altering the options of the
-- original ObservableQuery.
newNetworkStatus == NetworkStatus.refetch or
-- * The fetchMore method does not actually call the reobserve method, but,
-- if it did, it would definitely use a disposable Concast.
newNetworkStatus == NetworkStatus.fetchMore or
-- * Polling uses a disposable Concast so the polling options (which force
-- fetchPolicy to be "network-only") won't override the original options.
newNetworkStatus == NetworkSt
13 changes: 13 additions & 0 deletions tests/snapshots/tests__standard@assignment-comments.lua.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,16 @@ local isValid = -- test comment
-- Allow nil for conditional declaration
contextType == nil or (contextType["$$typeof"] == REACT_CONTEXT_TYPE and contextType._context == nil) -- Not a <Context.Consumer>

-- https://github.com/JohnnyMorganz/StyLua/issues/340
local useDisposableConcast =
-- * Refetching uses a disposable Concast to allow refetches using different
-- options/variables, without permanently altering the options of the
-- original ObservableQuery.
newNetworkStatus == NetworkStatus.refetch
-- * The fetchMore method does not actually call the reobserve method, but,
-- if it did, it would definitely use a disposable Concast.
or newNetworkStatus == NetworkStatus.fetchMore
-- * Polling uses a disposable Concast so the polling options (which force
-- fetchPolicy to be "network-only") won't override the original options.
or newNetworkStatus == NetworkSt