From acc758e6bd3527f0e40e6c02aeca765d3fa1130e Mon Sep 17 00:00:00 2001 From: Klaus Meinhardt Date: Fri, 26 Oct 2018 22:38:01 +0200 Subject: [PATCH 1/3] only search require calls in JS files --- src/compiler/program.ts | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 3e927c0b75f71..31a053ed5c7a6 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1890,8 +1890,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; @@ -1943,15 +1946,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)) { @@ -1961,15 +1971,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; } From 9db41b4c85f2e82d3434d7bd6ac0d33615bf17eb Mon Sep 17 00:00:00 2001 From: Klaus Meinhardt Date: Fri, 26 Oct 2018 23:07:16 +0200 Subject: [PATCH 2/3] fix compile error --- src/compiler/inspectValue.ts | 1 + src/compiler/sys.ts | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/compiler/inspectValue.ts b/src/compiler/inspectValue.ts index 86b45b65031b7..f791ba239ae52 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/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; From a9dc94425118e1a62257b68f69ee6e612dbbdadb Mon Sep 17 00:00:00 2001 From: Klaus Meinhardt Date: Tue, 30 Oct 2018 20:23:52 +0100 Subject: [PATCH 3/3] remove useless parens --- src/compiler/program.ts | 2 +- tests/cases/user/prettier/prettier | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 31a053ed5c7a6..3977e510116e5 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1890,7 +1890,7 @@ namespace ts { for (const node of file.statements) { collectModuleReferences(node, /*inAmbientModule*/ false); } - if ((file.flags & NodeFlags.PossiblyContainsDynamicImport)) { + if (file.flags & NodeFlags.PossiblyContainsDynamicImport) { collectDynamicImports(); } if (isJavaScriptFile) { 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