diff --git a/CHANGELOG.md b/CHANGELOG.md index 961e0c8a..a29d32a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/print/index.ts b/src/print/index.ts index a6d6cb32..66514be6 100644 --- a/src/print/index.ts +++ b/src/print/index.ts @@ -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()]), ]); @@ -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) { @@ -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) { diff --git a/test/formatting/samples/strict-mode-false/input.html b/test/formatting/samples/strict-mode-false/input.html index fdf53ca7..a73d77a9 100644 --- a/test/formatting/samples/strict-mode-false/input.html +++ b/test/formatting/samples/strict-mode-false/input.html @@ -6,3 +6,9 @@ + + + + + + diff --git a/test/formatting/samples/strict-mode-false/output.html b/test/formatting/samples/strict-mode-false/output.html index b7951e16..5b523939 100644 --- a/test/formatting/samples/strict-mode-false/output.html +++ b/test/formatting/samples/strict-mode-false/output.html @@ -6,3 +6,9 @@ + + + + + + diff --git a/test/formatting/samples/strict-mode-true/input.html b/test/formatting/samples/strict-mode-true/input.html index fdf53ca7..a73d77a9 100644 --- a/test/formatting/samples/strict-mode-true/input.html +++ b/test/formatting/samples/strict-mode-true/input.html @@ -6,3 +6,9 @@ + + + + + + diff --git a/test/formatting/samples/strict-mode-true/output.html b/test/formatting/samples/strict-mode-true/output.html index e029e6ff..44757b54 100644 --- a/test/formatting/samples/strict-mode-true/output.html +++ b/test/formatting/samples/strict-mode-true/output.html @@ -6,3 +6,9 @@ + + + + + + diff --git a/test/printer/samples/allow-shorthand-false.html b/test/printer/samples/allow-shorthand-false.html index 37b14903..98523e6e 100644 --- a/test/printer/samples/allow-shorthand-false.html +++ b/test/printer/samples/allow-shorthand-false.html @@ -3,3 +3,7 @@ + + + +