mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-09 07:21:18 -05:00
goToDefinition: Skip default and = imports
This commit is contained in:
@@ -50,20 +50,8 @@ namespace ts.GoToDefinition {
|
||||
// get the aliased symbol instead. This allows for goto def on an import e.g.
|
||||
// import {A, B} from "mod";
|
||||
// to jump to the implementation directly.
|
||||
if (symbol.flags & SymbolFlags.Alias) {
|
||||
const declaration = symbol.declarations[0];
|
||||
|
||||
// Go to the original declaration for cases:
|
||||
//
|
||||
// (1) when the aliased symbol was declared in the location(parent).
|
||||
// (2) when the aliased symbol is originating from a named import.
|
||||
//
|
||||
if (node.kind === SyntaxKind.Identifier &&
|
||||
(node.parent === declaration ||
|
||||
(declaration.kind === SyntaxKind.ImportSpecifier && declaration.parent && declaration.parent.kind === SyntaxKind.NamedImports))) {
|
||||
|
||||
symbol = typeChecker.getAliasedSymbol(symbol);
|
||||
}
|
||||
if (symbol.flags & SymbolFlags.Alias && shouldSkipAlias(node, symbol.declarations[0])) {
|
||||
symbol = typeChecker.getAliasedSymbol(symbol);
|
||||
}
|
||||
|
||||
// Because name in short-hand property assignment has two different meanings: property name and property value,
|
||||
@@ -136,6 +124,29 @@ namespace ts.GoToDefinition {
|
||||
return getDefinitionFromSymbol(typeChecker, type.symbol, node);
|
||||
}
|
||||
|
||||
// Go to the original declaration for cases:
|
||||
//
|
||||
// (1) when the aliased symbol was declared in the location(parent).
|
||||
// (2) when the aliased symbol is originating from an import.
|
||||
//
|
||||
function shouldSkipAlias(node: Node, declaration: Node): boolean {
|
||||
if (node.kind !== SyntaxKind.Identifier) {
|
||||
return false;
|
||||
}
|
||||
if (node.parent === declaration) {
|
||||
return true;
|
||||
}
|
||||
switch (declaration.kind) {
|
||||
case SyntaxKind.ImportClause:
|
||||
case SyntaxKind.ImportEqualsDeclaration:
|
||||
return true;
|
||||
case SyntaxKind.ImportSpecifier:
|
||||
return declaration.parent.kind === SyntaxKind.NamedImports;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function getDefinitionFromSymbol(typeChecker: TypeChecker, symbol: Symbol, node: Node): DefinitionInfo[] {
|
||||
const result: DefinitionInfo[] = [];
|
||||
const declarations = symbol.getDeclarations();
|
||||
|
||||
26
tests/cases/fourslash/goToDefinitionImports.ts
Normal file
26
tests/cases/fourslash/goToDefinitionImports.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
/// <reference path='fourslash.ts'/>
|
||||
|
||||
// @Filename: /a.ts
|
||||
////export default function /*fDef*/f() {}
|
||||
////export const /*xDef*/x = 0;
|
||||
|
||||
// @Filename: /b.ts
|
||||
/////*bDef*/declare const b: number;
|
||||
////export = b;
|
||||
|
||||
// @Filename: /b.ts
|
||||
////import f, { x } from "./a";
|
||||
////import * as /*aDef*/a from "./a";
|
||||
////import b = require("./b");
|
||||
/////*fUse*/f;
|
||||
/////*xUse*/x;
|
||||
/////*aUse*/a;
|
||||
/////*bUse*/b;
|
||||
|
||||
verify.goToDefinition({
|
||||
aUse: "aDef", // Namespace import isn't "skipped"
|
||||
fUse: "fDef",
|
||||
xUse: "xDef",
|
||||
bUse: "bDef",
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user