diff --git a/prettier/src/printHubl.ts b/prettier/src/printHubl.ts index b4a7486..8f0f44b 100644 --- a/prettier/src/printHubl.ts +++ b/prettier/src/printHubl.ts @@ -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") { @@ -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]; } diff --git a/prettier/tests/__snapshots__/run_tests.js.snap b/prettier/tests/__snapshots__/run_tests.js.snap index f004075..2087c51 100644 --- a/prettier/tests/__snapshots__/run_tests.js.snap +++ b/prettier/tests/__snapshots__/run_tests.js.snap @@ -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`] = `
{# A comment #} diff --git a/prettier/tests/brackets.html b/prettier/tests/brackets.html new file mode 100644 index 0000000..cb7feb4 --- /dev/null +++ b/prettier/tests/brackets.html @@ -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"] }} +