Fix handlebars path expressions starts with @ (#16358)

Co-authored-by: prince.yadav <prince1.yadav@tataaig.com>
Co-authored-by: fisker <lionkay@gmail.com>
This commit is contained in:
authored and GitHub committed 2024-06-11 10:08:12 +08:00
1 parent b85449ba5d
commit adcbc8bedb
7 files changed
+47 -33

No files matched your search

+13
View File
@@ -0,0 +1,13 @@
#### Fix handlebars path expressions starts with `@` (#16358 by @Princeyadav05)
<!-- prettier-ignore -->
```hbs
{{! Input }}
<div>{{@x.y.z}}</div>
{{! Prettier stable }}
<div>{{@x}}</div>
{{! Prettier main }}
<div>{{@x.y.z}}</div>
```
+4
View File
@@ -32,6 +32,10 @@ function clean(original, cloned /*, parent*/) {
if (original.type === "AttrNode" && original.name.toLowerCase() === "class") {
delete cloned.value;
}
if (original.type === "PathExpression") {
cloned.head = original.head.original;
}
}
clean.ignoredProperties = new Set(["loc", "selfClosing"]);
+16 -16
View File
@@ -366,10 +366,7 @@ function print(path, options, print) {
];
}
case "PathExpression":
return printPathExpression(node, print);
case "AtHead":
return node.name;
return printPathExpression(node);
case "BooleanLiteral":
return String(node.value);
@@ -389,6 +386,7 @@ function print(path, options, print) {
case "NullLiteral":
return "null";
case "AtHead": // Handled in `printPathExpression`
case "VarHead": // Handled in `printPathExpression`
case "ThisHead": // Handled in `printPathExpression`
default:
@@ -784,21 +782,23 @@ const PATH_EXPRESSION_FORBIDDEN_IN_FIRST_PART = new Set([
"null",
"undefined",
]);
const isPathExpressionPartNeedBrackets = (part, index) =>
(index !== 0 && PATH_EXPRESSION_FORBIDDEN_IN_FIRST_PART.has(part)) ||
/\s/.test(part) ||
/^\d/.test(part) ||
Array.prototype.some.call(part, (character) =>
PATH_EXPRESSION_FORBIDDEN_CHARACTERS.has(character),
);
function printPathExpression(node, print) {
if (node.head.type === "AtHead") {
return print("head");
const isPathExpressionPartNeedBrackets = (part, index) => {
if (index === 0 && part.startsWith("@")) {
return false;
}
return (
(index !== 0 && PATH_EXPRESSION_FORBIDDEN_IN_FIRST_PART.has(part)) ||
/\s/.test(part) ||
/^\d/.test(part) ||
Array.prototype.some.call(part, (character) =>
PATH_EXPRESSION_FORBIDDEN_CHARACTERS.has(character),
)
);
};
function printPathExpression(node) {
// check if node is a legacy path expression and leave it alone
if (node.tail.length === 0 && node.original.includes("/")) {
// check if node has data, or
// check if node is a legacy path expression (and leave it alone)
return node.original;
}
@@ -1,11 +1 @@
import { visitorKeys as glimmerVisitorKeys } from "@glimmer/syntax";
const visitorKeys = {
VarHead: [],
ThisHead: [],
AtHead: [],
...glimmerVisitorKeys,
PathExpression: [...glimmerVisitorKeys.PathExpression, "head"],
};
export default visitorKeys;
export { visitorKeys as default } from "@glimmer/syntax";
@@ -42,6 +42,10 @@ printWidth: 80
{{array.2.[@#].[1]}}
{{array.2.[a b]}}
{{this.test}}
{{@x.y.z}}
{{@array.[true]}}
{{@array.2.[@#].[1]}}
{{@array.2.[a b]}}
{{#if @foo}}
{{/if}}
<!-- legacy path format, which glimmer does not parse - we don't want to wrap with brackets -->
@@ -54,6 +58,10 @@ printWidth: 80
{{array.[2].[@#].[1]}}
{{array.[2].[a b]}}
{{this.test}}
{{@x.y.z}}
{{@array.[true]}}
{{@array.[2].[@#].[1]}}
{{@array.[2].[a b]}}
{{#if @foo}}{{/if}}
<!-- legacy path format, which glimmer does not parse - we don't want to wrap with brackets -->
{{foo/bar/baz ab=cd}}
@@ -3,6 +3,10 @@
{{array.2.[@#].[1]}}
{{array.2.[a b]}}
{{this.test}}
{{@x.y.z}}
{{@array.[true]}}
{{@array.2.[@#].[1]}}
{{@array.2.[a b]}}
{{#if @foo}}
{{/if}}
<!-- legacy path format, which glimmer does not parse - we don't want to wrap with brackets -->
@@ -1165,7 +1165,6 @@ exports[`visitor keys estree-json 1`] = `
exports[`visitor keys glimmer 1`] = `
{
"AtHead": [],
"AttrNode": [
"value",
],
@@ -1209,9 +1208,7 @@ exports[`visitor keys glimmer 1`] = `
],
"NullLiteral": [],
"NumberLiteral": [],
"PathExpression": [
"head",
],
"PathExpression": [],
"StringLiteral": [],
"SubExpression": [
"path",
@@ -1222,9 +1219,7 @@ exports[`visitor keys glimmer 1`] = `
"body",
],
"TextNode": [],
"ThisHead": [],
"UndefinedLiteral": [],
"VarHead": [],
}
`;