diff --git a/src/qweb/expression_parser.ts b/src/qweb/expression_parser.ts index a1b5f779..34f056da 100644 --- a/src/qweb/expression_parser.ts +++ b/src/qweb/expression_parser.ts @@ -257,16 +257,33 @@ export function compileExprToArray(expr: string, scope: { [key: string]: QWebVar const tokens = tokenize(expr); let i = 0; + let stack = []; // to track last opening [ or { while (i < tokens.length) { let token = tokens[i]; let prevToken = tokens[i - 1]; let nextToken = tokens[i + 1]; + let groupType = stack[stack.length - 1]; + + switch (token.type) { + case "LEFT_BRACE": + case "LEFT_BRACKET": + stack.push(token.type); + break; + case "RIGHT_BRACE": + case "RIGHT_BRACKET": + stack.pop(); + } + let isVar = token.type === "SYMBOL" && !RESERVED_WORDS.includes(token.value); if (token.type === "SYMBOL" && !RESERVED_WORDS.includes(token.value)) { if (prevToken) { // normalize missing tokens: {a} should be equivalent to {a:a} - if (isLeftSeparator(prevToken) && isRightSeparator(nextToken)) { + if ( + groupType === "LEFT_BRACE" && + isLeftSeparator(prevToken) && + isRightSeparator(nextToken) + ) { tokens.splice(i + 1, 0, { type: "COLON", value: ":" }, { ...token }); nextToken = tokens[i + 1]; } diff --git a/tests/qweb/qweb_expressions.test.ts b/tests/qweb/qweb_expressions.test.ts index f47856a5..4336d4c9 100644 --- a/tests/qweb/qweb_expressions.test.ts +++ b/tests/qweb/qweb_expressions.test.ts @@ -207,6 +207,17 @@ describe("expression evaluation", () => { expect(compileExpr("{a,b:3,c}", {})).toBe("{a:scope['a'],b:3,c:scope['c']}"); }); + test("works with short object description and lists ", () => { + expect(compileExpr("[a, b]", {})).toBe("[scope['a'],scope['b']]"); + expect(compileExpr("[a, b, c]", {})).toBe("[scope['a'],scope['b'],scope['c']]"); + expect(compileExpr("[a, {b, c},d]", {})).toBe( + "[scope['a'],{b:scope['b'],c:scope['c']},scope['d']]" + ); + expect(compileExpr("{a:[b, {c, d: e}]}", {})).toBe( + "{a:[scope['b'],{c:scope['c'],d:scope['e']}]}" + ); + }); + test("template strings", () => { expect(compileExpr("`hey`", {})).toBe("`hey`"); expect(compileExpr("`hey ${you}`", {})).toBe("`hey ${scope['you']}`");