@@ -6,6 +6,7 @@ import url, { fileURLToPath } from 'node:url'
66import { rolldownBuild } from '@sxzz/test-utils'
77import { afterEach , expect , test , vi } from 'vitest'
88import { LanguageContext } from '../src/custom-language.ts'
9+ import { TsgoCliGenerator } from '../src/generator/tsgo-cli.ts'
910import { TsgoGenerator } from '../src/generator/tsgo.ts'
1011import { dts } from '../src/index.ts'
1112import { loadTsgoApi , loadTsgoFileSystem } from '../src/tsgo.ts'
@@ -27,6 +28,19 @@ afterEach(async () => {
2728 vi . restoreAllMocks ( )
2829} )
2930
31+ // typescript-next's getExePath takes the Node modules as an argument
32+ function getTsgoPath ( ) : string {
33+ const getExePath : any = require (
34+ path . join ( path . dirname ( fileURLToPath ( moduleUrl ) ) , 'getExePath.js' ) ,
35+ )
36+ return ( typeof getExePath === 'function' ? getExePath : getExePath . default ) ( {
37+ fs,
38+ module,
39+ path,
40+ url,
41+ } )
42+ }
43+
3044function createGenerator ( vfs = true ) : TsgoGenerator {
3145 return new TsgoGenerator ( {
3246 moduleUrl,
@@ -223,12 +237,7 @@ test('vfs resolves new files in virtual directories and retains them across upda
223237} )
224238
225239test ( 'tsgo with custom path' , async ( ) => {
226- const getExePath : any = require (
227- path . join ( path . dirname ( fileURLToPath ( moduleUrl ) ) , 'getExePath.js' ) ,
228- )
229- const tsserverPath = (
230- typeof getExePath === 'function' ? getExePath : getExePath . default
231- ) ( { fs, module, path, url } )
240+ const tsserverPath = getTsgoPath ( )
232241 const { snapshot } = await rolldownBuild (
233242 path . resolve ( dirname , 'fixtures/basic.ts' ) ,
234243 [
@@ -372,3 +381,25 @@ test.each([false, true])(
372381 expect ( declaration ! . code ) . not . toContain ( 'valueB: number' )
373382 } ,
374383)
384+
385+ test ( 'tsgo CLI does not emit next to sources outside the tsconfig directory' , async ( ) => {
386+ const fixture = path . resolve ( dirname , 'fixtures/tsgo-outside-root' )
387+ const source = path . join ( fixture , 'shared/greet.ts' )
388+ const strayDts = path . join ( fixture , 'shared/greet.d.ts' )
389+ const cli = new TsgoCliGenerator ( {
390+ tsgoPath : getTsgoPath ( ) ,
391+ tsconfig : path . join ( fixture , 'pkg/tsconfig.json' ) ,
392+ languageContext : new LanguageContext ( [ ] ) ,
393+ } )
394+
395+ await rm ( strayDts , { force : true } )
396+ try {
397+ await cli . init ( )
398+ const result = await cli . emit ( '' , source )
399+ expect ( result . code ) . toContain ( 'interface Greeting' )
400+ expect ( fs . existsSync ( strayDts ) ) . toBe ( false )
401+ } finally {
402+ await cli . dispose ( )
403+ await rm ( strayDts , { force : true } )
404+ }
405+ } )
0 commit comments