Skip to content

Commit 6fdc5dc

Browse files
Move parameter name 'strict' checking to the grammar walker.
1 parent 00a4953 commit 6fdc5dc

3 files changed

Lines changed: 71 additions & 25 deletions

File tree

src/compiler/parser.ts

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,11 @@ module ts {
11691169

11701170
function finishNode<T extends Node>(node: T): T {
11711171
node.end = scanner.getStartPos();
1172+
1173+
if (isInStrictMode) {
1174+
node.flags |= NodeFlags.ParsedInStrictMode;
1175+
}
1176+
11721177
return node;
11731178
}
11741179

@@ -1703,16 +1708,7 @@ module ts {
17031708

17041709
for (var i = 0; i < parameterCount; i++) {
17051710
var parameter = parameters[i];
1706-
// It is a SyntaxError if the Identifier "eval" or the Identifier "arguments" occurs as the
1707-
// Identifier in a PropertySetParameterList of a PropertyAssignment that is contained in strict code
1708-
// or if its FunctionBody is strict code(11.1.5).
1709-
// It is a SyntaxError if the identifier eval or arguments appears within a FormalParameterList of a
1710-
// strict mode FunctionLikeDeclaration or FunctionExpression(13.1)
1711-
if (isInStrictMode && isEvalOrArgumentsIdentifier(parameter.name)) {
1712-
reportInvalidUseInStrictMode(parameter.name);
1713-
return;
1714-
}
1715-
else if (parameter.flags & NodeFlags.Rest) {
1711+
if (parameter.flags & NodeFlags.Rest) {
17161712
if (i !== (parameterCount - 1)) {
17171713
grammarErrorOnNode(parameter.name, Diagnostics.A_rest_parameter_must_be_last_in_a_parameter_list);
17181714
return;
@@ -4322,7 +4318,7 @@ module ts {
43224318
else {
43234319
// No parser errors were reported. Perform our stricted grammar checks.
43244320
file._syntacticDiagnostics = [];
4325-
performGrammarChecks(file);
4321+
performGrammarChecks(sourceText, file);
43264322
}
43274323

43284324
file._parserDiagnostics = undefined;
@@ -4363,9 +4359,54 @@ module ts {
43634359
return file;
43644360
}
43654361

4366-
function performGrammarChecks(child: Node) {
4362+
function performGrammarChecks(sourceText: string, file: SourceFileInternal) {
4363+
performNodeChecks(file);
4364+
4365+
function performNodeChecks(node: Node) {
4366+
// First recurse and perform all grammar checks on the children of this node. If any
4367+
// children had an grammar error, then skip reporting errors for this node or anything
4368+
// higher.
4369+
if (forEachChild(node, performNodeChecks)) {
4370+
return true;
4371+
}
4372+
4373+
// No grammar errors on any of our children. Check this node for grammar errors.
4374+
switch (node.kind) {
4375+
case SyntaxKind.Parameter:
4376+
return performParameterChecks(<ParameterDeclaration>node);
4377+
}
4378+
}
4379+
4380+
function grammarErrorOnNode(node: Node, message: DiagnosticMessage, arg0?: any, arg1?: any, arg2?: any): void {
4381+
var span = getErrorSpanForNode(node);
4382+
var start = span.end > span.pos ? skipTrivia(file.text, span.pos) : span.pos;
4383+
var length = span.end - start;
4384+
4385+
file._syntacticDiagnostics.push(createFileDiagnostic(file, start, length, message, arg0, arg1, arg2));
4386+
}
4387+
4388+
function reportInvalidUseInStrictMode(node: Identifier): void {
4389+
// declarationNameToString cannot be used here since it uses a backreference to 'parent' that is not yet set
4390+
var name = sourceText.substring(skipTrivia(sourceText, node.pos), node.end);
4391+
grammarErrorOnNode(node, Diagnostics.Invalid_use_of_0_in_strict_mode, name);
4392+
}
4393+
4394+
function performParameterChecks(node: ParameterDeclaration): boolean {
4395+
// It is a SyntaxError if the Identifier "eval" or the Identifier "arguments" occurs as the
4396+
// Identifier in a PropertySetParameterList of a PropertyAssignment that is contained in strict code
4397+
// or if its FunctionBody is strict code(11.1.5).
4398+
// It is a SyntaxError if the identifier eval or arguments appears within a FormalParameterList of a
4399+
// strict mode FunctionLikeDeclaration or FunctionExpression(13.1)
4400+
if (node.flags & NodeFlags.ParsedInStrictMode && isEvalOrArgumentsIdentifier(node.name)) {
4401+
reportInvalidUseInStrictMode(node.name);
4402+
return true;
4403+
}
4404+
4405+
return false;
4406+
}
43674407
}
43684408

4409+
43694410
export function createProgram(rootNames: string[], options: CompilerOptions, host: CompilerHost): Program {
43704411
var program: Program;
43714412
var files: SourceFile[] = [];

src/compiler/types.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -256,19 +256,23 @@ module ts {
256256
}
257257

258258
export const enum NodeFlags {
259-
Export = 0x00000001, // Declarations
260-
Ambient = 0x00000002, // Declarations
261-
QuestionMark = 0x00000004, // Parameter/Property/Method
262-
Rest = 0x00000008, // Parameter
263-
Public = 0x00000010, // Property/Method
264-
Private = 0x00000020, // Property/Method
265-
Protected = 0x00000040, // Property/Method
266-
Static = 0x00000080, // Property/Method
267-
MultiLine = 0x00000100, // Multi-line array or object literal
268-
Synthetic = 0x00000200, // Synthetic node (for full fidelity)
269-
DeclarationFile = 0x00000400, // Node is a .d.ts file
270-
Let = 0x00000800, // Variable declaration
271-
Const = 0x00001000, // Variable declaration
259+
Export = 0x00000001, // Declarations
260+
Ambient = 0x00000002, // Declarations
261+
QuestionMark = 0x00000004, // Parameter/Property/Method
262+
Rest = 0x00000008, // Parameter
263+
Public = 0x00000010, // Property/Method
264+
Private = 0x00000020, // Property/Method
265+
Protected = 0x00000040, // Property/Method
266+
Static = 0x00000080, // Property/Method
267+
MultiLine = 0x00000100, // Multi-line array or object literal
268+
Synthetic = 0x00000200, // Synthetic node (for full fidelity)
269+
DeclarationFile = 0x00000400, // Node is a .d.ts file
270+
Let = 0x00000800, // Variable declaration
271+
Const = 0x00001000, // Variable declaration
272+
273+
// Set if this node was parsed in strict mode. Used for grammar error checks, as well as
274+
// checking if the node can be reused in incremental settings.
275+
ParsedInStrictMode = 0x00002000,
272276

273277
Modifier = Export | Ambient | Public | Private | Protected | Static,
274278
AccessibilityModifier = Public | Private | Protected,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
// @target: ES5
12
"use strict";
23
var v = { set foo(eval) { } }

0 commit comments

Comments
 (0)