[FIX] qweb: add support for typeof expression

closes #469
This commit is contained in:
Géry Debongnie
2019-11-15 15:44:36 +01:00
committed by aab-odoo
parent f83846e054
commit 94a595ef5b
2 changed files with 9 additions and 3 deletions
+5 -3
View File
@@ -25,7 +25,7 @@
// Misc types, constants and helpers // 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" ")": "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; type Tokenizer = (expr: string) => Token | false;
@@ -160,9 +162,9 @@ const tokenizeOperator: Tokenizer = function(expr) {
const TOKENIZERS = [ const TOKENIZERS = [
tokenizeString, tokenizeString,
tokenizeNumber, tokenizeNumber,
tokenizeOperator,
tokenizeSymbol, tokenizeSymbol,
tokenizeStatic, tokenizeStatic,
tokenizeOperator
]; ];
/** /**
+4
View File
@@ -43,6 +43,9 @@ describe("tokenizer", () => {
{ type: "OPERATOR", value: "!==" }, { type: "OPERATOR", value: "!==" },
{ type: "OPERATOR", value: "!=" } { type: "OPERATOR", value: "!=" }
]); ]);
expect(tokenize("typeof a")).toEqual([
{"type": "OPERATOR", "value": "typeof "}, {"type": "SYMBOL", "value": "a"}
]);
}); });
test("strings", () => { test("strings", () => {
@@ -112,6 +115,7 @@ describe("expression evaluation", () => {
expect(compileExpr("!flag", {})).toBe("!context['flag']"); expect(compileExpr("!flag", {})).toBe("!context['flag']");
expect(compileExpr("-3", {})).toBe("-3"); expect(compileExpr("-3", {})).toBe("-3");
expect(compileExpr("-a", {})).toBe("-context['a']"); expect(compileExpr("-a", {})).toBe("-context['a']");
expect(compileExpr("typeof a", {})).toBe("typeof context['a']");
}); });
test("various binary operators", () => { test("various binary operators", () => {