Skip to content

Commit fe7a3a5

Browse files
author
Yui T
committed
Address CR
1 parent 8448ba7 commit fe7a3a5

3 files changed

Lines changed: 66 additions & 19 deletions

File tree

src/compiler/checker.ts

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11925,10 +11925,7 @@ module ts {
1192511925
// GRAMMAR CHECKING
1192611926
function isReservedwordInStrictMode(node: Identifier): boolean {
1192711927
// Check that originalStrictModeSyntaxKind is less than LastFurtureReservedWord to see if an Identifier is a strict-mode reserved word
11928-
if ((node.parserContextFlags & ParserContextFlags.StrictMode) && node.originalStrictModeSyntaxKind <= SyntaxKind.LastFutureReservedWord) {
11929-
return true;
11930-
}
11931-
return false
11928+
return (node.parserContextFlags & ParserContextFlags.StrictMode) && node.originalStrictModeSyntaxKind <= SyntaxKind.LastFutureReservedWord;
1193211929
}
1193311930

1193411931
function reportStrictModeGrammarErrorInClassDeclaration(identifier: Identifier, message: DiagnosticMessage, arg0?: any, arg1?: any, arg2?: any): boolean {
@@ -11959,7 +11956,7 @@ module ts {
1195911956
let name = element.name;
1196011957
if (name.originalStrictModeSyntaxKind) {
1196111958
let nameText = declarationNameToString(name);
11962-
reportError = grammarErrorOnNode(name, Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode, nameText);
11959+
reportError = reportError || grammarErrorOnNode(name, Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode, nameText);
1196311960
}
1196411961
}
1196511962
return reportError;
@@ -12012,22 +12009,31 @@ module ts {
1201212009
}
1201312010
// Report an error for each identifier in QualifiedName
1201412011
// Example:
12015-
// foo (x: public.private.bar) // no Error
12016-
// foo (x: public.private.package) // error at package
12012+
// foo (x: B.private.bar) // error at private
12013+
// foo (x: public.private.package) // error at public, private, and package
1201712014
else if (typeName.kind === SyntaxKind.QualifiedName) {
12018-
// Report strict mode at the property of memberExpression
12015+
// Walk from right to left and report a possible error at each Identifier in QualifiedName
12016+
// Example:
12017+
// x1: public.private.package // error at public and private
12018+
let qualifiedName = typeName;
12019+
while (qualifiedName && qualifiedName.kind === SyntaxKind.QualifiedName) {
12020+
checkGrammarTypeNameInStrictMode((<QualifiedName>qualifiedName).right);
12021+
qualifiedName = (<QualifiedName>qualifiedName).left;
12022+
}
12023+
12024+
// Report an error at the last Identifier in QualifiedName
1201912025
// Example:
12020-
// x1: public.private.package // error at package as memberExpression can be IdentifierName
12021-
checkGrammarTypeNameInStrictMode((<QualifiedName>typeName).right);
12026+
// x1: public.private.package // error at package
12027+
checkGrammarTypeNameInStrictMode(<Identifier>qualifiedName);
1202212028
}
1202312029
}
1202412030

1202512031
// This function will report an error for every identifier in property access expression
1202612032
// whether it violates strict mode reserved words.
1202712033
// Example:
1202812034
// public // error at public
12029-
// public.private.package // error at package
12030-
// public.private.B // no error
12035+
// public.private.package // error at public
12036+
// B.private.B // no error
1203112037
function checkGrammarHeritageClauseElementInStrictMode(expression: Expression) {
1203212038
// Example:
1203312039
// class C extends public // error at public
@@ -12037,10 +12043,18 @@ module ts {
1203712043
else if (expression && expression.kind === SyntaxKind.PropertyAccessExpression) {
1203812044
let propertyAccessExp = <PropertyAccessExpression>expression;
1203912045

12046+
// Walk from left to right in PropertyAccessExpression until we are at the left most expression
12047+
// in PropertyAccessExpression. According to grammar production of MemberExpression,
12048+
// the left component expression is a PrimaryExpression (i.e. Identifier) while the other
12049+
// component after dots can be IdentifierName.
12050+
while (propertyAccessExp && propertyAccessExp.kind === SyntaxKind.PropertyAccessExpression) {
12051+
propertyAccessExp = <PropertyAccessExpression>propertyAccessExp.expression;
12052+
}
12053+
1204012054
// Report strict mode at the property of memberExpression
1204112055
// Example:
12042-
// public.private.package // error at package as memberExpression can be IdentifierName
12043-
checkGrammarIdentifierInStrictMode((<PropertyAccessExpression>expression).name);
12056+
// public.private.package // error at public as it is parsed as an Identifier in the PropertyAccessExpression
12057+
checkGrammarIdentifierInStrictMode(propertyAccessExp);
1204412058
}
1204512059

1204612060
}

tests/baselines/reference/strictModeCode1.errors.txt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ tests/cases/compiler/strictModeCode1.ts(14,18): error TS1213: Identifier expecte
1414
tests/cases/compiler/strictModeCode1.ts(21,9): error TS1213: Identifier expected. 'public' is a reserved word in strict mode. Class definitions are automatically in strict mode.
1515
tests/cases/compiler/strictModeCode1.ts(21,17): error TS1213: Identifier expected. 'private' is a reserved word in strict mode. Class definitions are automatically in strict mode.
1616
tests/cases/compiler/strictModeCode1.ts(23,20): error TS1213: Identifier expected. 'public' is a reserved word in strict mode. Class definitions are automatically in strict mode.
17+
tests/cases/compiler/strictModeCode1.ts(25,20): error TS1213: Identifier expected. 'public' is a reserved word in strict mode. Class definitions are automatically in strict mode.
1718
tests/cases/compiler/strictModeCode1.ts(25,20): error TS2304: Cannot find name 'public'.
19+
tests/cases/compiler/strictModeCode1.ts(26,21): error TS1213: Identifier expected. 'public' is a reserved word in strict mode. Class definitions are automatically in strict mode.
1820
tests/cases/compiler/strictModeCode1.ts(26,21): error TS2304: Cannot find name 'public'.
19-
tests/cases/compiler/strictModeCode1.ts(26,36): error TS1213: Identifier expected. 'implements' is a reserved word in strict mode. Class definitions are automatically in strict mode.
2021
tests/cases/compiler/strictModeCode1.ts(27,17): error TS1213: Identifier expected. 'package' is a reserved word in strict mode. Class definitions are automatically in strict mode.
2122
tests/cases/compiler/strictModeCode1.ts(27,17): error TS2304: Cannot find name 'package'.
23+
tests/cases/compiler/strictModeCode1.ts(28,17): error TS1213: Identifier expected. 'package' is a reserved word in strict mode. Class definitions are automatically in strict mode.
2224
tests/cases/compiler/strictModeCode1.ts(28,17): error TS2304: Cannot find name 'package'.
2325

2426

25-
==== tests/cases/compiler/strictModeCode1.ts (22 errors) ====
27+
==== tests/cases/compiler/strictModeCode1.ts (24 errors) ====
2628
interface public { }
2729

2830
class Foo {
@@ -81,17 +83,21 @@ tests/cases/compiler/strictModeCode1.ts(28,17): error TS2304: Cannot find name '
8183

8284
class F implements public.private.B { }
8385
~~~~~~
86+
!!! error TS1213: Identifier expected. 'public' is a reserved word in strict mode. Class definitions are automatically in strict mode.
87+
~~~~~~
8488
!!! error TS2304: Cannot find name 'public'.
8589
class F1 implements public.private.implements { }
8690
~~~~~~
91+
!!! error TS1213: Identifier expected. 'public' is a reserved word in strict mode. Class definitions are automatically in strict mode.
92+
~~~~~~
8793
!!! error TS2304: Cannot find name 'public'.
88-
~~~~~~~~~~
89-
!!! error TS1213: Identifier expected. 'implements' is a reserved word in strict mode. Class definitions are automatically in strict mode.
9094
class G extends package { }
9195
~~~~~~~
9296
!!! error TS1213: Identifier expected. 'package' is a reserved word in strict mode. Class definitions are automatically in strict mode.
9397
~~~~~~~
9498
!!! error TS2304: Cannot find name 'package'.
9599
class H extends package.A { }
96100
~~~~~~~
101+
!!! error TS1213: Identifier expected. 'package' is a reserved word in strict mode. Class definitions are automatically in strict mode.
102+
~~~~~~~
97103
!!! error TS2304: Cannot find name 'package'.

tests/baselines/reference/strictModeCode2.errors.txt

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,25 @@ tests/cases/compiler/strictModeCode2.ts(13,20): error TS1212: Identifier expecte
1818
tests/cases/compiler/strictModeCode2.ts(13,28): error TS1212: Identifier expected. 'package' is a reserved word in strict mode
1919
tests/cases/compiler/strictModeCode2.ts(15,25): error TS9003: 'class' expressions are not currently supported.
2020
tests/cases/compiler/strictModeCode2.ts(17,9): error TS2300: Duplicate identifier 'b'.
21+
tests/cases/compiler/strictModeCode2.ts(17,12): error TS1215: Type expected. 'public' is a reserved word in strict mode
2122
tests/cases/compiler/strictModeCode2.ts(17,12): error TS2304: Cannot find name 'public'.
23+
tests/cases/compiler/strictModeCode2.ts(19,21): error TS1215: Type expected. 'private' is a reserved word in strict mode
2224
tests/cases/compiler/strictModeCode2.ts(19,21): error TS2304: Cannot find name 'private'.
25+
tests/cases/compiler/strictModeCode2.ts(20,22): error TS1215: Type expected. 'private' is a reserved word in strict mode
2326
tests/cases/compiler/strictModeCode2.ts(20,22): error TS2304: Cannot find name 'private'.
27+
tests/cases/compiler/strictModeCode2.ts(20,30): error TS1215: Type expected. 'package' is a reserved word in strict mode
28+
tests/cases/compiler/strictModeCode2.ts(21,22): error TS1215: Type expected. 'private' is a reserved word in strict mode
2429
tests/cases/compiler/strictModeCode2.ts(21,22): error TS2304: Cannot find name 'private'.
30+
tests/cases/compiler/strictModeCode2.ts(21,30): error TS1215: Type expected. 'package' is a reserved word in strict mode
2531
tests/cases/compiler/strictModeCode2.ts(21,38): error TS1215: Type expected. 'protected' is a reserved word in strict mode
2632
tests/cases/compiler/strictModeCode2.ts(22,9): error TS2300: Duplicate identifier 'b'.
33+
tests/cases/compiler/strictModeCode2.ts(22,12): error TS1215: Type expected. 'interface' is a reserved word in strict mode
2734
tests/cases/compiler/strictModeCode2.ts(22,12): error TS2304: Cannot find name 'interface'.
35+
tests/cases/compiler/strictModeCode2.ts(22,22): error TS1215: Type expected. 'package' is a reserved word in strict mode
36+
tests/cases/compiler/strictModeCode2.ts(22,30): error TS1215: Type expected. 'implements' is a reserved word in strict mode
2837

2938

30-
==== tests/cases/compiler/strictModeCode2.ts (27 errors) ====
39+
==== tests/cases/compiler/strictModeCode2.ts (36 errors) ====
3140
let let = 10;
3241

3342
function foo() {
@@ -86,24 +95,42 @@ tests/cases/compiler/strictModeCode2.ts(22,12): error TS2304: Cannot find name '
8695
~
8796
!!! error TS2300: Duplicate identifier 'b'.
8897
~~~~~~
98+
!!! error TS1215: Type expected. 'public' is a reserved word in strict mode
99+
~~~~~~
89100
!!! error TS2304: Cannot find name 'public'.
90101

91102
function foo(x: private.x) { }
92103
~~~~~~~
104+
!!! error TS1215: Type expected. 'private' is a reserved word in strict mode
105+
~~~~~~~
93106
!!! error TS2304: Cannot find name 'private'.
94107
function foo1(x: private.package.x) { }
95108
~~~~~~~
109+
!!! error TS1215: Type expected. 'private' is a reserved word in strict mode
110+
~~~~~~~
96111
!!! error TS2304: Cannot find name 'private'.
112+
~~~~~~~
113+
!!! error TS1215: Type expected. 'package' is a reserved word in strict mode
97114
function foo2(x: private.package.protected) { }
98115
~~~~~~~
116+
!!! error TS1215: Type expected. 'private' is a reserved word in strict mode
117+
~~~~~~~
99118
!!! error TS2304: Cannot find name 'private'.
119+
~~~~~~~
120+
!!! error TS1215: Type expected. 'package' is a reserved word in strict mode
100121
~~~~~~~~~
101122
!!! error TS1215: Type expected. 'protected' is a reserved word in strict mode
102123
let b: interface.package.implements.B;
103124
~
104125
!!! error TS2300: Duplicate identifier 'b'.
105126
~~~~~~~~~
127+
!!! error TS1215: Type expected. 'interface' is a reserved word in strict mode
128+
~~~~~~~~~
106129
!!! error TS2304: Cannot find name 'interface'.
130+
~~~~~~~
131+
!!! error TS1215: Type expected. 'package' is a reserved word in strict mode
132+
~~~~~~~~~~
133+
!!! error TS1215: Type expected. 'implements' is a reserved word in strict mode
107134
}
108135

109136

0 commit comments

Comments
 (0)