diff --git a/src/compiler/inspectValue.ts b/src/compiler/inspectValue.ts index fab4a57ef1bab..d9c32326b06b8 100644 --- a/src/compiler/inspectValue.ts +++ b/src/compiler/inspectValue.ts @@ -1,3 +1,4 @@ +/// /* @internal */ namespace ts { export interface InspectValueOptions { diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 080b17b8cbe0d..2452c9ac68768 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -2012,8 +2012,11 @@ namespace ts { for (const node of file.statements) { collectModuleReferences(node, /*inAmbientModule*/ false); } - if ((file.flags & NodeFlags.PossiblyContainsDynamicImport) || isJavaScriptFile) { - collectDynamicImportOrRequireCalls(file); + if (file.flags & NodeFlags.PossiblyContainsDynamicImport) { + collectDynamicImports(); + } + if (isJavaScriptFile) { + collectRequireCalls(); } file.imports = imports || emptyArray; @@ -2065,15 +2068,22 @@ namespace ts { } } - function collectDynamicImportOrRequireCalls(file: SourceFile) { - const r = /import|require/g; + function collectRequireCalls() { + const r = /\brequire\b/g; while (r.exec(file.text) !== null) { - const node = getNodeAtPosition(file, r.lastIndex); + const node = getNodeAtPosition(r.lastIndex); if (isRequireCall(node, /*checkArgumentIsStringLiteralLike*/ true)) { imports = append(imports, node.arguments[0]); } + } + } + + function collectDynamicImports() { + const r = /\bimport\b/g; + while (r.exec(file.text) !== null) { + const node = getNodeAtPosition(r.lastIndex, isJavaScriptFile); // we have to check the argument list has length of 1. We will still have to process these even though we have parsing error. - else if (isImportCall(node) && node.arguments.length === 1 && isStringLiteralLike(node.arguments[0])) { + if (isImportCall(node) && node.arguments.length === 1 && isStringLiteralLike(node.arguments[0])) { imports = append(imports, node.arguments[0] as StringLiteralLike); } else if (isLiteralImportTypeNode(node)) { @@ -2083,15 +2093,15 @@ namespace ts { } /** 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; + function getNodeAtPosition(position: number, allowJsDoc?: boolean): Node { + let current: Node = file; const getContainingChild = (child: Node) => { if (child.pos <= position && (position < child.end || (position === child.end && (child.kind === SyntaxKind.EndOfFileToken)))) { return child; } }; while (true) { - const child = isJavaScriptFile && hasJSDocNodes(current) && forEach(current.jsDoc, getContainingChild) || forEachChild(current, getContainingChild); + const child = allowJsDoc && hasJSDocNodes(current) && forEach(current.jsDoc, getContainingChild) || forEachChild(current, getContainingChild); if (!child) { return current; } diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index 8ee1e4571b367..9ddf7dc35debe 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -1,3 +1,5 @@ +/// + declare function setTimeout(handler: (...args: any[]) => void, timeout: number): any; declare function clearTimeout(handle: any): void; diff --git a/tests/cases/user/prettier/prettier b/tests/cases/user/prettier/prettier index 67f1c4877ee10..2402a2a07b434 160000 --- a/tests/cases/user/prettier/prettier +++ b/tests/cases/user/prettier/prettier @@ -1 +1 @@ -Subproject commit 67f1c4877ee1090b66d468a847caccca411a6f82 +Subproject commit 2402a2a07b434411dbc8a529b41d0d1d41befca5