From 5348d1f4bdf7c1ca828a96ee14c8cf58e4e3c0a9 Mon Sep 17 00:00:00 2001 From: Klaus Meinhardt Date: Fri, 26 Oct 2018 15:54:36 +0200 Subject: [PATCH] don't resolve import types in JSDoc of TS files --- src/compiler/program.ts | 8 ++++---- tests/baselines/reference/jsdocInTypeScript.errors.txt | 4 ++++ tests/baselines/reference/jsdocInTypeScript.js | 7 +++++++ tests/baselines/reference/jsdocInTypeScript.symbols | 6 ++++++ tests/baselines/reference/jsdocInTypeScript.trace.json | 1 + tests/baselines/reference/jsdocInTypeScript.types | 8 ++++++++ tests/cases/compiler/jsdocInTypeScript.ts | 7 +++++++ 7 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 tests/baselines/reference/jsdocInTypeScript.trace.json diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 9424fc98bc31c..15d66a997bdf5 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1952,7 +1952,7 @@ namespace ts { function collectDynamicImportOrRequireCalls(file: SourceFile) { const r = /import|require/g; while (r.exec(file.text) !== null) { - const node = getTokenAtPosition(file, r.lastIndex); + const node = getNodeAtPosition(file, r.lastIndex); if (isRequireCall(node, /*checkArgumentIsStringLiteralLike*/ true)) { imports = append(imports, node.arguments[0]); } @@ -1966,8 +1966,8 @@ namespace ts { } } - /** Returns a token if position is in [start-of-leading-trivia, end) */ - function getTokenAtPosition(sourceFile: SourceFile, position: number): Node { + /** Returns a token if position is in [start-of-leading-trivia, end), includes JSDoc only in JS files */ + function getNodeAtPosition(sourceFile: SourceFile, position: number): Node { let current: Node = sourceFile; const getContainingChild = (child: Node) => { if (child.pos <= position && (position < child.end || (position === child.end && (child.kind === SyntaxKind.EndOfFileToken)))) { @@ -1975,7 +1975,7 @@ namespace ts { } }; while (true) { - const child = hasJSDocNodes(current) && forEach(current.jsDoc, getContainingChild) || forEachChild(current, getContainingChild); + const child = isJavaScriptFile && hasJSDocNodes(current) && forEach(current.jsDoc, getContainingChild) || forEachChild(current, getContainingChild); if (!child) { return current; } diff --git a/tests/baselines/reference/jsdocInTypeScript.errors.txt b/tests/baselines/reference/jsdocInTypeScript.errors.txt index c8972bb0f0a1d..24f80e1c88903 100644 --- a/tests/baselines/reference/jsdocInTypeScript.errors.txt +++ b/tests/baselines/reference/jsdocInTypeScript.errors.txt @@ -71,4 +71,8 @@ tests/cases/compiler/jsdocInTypeScript.ts(42,12): error TS2503: Cannot find name /** @enum {string} */ var E = {}; E[""]; + + // make sure import types in JSDoc are not resolved + /** @type {import("should-not-be-resolved").Type} */ + var v = import(String()); \ No newline at end of file diff --git a/tests/baselines/reference/jsdocInTypeScript.js b/tests/baselines/reference/jsdocInTypeScript.js index ebff5629c379c..037d655ccf030 100644 --- a/tests/baselines/reference/jsdocInTypeScript.js +++ b/tests/baselines/reference/jsdocInTypeScript.js @@ -51,6 +51,10 @@ const obj = { foo: (a, b) => a + b }; /** @enum {string} */ var E = {}; E[""]; + +// make sure import types in JSDoc are not resolved +/** @type {import("should-not-be-resolved").Type} */ +var v = import(String()); //// [jsdocInTypeScript.js] @@ -86,3 +90,6 @@ var obj = { foo: function (a, b) { return a + b; } }; /** @enum {string} */ var E = {}; E[""]; +// make sure import types in JSDoc are not resolved +/** @type {import("should-not-be-resolved").Type} */ +var v = Promise.resolve().then(function () { return require(String()); }); diff --git a/tests/baselines/reference/jsdocInTypeScript.symbols b/tests/baselines/reference/jsdocInTypeScript.symbols index c65d1215abea5..3b56fe3b8480b 100644 --- a/tests/baselines/reference/jsdocInTypeScript.symbols +++ b/tests/baselines/reference/jsdocInTypeScript.symbols @@ -90,3 +90,9 @@ var E = {}; E[""]; >E : Symbol(E, Decl(jsdocInTypeScript.ts, 50, 3)) +// make sure import types in JSDoc are not resolved +/** @type {import("should-not-be-resolved").Type} */ +var v = import(String()); +>v : Symbol(v, Decl(jsdocInTypeScript.ts, 55, 3)) +>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) + diff --git a/tests/baselines/reference/jsdocInTypeScript.trace.json b/tests/baselines/reference/jsdocInTypeScript.trace.json new file mode 100644 index 0000000000000..0637a088a01e8 --- /dev/null +++ b/tests/baselines/reference/jsdocInTypeScript.trace.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/tests/baselines/reference/jsdocInTypeScript.types b/tests/baselines/reference/jsdocInTypeScript.types index 010efb68b7a6d..0b4bb8192cbe2 100644 --- a/tests/baselines/reference/jsdocInTypeScript.types +++ b/tests/baselines/reference/jsdocInTypeScript.types @@ -103,3 +103,11 @@ E[""]; >E : {} >"" : "" +// make sure import types in JSDoc are not resolved +/** @type {import("should-not-be-resolved").Type} */ +var v = import(String()); +>v : Promise +>import(String()) : Promise +>String() : string +>String : StringConstructor + diff --git a/tests/cases/compiler/jsdocInTypeScript.ts b/tests/cases/compiler/jsdocInTypeScript.ts index 4d1f0fbbe4273..be485f59c5540 100644 --- a/tests/cases/compiler/jsdocInTypeScript.ts +++ b/tests/cases/compiler/jsdocInTypeScript.ts @@ -1,3 +1,6 @@ +// @lib: es2015 +// @traceResolution: true + // JSDoc typedef tags are not bound TypeScript files. /** @typedef {function} T */ declare const x: T; @@ -50,3 +53,7 @@ const obj = { foo: (a, b) => a + b }; /** @enum {string} */ var E = {}; E[""]; + +// make sure import types in JSDoc are not resolved +/** @type {import("should-not-be-resolved").Type} */ +var v = import(String());