Skip to content

Commit 7b15002

Browse files
fix(tsgo): keep declarations for files outside the tsconfig dir in the temp outDir (#313)
Co-authored-by: Kevin Bot <[email protected]>
1 parent 9a424bc commit 7b15002

6 files changed

Lines changed: 62 additions & 16 deletions

File tree

‎src/generator/index.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import path from 'node:path'
21
import { isLegacyTsgo } from '../tsgo.ts'
32
import { OxcGenerator } from './oxc.ts'
43
import { TscGenerator } from './tsc.ts'
@@ -70,7 +69,6 @@ export function createGenerator(
7069
if (generator === 'tsgo' && isLegacyTsgo(tsgo.moduleUrl)) {
7170
return new TsgoCliGenerator({
7271
tsgoPath: tsgo.path!,
73-
rootDir: path.dirname(tsconfig!),
7472
tsconfig: tsconfig!,
7573
sourcemap,
7674
languageContext,

‎src/generator/tsgo-cli.ts‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ interface TsgoContext {
6464

6565
export interface TsgoCliGeneratorOptions {
6666
tsgoPath: string
67-
rootDir: string
6867
tsconfig: string
6968
sourcemap?: boolean
7069
languageContext: LanguageContext
@@ -73,18 +72,20 @@ export interface TsgoCliGeneratorOptions {
7372
export class TsgoCliGenerator implements Generator {
7473
private options: TsgoCliGeneratorOptions
7574
private context?: TsgoContext
75+
private rootDir: string
7676

7777
constructor(options: TsgoCliGeneratorOptions) {
7878
this.options = options
79+
this.rootDir = path.parse(path.resolve(options.tsconfig)).root
7980
}
8081

8182
async init(): Promise<void> {
82-
const { tsgoPath, rootDir, tsconfig, sourcemap } = this.options
83-
this.context = await runTsgo(tsgoPath, rootDir, tsconfig, sourcemap)
83+
const { tsgoPath, tsconfig, sourcemap } = this.options
84+
this.context = await runTsgo(tsgoPath, this.rootDir, tsconfig, sourcemap)
8485
}
8586

8687
async emit(_code: string, fileName: string): Promise<GeneratorResult> {
87-
const { rootDir, languageContext } = this.options
88+
const { languageContext } = this.options
8889
if (languageContext.isCustomLanguageFile(fileName)) {
8990
return {
9091
error: `tsgo does not support ${path.extname(fileName)} files.`,
@@ -96,10 +97,7 @@ export class TsgoCliGenerator implements Generator {
9697

9798
const dtsPath = path.resolve(
9899
this.context.path,
99-
path.relative(
100-
path.resolve(rootDir),
101-
filename_to_dts(fileName, languageContext),
102-
),
100+
path.relative(this.rootDir, filename_to_dts(fileName, languageContext)),
103101
)
104102
if (!existsSync(dtsPath)) {
105103
debug('[tsgo]', dtsPath, 'is missing')
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { greet, type Greeting } from '../shared/greet.ts'
2+
3+
export function hello(name: string): Greeting {
4+
return greet(name)
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"compilerOptions": {
3+
"declaration": true,
4+
"allowImportingTsExtensions": true
5+
},
6+
"include": ["index.ts"]
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export interface Greeting {
2+
message: string
3+
}
4+
5+
export function greet(name: string): Greeting {
6+
return { message: `hi ${name}` }
7+
}

‎tests/tsgo.test.ts‎

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import url, { fileURLToPath } from 'node:url'
66
import { rolldownBuild } from '@sxzz/test-utils'
77
import { afterEach, expect, test, vi } from 'vitest'
88
import { LanguageContext } from '../src/custom-language.ts'
9+
import { TsgoCliGenerator } from '../src/generator/tsgo-cli.ts'
910
import { TsgoGenerator } from '../src/generator/tsgo.ts'
1011
import { dts } from '../src/index.ts'
1112
import { 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+
3044
function 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

225239
test('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

Comments
 (0)