Skip to content

Commit a67b375

Browse files
committed
Handle module specifiers
1 parent a120c59 commit a67b375

15 files changed

+62
-46
lines changed

src/services/findAllReferences.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,6 @@ namespace ts.FindAllReferences {
5151
if (!node.parent) return undefined;
5252

5353
if (!isDeclaration(node.parent) && !isExportAssignment(node.parent)) {
54-
// Jsx Tags
55-
if (isJsxOpeningElement(node.parent) || isJsxClosingElement(node.parent)) {
56-
return node.parent.parent;
57-
}
58-
else if (isJsxSelfClosingElement(node.parent) ||
59-
isLabeledStatement(node.parent) ||
60-
isBreakOrContinueStatement(node.parent)) {
61-
return node.parent;
62-
}
63-
6454
// Special property assignment in javascript
6555
if (isInJSFile(node)) {
6656
const binaryExpression = isBinaryExpression(node.parent) ?
@@ -75,6 +65,29 @@ namespace ts.FindAllReferences {
7565
}
7666
}
7767

68+
// Jsx Tags
69+
if (isJsxOpeningElement(node.parent) || isJsxClosingElement(node.parent)) {
70+
return node.parent.parent;
71+
}
72+
else if (isJsxSelfClosingElement(node.parent) ||
73+
isLabeledStatement(node.parent) ||
74+
isBreakOrContinueStatement(node.parent)) {
75+
return node.parent;
76+
}
77+
else if (isStringLiteralLike(node)) {
78+
const validImport = tryGetImportFromModuleSpecifier(node);
79+
if (validImport) {
80+
const declOrStatement = findAncestor(validImport, node =>
81+
isDeclaration(node) ||
82+
isStatement(node) ||
83+
isJSDocTag(node)
84+
)! as NamedDeclaration | Statement | JSDocTag;
85+
return isDeclaration(declOrStatement) ?
86+
getDeclarationForDeclarationSpan(declOrStatement) :
87+
declOrStatement;
88+
}
89+
}
90+
7891
// Handle computed property name
7992
const propertyName = findAncestor(node, isComputedPropertyName);
8093
return propertyName ?

src/testRunner/unittests/tsserver/rename.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ namespace ts.projectSystem {
1919
locs: [
2020
protocolRenameSpanFromSubstring({
2121
fileText: bTs.content,
22-
text: "./a"
22+
text: "./a",
23+
declarationText: bTs.content
2324
})
2425
]
2526
}],
@@ -43,7 +44,8 @@ namespace ts.projectSystem {
4344
locs: [
4445
protocolRenameSpanFromSubstring({
4546
fileText: bTs.content,
46-
text: "./a"
47+
text: "./a",
48+
declarationText: bTs.content
4749
})
4850
]
4951
}],
@@ -68,7 +70,8 @@ namespace ts.projectSystem {
6870
locs: [
6971
protocolRenameSpanFromSubstring({
7072
fileText: bTs.content,
71-
text: "./a"
73+
text: "./a",
74+
declarationText: bTs.content
7275
})
7376
]
7477
}],

tests/cases/fourslash/findAllReferencesDynamicImport1.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// @Filename: foo.ts
44
//// export function foo() { return "foo"; }
55

6-
//// import("[|./foo|]")
7-
//// var x = import("[|./foo|]")
6+
//// [|import("[|{| "declarationRangeIndex": 0 |}./foo|]")|]
7+
//// [|var x = import("[|{| "declarationRangeIndex": 2 |}./foo|]")|]
88

9-
verify.singleReferenceGroup('module "/tests/cases/fourslash/foo"');
9+
verify.singleReferenceGroup('module "/tests/cases/fourslash/foo"', "./foo");

tests/cases/fourslash/findAllReferencesUmdModuleAsGlobalConst.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
////[|export as namespace [|{| "isWriteAccess": true, "isDefinition": true, "declarationRangeIndex": 0 |}THREE|];|]
1313

