Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 13 additions & 17 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1058,11 +1058,11 @@ namespace ts {
}

function parseIdentifierName(): Identifier {
return createIdentifier(isIdentifierOrKeyword());
return createIdentifier(tokenIsIdentifierOrKeyword(token));
}

function isLiteralPropertyName(): boolean {
return isIdentifierOrKeyword() ||
return tokenIsIdentifierOrKeyword(token) ||
token === SyntaxKind.StringLiteral ||
token === SyntaxKind.NumericLiteral;
}
Expand All @@ -1086,7 +1086,7 @@ namespace ts {
}

function isSimplePropertyName() {
return token === SyntaxKind.StringLiteral || token === SyntaxKind.NumericLiteral || isIdentifierOrKeyword();
return token === SyntaxKind.StringLiteral || token === SyntaxKind.NumericLiteral || tokenIsIdentifierOrKeyword(token);
}

function parseComputedPropertyName(): ComputedPropertyName {
Expand Down Expand Up @@ -1213,9 +1213,9 @@ namespace ts {
case ParsingContext.HeritageClauses:
return isHeritageClause();
case ParsingContext.ImportOrExportSpecifiers:
return isIdentifierOrKeyword();
return tokenIsIdentifierOrKeyword(token);
case ParsingContext.JsxAttributes:
return isIdentifierOrKeyword() || token === SyntaxKind.OpenBraceToken;
return tokenIsIdentifierOrKeyword(token) || token === SyntaxKind.OpenBraceToken;
case ParsingContext.JsxChildren:
return true;
case ParsingContext.JSDocFunctionParameters:
Expand Down Expand Up @@ -1254,7 +1254,7 @@ namespace ts {

function nextTokenIsIdentifierOrKeyword() {
nextToken();
return isIdentifierOrKeyword();
return tokenIsIdentifierOrKeyword(token);
}

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

if (matchesPattern) {
Expand Down Expand Up @@ -2282,7 +2282,7 @@ namespace ts {
}
}

if (isIdentifierOrKeyword()) {
if (tokenIsIdentifierOrKeyword(token)) {
return parsePropertyOrMethodSignature();
}
}
Expand Down Expand Up @@ -4101,13 +4101,9 @@ namespace ts {
}
}

function isIdentifierOrKeyword() {
return token >= SyntaxKind.Identifier;
}

function nextTokenIsIdentifierOrKeywordOnSameLine() {
nextToken();
return isIdentifierOrKeyword() && !scanner.hasPrecedingLineBreak();
return tokenIsIdentifierOrKeyword(token) && !scanner.hasPrecedingLineBreak();
}

function nextTokenIsFunctionKeywordOnSameLine() {
Expand All @@ -4117,7 +4113,7 @@ namespace ts {

function nextTokenIsIdentifierOrKeywordOrNumberOnSameLine() {
nextToken();
return (isIdentifierOrKeyword() || token === SyntaxKind.NumericLiteral) && !scanner.hasPrecedingLineBreak();
return (tokenIsIdentifierOrKeyword(token) || token === SyntaxKind.NumericLiteral) && !scanner.hasPrecedingLineBreak();
}

function isDeclaration(): boolean {
Expand Down Expand Up @@ -4170,7 +4166,7 @@ namespace ts {
case SyntaxKind.ImportKeyword:
nextToken();
return token === SyntaxKind.StringLiteral || token === SyntaxKind.AsteriskToken ||
token === SyntaxKind.OpenBraceToken || isIdentifierOrKeyword();
token === SyntaxKind.OpenBraceToken || tokenIsIdentifierOrKeyword(token);
case SyntaxKind.ExportKeyword:
nextToken();
if (token === SyntaxKind.EqualsToken || token === SyntaxKind.AsteriskToken ||
Expand Down Expand Up @@ -4777,7 +4773,7 @@ namespace ts {

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

return isIdentifierOrKeyword();
return tokenIsIdentifierOrKeyword(token);
}

export function parseJSDocTypeExpressionForTests(content: string, start: number, length: number) {
Expand Down
7 changes: 6 additions & 1 deletion src/compiler/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ namespace ts {
(message: DiagnosticMessage, length: number): void;
}

/* @internal */
export function tokenIsIdentifierOrKeyword(token: SyntaxKind): boolean {
return token >= SyntaxKind.Identifier;
}

export interface Scanner {
getStartPos(): number;
getToken(): SyntaxKind;
Expand Down Expand Up @@ -1590,7 +1595,7 @@ namespace ts {
// Scans a JSX identifier; these differ from normal identifiers in that
// they allow dashes
function scanJsxIdentifier(): SyntaxKind {
if (token === SyntaxKind.Identifier) {
if (tokenIsIdentifierOrKeyword(token)) {
let firstCharPosition = pos;
while (pos < end) {
let ch = text.charCodeAt(pos);
Expand Down
14 changes: 14 additions & 0 deletions tests/baselines/reference/keywordInJsxIdentifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//// [keywordInJsxIdentifier.tsx]

declare var React: any;
<foo class-id/>;
<foo class/>;
<foo class-id="1"/>;
<foo class="1"/>;


//// [keywordInJsxIdentifier.js]
React.createElement("foo", {"class-id": true});
React.createElement("foo", {"class": true});
React.createElement("foo", {"class-id": "1"});
React.createElement("foo", {"class": "1"});
17 changes: 17 additions & 0 deletions tests/baselines/reference/keywordInJsxIdentifier.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
=== tests/cases/compiler/keywordInJsxIdentifier.tsx ===

declare var React: any;
>React : Symbol(React, Decl(keywordInJsxIdentifier.tsx, 1, 11))

<foo class-id/>;
>class-id : Symbol(unknown)

<foo class/>;
>class : Symbol(unknown)

<foo class-id="1"/>;
>class-id : Symbol(unknown)

<foo class="1"/>;
>class : Symbol(unknown)

25 changes: 25 additions & 0 deletions tests/baselines/reference/keywordInJsxIdentifier.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
=== tests/cases/compiler/keywordInJsxIdentifier.tsx ===

declare var React: any;
>React : any

<foo class-id/>;
><foo class-id/> : any
>foo : any
>class-id : any

<foo class/>;
><foo class/> : any
>foo : any
>class : any

<foo class-id="1"/>;
><foo class-id="1"/> : any
>foo : any
>class-id : any

<foo class="1"/>;
><foo class="1"/> : any
>foo : any
>class : any

7 changes: 7 additions & 0 deletions tests/cases/compiler/keywordInJsxIdentifier.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//@jsx: react

declare var React: any;
<foo class-id/>;
<foo class/>;
<foo class-id="1"/>;
<foo class="1"/>;