Skip to content

Commit d6fa91e

Browse files
author
Andy Hanson
committed
goToDefinition: Skip default and = imports
1 parent 1a7c193 commit d6fa91e

File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
lines changed

src/services/goToDefinition.ts

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,8 @@ namespace ts.GoToDefinition {
5050
// get the aliased symbol instead. This allows for goto def on an import e.g.
5151
// import {A, B} from "mod";
5252
// to jump to the implementation directly.
53-
if (symbol.flags & SymbolFlags.Alias) {
54-
const declaration = symbol.declarations[0];
55-
56-
// Go to the original declaration for cases:
57-
//
58-
// (1) when the aliased symbol was declared in the location(parent).
59-
// (2) when the aliased symbol is originating from a named import.
60-
//
61-
if (node.kind === SyntaxKind.Identifier &&
62-
(node.parent === declaration ||
63-
(declaration.kind === SyntaxKind.ImportSpecifier && declaration.parent && declaration.parent.kind === SyntaxKind.NamedImports))) {
64-
65-
symbol = typeChecker.getAliasedSymbol(symbol);
66-
}
53+
if (symbol.flags & SymbolFlags.Alias && shouldSkipAlias(node, symbol.declarations[0])) {
54+
symbol = typeChecker.getAliasedSymbol(symbol);
6755
}
6856

6957
// Because name in short-hand property assignment has two different meanings: property name and property value,
@@ -136,6 +124,29 @@ namespace ts.GoToDefinition {
136124
return getDefinitionFromSymbol(typeChecker, type.symbol, node);
137125
}
138126

127+
// Go to the original declaration for cases:
128+
//
129+
// (1) when the aliased symbol was declared in the location(parent).
130+
// (2) when the aliased symbol is originating from an import.
131+
//
132+
function shouldSkipAlias(node: Node, declaration: Node): boolean {
133+
if (node.kind !== SyntaxKind.Identifier) {
134+
return false;
135+
}
136+
if (node.parent === declaration) {
137+
return true;
138+
}
139+
switch (declaration.kind) {
140+
case SyntaxKind.ImportClause:
141+
case SyntaxKind.ImportEqualsDeclaration:
142+
return true;
143+
case SyntaxKind.ImportSpecifier:
144+
return declaration.parent.kind === SyntaxKind.NamedImports;
145+
default:
146+
return false;
147+
}
148+
}
149+
139150
function getDefinitionFromSymbol(typeChecker: TypeChecker, symbol: Symbol, node: Node): DefinitionInfo[] {
140151
const result: DefinitionInfo[] = [];
141152
const declarations = symbol.getDeclarations();
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
// @Filename: /a.ts
4+
////export default function /*fDef*/f() {}
5+
////export const /*xDef*/x = 0;
6+
7+
// @Filename: /b.ts
8+
/////*bDef*/declare const b: number;
9+
////export = b;
10+
11+
// @Filename: /b.ts
12+
////import f, { x } from "./a";
13+
////import * as /*aDef*/a from "./a";
14+
////import b = require("./b");
15+
/////*fUse*/f;
16+
/////*xUse*/x;
17+
/////*aUse*/a;
18+
/////*bUse*/b;
19+
20+
verify.goToDefinition({
21+
aUse: "aDef", // Namespace import isn't "skipped"
22+
fUse: "fDef",
23+
xUse: "xDef",
24+
bUse: "bDef",
25+
});
26+

0 commit comments

Comments
 (0)