Skip to content

Commit bd5a49a

Browse files
authored
(fix) respect strict mode and shorthand options (sveltejs#332)
when formatting style/class directives fixes sveltejs#328
1 parent f3685aa commit bd5a49a

File tree

7 files changed

+34
-6
lines changed

7 files changed

+34
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 2.9.0 (unreleased)
44

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

78
## 2.8.1
89

src/print/index.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,10 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
619619
return concat([
620620
'class:',
621621
node.name,
622-
node.expression.type === 'Identifier' && node.expression.name === node.name
622+
node.expression.type === 'Identifier' &&
623+
node.expression.name === node.name &&
624+
options.svelteAllowShorthand &&
625+
!options.svelteStrictMode
623626
? ''
624627
: concat(['=', ...printJsExpression()]),
625628
]);
@@ -632,7 +635,7 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
632635
: '',
633636
];
634637

635-
if (isOrCanBeConvertedToShorthand(node)) {
638+
if (isOrCanBeConvertedToShorthand(node) || node.value === true) {
636639
if (options.svelteStrictMode) {
637640
return concat([...prefix, '="{', node.name, '}"']);
638641
} else if (options.svelteAllowShorthand) {
@@ -641,10 +644,6 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
641644
return concat([...prefix, '={', node.name, '}']);
642645
}
643646
} else {
644-
if (node.value === true) {
645-
return concat([...prefix]);
646-
}
647-
648647
const quotes = !isLoneMustacheTag(node.value) || options.svelteStrictMode;
649648
const attrNodeValue = printAttributeNodeValue(path, print, quotes, node);
650649
if (quotes) {

test/formatting/samples/strict-mode-false/input.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@
66
<input bind:value />
77
<input bind:value={value} />
88
<input bind:value="{value}" />
9+
<input class:value />
10+
<input class:value={value} />
11+
<input class:value="{value}" />
12+
<input style:value />
13+
<input style:value={value} />
14+
<input style:value="{value}" />

test/formatting/samples/strict-mode-false/output.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@
66
<input bind:value />
77
<input bind:value />
88
<input bind:value />
9+
<input class:value />
10+
<input class:value />
11+
<input class:value />
12+
<input style:value />
13+
<input style:value />
14+
<input style:value />

test/formatting/samples/strict-mode-true/input.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@
66
<input bind:value />
77
<input bind:value={value} />
88
<input bind:value="{value}" />
9+
<input class:value />
10+
<input class:value={value} />
11+
<input class:value="{value}" />
12+
<input style:value />
13+
<input style:value={value} />
14+
<input style:value="{value}" />

test/formatting/samples/strict-mode-true/output.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@
66
<input bind:value="{value}" />
77
<input bind:value="{value}" />
88
<input bind:value="{value}" />
9+
<input class:value="{value}" />
10+
<input class:value="{value}" />
11+
<input class:value="{value}" />
12+
<input style:value="{value}" />
13+
<input style:value="{value}" />
14+
<input style:value="{value}" />

test/printer/samples/allow-shorthand-false.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33
<input type="text" data={data} />
44

55
<input type="text" bind:value={value} />
6+
7+
<input type="text" class:value={value} />
8+
9+
<input type="text" style:value={value} />

0 commit comments

Comments
 (0)