Skip to content

Commit

Permalink
Merge pull request #77 from HubSpot/jm/spaces-in-brackets
Browse files Browse the repository at this point in the history
Check for valid name before reformatting bracket notation to use dots
  • Loading branch information
j-malt authored May 16, 2024
2 parents d0fdcc3 + a0b2c0c commit 5c26803
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
5 changes: 4 additions & 1 deletion prettier/src/printHubl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ const closeVar = (whitespace) => {
return whitespace.end ? "-}}" : "}}";
};

const isValidVariable = (testString: string) =>
/^[a-zA-Z_$][0-9a-zA-Z_$]*$/gm.test(testString);

// Recurvisely print if elif and else
const printElse = (node) => {
if (node.else_ && node.else_.typename === "If") {
Expand Down Expand Up @@ -275,7 +278,7 @@ function printHubl(node) {
if (
node.val.typename === "Literal" &&
typeof node.val.value === "string" &&
!node.val.value.includes("-")
isValidVariable(node.val.value)
) {
return [printHubl(node.target), ".", node.val.value];
}
Expand Down
30 changes: 30 additions & 0 deletions prettier/tests/__snapshots__/run_tests.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,36 @@ screenshotPath: ../images/template-previews/blog-post.png
`;
exports[`brackets.html 1`] = `
{% set test_object = {
"foo bar": "baz",
"another test value": "blah",
"a-hyphen": "test",
"shouldBeDotted": "test",
"should_Be_Dotted": "test"
}%}
{{ test_object["foo bar"] }}
{{ test_object["another test value"] }}
{{ test_object["a-hyphen"] }}
{{ test_object["shouldBeDotted"] }}
{{ test_object["should_Be_Dotted"] }}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{% set test_object = {
"foo bar": "baz",
"another test value": "blah",
"a-hyphen": "test",
"shouldBeDotted": "test",
"should_Be_Dotted": "test"
} %}
{{ test_object["foo bar"] }}
{{ test_object["another test value"] }}
{{ test_object["a-hyphen"] }}
{{ test_object.shouldBeDotted }}
{{ test_object.should_Be_Dotted }}
`;
exports[`comments.html 1`] = `
<div>
{# A comment #}
Expand Down
13 changes: 13 additions & 0 deletions prettier/tests/brackets.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% set test_object = {
"foo bar": "baz",
"another test value": "blah",
"a-hyphen": "test",
"shouldBeDotted": "test",
"should_Be_Dotted": "test"
}%}
{{ test_object["foo bar"] }}
{{ test_object["another test value"] }}
{{ test_object["a-hyphen"] }}
{{ test_object["shouldBeDotted"] }}
{{ test_object["should_Be_Dotted"] }}

0 comments on commit 5c26803

Please sign in to comment.