Skip to content

Commit d6f4143

Browse files
authored
(fix) abort patching ts when jsx project detected (#985)
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.
1 parent b44cf29 commit d6f4143

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

packages/typescript-plugin/src/index.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import type ts from 'typescript/lib/tsserverlibrary';
88
function init(modules: { typescript: typeof ts }) {
99
function create(info: ts.server.PluginCreateInfo) {
1010
const logger = new Logger(info.project.projectService.logger);
11+
12+
if (!isSvelteProject(info)) {
13+
logger.log('Detected that this is not a Svelte project, abort patching TypeScript');
14+
return info.languageService;
15+
}
16+
1117
logger.log('Starting Svelte plugin');
1218

1319
const snapshotManager = new SvelteSnapshotManager(
@@ -45,14 +51,25 @@ function init(modules: { typescript: typeof ts }) {
4551
compilerOptions.jsx = modules.typescript.JsxEmit.Preserve;
4652

4753
// detect which JSX namespace to use (svelte | svelteNative) if not specified or not compatible
48-
if (!compilerOptions.jsxFactory || !compilerOptions.jsxFactory.startsWith('svelte')) {
54+
if (!compilerOptions.jsxFactory?.startsWith('svelte')) {
4955
// Default to regular svelte, this causes the usage of the "svelte.JSX" namespace
5056
// We don't need to add a switch for svelte-native because the jsx is only relevant
5157
// within Svelte files, which this plugin does not deal with.
5258
compilerOptions.jsxFactory = 'svelte.createElement';
5359
}
5460
}
5561

62+
function isSvelteProject(info: ts.server.PluginCreateInfo) {
63+
// Add more checks like "no Svelte file found" or "no config file found"?
64+
const compilerOptions = info.project.getCompilerOptions();
65+
const isNoJsxProject =
66+
(!compilerOptions.jsx || compilerOptions.jsx === modules.typescript.JsxEmit.Preserve) &&
67+
(!compilerOptions.jsxFactory || compilerOptions.jsxFactory.startsWith('svelte')) &&
68+
!compilerOptions.jsxFragmentFactory &&
69+
!compilerOptions.jsxImportSource;
70+
return isNoJsxProject;
71+
}
72+
5673
return { create, getExternalFiles };
5774
}
5875

0 commit comments

Comments
 (0)