Skip to content

Commit a69dffd

Browse files
committed
fix: merge runtime & type deps
1 parent 9a0fec3 commit a69dffd

File tree

2 files changed

+31
-32
lines changed

2 files changed

+31
-32
lines changed

src/index.ts

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ import {
1010
type Node,
1111
type Span,
1212
type TSModuleDeclarationName,
13-
type TSTypeName,
14-
type TSTypeQuery,
15-
type TSTypeReference,
1613
type VariableDeclaration,
1714
type VariableDeclarator,
1815
} from 'oxc-parser'
@@ -115,7 +112,7 @@ export function dts(): Plugin {
115112
).id
116113
if (!binding) continue
117114
const original = s.sliceNode(node)
118-
const deps = [...collectDependencies(node), ...collectTypeDeps(node)]
115+
const deps = collectDependencies(node)
119116
const depsString = deps
120117
.map((node) => `() => ${s.sliceNode(node)}`)
121118
.join(', ')
@@ -196,11 +193,11 @@ export function dts(): Plugin {
196193
const [decl] = node.declarations
197194

198195
const raw = s.sliceNode(node)
199-
const deps = collectTypeDeps(node)
200-
const symbolId = register(raw, decl.id, deps, node)
201-
const depsString = collectTypeDeps(node)
196+
const deps = collectDependencies(node)
197+
const depsString = deps
202198
.map((node) => `() => ${s.sliceNode(node)}`)
203199
.join(', ')
200+
const symbolId = register(raw, decl.id, deps, node)
204201
const runtime = `[${symbolId}, ${depsString}]`
205202
s.overwriteNode(node, `var ${s.sliceNode(decl.id)} = ${runtime}`)
206203
}
@@ -214,39 +211,24 @@ function collectDependencies(node: Node): (Node & Span)[] {
214211
deps.add(node.superClass)
215212
} else if (
216213
(node.type === 'MethodDefinition' ||
217-
node.type === 'PropertyDefinition') &&
214+
node.type === 'PropertyDefinition' ||
215+
node.type === 'TSPropertySignature') &&
218216
(node.key.type === 'Identifier' ||
219217
node.key.type === 'MemberExpression') &&
220218
node.computed
221219
) {
222220
deps.add(node.key)
221+
} else if (node.type === 'TSTypeReference') {
222+
deps.add(node.typeName)
223+
} else if (node.type === 'TSTypeQuery') {
224+
deps.add(node.exprName)
223225
}
224226
},
225227
})
226228
return Array.from(deps)
227229
}
228230

229-
function collectTypeDeps(node: Node): TSTypeName[] {
230-
if (!node) return []
231-
const result: any[] = []
232-
233-
for (const value of Object.values(node)) {
234-
if (value?.type === 'TSTypeReference') {
235-
result.push((value as TSTypeReference).typeName)
236-
}
237-
if (value?.type === 'TSTypeQuery') {
238-
result.push((value as TSTypeQuery).exprName)
239-
}
240-
241-
if (typeof value === 'object') {
242-
result.push(...collectTypeDeps(value))
243-
}
244-
}
245-
246-
return result
247-
}
248-
249-
// patch `let x = 1;` to `declare let x: typeof 1;`
231+
// patch `let x = 1;` to `type x: 1;`
250232
function patchVariableDeclarator(
251233
s: MagicStringAST,
252234
node: VariableDeclaration,
@@ -255,7 +237,7 @@ function patchVariableDeclarator(
255237
if (decl.init && !decl.id.typeAnnotation) {
256238
s.overwriteNode(
257239
node,
258-
`declare ${node.kind} ${s.sliceNode(decl.id)}: typeof ${s.sliceNode(decl.init)}`,
240+
`type ${s.sliceNode(decl.id)} = ${s.sliceNode(decl.init)}`,
259241
)
260242
} else if (!node.declare) {
261243
s.prependLeft(node.start, 'declare ')

tests/index.test.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
import { access } from 'node:fs/promises'
1+
import { access, readFile, unlink } from 'node:fs/promises'
22
import path from 'node:path'
3+
import process from 'node:process'
34
import { outputToSnapshot, rollupBuild, testFixtures } from '@sxzz/test-utils'
45
import { createPatch } from 'diff'
56
import { build } from 'rolldown'
67
import { dts as rollupDts } from 'rollup-plugin-dts'
78
import { expect } from 'vitest'
89
import { dts } from '../src'
910

11+
const isUpdateEnabled =
12+
process.env.npm_lifecycle_script?.includes('-u') ||
13+
process.env.npm_lifecycle_script?.includes('--update')
14+
1015
await testFixtures(
1116
['tests/fixtures/*/index.d.ts', 'tests/fixtures/local/*/index.d.ts'],
1217
async (args, id) => {
@@ -31,6 +36,8 @@ await testFixtures(
3136
rollupSnapshot = cleanupCode(rollupSnapshot)
3237
const rolldownSnapshot = cleanupCode(snapshot)
3338
const diffPath = path.resolve(dirname, 'diff.patch')
39+
const knownDiffPath = path.resolve(dirname, 'known-diff.patch')
40+
3441
if (rollupSnapshot !== rolldownSnapshot) {
3542
const diff = createPatch(
3643
'diff.patch',
@@ -44,9 +51,19 @@ await testFixtures(
4451
stripTrailingCr: true,
4552
},
4653
)
47-
await expect(diff).toMatchFileSnapshot(diffPath)
54+
const knownDiff = await readFile(knownDiffPath, 'utf8').catch(() => null)
55+
if (knownDiff !== diff) {
56+
await expect(diff).toMatchFileSnapshot(diffPath)
57+
await unlink(knownDiffPath).catch(() => {})
58+
}
59+
} else if (isUpdateEnabled) {
60+
await Promise.all([
61+
unlink(diffPath).catch(() => {}),
62+
unlink(knownDiffPath).catch(() => {}),
63+
])
4864
} else {
4965
await expect(access(diffPath)).rejects.toThrow()
66+
await expect(access(knownDiffPath)).rejects.toThrow()
5067
}
5168
},
5269
{ snapshot: false },

0 commit comments

Comments
 (0)