Skip to content

Commit fd06132

Browse files
authored
Fix microsoft#47753 - Organize imports removes type imports that are only referenced in @link (jsdoc) (microsoft#47824)
* Added unit test * Added baseline test * Dirty solution * Code refactoring and improvements * Added more test cases * Refactor to use flatMap * Added utility function to get all Nodes with JSDocs * Minor improvements * Use recursion to check all tree levels * Removed unit test * Removed previous changes * Updated resolveEntityName call * Updated dontResolveAlias clause * Updated symbol flags * Updated baseline * Fix dont resolve alias problem * Updated tests
1 parent 18b08fc commit fd06132

File tree

4 files changed

+96
-16
lines changed

4 files changed

+96
-16
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -41898,7 +41898,7 @@ namespace ts {
4189841898
const symbol = getIntrinsicTagSymbol(name.parent as JsxOpeningLikeElement);
4189941899
return symbol === unknownSymbol ? undefined : symbol;
4190041900
}
41901-
const result = resolveEntityName(name, meaning, /*ignoreErrors*/ false, /*dontResolveAlias*/ !isJSDoc, getHostSignatureFromJSDoc(name));
41901+
const result = resolveEntityName(name, meaning, /*ignoreErrors*/ false, /* dontResolveAlias */ true, getHostSignatureFromJSDoc(name));
4190241902
if (!result && isJSDoc) {
4190341903
const container = findAncestor(name, or(isClassLike, isInterfaceDeclaration));
4190441904
if (container) {

tests/baselines/reference/jsdocLink3.baseline

+15-15
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@
5959
"text": "C",
6060
"kind": "linkName",
6161
"target": {
62-
"fileName": "/jsdocLink3.ts",
62+
"fileName": "/module1.ts",
6363
"textSpan": {
64-
"start": 0,
65-
"length": 18
64+
"start": 9,
65+
"length": 1
6666
}
6767
}
6868
},
@@ -87,10 +87,10 @@
8787
"text": "C",
8888
"kind": "linkName",
8989
"target": {
90-
"fileName": "/jsdocLink3.ts",
90+
"fileName": "/module1.ts",
9191
"textSpan": {
92-
"start": 0,
93-
"length": 18
92+
"start": 9,
93+
"length": 1
9494
}
9595
}
9696
},
@@ -110,10 +110,10 @@
110110
"text": "C()",
111111
"kind": "linkName",
112112
"target": {
113-
"fileName": "/jsdocLink3.ts",
113+
"fileName": "/module1.ts",
114114
"textSpan": {
115-
"start": 0,
116-
"length": 18
115+
"start": 9,
116+
"length": 1
117117
}
118118
}
119119
},
@@ -133,10 +133,10 @@
133133
"text": "C",
134134
"kind": "linkName",
135135
"target": {
136-
"fileName": "/jsdocLink3.ts",
136+
"fileName": "/module1.ts",
137137
"textSpan": {
138-
"start": 0,
139-
"length": 18
138+
"start": 9,
139+
"length": 1
140140
}
141141
}
142142
},
@@ -181,10 +181,10 @@
181181
"text": "C",
182182
"kind": "linkName",
183183
"target": {
184-
"fileName": "/jsdocLink3.ts",
184+
"fileName": "/module1.ts",
185185
"textSpan": {
186-
"start": 0,
187-
"length": 18
186+
"start": 9,
187+
"length": 1
188188
}
189189
}
190190
},
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @Filename: /module.ts
4+
////import type { ZodType } from './declaration';
5+
////
6+
/////** Intended to be used in combination with {@link ZodType} */
7+
////export function fun() { /* ... */ }
8+
9+
// @Filename: /declaration.ts
10+
//// type ZodType = {};
11+
//// export type { ZodType }
12+
13+
14+
verify.organizeImports(`import type { ZodType } from './declaration';
15+
16+
/** Intended to be used in combination with {@link ZodType} */
17+
export function fun() { /* ... */ }`
18+
);
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @Filename: /test.ts
4+
////import { TypeA, TypeB, TypeC, UnreferencedType } from './my-types';
5+
////
6+
/////**
7+
//// * MyClass {@link TypeA}
8+
//// */
9+
////export class MyClass {
10+
////
11+
//// /**
12+
//// * Some Property {@link TypeB}
13+
//// */
14+
//// public something;
15+
////
16+
//// /**
17+
//// * Some function {@link TypeC}
18+
//// */
19+
//// public myMethod() {
20+
////
21+
//// /**
22+
//// * Some lambda function {@link TypeC}
23+
//// */
24+
//// const someFunction = () => {
25+
//// return '';
26+
//// }
27+
//// someFunction();
28+
//// }
29+
////}
30+
31+
// @Filename: /my-types.ts
32+
//// export type TypeA = string;
33+
//// export class TypeB { }
34+
//// export type TypeC = () => string;
35+
36+
verify.organizeImports(`import { TypeA, TypeB, TypeC } from './my-types';
37+
38+
/**
39+
* MyClass {@link TypeA}
40+
*/
41+
export class MyClass {
42+
43+
/**
44+
* Some Property {@link TypeB}
45+
*/
46+
public something;
47+
48+
/**
49+
* Some function {@link TypeC}
50+
*/
51+
public myMethod() {
52+
53+
/**
54+
* Some lambda function {@link TypeC}
55+
*/
56+
const someFunction = () => {
57+
return '';
58+
}
59+
someFunction();
60+
}
61+
}`
62+
);

0 commit comments

Comments
 (0)