1414
// @Filename: /typings/global.d.ts
15-
////import * as _THREE from '[|three|]';
15+
////[|import * as _THREE from '[|{| "declarationRangeIndex": 2 |}three|]';|]
1616
////declare global {
17-
//// [|const [|{| "isWriteAccess": true, "isDefinition": true, "declarationRangeIndex": 3 |}THREE|]: typeof _THREE;|]
17+
//// [|const [|{| "isWriteAccess": true, "isDefinition": true, "declarationRangeIndex": 4 |}THREE|]: typeof _THREE;|]
1818
////}
1919

2020
// @Filename: /src/index.ts
@@ -38,7 +38,7 @@
3838
//// "files": ["/src/index.ts", "typings/global.d.ts"]
3939
////}
4040

41-
const [r0Def, r0, r1, r2Def, ...rest] = test.ranges();
41+
const [r0Def, r0, r1Def, r1, r2Def, ...rest] = test.ranges();
4242
// GH#29533
4343
// TODO:: this should be var THREE: typeof import instead of module name as var but thats existing issue and repros with quickInfo too.
4444
verify.singleReferenceGroup(`module "/node_modules/@types/three/index"

tests/cases/fourslash/findAllRefsExportEquals.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
////[|[|{| "declarationRangeIndex": 2 |}export|] = [|{| "declarationRangeIndex": 2 |}T|];|]
66

77
// @Filename: /b.ts
8-
////[|import [|{| "isWriteAccess": true, "isDefinition": true, "declarationRangeIndex": 5 |}T|] = require("[|./a|]");|]
8+
////[|import [|{| "isWriteAccess": true, "isDefinition": true, "declarationRangeIndex": 5 |}T|] = require("[|{| "declarationRangeIndex": 5 |}./a|]");|]
99

1010
const [r0Def, r0, r12Def, r1, r2, r3Def, r3, r4] = test.ranges();
1111
const mod = { definition: 'module "/a"', ranges: [r4, r1] };

tests/cases/fourslash/findAllRefsForModule.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
////export const x = 0;
77

88
// @Filename: /b.ts
9-
////import { x } from "[|./a|]";
9+
////[|import { x } from "[|{| "declarationRangeIndex": 0 |}./a|]";|]
1010

1111
// @Filename: /c/sub.js
12-
////const a = require("[|../a|]");
12+
////[|const a = require("[|{| "declarationRangeIndex": 2 |}../a|]");|]
1313

1414
// @Filename: /d.ts
1515
//// /// <reference path="[|./a.ts|]" />
1616

17-
const ranges = test.ranges();
18-
const [r0, r1, r2] = ranges;
17+
const [r0Def, r0, r1Def, r1, r2] = test.ranges();
18+
const ranges = [r0, r1, r2];
1919
verify.referenceGroups(ranges, [{ definition: 'module "/a"', ranges: [r0, r1, r2] }]);
2020
// Testing that it works with documentHighlights too
21-
verify.rangesAreDocumentHighlights();
21+
verify.rangesAreDocumentHighlights(ranges);

tests/cases/fourslash/findAllRefsForModuleGlobal.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
// @Filename: /b.ts
77
/////// <reference types="[|foo|]" />
8-
////import { x } from "[|foo|]";
9-
////[|declare module "[|{| "isWriteAccess": true, "isDefinition": true, "declarationRangeIndex": 2 |}foo|]" {}|]
8+
////[|import { x } from "[|{| "declarationRangeIndex": 1 |}foo|]";|]
9+
////[|declare module "[|{| "isWriteAccess": true, "isDefinition": true, "declarationRangeIndex": 3 |}foo|]" {}|]
1010

1111
verify.noErrors();
1212
verify.singleReferenceGroup('module "/node_modules/foo/index"', "foo");

tests/cases/fourslash/findAllRefsImportEqualsJsonFile.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
// @resolveJsonModule: true
66

77
// @Filename: /a.ts
8-
////[|import [|{| "isWriteAccess": true, "isDefinition": true, "declarationRangeIndex": 0 |}j|] = require("[|./j.json|]");|]
8+
////[|import [|{| "isWriteAccess": true, "isDefinition": true, "declarationRangeIndex": 0 |}j|] = require("[|{| "declarationRangeIndex": 0 |}./j.json|]");|]
99
////[|j|];
1010

1111
// @Filename: /b.js
12-
////[|const [|{| "isWriteAccess": true, "isDefinition": true, "declarationRangeIndex": 4 |}j|] = require("[|./j.json|]");|]
12+
////[|const [|{| "isWriteAccess": true, "isDefinition": true, "declarationRangeIndex": 4 |}j|] = require("[|{| "declarationRangeIndex": 4 |}./j.json|]");|]
1313
////[|j|];
1414

1515
// @Filename: /j.json

tests/cases/fourslash/findAllRefsModuleDotExports.ts

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

55
// @Filename: /a.js
6-
////const b = require("[|./b|]");
6+
////[|const b = require("[|{| "declarationRangeIndex": 0 |}./b|]");|]
77

88
// @Filename: /b.js
9-
////[|[|{| "declarationRangeIndex": 1 |}module|].exports = 0;|]
9+
////[|[|{| "declarationRangeIndex": 2 |}module|].exports = 0;|]
1010

11-
const [r0, rDef, r1] = test.ranges();
11+
const [r0Def, r0, rDef, r1] = test.ranges();
1212
verify.singleReferenceGroup('module "/b"', [r0, r1]);

tests/cases/fourslash/findAllRefs_importType_exportEquals.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
////[|[|{| "declarationRangeIndex": 4 |}export|] = [|{| "declarationRangeIndex": 4 |}T|];|]
99

1010
// @Filename: /b.ts
11-
////const x: import("[|./[|a|]|]") = 0;
12-
////const y: import("[|./[|a|]|]").U = "";
11+
////[|const x: import("[|{| "declarationRangeIndex": 7 |}./[|a|]|]") = 0;|]
12+
////[|const y: import("[|{| "declarationRangeIndex": 10 |}./[|a|]|]").U = "";|]
1313

1414
verify.noErrors();
1515

16-
const [r0Def, r0, r1Def, r1, r2Def, rExport, r2, r3, r3b, r4, r4b] = test.ranges();
16+
const [r0Def, r0, r1Def, r1, r2Def, rExport, r2, r3Def, r3, r3b, r4Def, r4, r4b] = test.ranges();
1717
verify.referenceGroups(r0, [{ definition: "type T = number\nnamespace T", ranges: [r0, r2, r3] }]);
1818
verify.referenceGroups(r1, [{ definition: "namespace T", ranges: [r1, r2] }]);
1919
const t: FourSlashInterface.ReferenceGroup = { definition: "type T = number\nnamespace T", ranges: [r0, r1, r2, r3] };

tests/cases/fourslash/findAllRefs_importType_js.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88
////[|module.exports.[|{| "isWriteAccess": true, "isDefinition": true, "declarationRangeIndex": 4 |}D|] = [|class [|{| "isWriteAccess": true, "isDefinition": true, "declarationRangeIndex": 6 |}D|] {}|];|]
99

1010
// @Filename: /b.js
11-
/////** @type {import("[|./a|]")} */
11+
/////** [|@type {import("[|{| "declarationRangeIndex": 8 |}./a|]")}|] */
1212
////const x = 0;
13-
/////** @type {import("[|./a|]").[|D|]} */
13+
/////** [|@type {import("[|{| "declarationRangeIndex": 10 |}./a|]").[|D|]}|] */
1414
////const y = 0;
1515

1616
verify.noErrors();
1717

1818
// TODO: GH#24025
1919

20-
const [rModuleDef, rModule, r0Def, r0, r1Def, r1, r2Def, r2, r3, r4, r5] = test.ranges();
20+
const [rModuleDef, rModule, r0Def, r0, r1Def, r1, r2Def, r2, r3Def, r3, r4Def, r4, r5] = test.ranges();
2121
verify.referenceGroups(rModule, [{ definition: 'module "/a"', ranges: [r3, r4, rModule] }]);
2222
verify.referenceGroups(r0, [
2323
{ definition: "(local class) C", ranges: [r0] },

tests/cases/fourslash/findAllRefs_importType_typeofImport.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
////export const x = 0;
55

66
// @Filename: /b.ts
7-
////const x: typeof import("[|./a|]") = { x: 0 };
8-
////const y: typeof import("[|./a|]") = { x: 0 };
7+
////[|const x: typeof import("[|{| "declarationRangeIndex": 0 |}./a|]") = { x: 0 };|]
8+
////[|const y: typeof import("[|{| "declarationRangeIndex": 2 |}./a|]") = { x: 0 };|]
99

10-
verify.singleReferenceGroup('module "/a"');
10+
verify.singleReferenceGroup('module "/a"', "./a");

tests/cases/fourslash/referencesForAmbients.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
////}|]
66
////
77
////[|declare module "[|{| "isWriteAccess": true, "isDefinition": true, "declarationRangeIndex": 4 |}bar|]" {
8-
//// [|export import [|{| "isWriteAccess": true, "isDefinition": true, "declarationRangeIndex": 6 |}foo|] = require("[|foo|]");|]
8+
//// [|export import [|{| "isWriteAccess": true, "isDefinition": true, "declarationRangeIndex": 6 |}foo|] = require("[|{| "declarationRangeIndex": 6 |}foo|]");|]
99
//// var f2: typeof [|foo|].[|f|];
1010
////}|]
1111
////
1212
////declare module "baz" {
13-
//// import bar = require("[|bar|]");
13+
//// [|import bar = require("[|{| "declarationRangeIndex": 11 |}bar|]");|]
1414
//// var f2: typeof bar.[|foo|];
1515
////}
1616

17-
const [moduleFoo0Def, moduleFoo0, f0Def, f0, moduleBar0Def, moduleBar0, foo0Def, foo0, moduleFoo1, foo1, f1, moduleBar1, foo2] = test.ranges();
17+
const [moduleFoo0Def, moduleFoo0, f0Def, f0, moduleBar0Def, moduleBar0, foo0Def, foo0, moduleFoo1, foo1, f1, moduleBar1Def, moduleBar1, foo2] = test.ranges();
1818
verify.singleReferenceGroup('module "foo"', [moduleFoo0, moduleFoo1]);
1919
verify.singleReferenceGroup('module "bar"', [moduleBar0, moduleBar1]);
2020
verify.singleReferenceGroup('(alias) module "foo"\nimport foo = require("foo")', [foo0, foo1, foo2]);

tests/cases/fourslash/referencesForExternalModuleNames.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
////}|]
77

88
// @Filename: referencesForGlobals_2.ts
9-
////import f = require("[|foo|]");
9+
////[|import f = require("[|{| "declarationRangeIndex": 2 |}foo|]");|]
1010

1111
verify.singleReferenceGroup('module "foo"', "foo");

tests/cases/fourslash/untypedModuleImport.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
////{}
55

66
// @Filename: a.ts
7-
////[|import /*foo*/[|{| "isWriteAccess": true, "isDefinition": true, "declarationRangeIndex": 0 |}foo|] from /*fooModule*/"[|{| "isInString": true |}foo|]";|]
7+
////[|import /*foo*/[|{| "isWriteAccess": true, "isDefinition": true, "declarationRangeIndex": 0 |}foo|] from /*fooModule*/"[|{| "isInString": true, "declarationRangeIndex": 0 |}foo|]";|]
88
////[|foo|]();
99

1010
goTo.file("a.ts");

0 commit comments

Comments
 (0)