From 02538d6953399dfe4f5014ca1bc04975080b5c05 Mon Sep 17 00:00:00 2001 From: Tom Prince Date: Tue, 23 Jul 2024 22:28:05 -0600 Subject: [PATCH] EDITOR: Enable strict typechecking of typescript. (#1493) * EDITOR: Tweak typescript language server configuration to match use. - We allow importing files with `.ts`/`.tsx` extensions. - We use an file-at-a-time transpiler, so we don't support features that require understanding the full type system. - We use the classic `React.createElement` transform. --- src/ScriptEditor/ScriptEditor.ts | 51 ++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/src/ScriptEditor/ScriptEditor.ts b/src/ScriptEditor/ScriptEditor.ts index 9a5c50d84..4cc22c335 100644 --- a/src/ScriptEditor/ScriptEditor.ts +++ b/src/ScriptEditor/ScriptEditor.ts @@ -63,26 +63,45 @@ export class ScriptEditor { // Add ts definitions for API const source = netscriptDefinitions.replace(/export /g, ""); - for (const languageDefaults of [ - monaco.languages.typescript.javascriptDefaults, - monaco.languages.typescript.typescriptDefaults, - ]) { + for (const [language, languageDefaults] of [ + ["javascript", monaco.languages.typescript.javascriptDefaults], + ["typescript", monaco.languages.typescript.typescriptDefaults], + ] as const) { languageDefaults.addExtraLib(source, "netscript.d.ts"); languageDefaults.addExtraLib(reactTypes, "react.d.ts"); languageDefaults.addExtraLib(reactDomTypes, "react-dom.d.ts"); + languageDefaults.setCompilerOptions({ + ...languageDefaults.getCompilerOptions(), + // We allow direct importing of `.ts`/`.tsx` files, so tell the typescript language server that. + allowImportingTsExtensions: true, + // We use file-at-a-time transpiler. See https://www.typescriptlang.org/tsconfig/#isolatedModules + isolatedModules: true, + // We use the classic (i.e. `React.createElement`:) react runtime. + jsx: monaco.languages.typescript.JsxEmit.React, + // We define `React` and `ReactDOM` as globals. Don't mark using them as errors. + allowUmdGlobalAccess: true, + // Enable strict typechecking. + // Note that checking in javascript is disabled by default but can be enabled via `// @ts-check`. + // This enables strictNullChecks, which impacts reported types, even in javascript. + strict: true, + noImplicitAny: language === "typescript", + noImplicitReturns: true, + }); + languageDefaults.setDiagnosticsOptions({ + ...languageDefaults.getDiagnosticsOptions(), + // Show semantic errors, even in javascript. + // Note that this will only happen if checking is enabled in javascript (e.g. by `// @ts-check`) + noSemanticValidation: false, + // Ignore these errors in the editor: + diagnosticCodesToIgnore: [ + // We define `React` and `ReactDOM` as globals. Don't mark using them as errors. + // Even though we set allowUmdGlobalAccess, it still shows a warning (instead of an error). + // - 'React' refers to a UMD global, but the current file is a module. Consider adding an import instead.(2686) + 2686, + ], + }); } - monaco.languages.typescript.typescriptDefaults.setCompilerOptions({ - ...monaco.languages.typescript.typescriptDefaults.getCompilerOptions(), - jsx: monaco.languages.typescript.JsxEmit.ReactJSX, - allowUmdGlobalAccess: true, - }); - /** - * Ignore these errors in the editor: - * - Cannot find module ''. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?(2792) - */ - monaco.languages.typescript.typescriptDefaults.setDiagnosticsOptions({ - diagnosticCodesToIgnore: [2792], - }); + monaco.languages.json.jsonDefaults.setModeConfiguration({ ...monaco.languages.json.jsonDefaults.modeConfiguration, //completion should be disabled because the