UNPKG

40.8 kBJavaScriptView Raw
1/**
2 * Copyright 2014 Shape Security, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License")
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17"use strict";
18
19// istanbul ignore next
20
21var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
22
23// istanbul ignore next
24
25var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
26
27// istanbul ignore next
28
29function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
30
31// istanbul ignore next
32
33function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
34
35var _shiftReducer = require("shift-reducer");
36
37var _utils = require("./utils");
38
39var _earlyErrorState = require("./early-error-state");
40
41var _patternAcceptor = require("./pattern-acceptor");
42
43function isStrictFunctionBody(_ref) {
44 var directives = _ref.directives;
45
46 return directives.some(function (directive) {
47 return directive.rawValue === "use strict";
48 });
49}
50
51function containsDuplicates(list) {
52 var uniqs = [];
53 for (var i = 0, l = list.length; i < l; ++i) {
54 var item = list[i];
55 if (uniqs.indexOf(item) >= 0) {
56 return true;
57 }
58 uniqs.push(item);
59 }
60 return false;
61}
62
63function isLabelledFunction(_x4) {
64 var _left;
65
66 var _again2 = true;
67
68 _function2: while (_again2) {
69 var node = _x4;
70 _again2 = false;
71
72 if (!(_left = node.type === "LabeledStatement")) {
73 return _left;
74 }
75
76 if (_left = node.body.type === "FunctionDeclaration") {
77 return _left;
78 }
79
80 _x4 = node.body;
81 _again2 = true;
82 continue _function2;
83 }
84}
85
86function isIterationStatement(_x5) {
87 var _again3 = true;
88
89 _function3: while (_again3) {
90 var node = _x5;
91 _again3 = false;
92
93 switch (node.type) {
94 case "LabeledStatement":
95 _x5 = node.body;
96 _again3 = true;
97 continue _function3;
98
99 case "DoWhileStatement":
100 case "ForInStatement":
101 case "ForOfStatement":
102 case "ForStatement":
103 case "WhileStatement":
104 return true;
105 }
106 return false;
107 }
108}
109
110function isSpecialMethod(methodDefinition) {
111 if (methodDefinition.name.type !== "StaticPropertyName" || methodDefinition.name.value !== "constructor") {
112 return false;
113 }
114 switch (methodDefinition.type) {
115 case "Getter":
116 case "Setter":
117 return true;
118 case "Method":
119 return methodDefinition.isGenerator;
120 }
121 /* istanbul ignore next */
122 throw new Error("not reached");
123}
124
125function enforceDuplicateConstructorMethods(node, s) {
126 var ctors = node.elements.filter(function (e) {
127 return !e.isStatic && e.method.type === "Method" && !e.method.isGenerator && e.method.name.type === "StaticPropertyName" && e.method.name.value === "constructor";
128 });
129 if (ctors.length > 1) {
130 ctors.slice(1).forEach(function (ctor) {
131 s = s.addError(new _earlyErrorState.EarlyError(ctor, "Duplicate constructor method in class"));
132 });
133 }
134 return s;
135}
136
137var SUPERCALL_ERROR = function SUPERCALL_ERROR(node) {
138 return new _earlyErrorState.EarlyError(node, "Calls to super must be in the \"constructor\" method of a class expression or class declaration that has a superclass");
139};
140var SUPERPROPERTY_ERROR = function SUPERPROPERTY_ERROR(node) {
141 return new _earlyErrorState.EarlyError(node, "Member access on super must be in a method");
142};
143var DUPLICATE_BINDING = function DUPLICATE_BINDING(node) {
144 return new _earlyErrorState.EarlyError(node, "Duplicate binding " + JSON.stringify(node.name));
145};
146var FREE_CONTINUE = function FREE_CONTINUE(node) {
147 return new _earlyErrorState.EarlyError(node, "Continue statement must be nested within an iteration statement");
148};
149var UNBOUND_CONTINUE = function UNBOUND_CONTINUE(node) {
150 return new _earlyErrorState.EarlyError(node, "Continue statement must be nested within an iteration statement with label " + JSON.stringify(node.label));
151};
152var FREE_BREAK = function FREE_BREAK(node) {
153 return new _earlyErrorState.EarlyError(node, "Break statement must be nested within an iteration statement or a switch statement");
154};
155var UNBOUND_BREAK = function UNBOUND_BREAK(node) {
156 return new _earlyErrorState.EarlyError(node, "Break statement must be nested within a statement with label " + JSON.stringify(node.label));
157};
158
159var EarlyErrorChecker = (function (_MonoidalReducer) {
160 _inherits(EarlyErrorChecker, _MonoidalReducer);
161
162 function EarlyErrorChecker() {
163 _classCallCheck(this, EarlyErrorChecker);
164
165 _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "constructor", this).call(this, _earlyErrorState.EarlyErrorState);
166 }
167
168 _createClass(EarlyErrorChecker, [{
169 key: "reduceAssignmentExpression",
170 value: function reduceAssignmentExpression() {
171 return _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceAssignmentExpression", this).apply(this, arguments).clearBoundNames();
172 }
173 }, {
174 key: "reduceArrowExpression",
175 value: function reduceArrowExpression(node, _ref2) {
176 var params = _ref2.params;
177 var body = _ref2.body;
178
179 params = params.enforceDuplicateLexicallyDeclaredNames(DUPLICATE_BINDING);
180 if (node.body.type === "FunctionBody") {
181 body = body.enforceConflictingLexicallyDeclaredNames(params.lexicallyDeclaredNames, DUPLICATE_BINDING);
182 if (isStrictFunctionBody(node.body)) {
183 params = params.enforceStrictErrors();
184 body = body.enforceStrictErrors();
185 }
186 }
187 body.yieldExpressions.forEach(function (node) {
188 body = body.addError(new _earlyErrorState.EarlyError(node, "Concise arrow bodies must not contain yield expressions"));
189 });
190 params.yieldExpressions.forEach(function (node) {
191 params = params.addError(new _earlyErrorState.EarlyError(node, "Arrow parameters must not contain yield expressions"));
192 });
193 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceArrowExpression", this).call(this, node, { params: params, body: body });
194 s = s.clearYieldExpressions();
195 s = s.observeVarBoundary();
196 return s;
197 }
198 }, {
199 key: "reduceBindingIdentifier",
200 value: function reduceBindingIdentifier(node) {
201 var s = this.identity;
202 if ((0, _utils.isRestrictedWord)(node.name) || (0, _utils.isStrictModeReservedWord)(node.name)) {
203 s = s.addStrictError(new _earlyErrorState.EarlyError(node, "The identifier " + JSON.stringify(node.name) + " must not be in binding position in strict mode"));
204 }
205 s = s.bindName(node.name, node);
206 return s;
207 }
208 }, {
209 key: "reduceBlock",
210 value: function reduceBlock() {
211 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceBlock", this).apply(this, arguments);
212 s = s.functionDeclarationNamesAreLexical();
213 s = s.enforceDuplicateLexicallyDeclaredNames(DUPLICATE_BINDING);
214 s = s.enforceConflictingLexicallyDeclaredNames(s.varDeclaredNames, DUPLICATE_BINDING);
215 s = s.observeLexicalBoundary();
216 return s;
217 }
218 }, {
219 key: "reduceBreakStatement",
220 value: function reduceBreakStatement(node) {
221 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceBreakStatement", this).apply(this, arguments);
222 s = node.label == null ? s.addFreeBreakStatement(node) : s.addFreeLabeledBreakStatement(node);
223 return s;
224 }
225 }, {
226 key: "reduceCallExpression",
227 value: function reduceCallExpression(node) {
228 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceCallExpression", this).apply(this, arguments);
229 if (node.callee.type === "Super") {
230 s = s.observeSuperCallExpression(node);
231 }
232 return s;
233 }
234 }, {
235 key: "reduceCatchClause",
236 value: function reduceCatchClause(node, _ref3) {
237 var binding = _ref3.binding;
238 var body = _ref3.body;
239
240 binding = binding.observeLexicalDeclaration();
241 binding = binding.enforceDuplicateLexicallyDeclaredNames(DUPLICATE_BINDING);
242 binding = binding.enforceConflictingLexicallyDeclaredNames(body.previousLexicallyDeclaredNames, DUPLICATE_BINDING);
243 binding.lexicallyDeclaredNames.forEachEntry(function (nodes, bindingName) {
244 if (body.varDeclaredNames.has(bindingName)) {
245 body.varDeclaredNames.get(bindingName).forEach(function (conflictingNode) {
246 if (body.forOfVarDeclaredNames.indexOf(conflictingNode) >= 0) {
247 binding = binding.addError(DUPLICATE_BINDING(conflictingNode));
248 }
249 });
250 }
251 });
252 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceCatchClause", this).call(this, node, { binding: binding, body: body });
253 s = s.observeLexicalBoundary();
254 return s;
255 }
256 }, {
257 key: "reduceClassDeclaration",
258 value: function reduceClassDeclaration(node, _ref4) {
259 var name = _ref4.name;
260 var _super = _ref4["super"];
261 var elements = _ref4.elements;
262
263 var s = name;
264 var sElements = this.fold(elements);
265 sElements = sElements.enforceStrictErrors();
266 if (node["super"] != null) {
267 _super = _super.enforceStrictErrors();
268 s = this.append(s, _super);
269 sElements = sElements.clearSuperCallExpressionsInConstructorMethod();
270 }
271 sElements = sElements.enforceSuperCallExpressions(SUPERCALL_ERROR);
272 sElements = sElements.enforceSuperPropertyExpressions(SUPERPROPERTY_ERROR);
273 s = this.append(s, sElements);
274 s = enforceDuplicateConstructorMethods(node, s);
275 s = s.observeLexicalDeclaration();
276 return s;
277 }
278 }, {
279 key: "reduceClassElement",
280 value: function reduceClassElement(node) {
281 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceClassElement", this).apply(this, arguments);
282 if (!node.isStatic && isSpecialMethod(node.method)) {
283 s = s.addError(new _earlyErrorState.EarlyError(node, "Constructors cannot be generators, getters or setters"));
284 }
285 if (node.isStatic && node.method.name.type === "StaticPropertyName" && node.method.name.value === "prototype") {
286 s = s.addError(new _earlyErrorState.EarlyError(node, "Static class methods cannot be named \"prototype\""));
287 }
288 return s;
289 }
290 }, {
291 key: "reduceClassExpression",
292 value: function reduceClassExpression(node, _ref5) {
293 var name = _ref5.name;
294 var _super = _ref5["super"];
295 var elements = _ref5.elements;
296
297 var s = node.name == null ? this.identity : name;
298 var sElements = this.fold(elements);
299 sElements = sElements.enforceStrictErrors();
300 if (node["super"] != null) {
301 _super = _super.enforceStrictErrors();
302 s = this.append(s, _super);
303 sElements = sElements.clearSuperCallExpressionsInConstructorMethod();
304 }
305 sElements = sElements.enforceSuperCallExpressions(SUPERCALL_ERROR);
306 sElements = sElements.enforceSuperPropertyExpressions(SUPERPROPERTY_ERROR);
307 s = this.append(s, sElements);
308 s = enforceDuplicateConstructorMethods(node, s);
309 s = s.clearBoundNames();
310 return s;
311 }
312 }, {
313 key: "reduceCompoundAssignmentExpression",
314 value: function reduceCompoundAssignmentExpression() {
315 return _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceCompoundAssignmentExpression", this).apply(this, arguments).clearBoundNames();
316 }
317 }, {
318 key: "reduceComputedMemberExpression",
319 value: function reduceComputedMemberExpression(node) {
320 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceComputedMemberExpression", this).apply(this, arguments);
321 if (node.object.type === "Super") {
322 s = s.observeSuperPropertyExpression(node);
323 }
324 return s;
325 }
326 }, {
327 key: "reduceContinueStatement",
328 value: function reduceContinueStatement(node) {
329 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceContinueStatement", this).apply(this, arguments);
330 s = node.label == null ? s.addFreeContinueStatement(node) : s.addFreeLabeledContinueStatement(node);
331 return s;
332 }
333 }, {
334 key: "reduceDoWhileStatement",
335 value: function reduceDoWhileStatement(node) {
336 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceDoWhileStatement", this).apply(this, arguments);
337 if (isLabelledFunction(node.body)) {
338 s = s.addError(new _earlyErrorState.EarlyError(node.body, "The body of a do-while statement must not be a labeled function declaration"));
339 }
340 s = s.clearFreeContinueStatements();
341 s = s.clearFreeBreakStatements();
342 return s;
343 }
344 }, {
345 key: "reduceExport",
346 value: function reduceExport() {
347 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceExport", this).apply(this, arguments);
348 s = s.functionDeclarationNamesAreLexical();
349 s = s.exportDeclaredNames();
350 return s;
351 }
352 }, {
353 key: "reduceExportFrom",
354 value: function reduceExportFrom(node) {
355 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceExportFrom", this).apply(this, arguments);
356 if (node.moduleSpecifier != null) {
357 s = s.clearExportedBindings();
358 }
359 return s;
360 }
361 }, {
362 key: "reduceExportSpecifier",
363 value: function reduceExportSpecifier(node) {
364 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceExportSpecifier", this).apply(this, arguments);
365 s = s.exportName(node.exportedName, node);
366 s = s.exportBinding(node.name || node.exportedName, node);
367 return s;
368 }
369 }, {
370 key: "reduceExportDefault",
371 value: function reduceExportDefault(node) {
372 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceExportDefault", this).apply(this, arguments);
373 s = s.functionDeclarationNamesAreLexical();
374 switch (node.body.type) {
375 case "FunctionDeclaration":
376 case "ClassDeclaration":
377 if (node.body.name.name !== "*default*") {
378 s = s.exportDeclaredNames();
379 }
380 break;
381 }
382 s = s.exportName("*default*", node);
383 return s;
384 }
385 }, {
386 key: "reduceFormalParameters",
387 value: function reduceFormalParameters() {
388 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceFormalParameters", this).apply(this, arguments);
389 s = s.observeLexicalDeclaration();
390 return s;
391 }
392 }, {
393 key: "reduceForStatement",
394 value: function reduceForStatement(node, _ref6) {
395 var init = _ref6.init;
396 var test = _ref6.test;
397 var update = _ref6.update;
398 var body = _ref6.body;
399
400 if (init != null) {
401 init = init.enforceDuplicateLexicallyDeclaredNames(DUPLICATE_BINDING);
402 init = init.enforceConflictingLexicallyDeclaredNames(body.varDeclaredNames, DUPLICATE_BINDING);
403 }
404 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceForStatement", this).call(this, node, { init: init, test: test, update: update, body: body });
405 if (node.init != null && node.init.type === "VariableDeclaration" && node.init.kind === "const") {
406 node.init.declarators.forEach(function (declarator) {
407 if (declarator.init == null) {
408 s = s.addError(new _earlyErrorState.EarlyError(declarator, "Constant lexical declarations must have an initialiser"));
409 }
410 });
411 }
412 if (isLabelledFunction(node.body)) {
413 s = s.addError(new _earlyErrorState.EarlyError(node.body, "The body of a for statement must not be a labeled function declaration"));
414 }
415 s = s.clearFreeContinueStatements();
416 s = s.clearFreeBreakStatements();
417 s = s.observeLexicalBoundary();
418 return s;
419 }
420 }, {
421 key: "reduceForInStatement",
422 value: function reduceForInStatement(node, _ref7) {
423 var left = _ref7.left;
424 var right = _ref7.right;
425 var body = _ref7.body;
426
427 left = left.enforceDuplicateLexicallyDeclaredNames(DUPLICATE_BINDING);
428 left = left.enforceConflictingLexicallyDeclaredNames(body.varDeclaredNames, DUPLICATE_BINDING);
429 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceForInStatement", this).call(this, node, { left: left, right: right, body: body });
430 if (isLabelledFunction(node.body)) {
431 s = s.addError(new _earlyErrorState.EarlyError(node.body, "The body of a for-in statement must not be a labeled function declaration"));
432 }
433 s = s.clearFreeContinueStatements();
434 s = s.clearFreeBreakStatements();
435 s = s.observeLexicalBoundary();
436 return s;
437 }
438 }, {
439 key: "reduceForOfStatement",
440 value: function reduceForOfStatement(node, _ref8) {
441 var left = _ref8.left;
442 var right = _ref8.right;
443 var body = _ref8.body;
444
445 left = left.recordForOfVars();
446 left = left.enforceDuplicateLexicallyDeclaredNames(DUPLICATE_BINDING);
447 left = left.enforceConflictingLexicallyDeclaredNames(body.varDeclaredNames, DUPLICATE_BINDING);
448 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceForOfStatement", this).call(this, node, { left: left, right: right, body: body });
449 if (isLabelledFunction(node.body)) {
450 s = s.addError(new _earlyErrorState.EarlyError(node.body, "The body of a for-of statement must not be a labeled function declaration"));
451 }
452 s = s.clearFreeContinueStatements();
453 s = s.clearFreeBreakStatements();
454 s = s.observeLexicalBoundary();
455 return s;
456 }
457 }, {
458 key: "reduceFunctionBody",
459 value: function reduceFunctionBody(node) {
460 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceFunctionBody", this).apply(this, arguments);
461 s = s.enforceDuplicateLexicallyDeclaredNames(DUPLICATE_BINDING);
462 s = s.enforceConflictingLexicallyDeclaredNames(s.varDeclaredNames, DUPLICATE_BINDING);
463 s = s.enforceFreeContinueStatementErrors(FREE_CONTINUE);
464 s = s.enforceFreeLabeledContinueStatementErrors(UNBOUND_CONTINUE);
465 s = s.enforceFreeBreakStatementErrors(FREE_BREAK);
466 s = s.enforceFreeLabeledBreakStatementErrors(UNBOUND_BREAK);
467 s = s.clearUsedLabelNames();
468 s = s.clearYieldExpressions();
469 if (isStrictFunctionBody(node)) {
470 s = s.enforceStrictErrors();
471 }
472 return s;
473 }
474 }, {
475 key: "reduceFunctionDeclaration",
476 value: function reduceFunctionDeclaration(node, _ref9) {
477 var name = _ref9.name;
478 var params = _ref9.params;
479 var body = _ref9.body;
480
481 var isSimpleParameterList = node.params.rest == null && node.params.items.every(function (i) {
482 return i.type === "BindingIdentifier";
483 });
484 var addError = !isSimpleParameterList || node.isGenerator ? "addError" : "addStrictError";
485 params.lexicallyDeclaredNames.forEachEntry(function (nodes /*, bindingName*/) {
486 if (nodes.length > 1) {
487 nodes.slice(1).forEach(function (dupeNode) {
488 params = params[addError](DUPLICATE_BINDING(dupeNode));
489 });
490 }
491 });
492 body = body.enforceConflictingLexicallyDeclaredNames(params.lexicallyDeclaredNames, DUPLICATE_BINDING);
493 body = body.enforceSuperCallExpressions(SUPERCALL_ERROR);
494 body = body.enforceSuperPropertyExpressions(SUPERPROPERTY_ERROR);
495 params = params.enforceSuperCallExpressions(SUPERCALL_ERROR);
496 params = params.enforceSuperPropertyExpressions(SUPERPROPERTY_ERROR);
497 if (node.isGenerator) {
498 params.yieldExpressions.forEach(function (node) {
499 params = params.addError(new _earlyErrorState.EarlyError(node, "Generator parameters must not contain yield expressions"));
500 });
501 }
502 params = params.clearNewTargetExpressions();
503 body = body.clearNewTargetExpressions();
504 if (isStrictFunctionBody(node.body)) {
505 params = params.enforceStrictErrors();
506 body = body.enforceStrictErrors();
507 }
508 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceFunctionDeclaration", this).call(this, node, { name: name, params: params, body: body });
509 s = s.clearYieldExpressions();
510 s = s.observeFunctionDeclaration();
511 return s;
512 }
513 }, {
514 key: "reduceFunctionExpression",
515 value: function reduceFunctionExpression(node, _ref10) {
516 var name = _ref10.name;
517 var params = _ref10.params;
518 var body = _ref10.body;
519
520 var isSimpleParameterList = node.params.rest == null && node.params.items.every(function (i) {
521 return i.type === "BindingIdentifier";
522 });
523 var addError = !isSimpleParameterList || node.isGenerator ? "addError" : "addStrictError";
524 params.lexicallyDeclaredNames.forEachEntry(function (nodes, bindingName) {
525 if (nodes.length > 1) {
526 nodes.slice(1).forEach(function (dupeNode) {
527 params = params[addError](new _earlyErrorState.EarlyError(dupeNode, "Duplicate binding " + JSON.stringify(bindingName)));
528 });
529 }
530 });
531 body = body.enforceConflictingLexicallyDeclaredNames(params.lexicallyDeclaredNames, DUPLICATE_BINDING);
532 body = body.enforceSuperCallExpressions(SUPERCALL_ERROR);
533 body = body.enforceSuperPropertyExpressions(SUPERPROPERTY_ERROR);
534 params = params.enforceSuperCallExpressions(SUPERCALL_ERROR);
535 params = params.enforceSuperPropertyExpressions(SUPERPROPERTY_ERROR);
536 if (node.isGenerator) {
537 params.yieldExpressions.forEach(function (node) {
538 params = params.addError(new _earlyErrorState.EarlyError(node, "Generator parameters must not contain yield expressions"));
539 });
540 }
541 params = params.clearNewTargetExpressions();
542 body = body.clearNewTargetExpressions();
543 if (isStrictFunctionBody(node.body)) {
544 params = params.enforceStrictErrors();
545 body = body.enforceStrictErrors();
546 }
547 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceFunctionExpression", this).call(this, node, { name: name, params: params, body: body });
548 s = s.clearBoundNames();
549 s = s.clearYieldExpressions();
550 s = s.observeVarBoundary();
551 return s;
552 }
553 }, {
554 key: "reduceGetter",
555 value: function reduceGetter(node, _ref11) {
556 var name = _ref11.name;
557 var body = _ref11.body;
558
559 body = body.enforceSuperCallExpressions(SUPERCALL_ERROR);
560 body = body.clearSuperPropertyExpressions();
561 body = body.clearNewTargetExpressions();
562 if (isStrictFunctionBody(node.body)) {
563 body = body.enforceStrictErrors();
564 }
565 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceGetter", this).call(this, node, { name: name, body: body });
566 s = s.observeVarBoundary();
567 return s;
568 }
569 }, {
570 key: "reduceIdentifierExpression",
571 value: function reduceIdentifierExpression(node) {
572 var s = this.identity;
573 if ((0, _utils.isStrictModeReservedWord)(node.name)) {
574 s = s.addStrictError(new _earlyErrorState.EarlyError(node, "The identifier " + JSON.stringify(node.name) + " must not be in expression position in strict mode"));
575 }
576 return s;
577 }
578 }, {
579 key: "reduceIfStatement",
580 value: function reduceIfStatement(node, _ref12) {
581 var test = _ref12.test;
582 var consequent = _ref12.consequent;
583 var alternate = _ref12.alternate;
584
585 if (isLabelledFunction(node.consequent)) {
586 consequent = consequent.addError(new _earlyErrorState.EarlyError(node.consequent, "The consequent of an if statement must not be a labeled function declaration"));
587 }
588 if (node.alternate != null && isLabelledFunction(node.alternate)) {
589 alternate = alternate.addError(new _earlyErrorState.EarlyError(node.alternate, "The alternate of an if statement must not be a labeled function declaration"));
590 }
591 if (node.consequent.type === "FunctionDeclaration") {
592 consequent = consequent.addStrictError(new _earlyErrorState.EarlyError(node.consequent, "FunctionDeclarations in IfStatements are disallowed in strict mode"));
593 consequent = consequent.observeLexicalBoundary();
594 }
595 if (node.alternate != null && node.alternate.type === "FunctionDeclaration") {
596 alternate = alternate.addStrictError(new _earlyErrorState.EarlyError(node.alternate, "FunctionDeclarations in IfStatements are disallowed in strict mode"));
597 alternate = alternate.observeLexicalBoundary();
598 }
599 return _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceIfStatement", this).call(this, node, { test: test, consequent: consequent, alternate: alternate });
600 }
601 }, {
602 key: "reduceImport",
603 value: function reduceImport() {
604 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceImport", this).apply(this, arguments);
605 s = s.observeLexicalDeclaration();
606 return s;
607 }
608 }, {
609 key: "reduceImportNamespace",
610 value: function reduceImportNamespace() {
611 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceImportNamespace", this).apply(this, arguments);
612 s = s.observeLexicalDeclaration();
613 return s;
614 }
615 }, {
616 key: "reduceLabeledStatement",
617 value: function reduceLabeledStatement(node) {
618 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceLabeledStatement", this).apply(this, arguments);
619 if (node.label === "yield") {
620 s = s.addStrictError(new _earlyErrorState.EarlyError(node, "The identifier " + JSON.stringify(node.label) + " must not be in label position in strict mode"));
621 }
622 if (s.usedLabelNames.indexOf(node.label) >= 0) {
623 s = s.addError(new _earlyErrorState.EarlyError(node, "Label " + JSON.stringify(node.label) + " has already been declared"));
624 }
625 if (node.body.type === "FunctionDeclaration") {
626 s = s.addStrictError(new _earlyErrorState.EarlyError(node, "Labeled FunctionDeclarations are disallowed in strict mode"));
627 }
628 s = isIterationStatement(node.body) ? s.observeIterationLabel(node.label) : s.observeNonIterationLabel(node.label);
629 return s;
630 }
631 }, {
632 key: "reduceLiteralRegExpExpression",
633 value: function reduceLiteralRegExpExpression(node) {
634 var s = this.identity;
635 // NOTE: the RegExp pattern acceptor is disabled until we have more confidence in its correctness (more tests)
636 //if (!PatternAcceptor.test(node.pattern, node.flags.indexOf("u") >= 0)) {
637 // s = s.addError(new EarlyError(node, "Invalid regular expression pattern"));
638 //}
639 if (!/^[igmyu]*$/.test(node.flags) || containsDuplicates(node.flags)) {
640 s = s.addError(new _earlyErrorState.EarlyError(node, "Invalid regular expression flags"));
641 }
642 return s;
643 }
644 }, {
645 key: "reduceMethod",
646 value: function reduceMethod(node, _ref13) {
647 var name = _ref13.name;
648 var params = _ref13.params;
649 var body = _ref13.body;
650
651 params = params.enforceDuplicateLexicallyDeclaredNames(DUPLICATE_BINDING);
652 body = body.enforceConflictingLexicallyDeclaredNames(params.lexicallyDeclaredNames, DUPLICATE_BINDING);
653 if (node.name.type === "StaticPropertyName" && node.name.value === "constructor") {
654 body = body.observeConstructorMethod();
655 params = params.observeConstructorMethod();
656 } else {
657 body = body.enforceSuperCallExpressions(SUPERCALL_ERROR);
658 params = params.enforceSuperCallExpressions(SUPERCALL_ERROR);
659 }
660 if (node.isGenerator) {
661 params.yieldExpressions.forEach(function (node) {
662 params = params.addError(new _earlyErrorState.EarlyError(node, "Generator parameters must not contain yield expressions"));
663 });
664 }
665 body = body.clearSuperPropertyExpressions();
666 params = params.clearSuperPropertyExpressions();
667 params = params.clearNewTargetExpressions();
668 body = body.clearNewTargetExpressions();
669 if (isStrictFunctionBody(node.body)) {
670 params = params.enforceStrictErrors();
671 body = body.enforceStrictErrors();
672 }
673 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceMethod", this).call(this, node, { name: name, params: params, body: body });
674 s = s.clearYieldExpressions();
675 s = s.observeVarBoundary();
676 return s;
677 }
678 }, {
679 key: "reduceModule",
680 value: function reduceModule() {
681 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceModule", this).apply(this, arguments);
682 s = s.functionDeclarationNamesAreLexical();
683 s = s.enforceDuplicateLexicallyDeclaredNames(DUPLICATE_BINDING);
684 s = s.enforceConflictingLexicallyDeclaredNames(s.varDeclaredNames, DUPLICATE_BINDING);
685 s.exportedNames.forEachEntry(function (nodes, bindingName) {
686 if (nodes.length > 1) {
687 nodes.slice(1).forEach(function (dupeNode) {
688 s = s.addError(new _earlyErrorState.EarlyError(dupeNode, "Duplicate export " + JSON.stringify(bindingName)));
689 });
690 }
691 });
692 s.exportedBindings.forEachEntry(function (nodes, bindingName) {
693 if (bindingName !== "*default*" && !s.lexicallyDeclaredNames.has(bindingName) && !s.varDeclaredNames.has(bindingName)) {
694 nodes.forEach(function (undeclaredNode) {
695 s = s.addError(new _earlyErrorState.EarlyError(undeclaredNode, "Exported binding " + JSON.stringify(bindingName) + " is not declared"));
696 });
697 }
698 });
699 s.newTargetExpressions.forEach(function (node) {
700 s = s.addError(new _earlyErrorState.EarlyError(node, "new.target must be within function (but not arrow expression) code"));
701 });
702 s = s.enforceFreeContinueStatementErrors(FREE_CONTINUE);
703 s = s.enforceFreeLabeledContinueStatementErrors(UNBOUND_CONTINUE);
704 s = s.enforceFreeBreakStatementErrors(FREE_BREAK);
705 s = s.enforceFreeLabeledBreakStatementErrors(UNBOUND_BREAK);
706 s = s.enforceSuperCallExpressions(SUPERCALL_ERROR);
707 s = s.enforceSuperPropertyExpressions(SUPERPROPERTY_ERROR);
708 s = s.enforceStrictErrors();
709 return s;
710 }
711 }, {
712 key: "reduceNewTargetExpression",
713 value: function reduceNewTargetExpression(node) {
714 return this.identity.observeNewTargetExpression(node);
715 }
716 }, {
717 key: "reduceObjectExpression",
718 value: function reduceObjectExpression(node) {
719 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceObjectExpression", this).apply(this, arguments);
720 s = s.enforceSuperCallExpressionsInConstructorMethod(SUPERCALL_ERROR);
721 var protos = node.properties.filter(function (p) {
722 return p.type === "DataProperty" && p.name.type === "StaticPropertyName" && p.name.value === "__proto__";
723 });
724 protos.slice(1).forEach(function (n) {
725 s = s.addError(new _earlyErrorState.EarlyError(n, "Duplicate __proto__ property in object literal not allowed"));
726 });
727 return s;
728 }
729 }, {
730 key: "reduceUpdateExpression",
731 value: function reduceUpdateExpression(node) {
732 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceUpdateExpression", this).apply(this, arguments);
733 s = s.clearBoundNames();
734 return s;
735 }
736 }, {
737 key: "reduceUnaryExpression",
738 value: function reduceUnaryExpression(node) {
739 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceUnaryExpression", this).apply(this, arguments);
740 if (node.operator === "delete" && node.operand.type === "IdentifierExpression") {
741 s = s.addStrictError(new _earlyErrorState.EarlyError(node, "Identifier expressions must not be deleted in strict mode"));
742 }
743 return s;
744 }
745 }, {
746 key: "reduceScript",
747 value: function reduceScript(node) {
748 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceScript", this).apply(this, arguments);
749 s = s.enforceDuplicateLexicallyDeclaredNames(DUPLICATE_BINDING);
750 s = s.enforceConflictingLexicallyDeclaredNames(s.varDeclaredNames, DUPLICATE_BINDING);
751 s.newTargetExpressions.forEach(function (node) {
752 s = s.addError(new _earlyErrorState.EarlyError(node, "new.target must be within function (but not arrow expression) code"));
753 });
754 s = s.enforceFreeContinueStatementErrors(FREE_CONTINUE);
755 s = s.enforceFreeLabeledContinueStatementErrors(UNBOUND_CONTINUE);
756 s = s.enforceFreeBreakStatementErrors(FREE_BREAK);
757 s = s.enforceFreeLabeledBreakStatementErrors(UNBOUND_BREAK);
758 s = s.enforceSuperCallExpressions(SUPERCALL_ERROR);
759 s = s.enforceSuperPropertyExpressions(SUPERPROPERTY_ERROR);
760 if (isStrictFunctionBody(node)) {
761 s = s.enforceStrictErrors();
762 }
763 return s;
764 }
765 }, {
766 key: "reduceSetter",
767 value: function reduceSetter(node, _ref14) {
768 var name = _ref14.name;
769 var param = _ref14.param;
770 var body = _ref14.body;
771
772 param = param.observeLexicalDeclaration();
773 param = param.enforceDuplicateLexicallyDeclaredNames(DUPLICATE_BINDING);
774 body = body.enforceConflictingLexicallyDeclaredNames(param.lexicallyDeclaredNames, DUPLICATE_BINDING);
775 param = param.enforceSuperCallExpressions(SUPERCALL_ERROR);
776 body = body.enforceSuperCallExpressions(SUPERCALL_ERROR);
777 param = param.clearSuperPropertyExpressions();
778 body = body.clearSuperPropertyExpressions();
779 param = param.clearNewTargetExpressions();
780 body = body.clearNewTargetExpressions();
781 if (isStrictFunctionBody(node.body)) {
782 param = param.enforceStrictErrors();
783 body = body.enforceStrictErrors();
784 }
785 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceSetter", this).call(this, node, { name: name, param: param, body: body });
786 s = s.observeVarBoundary();
787 return s;
788 }
789 }, {
790 key: "reduceStaticMemberExpression",
791 value: function reduceStaticMemberExpression(node) {
792 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceStaticMemberExpression", this).apply(this, arguments);
793 if (node.object.type === "Super") {
794 s = s.observeSuperPropertyExpression(node);
795 }
796 return s;
797 }
798 }, {
799 key: "reduceSwitchStatement",
800 value: function reduceSwitchStatement(node, _ref15) {
801 var discriminant = _ref15.discriminant;
802 var cases = _ref15.cases;
803
804 var sCases = this.fold(cases);
805 sCases = sCases.functionDeclarationNamesAreLexical();
806 sCases = sCases.enforceDuplicateLexicallyDeclaredNames(DUPLICATE_BINDING);
807 sCases = sCases.enforceConflictingLexicallyDeclaredNames(sCases.varDeclaredNames, DUPLICATE_BINDING);
808 sCases = sCases.observeLexicalBoundary();
809 var s = this.append(discriminant, sCases);
810 s = s.clearFreeBreakStatements();
811 return s;
812 }
813 }, {
814 key: "reduceSwitchStatementWithDefault",
815 value: function reduceSwitchStatementWithDefault(node, _ref16) {
816 var discriminant = _ref16.discriminant;
817 var preDefaultCases = _ref16.preDefaultCases;
818 var defaultCase = _ref16.defaultCase;
819 var postDefaultCases = _ref16.postDefaultCases;
820
821 var sCases = this.append(defaultCase, this.append(this.fold(preDefaultCases), this.fold(postDefaultCases)));
822 sCases = sCases.functionDeclarationNamesAreLexical();
823 sCases = sCases.enforceDuplicateLexicallyDeclaredNames(DUPLICATE_BINDING);
824 sCases = sCases.enforceConflictingLexicallyDeclaredNames(sCases.varDeclaredNames, DUPLICATE_BINDING);
825 sCases = sCases.observeLexicalBoundary();
826 var s = this.append(discriminant, sCases);
827 s = s.clearFreeBreakStatements();
828 return s;
829 }
830 }, {
831 key: "reduceVariableDeclaration",
832 value: function reduceVariableDeclaration(node) {
833 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceVariableDeclaration", this).apply(this, arguments);
834 switch (node.kind) {
835 case "const":
836 case "let":
837 {
838 s = s.observeLexicalDeclaration();
839 if (s.lexicallyDeclaredNames.has("let")) {
840 s.lexicallyDeclaredNames.get("let").forEach(function (n) {
841 s = s.addError(new _earlyErrorState.EarlyError(n, "Lexical declarations must not have a binding named \"let\""));
842 });
843 }
844 break;
845 }
846 case "var":
847 s = s.observeVarDeclaration();
848 break;
849 }
850 return s;
851 }
852 }, {
853 key: "reduceVariableDeclarationStatement",
854 value: function reduceVariableDeclarationStatement(node) {
855 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceVariableDeclarationStatement", this).apply(this, arguments);
856 if (node.declaration.kind === "const") {
857 node.declaration.declarators.forEach(function (declarator) {
858 if (declarator.init == null) {
859 s = s.addError(new _earlyErrorState.EarlyError(declarator, "Constant lexical declarations must have an initialiser"));
860 }
861 });
862 }
863 return s;
864 }
865 }, {
866 key: "reduceWhileStatement",
867 value: function reduceWhileStatement(node) {
868 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceWhileStatement", this).apply(this, arguments);
869 if (isLabelledFunction(node.body)) {
870 s = s.addError(new _earlyErrorState.EarlyError(node.body, "The body of a while statement must not be a labeled function declaration"));
871 }
872 s = s.clearFreeContinueStatements().clearFreeBreakStatements();
873 return s;
874 }
875 }, {
876 key: "reduceWithStatement",
877 value: function reduceWithStatement(node) {
878 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceWithStatement", this).apply(this, arguments);
879 if (isLabelledFunction(node.body)) {
880 s = s.addError(new _earlyErrorState.EarlyError(node.body, "The body of a with statement must not be a labeled function declaration"));
881 }
882 s = s.addStrictError(new _earlyErrorState.EarlyError(node, "Strict mode code must not include a with statement"));
883 return s;
884 }
885 }, {
886 key: "reduceYieldExpression",
887 value: function reduceYieldExpression(node) {
888 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceYieldExpression", this).apply(this, arguments);
889 s = s.observeYieldExpression(node);
890 return s;
891 }
892 }, {
893 key: "reduceYieldGeneratorExpression",
894 value: function reduceYieldGeneratorExpression(node) {
895 var s = _get(Object.getPrototypeOf(EarlyErrorChecker.prototype), "reduceYieldGeneratorExpression", this).apply(this, arguments);
896 s = s.observeYieldExpression(node);
897 return s;
898 }
899 }], [{
900 key: "check",
901 value: function check(node) {
902 return (0, _shiftReducer["default"])(new EarlyErrorChecker(), node).errors;
903 }
904 }]);
905
906 return EarlyErrorChecker;
907})(_shiftReducer.MonoidalReducer);
908
909exports.EarlyErrorChecker = EarlyErrorChecker;
\No newline at end of file