1 description("Tests to ensure that we can use ES reserved words as property names.");
3 var reservedWords
= ["true", "false", "null", "break", "case", "catch", "continue", "debugger", "default", "delete", "do", "else", "finally", "for",
4 "function", "if", "in", "instanceof", "new", "return", "switch", "this", "throw", "try", "typeof", "var", "void", "while", "with",
5 "class", "const", "enum", "export", "extends", "import", "super"];
7 var strictReservedWords
= [
19 var unreservedWords
= [
39 function testWordEvalAndFunction(str
, strShouldThrow
) {
42 shouldThrow("(function(){"+str
+"}); true");
45 shouldBeTrue("(function(){"+str
+"}); true");
49 function testWord(word
, strictPrefix
, expectedResult
) {
50 testWordEvalAndFunction(strictPrefix
+ "var " + word
+ "; true", expectedResult
);
51 testWordEvalAndFunction(strictPrefix
+ "var " + word
+ " = 42; " + word
+ " === 42", expectedResult
);
52 testWordEvalAndFunction(strictPrefix
+ "function g(" + word
+ "){ " + strictPrefix
+ " }; true", expectedResult
);
53 testWordEvalAndFunction(strictPrefix
+ "/" + word
+ "/.test(function g(" + word
+ "){ " + strictPrefix
+ " })", expectedResult
);
54 testWordEvalAndFunction(strictPrefix
+ "try{}catch(" + word
+ "){}; true", expectedResult
);
55 testWordEvalAndFunction(strictPrefix
+ "function " + word
+ "(){ " + strictPrefix
+ " }; true", expectedResult
);
56 // These should be allowed for all words, even reserved ones.
57 testWordEvalAndFunction(strictPrefix
+ "({ \"" + word
+ "\": 42 }." + word
+ " === 42)", false);
58 testWordEvalAndFunction(strictPrefix
+ "({ " + word
+ ": 42 }." + word
+ " === 42)", false);
59 testWordEvalAndFunction(strictPrefix
+ "({ get " + word
+ "(){}, set " + word
+ "(_){}, parsedOkay: 42 }.parsedOkay === 42)", false);
62 function testWordStrictAndNonStrict(word
, condition
) {
63 testWord(word
, '', condition
== "keyword");
64 testWord(word
, '"use strict";', condition
!= "identifier");
67 for (var i
= 0; i
< reservedWords
.length
; i
++)
68 testWordStrictAndNonStrict(reservedWords
[i
], "keyword");
69 for (var i
= 0; i
< strictReservedWords
.length
; i
++)
70 testWordStrictAndNonStrict(strictReservedWords
[i
], "strict");
71 for (var i
= 0; i
< unreservedWords
.length
; i
++)
72 testWordStrictAndNonStrict(unreservedWords
[i
], "identifier");
74 // test access via window.
76 shouldBeTrue("window.yield === 42");