Skip to content

Commit

Permalink
Format spread operator (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonyfaris authored Nov 17, 2021
1 parent c4bd34c commit 2c164f7
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/polite-elephants-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'prettier-plugin-astro': patch
---

Format spread operator
6 changes: 6 additions & 0 deletions src/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ export interface CodeSpanNode extends BaseNode {
raw: string;
}

export interface SpreadNode extends BaseNode {
type: 'Spread';
expression: ExpressionNode;
}

export interface ExpressionNode {
type: 'Expression';
start: number;
Expand Down Expand Up @@ -256,5 +261,6 @@ export type anyNode =
| MustacheTagNode
| ScriptNode
| SlotNode
| SpreadNode
| StyleNode
| TextNode;
21 changes: 10 additions & 11 deletions src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,17 +399,16 @@ function print(path: AstPath, opts: ParserOptions, print: printFn): Doc {
}),
'}',
];
// TODO: ADD TEST OR REMOVE
// case 'Spread':
// return [
// line,
// '{...',
// printJS(path, print, 'expression', {
// forceSingleQuote: true,
// forceSingleLine: false,
// }),
// '}',
// ];
case 'Spread':
return [
line,
'{...',
printJS(path, print, 'expression', {
forceSingleQuote: true,
forceSingleLine: false,
}),
'}',
];
case 'Comment':
return ['<!--', getUnencodedText(node), '-->'];
case 'CodeSpan':
Expand Down
2 changes: 2 additions & 0 deletions test/astro-prettier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,5 @@ test('Can format an Astro file with prettier "astroAllowShorthand: false" option
test('Format nested style tag content', Prettier, 'format-nested-style-tag-content');

test('Format nested sass style tag content', Prettier, 'format-nested-sass-style-tag-content');

test('Format spread operator', Prettier, 'spread-operator');
4 changes: 4 additions & 0 deletions test/fixtures/spread-operator/input.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
const meta = { title: "My Title", lang: "en" }
---
<BaseLayout { ...meta }>Foo</BaseLayout>
5 changes: 5 additions & 0 deletions test/fixtures/spread-operator/output.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
const meta = { title: "My Title", lang: "en" };
---

<BaseLayout {...meta}>Foo</BaseLayout>

0 comments on commit 2c164f7

Please sign in to comment.