Skip to content

Commit 3fb8873

Browse files
ajafffsandersn
authored andcommitted
don't resolve import types in JSDoc of TS files (microsoft#28158)
1 parent 0e372e9 commit 3fb8873

File tree

7 files changed

+37
-4
lines changed

7 files changed

+37
-4
lines changed

src/compiler/program.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1946,7 +1946,7 @@ namespace ts {
19461946
function collectDynamicImportOrRequireCalls(file: SourceFile) {
19471947
const r = /import|require/g;
19481948
while (r.exec(file.text) !== null) {
1949-
const node = getTokenAtPosition(file, r.lastIndex);
1949+
const node = getNodeAtPosition(file, r.lastIndex);
19501950
if (isRequireCall(node, /*checkArgumentIsStringLiteralLike*/ true)) {
19511951
imports = append(imports, node.arguments[0]);
19521952
}
@@ -1960,16 +1960,16 @@ namespace ts {
19601960
}
19611961
}
19621962

1963-
/** Returns a token if position is in [start-of-leading-trivia, end) */
1964-
function getTokenAtPosition(sourceFile: SourceFile, position: number): Node {
1963+
/** Returns a token if position is in [start-of-leading-trivia, end), includes JSDoc only in JS files */
1964+
function getNodeAtPosition(sourceFile: SourceFile, position: number): Node {
19651965
let current: Node = sourceFile;
19661966
const getContainingChild = (child: Node) => {
19671967
if (child.pos <= position && (position < child.end || (position === child.end && (child.kind === SyntaxKind.EndOfFileToken)))) {
19681968
return child;
19691969
}
19701970
};
19711971
while (true) {
1972-
const child = hasJSDocNodes(current) && forEach(current.jsDoc, getContainingChild) || forEachChild(current, getContainingChild);
1972+
const child = isJavaScriptFile && hasJSDocNodes(current) && forEach(current.jsDoc, getContainingChild) || forEachChild(current, getContainingChild);
19731973
if (!child) {
19741974
return current;
19751975
}

tests/baselines/reference/jsdocInTypeScript.errors.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,8 @@ tests/cases/compiler/jsdocInTypeScript.ts(42,12): error TS2503: Cannot find name
7171
/** @enum {string} */
7272
var E = {};
7373
E[""];
74+
75+
// make sure import types in JSDoc are not resolved
76+
/** @type {import("should-not-be-resolved").Type} */
77+
var v = import(String());
7478

tests/baselines/reference/jsdocInTypeScript.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ const obj = { foo: (a, b) => a + b };
5151
/** @enum {string} */
5252
var E = {};
5353
E[""];
54+
55+
// make sure import types in JSDoc are not resolved
56+
/** @type {import("should-not-be-resolved").Type} */
57+
var v = import(String());
5458

5559

5660
//// [jsdocInTypeScript.js]
@@ -86,3 +90,6 @@ var obj = { foo: function (a, b) { return a + b; } };
8690
/** @enum {string} */
8791
var E = {};
8892
E[""];
93+
// make sure import types in JSDoc are not resolved
94+
/** @type {import("should-not-be-resolved").Type} */
95+
var v = Promise.resolve().then(function () { return require(String()); });

tests/baselines/reference/jsdocInTypeScript.symbols

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,9 @@ var E = {};
9090
E[""];
9191
>E : Symbol(E, Decl(jsdocInTypeScript.ts, 50, 3))
9292

93+
// make sure import types in JSDoc are not resolved
94+
/** @type {import("should-not-be-resolved").Type} */
95+
var v = import(String());
96+
>v : Symbol(v, Decl(jsdocInTypeScript.ts, 55, 3))
97+
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --) ... and 1 more)
98+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

tests/baselines/reference/jsdocInTypeScript.types

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,11 @@ E[""];
103103
>E : {}
104104
>"" : ""
105105

106+
// make sure import types in JSDoc are not resolved
107+
/** @type {import("should-not-be-resolved").Type} */
108+
var v = import(String());
109+
>v : Promise<any>
110+
>import(String()) : Promise<any>
111+
>String() : string
112+
>String : StringConstructor
113+

tests/cases/compiler/jsdocInTypeScript.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// @lib: es2015
2+
// @traceResolution: true
3+
14
// JSDoc typedef tags are not bound TypeScript files.
25
/** @typedef {function} T */
36
declare const x: T;
@@ -50,3 +53,7 @@ const obj = { foo: (a, b) => a + b };
5053
/** @enum {string} */
5154
var E = {};
5255
E[""];
56+
57+
// make sure import types in JSDoc are not resolved
58+
/** @type {import("should-not-be-resolved").Type} */
59+
var v = import(String());

0 commit comments

Comments
 (0)