Skip to content

Commit 0d5ab4f

Browse files
authored
chore: remove tsconfig helper prompt (#1315)
1 parent 880dd48 commit 0d5ab4f

File tree

2 files changed

+5
-254
lines changed

2 files changed

+5
-254
lines changed

client/src/commands.ts

Lines changed: 5 additions & 213 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,11 @@ import type {
3737
Location,
3838
Position,
3939
} from "vscode-languageclient/node";
40-
import { getWorkspacesEnabledInfo, isPathEnabled } from "./enable";
40+
import { getWorkspacesEnabledInfo } from "./enable";
4141
import { denoUpgradePromptAndExecute } from "./upgrade";
4242
import * as fs from "fs";
4343
import * as path from "path";
4444
import * as process from "process";
45-
import * as jsoncParser from "jsonc-parser/lib/esm/main.js";
4645
import { semver } from "./semver";
4746

4847
// deno-lint-ignore no-explicit-any
@@ -72,11 +71,12 @@ export function cacheActiveDocument(
7271
}
7372

7473
export function clearHiddenPromptStorage(
75-
context: vscode.ExtensionContext,
74+
_context: vscode.ExtensionContext,
7675
_extensionContext: DenoExtensionContext,
7776
): Callback {
7877
return () => {
79-
context.globalState.update("deno.tsConfigPathsWithPromptHidden", []);
78+
// Reset states for 'Don't show this again' prompt responses, etc.
79+
// Currently unused.
8080
};
8181
}
8282

@@ -252,9 +252,8 @@ export function startLanguageServer(
252252
extensionContext.clientSubscriptions.push(
253253
extensionContext.client.onNotification(
254254
"deno/didChangeDenoConfiguration",
255-
async ({ changes }: DidChangeDenoConfigurationParams) => {
255+
({ changes }: DidChangeDenoConfigurationParams) => {
256256
let changedScopes = false;
257-
const addedDenoJsonUris = [];
258257
for (const change of changes) {
259258
if (change.configurationType != "denoJson") {
260259
continue;
@@ -263,7 +262,6 @@ export function startLanguageServer(
263262
const scopePath = vscode.Uri.parse(change.scopeUri).fsPath;
264263
scopesWithDenoJson.add(scopePath);
265264
changedScopes = true;
266-
addedDenoJsonUris.push(vscode.Uri.parse(change.fileUri));
267265
} else if (change.type == "removed") {
268266
const scopePath = vscode.Uri.parse(change.scopeUri).fsPath;
269267
scopesWithDenoJson.delete(scopePath);
@@ -274,13 +272,6 @@ export function startLanguageServer(
274272
extensionContext.tsApi?.refresh();
275273
}
276274
extensionContext.tasksSidebar.refresh();
277-
for (const addedDenoJsonUri of addedDenoJsonUris) {
278-
await maybeShowTsConfigPrompt(
279-
context,
280-
extensionContext,
281-
addedDenoJsonUri,
282-
);
283-
}
284275
},
285276
),
286277
);
@@ -358,167 +349,6 @@ function showWelcomePageIfFirstUse(
358349
}
359350
}
360351

361-
function isObject(value: unknown) {
362-
return value && typeof value == "object" && !Array.isArray(value);
363-
}
364-
365-
/**
366-
* For a discovered deno.json file, see if there's an adjacent tsconfig.json.
367-
* Offer options to either copy over the compiler options from it, or disable
368-
* the Deno LSP if it contains plugins.
369-
*/
370-
async function maybeShowTsConfigPrompt(
371-
context: vscode.ExtensionContext,
372-
extensionContext: DenoExtensionContext,
373-
denoJsonUri: vscode.Uri,
374-
) {
375-
const denoJsonPath = denoJsonUri.fsPath;
376-
if (!isPathEnabled(extensionContext, denoJsonPath)) {
377-
return;
378-
}
379-
const scopePath = path.dirname(denoJsonPath) + path.sep;
380-
const tsConfigPath = path.join(scopePath, "tsconfig.json");
381-
const tsConfigPathsWithPromptHidden = context.globalState.get<string[]>(
382-
"deno.tsConfigPathsWithPromptHidden",
383-
) ?? [];
384-
if (tsConfigPathsWithPromptHidden?.includes?.(tsConfigPath)) {
385-
return;
386-
}
387-
let tsConfigContent;
388-
try {
389-
const tsConfigText = await fs.promises.readFile(tsConfigPath, {
390-
encoding: "utf8",
391-
});
392-
tsConfigContent = jsoncParser.parse(tsConfigText);
393-
} catch {
394-
return;
395-
}
396-
const compilerOptions = tsConfigContent?.compilerOptions;
397-
if (!isObject(compilerOptions)) {
398-
return;
399-
}
400-
for (const key in compilerOptions) {
401-
if (!ALLOWED_COMPILER_OPTIONS.includes(key)) {
402-
delete compilerOptions[key];
403-
}
404-
}
405-
if (Object.entries(compilerOptions).length == 0) {
406-
return;
407-
}
408-
const plugins = compilerOptions?.plugins;
409-
let selection;
410-
if (Array.isArray(plugins) && plugins.length) {
411-
// This tsconfig.json contains plugins. Prompt to disable the LSP.
412-
const workspaceFolders = vscode.workspace.workspaceFolders ?? [];
413-
let scopeFolderEntry = null;
414-
const folderEntries = workspaceFolders.map((f) =>
415-
[f.uri.fsPath + path.sep, f] as const
416-
);
417-
folderEntries.sort();
418-
folderEntries.reverse();
419-
for (const folderEntry of folderEntries) {
420-
if (scopePath.startsWith(folderEntry[0])) {
421-
scopeFolderEntry = folderEntry;
422-
break;
423-
}
424-
}
425-
if (!scopeFolderEntry) {
426-
return;
427-
}
428-
const [scopeFolderPath, scopeFolder] = scopeFolderEntry;
429-
selection = await vscode.window.showInformationMessage(
430-
`A tsconfig.json with compiler options was discovered in a Deno-enabled folder. For projects with compiler plugins, it is recommended to disable the Deno language server if you are seeing errors (${tsConfigPath}).`,
431-
"Disable Deno LSP",
432-
"Hide this message",
433-
);
434-
if (selection == "Disable Deno LSP") {
435-
const config = vscode.workspace.getConfiguration(
436-
EXTENSION_NS,
437-
scopeFolder,
438-
);
439-
if (scopePath == scopeFolderPath) {
440-
await config.update("enable", false);
441-
} else {
442-
let disablePaths = config.get<string[]>("disablePaths");
443-
if (!Array.isArray(disablePaths)) {
444-
disablePaths = [];
445-
}
446-
const relativeUri = scopePath.substring(scopeFolderPath.length).replace(
447-
/\\/g,
448-
"/",
449-
).replace(/\/*$/, "");
450-
disablePaths.push(relativeUri);
451-
await config.update("disablePaths", disablePaths);
452-
}
453-
}
454-
} else {
455-
// This tsconfig.json has compiler options which may be copied to a
456-
// deno.json. If the deno.json has no compiler options, prompt to copy them
457-
// over.
458-
let denoJsonText;
459-
let denoJsonContent;
460-
try {
461-
denoJsonText = await fs.promises.readFile(denoJsonPath, {
462-
encoding: "utf8",
463-
});
464-
denoJsonContent = jsoncParser.parse(denoJsonText);
465-
} catch {
466-
return;
467-
}
468-
if (!isObject(denoJsonContent) || "compilerOptions" in denoJsonContent) {
469-
return;
470-
}
471-
selection = await vscode.window.showInformationMessage(
472-
`A tsconfig.json with compiler options was discovered in a Deno-enabled folder. Would you like to copy these to your Deno configuration file? Note that only a subset of options are supported (${tsConfigPath}).`,
473-
"Copy to deno.json[c]",
474-
"Hide this message",
475-
);
476-
if (selection == "Copy to deno.json[c]") {
477-
try {
478-
let newDenoJsonContent = jsoncParser.applyEdits(
479-
denoJsonText,
480-
jsoncParser.modify(
481-
denoJsonText,
482-
["compilerOptions"],
483-
compilerOptions,
484-
{ formattingOptions: { insertSpaces: true, tabSize: 2 } },
485-
),
486-
);
487-
const unstable = Array.isArray(denoJsonContent.unstable)
488-
? denoJsonContent.unstable as unknown[]
489-
: [];
490-
if (!unstable.includes("sloppy-imports")) {
491-
unstable.push("sloppy-imports");
492-
newDenoJsonContent = jsoncParser.applyEdits(
493-
newDenoJsonContent,
494-
jsoncParser.modify(
495-
newDenoJsonContent,
496-
["unstable"],
497-
unstable,
498-
{ formattingOptions: { insertSpaces: true, tabSize: 2 } },
499-
),
500-
);
501-
}
502-
await fs.promises.writeFile(denoJsonPath, newDenoJsonContent);
503-
} catch (error) {
504-
vscode.window.showErrorMessage(
505-
`Could not modify "${denoJsonPath}": ${error}`,
506-
);
507-
}
508-
}
509-
}
510-
if (selection == "Hide this message") {
511-
const tsConfigPathsWithPromptHidden = context.globalState.get<string[]>(
512-
"deno.tsConfigPathsWithPromptHidden",
513-
) ?? [];
514-
tsConfigPathsWithPromptHidden?.push?.(tsConfigPath);
515-
context.globalState.update(
516-
"deno.tsConfigPathsWithPromptHidden",
517-
tsConfigPathsWithPromptHidden,
518-
);
519-
}
520-
}
521-
522352
export function showReferences(
523353
_content: vscode.ExtensionContext,
524354
extensionContext: DenoExtensionContext,
@@ -726,41 +556,3 @@ function isDenoDisabledCompletely(): boolean {
726556
vscode.workspace.getConfiguration(EXTENSION_NS, f)
727557
).every(isScopeDisabled);
728558
}
729-
730-
// Keep this in sync with the supported compiler options set in CLI. Currently:
731-
// https://github.com/denoland/deno_config/blob/0.47.1/src/deno_json/ts.rs#L85-L119
732-
const ALLOWED_COMPILER_OPTIONS = [
733-
"allowUnreachableCode",
734-
"allowUnusedLabels",
735-
"checkJs",
736-
"emitDecoratorMetadata",
737-
"exactOptionalPropertyTypes",
738-
"experimentalDecorators",
739-
"isolatedDeclarations",
740-
"jsx",
741-
"jsxFactory",
742-
"jsxFragmentFactory",
743-
"jsxImportSource",
744-
"jsxPrecompileSkipElements",
745-
"lib",
746-
"noErrorTruncation",
747-
"noFallthroughCasesInSwitch",
748-
"noImplicitAny",
749-
"noImplicitOverride",
750-
"noImplicitReturns",
751-
"noImplicitThis",
752-
"noPropertyAccessFromIndexSignature",
753-
"noUncheckedIndexedAccess",
754-
"noUnusedLocals",
755-
"noUnusedParameters",
756-
"rootDirs",
757-
"strict",
758-
"strictBindCallApply",
759-
"strictBuiltinIteratorReturn",
760-
"strictFunctionTypes",
761-
"strictNullChecks",
762-
"strictPropertyInitialization",
763-
"types",
764-
"useUnknownInCatchVariables",
765-
"verbatimModuleSyntax",
766-
];

client/src/enable.ts

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,13 @@ import { ENABLE, ENABLE_PATHS, EXTENSION_NS } from "./constants";
44

55
import * as vscode from "vscode";
66
import { DenoExtensionContext, EnableSettings } from "./types";
7-
import * as os from "os";
8-
import * as path from "path";
97

108
export interface WorkspaceEnabledInfo {
119
folder: vscode.WorkspaceFolder;
1210
enabled: boolean | undefined;
1311
hasDenoConfig: boolean;
1412
}
1513

16-
const PARENT_RELATIVE_REGEX = os.platform() === "win32"
17-
? /\.\.(?:[/\\]|$)/
18-
: /\.\.(?:\/|$)/;
19-
20-
/** Checks if `parent` is an ancestor of `child`. */
21-
function pathStartsWith(child: string, parent: string) {
22-
if (path.isAbsolute(child) !== path.isAbsolute(parent)) {
23-
return false;
24-
}
25-
const relative = path.relative(parent, child);
26-
return !relative.match(PARENT_RELATIVE_REGEX);
27-
}
28-
29-
export function isPathEnabled(
30-
extensionContext: DenoExtensionContext,
31-
filePath: string,
32-
) {
33-
const enableSettings =
34-
extensionContext.enableSettingsByFolder?.find(([workspace, _]) =>
35-
pathStartsWith(filePath, workspace)
36-
)?.[1] ?? extensionContext.enableSettingsUnscoped ??
37-
{ enable: null, enablePaths: null, disablePaths: [] };
38-
const scopesWithDenoJson = extensionContext.scopesWithDenoJson ?? new Set();
39-
for (const path of enableSettings.disablePaths) {
40-
if (pathStartsWith(filePath, path)) {
41-
return false;
42-
}
43-
}
44-
if (enableSettings.enablePaths) {
45-
return enableSettings.enablePaths.some((p) => pathStartsWith(filePath, p));
46-
}
47-
if (enableSettings.enable != null) {
48-
return enableSettings.enable;
49-
}
50-
return [...scopesWithDenoJson].some((scope) =>
51-
pathStartsWith(filePath, scope)
52-
);
53-
}
54-
5514
export async function getWorkspacesEnabledInfo() {
5615
const result: WorkspaceEnabledInfo[] = [];
5716
for (const folder of vscode.workspace.workspaceFolders ?? []) {

0 commit comments

Comments
 (0)