diff --git a/src/qweb/expression_parser.ts b/src/qweb/expression_parser.ts index e5d2fee2..f8a94e56 100644 --- a/src/qweb/expression_parser.ts +++ b/src/qweb/expression_parser.ts @@ -25,7 +25,7 @@ // Misc types, constants and helpers //------------------------------------------------------------------------------ -const RESERVED_WORDS = "true,false,NaN,null,undefined,debugger,console,window,in,instanceof,new,function,return,this,typeof,eval,void,Math,RegExp,Array,Object,Date".split( +const RESERVED_WORDS = "true,false,NaN,null,undefined,debugger,console,window,in,instanceof,new,function,return,this,eval,void,Math,RegExp,Array,Object,Date".split( "," ); @@ -77,7 +77,9 @@ const STATIC_TOKEN_MAP: { [key: string]: TKind } = { ")": "RIGHT_PAREN" }; -const OPERATORS = ".,===,==,+,!==,!=,!,||,&&,>=,>,<=,<,?,-,*,/,%".split(","); +// note that the space after typeof is relevant. It makes sure that the formatted +// expression has a space after typeof +const OPERATORS = ".,===,==,+,!==,!=,!,||,&&,>=,>,<=,<,?,-,*,/,%,typeof ".split(","); type Tokenizer = (expr: string) => Token | false; @@ -160,9 +162,9 @@ const tokenizeOperator: Tokenizer = function(expr) { const TOKENIZERS = [ tokenizeString, tokenizeNumber, + tokenizeOperator, tokenizeSymbol, tokenizeStatic, - tokenizeOperator ]; /** diff --git a/tests/qweb/qweb_expressions.test.ts b/tests/qweb/qweb_expressions.test.ts index e818d44e..8e9563f1 100644 --- a/tests/qweb/qweb_expressions.test.ts +++ b/tests/qweb/qweb_expressions.test.ts @@ -43,6 +43,9 @@ describe("tokenizer", () => { { type: "OPERATOR", value: "!==" }, { type: "OPERATOR", value: "!=" } ]); + expect(tokenize("typeof a")).toEqual([ + {"type": "OPERATOR", "value": "typeof "}, {"type": "SYMBOL", "value": "a"} + ]); }); test("strings", () => { @@ -112,6 +115,7 @@ describe("expression evaluation", () => { expect(compileExpr("!flag", {})).toBe("!context['flag']"); expect(compileExpr("-3", {})).toBe("-3"); expect(compileExpr("-a", {})).toBe("-context['a']"); + expect(compileExpr("typeof a", {})).toBe("typeof context['a']"); }); test("various binary operators", () => {