Skip to content

Commit

Permalink
Fix shorthand attributes incorrectly converted to strings. (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
yamac-kurtulus authored Dec 17, 2021
1 parent 8265c7d commit ab70152
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/nice-impalas-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'prettier-plugin-astro': patch
---

Fix a bug in "allow shorthand" option (#87)
24 changes: 13 additions & 11 deletions src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
isNodeWithChildren,
isOrCanBeConvertedToShorthand,
isPreTagContent,
isShorthandAndMustBeConvertedToBinaryExpression,
isTextNode,
isTextNodeEndingWithWhitespace,
isTextNodeStartingWithLinebreak,
Expand Down Expand Up @@ -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':
Expand Down
13 changes: 12 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(arrays: T[][]): T[] {
return ([] as T[]).concat.apply([], arrays);
}
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/option-astro-allow-shorthand-false/input.astro
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
import Comp from "./comp.astro"
---
<Comp options={options} />
<Comp options={options} />
<Comp {options} />
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ import Comp from "./comp.astro";
---

<Comp options={options} />
<Comp options={options} />
4 changes: 3 additions & 1 deletion test/fixtures/option-astro-allow-shorthand-true/input.astro
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
---
import Comp from "./comp.astro"
---
<Comp options={options} />
<Comp options={options} />
<Comp {options} />
<Comp options={ options } />
2 changes: 2 additions & 0 deletions test/fixtures/option-astro-allow-shorthand-true/output.astro
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ import Comp from "./comp.astro";
---

<Comp {options} />
<Comp {options} />
<Comp {options} />

0 comments on commit ab70152

Please sign in to comment.