8000 Merge pull request #4705 from Microsoft/keywordInJsxIdentifier · johnangularjs/TypeScript@c90276f · GitHub
[go: up one dir, main page]

Skip to content

Commit c90276f

Browse files
committed
Merge pull request microsoft#4705 from Microsoft/keywordInJsxIdentifier
allow jsx identifiers to start with keywords
2 parents 047b6ac + 5989d48 commit c90276f

File tree

6 files changed

+82
-18
lines changed

6 files changed

+82
-18
lines changed

src/compiler/parser.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,11 +1058,11 @@ namespace ts {
10581058
}
10591059

10601060
function parseIdentifierName(): Identifier {
1061-
return createIdentifier(isIdentifierOrKeyword());
1061+
return createIdentifier(tokenIsIdentifierOrKeyword(token));
10621062
}
10631063

10641064
function isLiteralPropertyName(): boolean {
1065-
return isIdentifierOrKeyword() ||
1065+
return tokenIsIdentifierOrKeyword(token) ||
10661066
token === SyntaxKind.StringLiteral ||
10671067
token === SyntaxKind.NumericLiteral;
10681068
}
@@ -1086,7 +1086,7 @@ namespace ts {
10861086
}
10871087

10881088
function isSimplePropertyName() {
1089-
return token === SyntaxKind.StringLiteral || token === SyntaxKind.NumericLiteral || isIdentifierOrKeyword();
1089+
return token === SyntaxKind.StringLiteral || token === SyntaxKind.NumericLiteral || tokenIsIdentifierOrKeyword(token);
10901090
}
10911091

