Skip to content

Commit dda6583

Browse files
authored
go-to-type-definition, and find-all-refs should work for import.meta (microsoft#44364)
* go-to-type-definition, and find-all-refs should work for import.meta * search `meta` instead of `import.meta` * remove `definition` * fix compilation error * update baseline * revert definition
1 parent 733eff2 commit dda6583

9 files changed

+199
-0
lines changed

src/services/findAllReferences.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,10 @@ namespace ts.FindAllReferences {
890890
node.kind === SyntaxKind.ReadonlyKeyword ? isReadonlyTypeOperator : undefined);
891891
}
892892

893+
if (isImportMeta(node.parent) && node.parent.name === node) {
894+
return getAllReferencesForImportMeta(sourceFiles, cancellationToken);
895+
}
896+
893897
if (isStaticModifier(node) && isClassStaticBlockDeclaration(node.parent)) {
894898
return [{ definition: { type: DefinitionKind.Keyword, node }, references: [nodeEntry(node)] }];
895899
}
@@ -1435,6 +1439,19 @@ namespace ts.FindAllReferences {
14351439
}
14361440
}
14371441

1442+
function getAllReferencesForImportMeta(sourceFiles: readonly SourceFile[], cancellationToken: CancellationToken): SymbolAndEntries[] | undefined {
1443+
const references = flatMap(sourceFiles, sourceFile => {
1444+
cancellationToken.throwIfCancellationRequested();
1445+
return mapDefined(getPossibleSymbolReferenceNodes(sourceFile, "meta", sourceFile), node => {
1446+
const parent = node.parent;
1447+
if (isImportMeta(parent)) {
1448+
return nodeEntry(parent);
1449+
}
1450+
});
1451+
});
1452+
return references.length ? [{ definition: { type: DefinitionKind.Keyword, node: references[0].node }, references }] : undefined;
1453+
}
1454+
14381455
function getAllReferencesForKeyword(sourceFiles: readonly SourceFile[], keywordKind: SyntaxKind, cancellationToken: CancellationToken, filter?: (node: Node) => boolean): SymbolAndEntries[] | undefined {
14391456
const references = flatMap(sourceFiles, sourceFile => {
14401457
cancellationToken.throwIfCancellationRequested();

src/services/goToDefinition.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ namespace ts.GoToDefinition {
198198
return undefined;
199199
}
200200

201+
if (isImportMeta(node.parent) && node.parent.name === node) {
202+
return definitionFromType(typeChecker.getTypeAtLocation(node.parent), typeChecker, node.parent);
203+
}
204+
201205
const symbol = getSymbol(node, typeChecker);
202206
if (!symbol) return undefined;
203207

src/services/services.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,6 +1724,9 @@ namespace ts {
17241724
if (isNamedTupleMember(node.parent) && node.pos === node.parent.pos) {
17251725
return node.parent;
17261726
}
1727+
if (isImportMeta(node.parent) && node.parent.name === node) {
1728+
return node.parent;
1729+
}
17271730
return node;
17281731
}
17291732

@@ -1740,6 +1743,8 @@ namespace ts {
17401743
case SyntaxKind.SuperKeyword:
17411744
case SyntaxKind.NamedTupleMember:
17421745
return true;
1746+
case SyntaxKind.MetaProperty:
1747+
return isImportMeta(node);
17431748
default:
17441749
return false;
17451750
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// === /tests/cases/fourslash/foo.ts ===
2+
// /// <reference no-default-lib="true"/>
3+
// /// <reference path='./bar.d.ts' />
4+
// import./*FIND ALL REFS*/[|meta|];
5+
// import.[|meta|];
6+
7+
// === /tests/cases/fourslash/baz.ts ===
8+
// /// <reference no-default-lib="true"/>
9+
// /// <reference path='./bar.d.ts' />
10+
// let x = import
11+
// . // hai :)
12+
// [|meta|];
13+
14+
[
15+
{
16+
"definition": {
17+
"containerKind": "",
18+
"containerName": "",
19+
"fileName": "/tests/cases/fourslash/foo.ts",
20+
"kind": "keyword",
21+
"textSpan": {
22+
"start": 82,
23+
"length": 4
24+
},
25+
"displayParts": [
26+
{
27+
"kind": "keyword"
28+
}
29+
]
30+
},
31+
"references": [
32+
{
33+
"textSpan": {
34+
"start": 82,
35+
"length": 4
36+
},
37+
"fileName": "/tests/cases/fourslash/foo.ts",
38+
"isWriteAccess": false,
39+
"isDefinition": false
40+
},
41+
{
42+
"textSpan": {
43+
"start": 95,
44+
"length": 4
45+
},
46+
"fileName": "/tests/cases/fourslash/foo.ts",
47+
"isWriteAccess": false,
48+
"isDefinition": false
49+
},
50+
{
51+
"textSpan": {
52+
"start": 109,
53+
"length": 4
54+
},
55+
"fileName": "/tests/cases/fourslash/baz.ts",
56+
"isWriteAccess": false,
57+
"isDefinition": false
58+
}
59+
]
60+
}
61+
]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
[
2+
{
3+
"marker": {
4+
"fileName": "/tests/cases/fourslash/foo.ts",
5+
"position": 77,
6+
"name": "1"
7+
},
8+
"quickInfo": {
9+
"kind": "",
10+
"kindModifiers": "",
11+
"textSpan": {
12+
"start": 75,
13+
"length": 6
14+
},
15+
"displayParts": [],
16+
"documentation": []
17+
}
18+
},
19+
{
20+
"marker": {
21+
"fileName": "/tests/cases/fourslash/foo.ts",
22+
"position": 84,
23+
"name": "2"
24+
},
25+
"quickInfo": {
26+
"kind": "interface",
27+
"kindModifiers": "declare",
28+
"textSpan": {
29+
"start": 75,
30+
"length": 11
31+
},
32+
"displayParts": [],
33+
"documentation": [
34+
{
35+
"text": "The type of `import.meta`.\n\nIf you need to declare that a given property exists on `import.meta`,\nthis type may be augmented via interface merging.",
36+
"kind": "text"
37+
}
38+
]
39+
}
40+
}
41+
]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// @noLib: true
2+
3+
/// <reference path='fourslash.ts'/>
4+
5+
// @module: esnext
6+
// @Filename: foo.ts
7+
/////// <reference no-default-lib="true"/>
8+
/////// <reference path='./bar.d.ts' />
9+
////import./**/meta;
10+
////import.[|meta|];
11+
12+
//@Filename: bar.d.ts
13+
////interface ImportMeta {
14+
////}
15+
16+
// @Filename: baz.ts
17+
/////// <reference no-default-lib="true"/>
18+
/////// <reference path='./bar.d.ts' />
19+
////let x = import
20+
//// . // hai :)
21+
//// meta;
22+
23+
verify.baselineFindAllReferences("");
24+
25+
goTo.rangeStart(test.ranges()[0]);
26+
verify.renameInfoFailed();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @module: esnext
4+
// @Filename: foo.ts
5+
/////// <reference no-default-lib="true"/>
6+
/////// <reference path='./bar.d.ts' />
7+
////import.me/*reference*/ta;
8+
9+
//@Filename: bar.d.ts
10+
////interface ImportMeta {
11+
////}
12+
13+
verify.goToDefinition("reference", []);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @module: esnext
4+
// @Filename: foo.ts
5+
/////// <reference no-default-lib="true"/>
6+
/////// <reference path='./bar.d.ts' />
7+
////import.me/*reference*/ta;
8+
9+
//@Filename: bar.d.ts
10+
////interface /*definition*/ImportMeta {
11+
////}
12+
13+
verify.goToType("reference", "definition");
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
///<reference path="fourslash.ts" />
2+
3+
// @module: esnext
4+
// @Filename: foo.ts
5+
/////// <reference no-default-lib="true"/>
6+
/////// <reference path='./bar.d.ts' />
7+
////im/*1*/port.me/*2*/ta;
8+
9+
//@Filename: bar.d.ts
10+
/////**
11+
//// * The type of `import.meta`.
12+
//// *
13+
//// * If you need to declare that a given property exists on `import.meta`,
14+
//// * this type may be augmented via interface merging.
15+
//// */
16+
//// interface ImportMeta {
17+
////}
18+
19+
verify.baselineQuickInfo()

0 commit comments

Comments
 (0)