Skip to content

Commit f5101e2

Browse files
author
Andy
authored
Find-all-references: Don't crash on 'typeof import' (#23448)
* Find-all-references: Don't crash on 'typeof import' * Move `| ImportTypeNode` out of `AnyImportOrReExport`
1 parent 391c056 commit f5101e2

File tree

6 files changed

+24
-3
lines changed

6 files changed

+24
-3
lines changed

src/compiler/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3193,7 +3193,8 @@ namespace ts {
31933193
export type AnyValidImportOrReExport =
31943194
| (ImportDeclaration | ExportDeclaration) & { moduleSpecifier: StringLiteral }
31953195
| ImportEqualsDeclaration & { moduleReference: ExternalModuleReference & { expression: StringLiteral } }
3196-
| RequireOrImportCall;
3196+
| RequireOrImportCall
3197+
| ImportTypeNode & { argument: LiteralType };
31973198

31983199
/* @internal */
31993200
export type RequireOrImportCall = CallExpression & { arguments: [StringLiteralLike] };

src/compiler/utilities.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1710,8 +1710,10 @@ namespace ts {
17101710
return (node.parent as ExternalModuleReference).parent as AnyValidImportOrReExport;
17111711
case SyntaxKind.CallExpression:
17121712
return node.parent as AnyValidImportOrReExport;
1713+
case SyntaxKind.LiteralType:
1714+
return cast(node.parent.parent, isImportTypeNode) as ImportTypeNode & { argument: LiteralType };
17131715
default:
1714-
return Debug.fail(Debug.showSyntaxKind(node));
1716+
return Debug.fail(Debug.showSyntaxKind(node.parent));
17151717
}
17161718
}
17171719

@@ -4926,6 +4928,10 @@ namespace ts {
49264928
return node.kind === SyntaxKind.LiteralType;
49274929
}
49284930

4931+
export function isImportTypeNode(node: Node): node is ImportTypeNode {
4932+
return node.kind === SyntaxKind.ImportType;
4933+
}
4934+
49294935
// Binding patterns
49304936

49314937
export function isObjectBindingPattern(node: Node): node is ObjectBindingPattern {

src/services/importTracker.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace ts.FindAllReferences {
3333
interface AmbientModuleDeclaration extends ModuleDeclaration { body?: ModuleBlock; }
3434
type SourceFileLike = SourceFile | AmbientModuleDeclaration;
3535
// Identifier for the case of `const x = require("y")`.
36-
type Importer = AnyImportOrReExport | Identifier;
36+
type Importer = AnyImportOrReExport | ImportTypeNode | Identifier;
3737
type ImporterOrCallExpression = Importer | CallExpression;
3838

3939
/** Returns import statements that directly reference the exporting module, and a list of files that may access the module through a namespace. */
@@ -215,6 +215,10 @@ namespace ts.FindAllReferences {
215215
return;
216216
}
217217

218+
if (decl.kind === SyntaxKind.ImportType) {
219+
return;
220+
}
221+
218222
// Ignore if there's a grammar error
219223
if (decl.moduleSpecifier.kind !== SyntaxKind.StringLiteral) {
220224
return;

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3169,6 +3169,7 @@ declare namespace ts {
31693169
function isIndexedAccessTypeNode(node: Node): node is IndexedAccessTypeNode;
31703170
function isMappedTypeNode(node: Node): node is MappedTypeNode;
31713171
function isLiteralTypeNode(node: Node): node is LiteralTypeNode;
3172+
function isImportTypeNode(node: Node): node is ImportTypeNode;
31723173
function isObjectBindingPattern(node: Node): node is ObjectBindingPattern;
31733174
function isArrayBindingPattern(node: Node): node is ArrayBindingPattern;
31743175
function isBindingElement(node: Node): node is BindingElement;

tests/baselines/reference/api/typescript.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3169,6 +3169,7 @@ declare namespace ts {
31693169
function isIndexedAccessTypeNode(node: Node): node is IndexedAccessTypeNode;
31703170
function isMappedTypeNode(node: Node): node is MappedTypeNode;
31713171
function isLiteralTypeNode(node: Node): node is LiteralTypeNode;
3172+
function isImportTypeNode(node: Node): node is ImportTypeNode;
31723173
function isObjectBindingPattern(node: Node): node is ObjectBindingPattern;
31733174
function isArrayBindingPattern(node: Node): node is ArrayBindingPattern;
31743175
function isBindingElement(node: Node): node is BindingElement;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /a.ts
4+
////export const [|{| "isWriteAccess": true, "isDefinition": true |}x|] = 0;
5+
////declare const a: typeof import("./a");
6+
////a.[|x|];
7+
8+
verify.singleReferenceGroup("const x: 0");

0 commit comments

Comments
 (0)