Skip to content

Commit

Permalink
(fix) respect strict mode and shorthand options
Browse files Browse the repository at this point in the history
when formatting style/class directives
fies sveltejs#328
  • Loading branch information
dummdidumm committed Dec 9, 2022
1 parent f3685aa commit 71df481
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 2.9.0 (unreleased)

- (feat) support style modifiers ([#330](https://github.com/sveltejs/prettier-plugin-svelte/issues/330))
- (fix) respect strict mode and shorthand options when formatting style/class directives ([#328](https://github.com/sveltejs/prettier-plugin-svelte/issues/328))

## 2.8.1

Expand Down
11 changes: 5 additions & 6 deletions src/print/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,10 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
return concat([
'class:',
node.name,
node.expression.type === 'Identifier' && node.expression.name === node.name
node.expression.type === 'Identifier' &&
node.expression.name === node.name &&
options.svelteAllowShorthand &&
!options.svelteStrictMode
? ''
: concat(['=', ...printJsExpression()]),
]);
Expand All @@ -632,7 +635,7 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
: '',
];

if (isOrCanBeConvertedToShorthand(node)) {
if (isOrCanBeConvertedToShorthand(node) || node.value === true) {
if (options.svelteStrictMode) {
return concat([...prefix, '="{', node.name, '}"']);
} else if (options.svelteAllowShorthand) {
Expand All @@ -641,10 +644,6 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
return concat([...prefix, '={', node.name, '}']);
}
} else {
if (node.value === true) {
return concat([...prefix]);
}

const quotes = !isLoneMustacheTag(node.value) || options.svelteStrictMode;
const attrNodeValue = printAttributeNodeValue(path, print, quotes, node);
if (quotes) {
Expand Down
6 changes: 6 additions & 0 deletions test/formatting/samples/strict-mode-false/input.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@
<input bind:value />
<input bind:value={value} />
<input bind:value="{value}" />
<input class:value />
<input class:value={value} />
<input class:value="{value}" />
<input style:value />
<input style:value={value} />
<input style:value="{value}" />
6 changes: 6 additions & 0 deletions test/formatting/samples/strict-mode-false/output.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@
<input bind:value />
<input bind:value />
<input bind:value />
<input class:value />
<input class:value />
<input class:value />
<input style:value />
<input style:value />
<input style:value />
6 changes: 6 additions & 0 deletions test/formatting/samples/strict-mode-true/input.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@
<input bind:value />
<input bind:value={value} />
<input bind:value="{value}" />
<input class:value />
<input class:value={value} />
<input class:value="{value}" />
<input style:value />
<input style:value={value} />
<input style:value="{value}" />
6 changes: 6 additions & 0 deletions test/formatting/samples/strict-mode-true/output.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@
<input bind:value="{value}" />
<input bind:value="{value}" />
<input bind:value="{value}" />
<input class:value="{value}" />
<input class:value="{value}" />
<input class:value="{value}" />
<input style:value="{value}" />
<input style:value="{value}" />
<input style:value="{value}" />
4 changes: 4 additions & 0 deletions test/printer/samples/allow-shorthand-false.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
<input type="text" data={data} />

<input type="text" bind:value={value} />

<input type="text" class:value={value} />

<input type="text" style:value={value} />

0 comments on commit 71df481

Please sign in to comment.