Resolve issue #555

This commit is contained in:
Eric MORAND 2023-12-03 17:05:50 +00:00
parent 1b3bb7d32e
commit b1e132b2be
3 changed files with 30 additions and 1 deletions

View File

@ -1164,7 +1164,20 @@ export const createParser = (
let token;
let key: TwingBaseExpressionNode;
if ((token = stream.nextIf("STRING")) || (token = stream.nextIf("NAME")) || (token = stream.nextIf("NUMBER"))) {
if (token = stream.nextIf("NAME")) {
key = createConstantNode(token.value, token.line, token.column);
// {a} is a shortcut for {a:a}
if (stream.test("PUNCTUATION", [',', '}'])) {
elements.push({
key,
value: createNameNode(token.value, token.line, token.column)
});
continue;
}
}
else if ((token = stream.nextIf("STRING")) || (token = stream.nextIf("NUMBER"))) {
key = createConstantNode(token.value, token.line, token.column);
}
else if (stream.test("PUNCTUATION", '(')) {

View File

@ -1,2 +1,3 @@
import "./hash";
import "./invalid-key";
import "./with-value-only";

View File

@ -0,0 +1,15 @@
import {runTest} from "../../TestBase";
runTest({
description: 'Hash key can be omitted if it is the same as the variable name',
templates: {
"index.twig": `
{% set foo = "foo" %}
{% set hash = { foo, bar: "bar" } %}
{{ hash|join }}
`
},
expectation: `
foobar
`
});