diff --git a/.changeset/nice-impalas-live.md b/.changeset/nice-impalas-live.md new file mode 100644 index 0000000..5f2eee2 --- /dev/null +++ b/.changeset/nice-impalas-live.md @@ -0,0 +1,5 @@ +--- +'prettier-plugin-astro': patch +--- + +Fix a bug in "allow shorthand" option (#87) diff --git a/src/printer.ts b/src/printer.ts index 3aff6a9..374513e 100644 --- a/src/printer.ts +++ b/src/printer.ts @@ -33,6 +33,7 @@ import { isNodeWithChildren, isOrCanBeConvertedToShorthand, isPreTagContent, + isShorthandAndMustBeConvertedToBinaryExpression, isTextNode, isTextNodeEndingWithWhitespace, isTextNodeStartingWithLinebreak, @@ -327,18 +328,19 @@ function print(path: AstPath, opts: ParserOptions, print: printFn): Doc { case 'Attribute': { if (isOrCanBeConvertedToShorthand(node, opts)) { return [line, '{', node.name, '}']; - } else { - if (node.value === true) { - return [line, node.name]; - } + } else if (isShorthandAndMustBeConvertedToBinaryExpression(node, opts)) { + const attrNodeValue = printAttributeNodeValue(path, print, true, node); + return [line, node.name, '=', '{', attrNodeValue, '}']; + } else if (node.value === true) { + return [line, node.name]; + } - const quotes = !isLoneMustacheTag(node.value); - const attrNodeValue = printAttributeNodeValue(path, print, quotes, node); - if (quotes) { - return [line, node.name, '=', '"', attrNodeValue, '"']; - } else { - return [line, node.name, '=', attrNodeValue]; - } + const quotes = !isLoneMustacheTag(node.value); + const attrNodeValue = printAttributeNodeValue(path, print, quotes, node); + if (quotes) { + return [line, node.name, '=', '"', attrNodeValue, '"']; + } else { + return [line, node.name, '=', attrNodeValue]; } } case 'Expression': diff --git a/src/utils.ts b/src/utils.ts index 12b1e2f..5afd443 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -61,13 +61,24 @@ export function isOrCanBeConvertedToShorthand(node: AttributeNode, opts: ParserO if (isLoneMustacheTag(node.value)) { const expression = node.value[0].expression; - return expression.codeChunks[0] === node.name; + return expression.codeChunks[0].trim() === node.name; // return (expression.type === 'Identifier' && expression.name === node.name) || (expression.type === 'Expression' && expression.codeChunks[0] === node.name); } return false; } +/** + * True if node is of type `{a}` and astroAllowShorthand is false + */ +export function isShorthandAndMustBeConvertedToBinaryExpression(node: AttributeNode, opts: ParserOptions): boolean { + if (opts.astroAllowShorthand) return false; + if (isAttributeShorthand(node.value)) { + return true; + } + return false; +} + export function flatten(arrays: T[][]): T[] { return ([] as T[]).concat.apply([], arrays); } diff --git a/test/fixtures/option-astro-allow-shorthand-false/input.astro b/test/fixtures/option-astro-allow-shorthand-false/input.astro index 1c12074..7559455 100644 --- a/test/fixtures/option-astro-allow-shorthand-false/input.astro +++ b/test/fixtures/option-astro-allow-shorthand-false/input.astro @@ -1,4 +1,5 @@ --- import Comp from "./comp.astro" --- - \ No newline at end of file + + \ No newline at end of file diff --git a/test/fixtures/option-astro-allow-shorthand-false/output.astro b/test/fixtures/option-astro-allow-shorthand-false/output.astro index 7ee3be2..80937eb 100644 --- a/test/fixtures/option-astro-allow-shorthand-false/output.astro +++ b/test/fixtures/option-astro-allow-shorthand-false/output.astro @@ -3,3 +3,4 @@ import Comp from "./comp.astro"; --- + diff --git a/test/fixtures/option-astro-allow-shorthand-true/input.astro b/test/fixtures/option-astro-allow-shorthand-true/input.astro index 1c12074..51faf97 100644 --- a/test/fixtures/option-astro-allow-shorthand-true/input.astro +++ b/test/fixtures/option-astro-allow-shorthand-true/input.astro @@ -1,4 +1,6 @@ --- import Comp from "./comp.astro" --- - \ No newline at end of file + + + \ No newline at end of file diff --git a/test/fixtures/option-astro-allow-shorthand-true/output.astro b/test/fixtures/option-astro-allow-shorthand-true/output.astro index 61f22be..7fc3a32 100644 --- a/test/fixtures/option-astro-allow-shorthand-true/output.astro +++ b/test/fixtures/option-astro-allow-shorthand-true/output.astro @@ -3,3 +3,5 @@ import Comp from "./comp.astro"; --- + +