Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand All @@ -1966,16 +1966,16 @@ 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)))) {
return child;
}
};
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;
}
Expand Down
4 changes: 4 additions & 0 deletions tests/baselines/reference/jsdocInTypeScript.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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());

7 changes: 7 additions & 0 deletions tests/baselines/reference/jsdocInTypeScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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()); });
6 changes: 6 additions & 0 deletions tests/baselines/reference/jsdocInTypeScript.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -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)

1 change: 1 addition & 0 deletions tests/baselines/reference/jsdocInTypeScript.trace.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
8 changes: 8 additions & 0 deletions tests/baselines/reference/jsdocInTypeScript.types
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>
>import(String()) : Promise<any>
>String() : string
>String : StringConstructor

7 changes: 7 additions & 0 deletions tests/cases/compiler/jsdocInTypeScript.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// @lib: es2015
// @traceResolution: true

// JSDoc typedef tags are not bound TypeScript files.
/** @typedef {function} T */
declare const x: T;
Expand Down Expand Up @@ -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());