-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathstrict.js
More file actions
92 lines (77 loc) · 6.61 KB
/
strict.js
File metadata and controls
92 lines (77 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* global suite test */
"use strict";
var assert = require("assert");
var esvalid = require("../");
var helpers = require("../test-helpers");
var STMT = helpers.STMT, BLOCK = helpers.BLOCK, EXPR = helpers.EXPR,
NUM = helpers.NUM, STR = helpers.STR, ID = helpers.ID, CATCH = helpers.CATCH;
var wrapProgram = helpers.wrapProgram, wrapIter = helpers.wrapIter,
FE = helpers.FE, FD = helpers.FD, label = helpers.label, exprStmt = helpers.exprStmt;
var validExpr = helpers.validExpr, invalidExpr = helpers.invalidExpr,
validStmt = helpers.validStmt, invalidStmt = helpers.invalidStmt;
suite("strict mode", function() {
// wrap zero or more statements in a strict-mode function expression
function strictFE() { return FE.apply(null, [exprStmt({type: "Literal", value: "use strict"})].concat([].slice.call(arguments))); }
test("basic directive support", function() {
validStmt(FD(exprStmt({type: "Literal", value: "use strict"})));
validStmt(FD(exprStmt({type: "Literal", value: "directive"}), exprStmt({type: "Literal", value: "use strict"})));
validStmt(exprStmt(FE(exprStmt({type: "Literal", value: "use strict"}))));
validStmt(exprStmt(FE(exprStmt({type: "Literal", value: "directive"}), exprStmt({type: "Literal", value: "use strict"}))));
validStmt(exprStmt({type: "Literal", value: "use strict"}));
validExpr(FE(exprStmt({type: "Literal", value: "use strict"})));
});
test("CatchClause param must not be restricted in strict mode", function() {
validStmt({type: "TryStatement", block: BLOCK, handler: {type: "CatchClause", param: {type: "Identifier", name: "eval"}, body: BLOCK}});
validStmt({type: "TryStatement", block: BLOCK, handler: {type: "CatchClause", param: {type: "Identifier", name: "arguments"}, body: BLOCK}});
validExpr(strictFE({type: "TryStatement", block: BLOCK, handler: {type: "CatchClause", param: {type: "Identifier", name: "x"}, body: BLOCK}}));
invalidExpr(1, strictFE({type: "TryStatement", block: BLOCK, handler: {type: "CatchClause", param: {type: "Identifier", name: "eval"}, body: BLOCK}}));
invalidExpr(1, strictFE({type: "TryStatement", block: BLOCK, handler: {type: "CatchClause", param: {type: "Identifier", name: "arguments"}, body: BLOCK}}));
});
test("Function names must not be restricted in strict mode", function() {
validExpr(strictFE(exprStmt({type: "FunctionExpression", id: null, params: [], body: BLOCK})));
validExpr({type: "FunctionExpression", id: {type: "Identifier", name: "eval"}, params: [], body: BLOCK});
validExpr({type: "FunctionExpression", id: {type: "Identifier", name: "arguments"}, params: [], body: BLOCK});
validStmt({type: "FunctionDeclaration", id: {type: "Identifier", name: "eval"}, params: [], body: BLOCK});
validStmt({type: "FunctionDeclaration", id: {type: "Identifier", name: "arguments"}, params: [], body: BLOCK});
invalidExpr(1, strictFE(exprStmt({type: "FunctionExpression", id: {type: "Identifier", name: "eval"}, params: [], body: BLOCK})));
invalidExpr(1, strictFE(exprStmt({type: "FunctionExpression", id: {type: "Identifier", name: "arguments"}, params: [], body: BLOCK})));
invalidExpr(1, strictFE({type: "FunctionDeclaration", id: {type: "Identifier", name: "eval"}, params: [], body: BLOCK}));
invalidExpr(1, strictFE({type: "FunctionDeclaration", id: {type: "Identifier", name: "arguments"}, params: [], body: BLOCK}));
});
test("FunctionDeclaration parameter names must be unique in strict mode", function() {
validExpr(strictFE({type: "FunctionDeclaration", id: ID, params: [ID], body: BLOCK}));
validExpr(strictFE({type: "FunctionDeclaration", id: ID, params: [{type: "Identifier", name: "a"}, {type: "Identifier", name: "A"}], body: BLOCK}));
validStmt({type: "FunctionDeclaration", id: ID, params: [ID, ID], body: BLOCK});
invalidExpr(1, strictFE({type: "FunctionDeclaration", id: ID, params: [ID, ID], body: BLOCK}));
});
test("FunctionExpression parameter names must be unique in strict mode", function() {
validExpr(strictFE(exprStmt({type: "FunctionExpression", id: ID, params: [ID], body: BLOCK})));
validExpr(strictFE(exprStmt({type: "FunctionExpression", id: ID, params: [{type: "Identifier", name: "a"}, {type: "Identifier", name: "A"}], body: BLOCK})));
validExpr({type: "FunctionExpression", id: ID, params: [ID, ID], body: BLOCK});
invalidExpr(1, strictFE(exprStmt({type: "FunctionExpression", id: ID, params: [ID, ID], body: BLOCK})));
});
test("Identifier FutureReservedWords", function() {
validExpr({type: "Identifier", name: "let"});
validExpr({type: "Identifier", name: "yield"}); // ES5 only
// invalidExpr(1, strictFE(exprStmt({type: "Identifier", name: "let"})));
// invalidExpr(1, strictFE(exprStmt({type: "Identifier", name: "yield"})));
});
test("ObjectExpression duplicate keys", function() {
validExpr({type: "ObjectExpression", properties: [{kind: "init", key: {type: "Literal", value: "a"}, value: EXPR}, {kind: "init", key: {type: "Literal", value: "a"}, value: EXPR}]});
validExpr({type: "ObjectExpression", properties: [{kind: "init", key: {type: "Literal", value: "__proto__"}, value: EXPR}, {kind: "init", key: {type: "Literal", value: "a"}, value: EXPR}]});
validExpr(strictFE(exprStmt({type: "ObjectExpression", properties: [{kind: "init", key: {type: "Literal", value: "hasOwnProperty"}, value: EXPR}, {kind: "init", key: {type: "Literal", value: "a"}, value: EXPR}]})));
invalidExpr(1, strictFE(exprStmt({type: "ObjectExpression", properties: [{kind: "init", key: {type: "Literal", value: "a"}, value: EXPR}, {kind: "init", key: {type: "Literal", value: "a"}, value: EXPR}]})));
invalidExpr(1, strictFE(exprStmt({type: "ObjectExpression", properties: [{kind: "init", key: {type: "Literal", value: "a"}, value: EXPR}, {kind: "init", key: {type: "Identifier", name: "a"}, value: EXPR}]})));
invalidExpr(1, strictFE(exprStmt({type: "ObjectExpression", properties: [{kind: "init", key: {type: "Literal", value: "0"}, value: EXPR}, {kind: "init", key: {type: "Literal", value: 0}, value: EXPR}]})));
});
test("UnaryExpression delete with unqualified identifier", function() {
validExpr({type: "UnaryExpression", operator: "delete", argument: NUM});
validExpr({type: "UnaryExpression", operator: "delete", argument: ID});
validExpr(strictFE(exprStmt({type: "UnaryExpression", operator: "delete", argument: NUM})));
invalidExpr(1, strictFE(exprStmt({type: "UnaryExpression", operator: "delete", argument: ID})));
});
test("WithStatement not allowed", function() {
validStmt({type: "WithStatement", object: EXPR, body: STMT});
invalidExpr(1, strictFE({type: "WithStatement", object: EXPR, body: STMT}));
});
});