Skip to content

Commit 8a61566

Browse files
Extract context flags into their own enum.
1 parent 14f90b8 commit 8a61566

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

src/compiler/parser.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -897,12 +897,16 @@ module ts {
897897
// understand when these values should be changed versus when they should be inherited.
898898
var strictModeContext = false;
899899
var disallowInContext = false;
900+
var yieldContext: boolean = false;
901+
var generatorParameterContext: boolean = false;
900902
var contextFlags: number = 0;
901903

902904
function updateContextFlags() {
903905
contextFlags =
904-
(strictModeContext ? NodeFlags.ParsedInStrictModeContext : 0) |
905-
(disallowInContext ? NodeFlags.ParsedInDisallowInContext : 0);
906+
(strictModeContext ? ParserContextFlags.ParsedInStrictModeContext : 0) |
907+
(disallowInContext ? ParserContextFlags.ParsedInDisallowInContext : 0) |
908+
(yieldContext ? ParserContextFlags.ParsedInYieldContext : 0) |
909+
(generatorParameterContext ? ParserContextFlags.ParsedInGeneratorParameterContext : 0);
906910
}
907911

908912
function setStrictModeContext(val: boolean) {
@@ -3759,7 +3763,7 @@ module ts {
37593763
}
37603764

37613765
function checkBinaryExpression(node: BinaryExpression) {
3762-
if (node.parserContextFlags & NodeFlags.ParsedInStrictModeContext) {
3766+
if (node.parserContextFlags & ParserContextFlags.ParsedInStrictModeContext) {
37633767
if (isLeftHandSideExpression(node.left) && isAssignmentOperator(node.operator)) {
37643768
if (isEvalOrArgumentsIdentifier(node.left)) {
37653769
// ECMA 262 (Annex C) The identifier eval or arguments may not appear as the LeftHandSideExpression of an
@@ -3911,7 +3915,7 @@ module ts {
39113915
var colonStart = skipTrivia(sourceText, node.variable.end);
39123916
return grammarErrorAtPos(colonStart, ":".length, Diagnostics.Catch_clause_parameter_cannot_have_a_type_annotation);
39133917
}
3914-
if (node.parserContextFlags & NodeFlags.ParsedInStrictModeContext && isEvalOrArgumentsIdentifier(node.variable)) {
3918+
if (node.parserContextFlags & ParserContextFlags.ParsedInStrictModeContext && isEvalOrArgumentsIdentifier(node.variable)) {
39153919
// It is a SyntaxError if a TryStatement with a Catch occurs within strict code and the Identifier of the
39163920
// Catch production is eval or arguments
39173921
return reportInvalidUseInStrictMode(node.variable);
@@ -4031,7 +4035,7 @@ module ts {
40314035
}
40324036

40334037
function checkFunctionName(name: Node) {
4034-
if (name && name.parserContextFlags & NodeFlags.ParsedInStrictModeContext && isEvalOrArgumentsIdentifier(name)) {
4038+
if (name && name.parserContextFlags & ParserContextFlags.ParsedInStrictModeContext && isEvalOrArgumentsIdentifier(name)) {
40354039
// It is a SyntaxError to use within strict mode code the identifiers eval or arguments as the
40364040
// Identifier of a FunctionLikeDeclaration or FunctionExpression or as a formal parameter name(13.1)
40374041
return reportInvalidUseInStrictMode(<Identifier>name);
@@ -4152,7 +4156,7 @@ module ts {
41524156
var GetAccessor = 2;
41534157
var SetAccesor = 4;
41544158
var GetOrSetAccessor = GetAccessor | SetAccesor;
4155-
var inStrictMode = (node.parserContextFlags & NodeFlags.ParsedInStrictModeContext) !== 0;
4159+
var inStrictMode = (node.parserContextFlags & ParserContextFlags.ParsedInStrictModeContext) !== 0;
41564160

41574161
for (var i = 0, n = node.properties.length; i < n; i++) {
41584162
var prop = node.properties[i];
@@ -4216,7 +4220,7 @@ module ts {
42164220

42174221
function checkNumericLiteral(node: LiteralExpression): boolean {
42184222
if (node.flags & NodeFlags.OctalLiteral) {
4219-
if (node.parserContextFlags & NodeFlags.ParsedInStrictModeContext) {
4223+
if (node.parserContextFlags & ParserContextFlags.ParsedInStrictModeContext) {
42204224
return grammarErrorOnNode(node, Diagnostics.Octal_literals_are_not_allowed_in_strict_mode);
42214225
}
42224226
else if (languageVersion >= ScriptTarget.ES5) {
@@ -4360,7 +4364,7 @@ module ts {
43604364
// or if its FunctionBody is strict code(11.1.5).
43614365
// It is a SyntaxError if the identifier eval or arguments appears within a FormalParameterList of a
43624366
// strict mode FunctionLikeDeclaration or FunctionExpression(13.1)
4363-
if (node.parserContextFlags & NodeFlags.ParsedInStrictModeContext && isEvalOrArgumentsIdentifier(node.name)) {
4367+
if (node.parserContextFlags & ParserContextFlags.ParsedInStrictModeContext && isEvalOrArgumentsIdentifier(node.name)) {
43644368
return reportInvalidUseInStrictMode(node.name);
43654369
}
43664370
}
@@ -4419,13 +4423,13 @@ module ts {
44194423
// The identifier eval or arguments may not appear as the LeftHandSideExpression of an
44204424
// Assignment operator(11.13) or of a PostfixExpression(11.3) or as the UnaryExpression
44214425
// operated upon by a Prefix Increment(11.4.4) or a Prefix Decrement(11.4.5) operator.
4422-
if (node.parserContextFlags & NodeFlags.ParsedInStrictModeContext && isEvalOrArgumentsIdentifier(node.operand)) {
4426+
if (node.parserContextFlags & ParserContextFlags.ParsedInStrictModeContext && isEvalOrArgumentsIdentifier(node.operand)) {
44234427
return reportInvalidUseInStrictMode(<Identifier>node.operand);
44244428
}
44254429
}
44264430

44274431
function checkPrefixOperator(node: UnaryExpression) {
4428-
if (node.parserContextFlags & NodeFlags.ParsedInStrictModeContext) {
4432+
if (node.parserContextFlags & ParserContextFlags.ParsedInStrictModeContext) {
44294433
// The identifier eval or arguments may not appear as the LeftHandSideExpression of an
44304434
// Assignment operator(11.13) or of a PostfixExpression(11.3) or as the UnaryExpression
44314435
// operated upon by a Prefix Increment(11.4.4) or a Prefix Decrement(11.4.5) operator
@@ -4610,7 +4614,7 @@ module ts {
46104614
if (!inAmbientContext && !node.initializer && isConst(node)) {
46114615
return grammarErrorOnNode(node, Diagnostics.const_declarations_must_be_initialized);
46124616
}
4613-
if (node.parserContextFlags & NodeFlags.ParsedInStrictModeContext && isEvalOrArgumentsIdentifier(node.name)) {
4617+
if (node.parserContextFlags & ParserContextFlags.ParsedInStrictModeContext && isEvalOrArgumentsIdentifier(node.name)) {
46144618
// It is a SyntaxError if a VariableDeclaration or VariableDeclarationNoIn occurs within strict code
46154619
// and its Identifier is eval or arguments
46164620
return reportInvalidUseInStrictMode(node.name);
@@ -4672,7 +4676,7 @@ module ts {
46724676
}
46734677

46744678
function checkWithStatement(node: WithStatement): boolean {
4675-
if (node.parserContextFlags & NodeFlags.ParsedInStrictModeContext) {
4679+
if (node.parserContextFlags & ParserContextFlags.ParsedInStrictModeContext) {
46764680
// Strict mode code may not include a WithStatement. The occurrence of a WithStatement in such
46774681
// a context is an
46784682
return grammarErrorOnFirstToken(node, Diagnostics.with_statements_are_not_allowed_in_strict_mode);

src/compiler/types.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -270,18 +270,22 @@ module ts {
270270
Let = 0x00000800, // Variable declaration
271271
Const = 0x00001000, // Variable declaration
272272

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-
ParsedInStrictModeContext = 0x00002000,
276-
ParsedInDisallowInContext = 0x00004000,
277-
278-
OctalLiteral = 0x00008000,
273+
OctalLiteral = 0x00002000,
279274

280275
Modifier = Export | Ambient | Public | Private | Protected | Static,
281276
AccessibilityModifier = Public | Private | Protected,
282277
BlockScoped = Let | Const
283278
}
284279

280+
export const enum ParserContextFlags {
281+
// Set if this node was parsed in strict mode. Used for grammar error checks, as well as
282+
// checking if the node can be reused in incremental settings.
283+
ParsedInStrictModeContext = 0x1,
284+
ParsedInDisallowInContext = 0x2,
285+
ParsedInYieldContext = 0x4,
286+
ParsedInGeneratorParameterContext = 0x8,
287+
}
288+
285289
export interface Node extends TextRange {
286290
kind: SyntaxKind;
287291
flags: NodeFlags;

0 commit comments

Comments
 (0)