From 07c67af67336b2c01b8e4c6930bbb773feae9808 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Wed, 5 May 2021 09:46:30 +0200 Subject: [PATCH] (fix) abort patching ts when jsx project detected Don't patch TypeScript with Svelte stuff if we detect that certain JSX options are set, which hints at a React project or something similar. --- packages/typescript-plugin/src/index.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/typescript-plugin/src/index.ts b/packages/typescript-plugin/src/index.ts index abc000a2d..d0da77bc2 100644 --- a/packages/typescript-plugin/src/index.ts +++ b/packages/typescript-plugin/src/index.ts @@ -8,6 +8,12 @@ import type ts from 'typescript/lib/tsserverlibrary'; function init(modules: { typescript: typeof ts }) { function create(info: ts.server.PluginCreateInfo) { const logger = new Logger(info.project.projectService.logger); + + if (!isSvelteProject(info)) { + logger.log('Detected that this is not a Svelte project, abort patching TypeScript'); + return info.languageService; + } + logger.log('Starting Svelte plugin'); const snapshotManager = new SvelteSnapshotManager( @@ -45,7 +51,7 @@ function init(modules: { typescript: typeof ts }) { compilerOptions.jsx = modules.typescript.JsxEmit.Preserve; // detect which JSX namespace to use (svelte | svelteNative) if not specified or not compatible - if (!compilerOptions.jsxFactory || !compilerOptions.jsxFactory.startsWith('svelte')) { + if (!compilerOptions.jsxFactory?.startsWith('svelte')) { // Default to regular svelte, this causes the usage of the "svelte.JSX" namespace // We don't need to add a switch for svelte-native because the jsx is only relevant // within Svelte files, which this plugin does not deal with. @@ -53,6 +59,17 @@ function init(modules: { typescript: typeof ts }) { } } + function isSvelteProject(info: ts.server.PluginCreateInfo) { + // Add more checks like "no Svelte file found" or "no config file found"? + const compilerOptions = info.project.getCompilerOptions(); + const isNoJsxProject = + (!compilerOptions.jsx || compilerOptions.jsx === modules.typescript.JsxEmit.Preserve) && + (!compilerOptions.jsxFactory || compilerOptions.jsxFactory.startsWith('svelte')) && + !compilerOptions.jsxFragmentFactory && + !compilerOptions.jsxImportSource; + return isNoJsxProject; + } + return { create, getExternalFiles }; }