Skip to content

Commit

Permalink
fix: preserve commas in array expressions
Browse files Browse the repository at this point in the history
fixes #432
  • Loading branch information
dummdidumm committed Aug 20, 2024
1 parent a1a3f92 commit 5630294
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 3.2.7 (unreleased)

- (fix) force quote style inside style directives
- (fix) preserve commas in array expressions
- (fix) Svelte 5: properly determine end of snippet parameters with TS annotations

## 3.2.6
Expand Down
11 changes: 10 additions & 1 deletion src/print/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1206,7 +1206,16 @@ function _expandNode(node: any, parent?: any): string {
switch (node.type) {
case 'ArrayExpression':
case 'ArrayPattern':
return ' [' + node.elements.map(_expandNode).join(',').slice(1) + ']';
return (
' [' +
node.elements
// handle null specifically here; else it would become the empty string, but that would mean
// fewer elements in the array, which would change the meaning of the array
.map((el: any) => (el === null ? ' ' : _expandNode(el)))
.join(',')
.slice(1) +
']'
);
case 'AssignmentPattern':
return _expandNode(node.left) + ' =' + _expandNode(node.right);
case 'Identifier':
Expand Down
4 changes: 4 additions & 0 deletions test/printer/samples/each-block-destructured.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{#each animals as [key, value]}
<p>{key}: {value}</p>
{/each}

{#each animals as [, value]}
<p>{value}</p>
{/each}

0 comments on commit 5630294

Please sign in to comment.