Skip to content

Find-all-references: Don't crash on 'typeof import' #23448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
2 commits merged into from
Apr 17, 2018
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
3 changes: 2 additions & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3193,7 +3193,8 @@ namespace ts {
export type AnyValidImportOrReExport =
| (ImportDeclaration | ExportDeclaration) & { moduleSpecifier: StringLiteral }
| ImportEqualsDeclaration & { moduleReference: ExternalModuleReference & { expression: StringLiteral } }
| RequireOrImportCall;
| RequireOrImportCall
| ImportTypeNode & { argument: LiteralType };

/* @internal */
export type RequireOrImportCall = CallExpression & { arguments: [StringLiteralLike] };
Expand Down
8 changes: 7 additions & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1710,8 +1710,10 @@ namespace ts {
return (node.parent as ExternalModuleReference).parent as AnyValidImportOrReExport;
case SyntaxKind.CallExpression:
return node.parent as AnyValidImportOrReExport;
case SyntaxKind.LiteralType:
return cast(node.parent.parent, isImportTypeNode) as ImportTypeNode & { argument: LiteralType };
default:
return Debug.fail(Debug.showSyntaxKind(node));
return Debug.fail(Debug.showSyntaxKind(node.parent));
}
}

Expand Down Expand Up @@ -4926,6 +4928,10 @@ namespace ts {
return node.kind === SyntaxKind.LiteralType;
}

export function isImportTypeNode(node: Node): node is ImportTypeNode {
return node.kind === SyntaxKind.ImportType;
}

// Binding patterns

export function isObjectBindingPattern(node: Node): node is ObjectBindingPattern {
Expand Down
6 changes: 5 additions & 1 deletion src/services/importTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace ts.FindAllReferences {
interface AmbientModuleDeclaration extends ModuleDeclaration { body?: ModuleBlock; }
type SourceFileLike = SourceFile | AmbientModuleDeclaration;
// Identifier for the case of `const x = require("y")`.
type Importer = AnyImportOrReExport | Identifier;
type Importer = AnyImportOrReExport | ImportTypeNode | Identifier;
type ImporterOrCallExpression = Importer | CallExpression;

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

if (decl.kind === SyntaxKind.ImportType) {
return;
}

// Ignore if there's a grammar error
if (decl.moduleSpecifier.kind !== SyntaxKind.StringLiteral) {
return;
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3169,6 +3169,7 @@ declare namespace ts {
function isIndexedAccessTypeNode(node: Node): node is IndexedAccessTypeNode;
function isMappedTypeNode(node: Node): node is MappedTypeNode;
function isLiteralTypeNode(node: Node): node is LiteralTypeNode;
function isImportTypeNode(node: Node): node is ImportTypeNode;
function isObjectBindingPattern(node: Node): node is ObjectBindingPattern;
function isArrayBindingPattern(node: Node): node is ArrayBindingPattern;
function isBindingElement(node: Node): node is BindingElement;
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3169,6 +3169,7 @@ declare namespace ts {
function isIndexedAccessTypeNode(node: Node): node is IndexedAccessTypeNode;
function isMappedTypeNode(node: Node): node is MappedTypeNode;
function isLiteralTypeNode(node: Node): node is LiteralTypeNode;
function isImportTypeNode(node: Node): node is ImportTypeNode;
function isObjectBindingPattern(node: Node): node is ObjectBindingPattern;
function isArrayBindingPattern(node: Node): node is ArrayBindingPattern;
function isBindingElement(node: Node): node is BindingElement;
Expand Down
8 changes: 8 additions & 0 deletions tests/cases/fourslash/findAllRefsTypeofImport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// <reference path='fourslash.ts' />

// @Filename: /a.ts
////export const [|{| "isWriteAccess": true, "isDefinition": true |}x|] = 0;
////declare const a: typeof import("./a");
////a.[|x|];

verify.singleReferenceGroup("const x: 0");