Skip to content

Commit bed1e02

Browse files
author
Andy
authored
Merge pull request #15783 from Microsoft/goToDef-default
goToDefinition: Skip default and `=` imports
2 parents e6d472c + aaf6b83 commit bed1e02

File tree

7 files changed

+65
-25
lines changed

7 files changed

+65
-25
lines changed

src/services/goToDefinition.ts

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,10 @@ 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);
53+
if (symbol.flags & SymbolFlags.Alias && shouldSkipAlias(node, symbol.declarations[0])) {
54+
const aliased = typeChecker.getAliasedSymbol(symbol);
55+
if (aliased.declarations) {
56+
symbol = aliased;
6657
}
6758
}
6859

@@ -136,6 +127,29 @@ namespace ts.GoToDefinition {
136127
return getDefinitionFromSymbol(typeChecker, type.symbol, node);
137128
}
138129

130+
// Go to the original declaration for cases:
131+
//
132+
// (1) when the aliased symbol was declared in the location(parent).
133+
// (2) when the aliased symbol is originating from an import.
134+
//
135+
function shouldSkipAlias(node: Node, declaration: Node): boolean {
136+
if (node.kind !== SyntaxKind.Identifier) {
137+
return false;
138+
}
139+
if (node.parent === declaration) {
140+
return true;
141+
}
142+
switch (declaration.kind) {
143+
case SyntaxKind.ImportClause:
144+
case SyntaxKind.ImportEqualsDeclaration:
145+
return true;
146+
case SyntaxKind.ImportSpecifier:
147+
return declaration.parent.kind === SyntaxKind.NamedImports;
148+
default:
149+
return false;
150+
}
151+
}
152+
139153
function getDefinitionFromSymbol(typeChecker: TypeChecker, symbol: Symbol, node: Node): DefinitionInfo[] {
140154
const result: DefinitionInfo[] = [];
141155
const declarations = symbol.getDeclarations();

tests/cases/fourslash/ambientShorthandGotoDefinition.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
verify.quickInfoAt("useFoo", "import foo");
1414
verify.goToDefinition({
15-
useFoo: "importFoo",
15+
useFoo: "module",
1616
importFoo: "module"
1717
});
1818

@@ -27,6 +27,6 @@ verify.goToDefinition({
2727

2828
verify.quickInfoAt("useBang", "import bang = require(\"jquery\")");
2929
verify.goToDefinition({
30-
useBang: "importBang",
30+
useBang: "module",
3131
importBang: "module"
3232
});
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+

tests/cases/fourslash/goToDefinition_untypedModule.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
////not read
55

66
// @Filename: /a.ts
7-
////import { f } from "foo";
8-
/////**/f();
7+
////import { /*def*/f } from "foo";
8+
/////*use*/f();
99

10-
verify.goToDefinition("", []);
10+
verify.goToDefinition("use", "def");

tests/cases/fourslash/quickInfoMeaning.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
// @Filename: foo.d.ts
99
////declare const /*foo_value_declaration*/foo: number;
1010
////declare module "foo_module" {
11-
//// interface I { x: number; y: number }
11+
//// interface /*foo_type_declaration*/I { x: number; y: number }
1212
//// export = I;
1313
////}
1414

1515
// @Filename: foo_user.ts
1616
///////<reference path="foo.d.ts" />
17-
////import /*foo_type_declaration*/foo = require("foo_module");
17+
////import foo = require("foo_module");
1818
////const x = foo/*foo_value*/;
1919
////const i: foo/*foo_type*/ = { x: 1, y: 2 };
2020

@@ -39,13 +39,13 @@ verify.goToDefinitionIs("foo_type_declaration");
3939
// @Filename: bar.d.ts
4040
////declare interface /*bar_type_declaration*/bar { x: number; y: number }
4141
////declare module "bar_module" {
42-
//// const x: number;
42+
//// const /*bar_value_declaration*/x: number;
4343
//// export = x;
4444
////}
4545

4646
// @Filename: bar_user.ts
4747
///////<reference path="bar.d.ts" />
48-
////import /*bar_value_declaration*/bar = require("bar_module");
48+
////import bar = require("bar_module");
4949
////const x = bar/*bar_value*/;
5050
////const i: bar/*bar_type*/ = { x: 1, y: 2 };
5151

tests/cases/fourslash/tsxGoToDefinitionClassInDifferentFile.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
// @jsx: preserve
44

55
// @Filename: C.tsx
6-
////export default class C {}
6+
////export default class /*def*/C {}
77

88
// @Filename: a.tsx
9-
////import /*def*/C from "./C";
9+
////import C from "./C";
1010
////const foo = </*use*/C />;
1111

1212
verify.noErrors();

tests/cases/fourslash/untypedModuleImport.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ const [r0, r1, r2] = test.ranges();
1717
verify.singleReferenceGroup('"foo"', [r1]);
1818

1919
goTo.marker("foo");
20-
verify.goToDefinitionIs([]);
20+
verify.goToDefinitionIs("foo");
2121
verify.quickInfoIs("import foo");
2222
verify.singleReferenceGroup("import foo", [r0, r2]);

0 commit comments

Comments
 (0)