Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 869a821

Browse filesBrowse files
authored
Make javascript require goto def similar to ts import equals (microsoft#36487)
Fixes microsoft#34996
1 parent 39311da commit 869a821
Copy full SHA for 869a821

File tree

Expand file treeCollapse file tree

3 files changed

+33
-1
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+33
-1
lines changed

‎src/services/goToDefinition.ts

Copy file name to clipboardExpand all lines: src/services/goToDefinition.ts
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,15 @@ namespace ts.GoToDefinition {
207207
return aliased;
208208
}
209209
}
210+
if (symbol && isInJSFile(node)) {
211+
const requireCall = forEach(symbol.declarations, d => isVariableDeclaration(d) && !!d.initializer && isRequireCall(d.initializer, /*checkArgumentIsStringLiteralLike*/ true) ? d.initializer : undefined);
212+
if (requireCall) {
213+
const moduleSymbol = checker.getSymbolAtLocation(requireCall.arguments[0]);
214+
if (moduleSymbol) {
215+
return checker.resolveExternalModuleSymbol(moduleSymbol);
216+
}
217+
}
218+
}
210219
return symbol;
211220
}
212221

+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
// @allowJs: true
4+
5+
// @Filename: /foo.js
6+
//// /*moduleDef*/class Blah {
7+
//// abc = 123;
8+
////}
9+
////module.exports.Blah = Blah;
10+
11+
// @Filename: /bar.js
12+
////const [|/*importDef*/BlahModule|] = require("./foo.js");
13+
////new [|/*importUsage*/BlahModule|].Blah()
14+
15+
// @Filename: /barTs.ts
16+
////import [|/*importDefTs*/BlahModule|] = require("./foo.js");
17+
////new [|/*importUsageTs*/BlahModule|].Blah()
18+
19+
verify.goToDefinition(["importDef", "importUsage", "importDefTs", "importUsageTs"], "moduleDef");

‎tests/cases/fourslash/goToDefinitionSignatureAlias_require.ts

Copy file name to clipboardExpand all lines: tests/cases/fourslash/goToDefinitionSignatureAlias_require.ts
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@
99
////const f = require("./a");
1010
////[|/*use*/f|]();
1111

12-
verify.goToDefinition("use", "f");
12+
// @Filename: /bar.ts
13+
////import f = require("./a");
14+
////[|/*useTs*/f|]();
15+
16+
verify.goToDefinition(["use", "useTs"], "f");

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.