10921092
function parseComputedPropertyName(): ComputedPropertyName {
@@ -1213,9 +1213,9 @@ namespace ts {
12131213
case ParsingContext.HeritageClauses:
12141214
return isHeritageClause();
12151215
case ParsingContext.ImportOrExportSpecifiers:
1216-
return isIdentifierOrKeyword();
1216+
return tokenIsIdentifierOrKeyword(token);
12171217
case ParsingContext.JsxAttributes:
1218-
return isIdentifierOrKeyword() || token === SyntaxKind.OpenBraceToken;
1218+
return tokenIsIdentifierOrKeyword(token) || token === SyntaxKind.OpenBraceToken;
12191219
case ParsingContext.JsxChildren:
12201220
return true;
12211221
case ParsingContext.JSDocFunctionParameters:
@@ -1254,7 +1254,7 @@ namespace ts {
12541254

12551255
function nextTokenIsIdentifierOrKeyword() {
12561256
nextToken();
1257-
return isIdentifierOrKeyword();
1257+
return tokenIsIdentifierOrKeyword(token);
12581258
}
12591259

12601260
function isHeritageClauseExtendsOrImplementsKeyword(): boolean {
@@ -1824,7 +1824,7 @@ namespace ts {
18241824
// the code would be implicitly: "name.identifierOrKeyword; identifierNameOrKeyword".
18251825
// In the first case though, ASI will not take effect because there is not a
18261826
// line terminator after the identifier or keyword.
1827-
if (scanner.hasPrecedingLineBreak() && isIdentifierOrKeyword()) {
1827+
if (scanner.hasPrecedingLineBreak() && tokenIsIdentifierOrKeyword(token)) {
18281828
let matchesPattern = lookAhead(nextTokenIsIdentifierOrKeywordOnSameLine);
18291829

18301830
if (matchesPattern) {
@@ -2282,7 +2282,7 @@ namespace ts {
22822282
}
22832283
}
22842284

2285-
if (isIdentifierOrKeyword()) {
2285+
if (tokenIsIdentifierOrKeyword(token)) {
22862286
return parsePropertyOrMethodSignature();
22872287
}
22882288
}
@@ -4101,13 +4101,9 @@ namespace ts {
41014101
}
41024102
}
41034103

4104-
function isIdentifierOrKeyword() {
4105-
return token >= SyntaxKind.Identifier;
4106-
}
4107-
41084104
function nextTokenIsIdentifierOrKeywordOnSameLine() {
41094105
nextToken();
4110-
return isIdentifierOrKeyword() && !scanner.hasPrecedingLineBreak();
4106+
return tokenIsIdentifierOrKeyword(token) && !scanner.hasPrecedingLineBreak();
41114107
}
41124108

41134109
function nextTokenIsFunctionKeywordOnSameLine() {
@@ -4117,7 +4113,7 @@ namespace ts {
41174113

41184114
function nextTokenIsIdentifierOrKeywordOrNumberOnSameLine() {
41194115
nextToken();
4120-
return (isIdentifierOrKeyword() || token === SyntaxKind.NumericLiteral) && !scanner.hasPrecedingLineBreak();
4116+
return (tokenIsIdentifierOrKeyword(token) || token === SyntaxKind.NumericLiteral) && !scanner.hasPrecedingLineBreak();
41214117
}
41224118

41234119
function isDeclaration(): boolean {
@@ -4170,7 +4166,7 @@ namespace ts {
41704166
case SyntaxKind.ImportKeyword:
41714167
nextToken();
41724168
return token === SyntaxKind.StringLiteral || token === SyntaxKind.AsteriskToken ||
4173-
token === SyntaxKind.OpenBraceToken || isIdentifierOrKeyword();
4169+
token === SyntaxKind.OpenBraceToken || tokenIsIdentifierOrKeyword(token);
41744170
case SyntaxKind.ExportKeyword:
41754171
nextToken();
41764172
if (token === SyntaxKind.EqualsToken || token === SyntaxKind.AsteriskToken ||
@@ -4777,7 +4773,7 @@ namespace ts {
47774773

47784774
// It is very important that we check this *after* checking indexers because
47794775
// the [ token can start an index signature or a computed property name
4780-
if (isIdentifierOrKeyword() ||
4776+
if (tokenIsIdentifierOrKeyword(token) ||
47814777
token === SyntaxKind.StringLiteral ||
47824778
token === SyntaxKind.NumericLiteral ||
47834779
token === SyntaxKind.AsteriskToken ||
@@ -5320,7 +5316,7 @@ namespace ts {
53205316
return true;
53215317
}
53225318

5323-
return isIdentifierOrKeyword();
5319+
return tokenIsIdentifierOrKeyword(token);
53245320
}
53255321

53265322
export function parseJSDocTypeExpressionForTests(content: string, start: number, length: number) {

src/compiler/scanner.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ namespace ts {
66
(message: DiagnosticMessage, length: number): void;
77
}
88

9+
/* @internal */
10+
export function tokenIsIdentifierOrKeyword(token: SyntaxKind): boolean {
11+
return token >= SyntaxKind.Identifier;
12+
}
13+
914
export interface Scanner {
1015
getStartPos(): number;
1116
getToken(): SyntaxKind;
@@ -1590,7 +1595,7 @@ namespace ts {
15901595
// Scans a JSX identifier; these differ from normal identifiers in that
15911596
// they allow dashes
15921597
function scanJsxIdentifier(): SyntaxKind {
1593-
if (token === SyntaxKind.Identifier) {
1598+
if (tokenIsIdentifierOrKeyword(token)) {
15941599
let firstCharPosition = pos;
15951600
while (pos < end) {
15961601
let ch = text.charCodeAt(pos);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//// [keywordInJsxIdentifier.tsx]
2+
3+
declare var React: any;
4+
<foo class-id/>;
5+
<foo class/>;
6+
<foo class-id="1"/>;
7+
<foo class="1"/>;
8+
9+
10+
//// [keywordInJsxIdentifier.js]
11+
React.createElement("foo", {"class-id&qu 10000 ot;: true});
12+
React.createElement("foo", {"class": true});
13+
React.createElement("foo", {"class-id": "1"});
14+
React.createElement("foo", {"class": "1"});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/compiler/keywordInJsxIdentifier.tsx ===
2+
3+
declare var React: any;
4+
>React : Symbol(React, Decl(keywordInJsxIdentifier.tsx, 1, 11))
5+
6+
<foo class-id/>;
7+
>class-id : Symbol(unknown)
8+
9+
<foo class/>;
10+
>class : Symbol(unknown)
11+
12+
<foo class-id="1"/>;
13+
>class-id : Symbol(unknown)
14+
15+
<foo class="1"/>;
16+
>class : Symbol(unknown)
17+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
=== tests/cases/compiler/keywordInJsxIdentifier.tsx ===
2+
3+
declare var React: any;
4+
>React : any
5+
6+
<foo class-id/>;
7+
><foo class-id/> : any
8+
>foo : any
9+
>class-id : any
10+
11+
<foo class/>;
12+
><foo class/> : any
13+
>foo : any
14+
>class : any
15+
16+
<foo class-id="1"/>;
17+
><foo class-id="1"/> : any
18+
>foo : any
19+
>class-id : any
20+
21+
<foo class="1"/>;
22+
><foo class="1"/> : any
23+
>foo : any
24+
>class : any
25+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//@jsx: react
2+
3+
declare var React: any;
4+
<foo class-id/>;
5+
<foo class/>;
6+
<foo class-id="1"/>;
7+
<foo class="1"/>;

0 commit comments

Comments
 (0)
0