summaryrefslogtreecommitdiff
path: root/cli/tsc/99_main_compiler.js
diff options
context:
space:
mode:
authorNayeem Rahman <nayeemrmn99@gmail.com>2024-09-17 18:28:51 +0100
committerGitHub <noreply@github.com>2024-09-17 18:28:51 +0100
commitd4a06251c54fc004e189469e493b1261be200300 (patch)
treefd1778c0dc7dae678fbe13389e1f33fddbcd2d71 /cli/tsc/99_main_compiler.js
parentb4faf609484726d827b43140b4ce2bc8e58163df (diff)
feat(lsp): auto-import types with 'import type' (#25662)
Diffstat (limited to 'cli/tsc/99_main_compiler.js')
-rw-r--r--cli/tsc/99_main_compiler.js39
1 files changed, 34 insertions, 5 deletions
diff --git a/cli/tsc/99_main_compiler.js b/cli/tsc/99_main_compiler.js
index 7fac8cd35..c5769168f 100644
--- a/cli/tsc/99_main_compiler.js
+++ b/cli/tsc/99_main_compiler.js
@@ -516,6 +516,7 @@ delete Object.prototype.__proto__;
/** @typedef {{
* ls: ts.LanguageService & { [k:string]: any },
* compilerOptions: ts.CompilerOptions,
+ * forceEnabledVerbatimModuleSyntax: boolean,
* }} LanguageServiceEntry */
/** @type {{ unscoped: LanguageServiceEntry, byScope: Map<string, LanguageServiceEntry> }} */
const languageServiceEntries = {
@@ -1025,7 +1026,7 @@ delete Object.prototype.__proto__;
: ts.sortAndDeduplicateDiagnostics(
checkFiles.map((s) => program.getSemanticDiagnostics(s)).flat(),
)),
- ].filter(filterMapDiagnostic);
+ ].filter(filterMapDiagnostic.bind(null, false));
// emit the tsbuildinfo file
// @ts-ignore: emitBuildInfo is not exposed (https://github.com/microsoft/TypeScript/issues/49871)
@@ -1040,11 +1041,28 @@ delete Object.prototype.__proto__;
debug("<<< exec stop");
}
- /** @param {ts.Diagnostic} diagnostic */
- function filterMapDiagnostic(diagnostic) {
+ /**
+ * @param {boolean} isLsp
+ * @param {ts.Diagnostic} diagnostic
+ */
+ function filterMapDiagnostic(isLsp, diagnostic) {
if (IGNORED_DIAGNOSTICS.includes(diagnostic.code)) {
return false;
}
+ if (isLsp) {
+ // TS1484: `...` is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
+ // We force-enable `verbatimModuleSyntax` in the LSP so the `type`
+ // modifier is used when auto-importing types. But we don't want this
+ // diagnostic unless it was explicitly enabled by the user.
+ if (diagnostic.code == 1484) {
+ const entry = (lastRequestScope
+ ? languageServiceEntries.byScope.get(lastRequestScope)
+ : null) ?? languageServiceEntries.unscoped;
+ if (entry.forceEnabledVerbatimModuleSyntax) {
+ return false;
+ }
+ }
+ }
// make the diagnostic for using an `export =` in an es module a warning
if (diagnostic.code === 1203) {
diagnostic.category = ts.DiagnosticCategory.Warning;
@@ -1140,10 +1158,12 @@ delete Object.prototype.__proto__;
"strict": true,
"target": "esnext",
"useDefineForClassFields": true,
+ "verbatimModuleSyntax": true,
"jsx": "react",
"jsxFactory": "React.createElement",
"jsxFragmentFactory": "React.Fragment",
}),
+ forceEnabledVerbatimModuleSyntax: true,
};
setLogDebug(enableDebugLogging, "TSLS");
debug("serverInit()");
@@ -1209,8 +1229,17 @@ delete Object.prototype.__proto__;
const ls = oldEntry
? oldEntry.ls
: ts.createLanguageService(host, documentRegistry);
+ let forceEnabledVerbatimModuleSyntax = false;
+ if (!config["verbatimModuleSyntax"]) {
+ config["verbatimModuleSyntax"] = true;
+ forceEnabledVerbatimModuleSyntax = true;
+ }
const compilerOptions = lspTsConfigToCompilerOptions(config);
- newByScope.set(scope, { ls, compilerOptions });
+ newByScope.set(scope, {
+ ls,
+ compilerOptions,
+ forceEnabledVerbatimModuleSyntax,
+ });
languageServiceEntries.byScope.delete(scope);
}
for (const oldEntry of languageServiceEntries.byScope.values()) {
@@ -1275,7 +1304,7 @@ delete Object.prototype.__proto__;
...ls.getSemanticDiagnostics(specifier),
...ls.getSuggestionDiagnostics(specifier),
...ls.getSyntacticDiagnostics(specifier),
- ].filter(filterMapDiagnostic));
+ ].filter(filterMapDiagnostic.bind(null, true)));
}
return respond(id, diagnosticMap);
} catch (e) {