@@ -4,7 +4,7 @@ import { createRequire } from 'node:module'
44import { decode } from '@jridgewell/sourcemap-codec'
55import { SourceMap } from '@volar/language-core'
66import { svelte2tsx } from 'svelte2tsx'
7- import { createTwoslasher as _createTwoSlasher , defaultCompilerOptions , defaultHandbookOptions , findFlagNotations , findQueryMarkers } from 'twoslash'
7+ import { createTwoslasher as createTwoSlasherBase , defaultCompilerOptions , defaultHandbookOptions , findFlagNotations , findQueryMarkers } from 'twoslash'
88import { createPositionConverter , removeCodeRanges , resolveNodePositions } from 'twoslash-protocol'
99import ts from 'typescript'
1010
@@ -15,24 +15,18 @@ export interface CreateTwoslashSvelteOptions extends CreateTwoslashOptions {
1515 * @default false
1616 */
1717 debugShowGeneratedCode ?: boolean
18- /**
19- * Use the version 4 definitions for Svelte.
20- *
21- * @default false
22- */
23- useVersion4TypeDefinitions ?: boolean
2418}
2519
2620/**
2721 * Create a twoslasher instance that add additional support for Svelte
2822 */
2923export function createTwoslasher ( createOptions : CreateTwoslashSvelteOptions = { } ) : TwoslashInstance {
3024 const require = createRequire ( import . meta. url )
31- const _twoslasher = _createTwoSlasher ( createOptions )
25+ const twoslasherBase = createTwoSlasherBase ( createOptions )
3226
3327 function twoslasher ( code : string , extension ?: string , options : TwoslashExecuteOptions = { } ) {
3428 if ( extension !== 'svelte' ) {
35- return _twoslasher ( code , extension , options )
29+ return twoslasherBase ( code , extension , options )
3630 }
3731
3832 const compilerOptions : ts . CompilerOptions = {
@@ -93,21 +87,17 @@ export function createTwoslasher(createOptions: CreateTwoslashSvelteOptions = {}
9387 }
9488 return offsets [ offsets . length - 1 ] ?. [ 0 ]
9589 }
96- const svelte2tsxPackageJsonPath = require . resolve ( 'svelte2tsx/package.json' )
97-
98- const references = createOptions . useVersion4TypeDefinitions
99- ? [
100- `/// <reference path="${ new URL ( 'svelte-shims-v4.d.ts' , svelte2tsxPackageJsonPath ) . pathname } " />` ,
101- `/// <reference path="${ new URL ( 'svelte-jsx-v4.d.ts' , svelte2tsxPackageJsonPath ) . pathname } " />` ,
102- ]
103- : [
104- `/// <reference path="${ new URL ( 'svelte-shims.d.ts' , svelte2tsxPackageJsonPath ) . pathname } " />` ,
105- `/// <reference path="${ new URL ( 'svelte-jsx.d.ts' , svelte2tsxPackageJsonPath ) . pathname } " />` ,
106- ] . join ( '\n' )
10790
108- const result = _twoslasher ( ` ${ references } \n ${ compiled . code } ` , 'tsx' , {
91+ const result = twoslasherBase ( compiled . code , 'tsx' , {
10992 ...options ,
110- compilerOptions,
93+ compilerOptions : {
94+ ...compilerOptions ,
95+ types : [
96+ ...( compilerOptions . types ?? [ ] ) ,
97+ require . resolve ( `svelte2tsx/svelte-jsx-v4.d.ts` ) ,
98+ require . resolve ( `svelte2tsx/svelte-shims-v4.d.ts` ) ,
99+ ] ,
100+ } ,
111101 handbookOptions : {
112102 ...handbookOptions ,
113103 keepNotations : true ,
@@ -193,7 +183,7 @@ export function createTwoslasher(createOptions: CreateTwoslashSvelteOptions = {}
193183 return result
194184 }
195185
196- twoslasher . getCacheMap = _twoslasher . getCacheMap
186+ twoslasher . getCacheMap = twoslasherBase . getCacheMap
197187
198188 return twoslasher
199189}
0 commit comments