fix(vue): preserve array/object destructuring in v-for without extra parentheses#178
Merged
Merged
Conversation
…parentheses
The formatter was incorrectly adding parentheses around destructuring
patterns in v-for directives. For example:
- `[key, value] of Object.entries(obj)` became `([key, value]) of ...`
- `{ foo, bar } of items` became `({ foo, bar }) of ...`
The issue was in the `format_v_for` function which added parentheses
whenever the left side contained a comma but no parenthesis. This logic
didn't account for array destructuring `[...]` or object destructuring
`{...}` which are already valid grouping constructs.
The fix checks if the left side starts with `[`, `{`, or `(` before
deciding whether to wrap with parentheses. This ensures:
- Tuple unpacking still gets parentheses: `a, i` → `(a, i)`
- Array destructuring is preserved: `[key, value]` stays as-is
- Object destructuring is preserved: `{ foo, bar }` stays as-is
- Mixed patterns work correctly: `([key, value], i)` stays as-is
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The formatter was incorrectly adding parentheses around destructuring patterns in
v-fordirectives. For example:[key, value] of Object.entries(obj)became([key, value]) of ...{ foo, bar } of itemsbecame({ foo, bar }) of ...This changes the semantics in Vue templates, where
(key, value)is interpreted as Vue's v-for index syntax rather than JavaScript array destructuring.Cause
The issue was in the
format_v_forfunction which added parentheses whenever the left side contained a comma but no parenthesis:This logic didn't account for array destructuring
[...]or object destructuring{...}which are already valid grouping constructs.Solution
The fix checks if the left side starts with
[,{, or(before deciding whether to wrap with parentheses:This ensures:
a, i→(a, i)[key, value]stays as-is{ foo, bar }stays as-is([key, value], i)stays as-isTesting
Added test cases covering:
a in b,a of b(a, i) in b[key, value] in Object.entries(obj){ foo, bar } in items([key, value], i) in Object.entries(obj)