Skip to content

Commit

Permalink
chore: use ts.identifierToKeywordKind instead of originalKeywordKind
Browse files Browse the repository at this point in the history
  • Loading branch information
sosukesuzuki committed Mar 5, 2023
1 parent f9d0594 commit d022d8e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
24 changes: 19 additions & 5 deletions packages/typescript-estree/src/node-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { getModifiers } from './getModifiers';
import { xhtmlEntities } from './jsx/xhtml-entities';
import type { TSESTree } from './ts-estree';
import { AST_NODE_TYPES, AST_TOKEN_TYPES } from './ts-estree';
import { typescriptVersionIsAtLeast } from './version-check';

const isAtLeast50 = typescriptVersionIsAtLeast['5.0'];

const SyntaxKind = ts.SyntaxKind;

Expand Down Expand Up @@ -433,12 +436,19 @@ export function isChildUnwrappableOptionalChain(
export function getTokenType(
token: ts.Identifier | ts.Token<ts.SyntaxKind>,
): Exclude<AST_TOKEN_TYPES, AST_TOKEN_TYPES.Line | AST_TOKEN_TYPES.Block> {
if ('originalKeywordKind' in token && token.originalKeywordKind) {
if (token.originalKeywordKind === SyntaxKind.NullKeyword) {
let keywordKind: ts.SyntaxKind | undefined;
if (isAtLeast50 && token.kind === SyntaxKind.Identifier) {
keywordKind = ts.identifierToKeywordKind(token as ts.Identifier);
} else if ('originalKeywordKind' in token) {
// eslint-disable-next-line deprecation/deprecation -- intentional fallback for older TS versions
keywordKind = token.originalKeywordKind;
}
if (keywordKind) {
if (keywordKind === SyntaxKind.NullKeyword) {
return AST_TOKEN_TYPES.Null;
} else if (
token.originalKeywordKind >= SyntaxKind.FirstFutureReservedWord &&
token.originalKeywordKind <= SyntaxKind.LastKeyword
keywordKind >= SyntaxKind.FirstFutureReservedWord &&
keywordKind <= SyntaxKind.LastKeyword
) {
return AST_TOKEN_TYPES.Identifier;
}
Expand Down Expand Up @@ -663,7 +673,11 @@ export function firstDefined<T, U>(
}

export function identifierIsThisKeyword(id: ts.Identifier): boolean {
return id.originalKeywordKind === SyntaxKind.ThisKeyword;
return (
// eslint-disable-next-line deprecation/deprecation -- intentional for older TS versions
(isAtLeast50 ? ts.identifierToKeywordKind(id) : id.originalKeywordKind) ===
SyntaxKind.ThisKeyword
);
}

export function isThisIdentifier(
Expand Down
2 changes: 2 additions & 0 deletions packages/typescript-estree/src/version-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const versions = [
'4.6',
'4.7',
'4.8',
'4.9',
'5.0',
] as const;
type Versions = typeof versions extends ArrayLike<infer U> ? U : never;

Expand Down

0 comments on commit d022d8e

Please sign in to comment.