Skip to content

Commit ae94e99

Browse files
committed
fix: handle param name renaming
closes #115
1 parent 1f9ad26 commit ae94e99

File tree

9 files changed

+102
-55
lines changed

9 files changed

+102
-55
lines changed

src/fake-js.ts

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type Dep = t.Expression & { replace?: (newNode: t.Node) => void }
2828
interface SymbolInfo {
2929
decl: t.Declaration
3030
bindings: t.Identifier[]
31+
params: t.TSTypeParameter[]
3132
deps: Dep[]
3233
}
3334

@@ -177,15 +178,27 @@ export function createFakeJsPlugin({
177178
// @ts-expect-error
178179
decl.id = binding
179180
}
181+
182+
const params: t.TSTypeParameter[] =
183+
'typeParameters' in decl &&
184+
decl.typeParameters?.type === 'TSTypeParameterDeclaration'
185+
? decl.typeParameters.params
186+
: []
187+
180188
const deps = collectDependencies(decl, namespaceStmts)
181189

182190
const elements: t.Expression[] = [
183-
t.numericLiteral(0),
184-
...deps.map((dep) => t.arrowFunctionExpression([], dep)),
185-
...(sideEffect
186-
? [t.callExpression(t.identifier('sideEffect'), [bindings[0]])]
187-
: []),
191+
t.numericLiteral(0 /* placeholder */),
192+
t.arrowFunctionExpression(
193+
params.map((param) => t.identifier(param.name)),
194+
t.arrayExpression(deps),
195+
),
188196
]
197+
if (sideEffect) {
198+
elements.push(
199+
t.callExpression(t.identifier('sideEffect'), [bindings[0]]),
200+
)
201+
}
189202
const runtime: t.ArrayExpression = t.arrayExpression(elements)
190203

191204
if (decl !== stmt) {
@@ -196,10 +209,11 @@ export function createFakeJsPlugin({
196209
decl,
197210
deps,
198211
bindings,
212+
params,
199213
})
200214
elements[0] = t.numericLiteral(symbolId)
201215

202-
// var ${binding} = [${symbolId}, () => ${dep}, ..., sideEffect()]
216+
// var ${binding} = [${symbolId}, (param, ...) => [dep, ...], sideEffect()]
203217
const runtimeAssignment: t.VariableDeclaration = {
204218
type: 'VariableDeclaration',
205219
kind: 'var',
@@ -284,7 +298,8 @@ export function createFakeJsPlugin({
284298
return null
285299
}
286300

287-
const [symbolIdNode, ...depsFns] = decl.init.elements as t.Expression[]
301+
const [symbolIdNode, depsFn /*, ignore sideEffect */] = decl.init
302+
.elements as [t.Expression, t.ArrowFunctionExpression]
288303
if (symbolIdNode?.type !== 'NumericLiteral') {
289304
return null
290305
}
@@ -300,18 +315,21 @@ export function createFakeJsPlugin({
300315
overwriteNode(original.bindings[i], transformedBinding)
301316
}
302317

303-
const transformedDeps = depsFns
304-
.filter((node) => node?.type === 'ArrowFunctionExpression')
305-
.map((node) => node.body)
306-
307-
if (original.deps.length) {
308-
for (let i = 0; i < original.deps.length; i++) {
309-
const originalDep = original.deps[i]
310-
if (originalDep.replace) {
311-
originalDep.replace(transformedDeps[i])
312-
} else {
313-
Object.assign(originalDep, transformedDeps[i])
314-
}
318+
const transformedParams = depsFn.params as t.Identifier[]
319+
for (let i = 0; i < original.params.length; i++) {
320+
const originalParam = original.params[i]
321+
const transformedParam = transformedParams[i]
322+
originalParam.name = transformedParam.name
323+
}
324+
325+
const transformedDeps = (depsFn.body as t.ArrayExpression)
326+
.elements as t.Expression[]
327+
for (let i = 0; i < original.deps.length; i++) {
328+
const originalDep = original.deps[i]
329+
if (originalDep.replace) {
330+
originalDep.replace(transformedDeps[i])
331+
} else {
332+
Object.assign(originalDep, transformedDeps[i])
315333
}
316334
}
317335

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Index: diff.patch
2+
===================================================================
3+
--- diff.patch
4+
+++ diff.patch
5+
@@ -1,5 +1,5 @@
6+
// index.d.ts
7+
-type Config1<Client> = Client
8+
+type Config1<Client$1> = Client$1
9+
type Client = any
10+
-type Config2<Client> = Client
11+
+type Config2<Client$1> = Client$1
12+
export { Client, Config1, Config2 }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from './mod'
2+
3+
export type Client = any
4+
export type Config2<Client> = Client
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
type Client = any
2+
export type Config1<Client> = Client
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// index.d.ts
2+
//#region tests/rollup-plugin-dts/generic-shadowing/mod.d.ts
3+
type Config1<Client$1> = Client$1;
4+
//#endregion
5+
//#region tests/rollup-plugin-dts/generic-shadowing/index.d.ts
6+
type Client = any;
7+
type Config2<Client$1> = Client$1;
8+
//#endregion
9+
export { Client, Config1, Config2 };

tests/rollup-plugin-dts/generics/known-diff.patch

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,33 @@ Index: diff.patch
22
===================================================================
33
--- diff.patch
44
+++ diff.patch
5-
@@ -30,6 +30,5 @@
6-
declare function fn<T = G>(g: T, h: Gen<H>): void
5+
@@ -13,23 +13,22 @@
6+
interface M {}
7+
interface N {}
8+
interface O {}
9+
interface P {}
10+
-declare type Gen<T> = T
11+
-interface I1<T = A> {
12+
- a: T
13+
+declare type Gen<T$1> = T$1
14+
+interface I1<T$1 = A> {
15+
+ a: T$1
16+
b: Gen<B>
17+
}
18+
-declare type Ty<T = C> = {
19+
- c: T
20+
+declare type Ty<T$1 = C> = {
21+
+ c: T$1
22+
d: Gen<D>
23+
}
24+
-declare class Cl<T = E> {
25+
- e: T
26+
+declare class Cl<T$1 = E> {
27+
+ e: T$1
28+
f: Gen<F>
29+
}
30+
-declare function fn<T = G>(g: T, h: Gen<H>): void
31+
+declare function fn<T$1 = G>(g: T$1, h: Gen<H>): void
732
declare type TyFn = <T = J>(j: T, k: Gen<K>) => L
833
declare type TyCtor = new <T = M>(m: T, n: Gen<N>) => O
934
interface I2 extends Gen<P> {}

tests/rollup-plugin-dts/generics/snapshot.d.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@ interface M {}
1515
interface N {}
1616
interface O {}
1717
interface P {}
18-
declare type Gen<T> = T;
19-
interface I1<T = A> {
20-
a: T;
18+
declare type Gen<T$1> = T$1;
19+
interface I1<T$1 = A> {
20+
a: T$1;
2121
b: Gen<B>;
2222
}
23-
declare type Ty<T = C> = {
24-
c: T;
23+
declare type Ty<T$1 = C> = {
24+
c: T$1;
2525
d: Gen<D>;
2626
};
27-
declare class Cl<T = E> {
28-
e: T;
27+
declare class Cl<T$1 = E> {
28+
e: T$1;
2929
f: Gen<F>;
3030
}
31-
declare function fn<T = G>(g: T, h: Gen<H>): void;
31+
declare function fn<T$1 = G>(g: T$1, h: Gen<H>): void;
3232
declare type TyFn = <T = J>(j: T, k: Gen<K>) => L;
3333
declare type TyCtor = new <T = M>(m: T, n: Gen<N>) => O;
3434
interface I2 extends Gen<P> {}

tests/rollup-plugin-dts/shadowing/known-diff.patch

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,15 @@ Index: diff.patch
22
===================================================================
33
--- diff.patch
44
+++ diff.patch
5-
@@ -1,5 +1,19 @@
5+
@@ -1,5 +1,7 @@
66
// index.d.ts
7-
+interface A {}
8-
+interface B {}
9-
+interface C {}
10-
+interface D {}
11-
+interface E {}
12-
+interface F {}
13-
+interface G {}
147
+interface H {}
15-
+interface I {}
168
+interface J {}
17-
+interface K {}
18-
+interface L {}
19-
+interface M {}
20-
+interface N {}
219
declare class GenericKlass<A = any, B = A> {
2210
a: A
2311
b: B
2412
}
25-
@@ -8,14 +22,11 @@
13+
@@ -8,14 +10,11 @@
2614
d: D
2715
}
2816
declare function genericFunction<E = any, F = E>(e: E): F

tests/rollup-plugin-dts/shadowing/snapshot.d.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
11
// index.d.ts
22
//#region tests/rollup-plugin-dts/shadowing/exports.d.ts
3-
interface A {}
4-
interface B {}
5-
interface C {}
6-
interface D {}
7-
interface E {}
8-
interface F {}
9-
interface G {}
3+
104
interface H {}
11-
interface I {}
125
interface J {}
13-
interface K {}
14-
interface L {}
15-
interface M {}
16-
interface N {}
176
declare class GenericKlass<A = any, B = A> {
187
a: A;
198
b: B;

0 commit comments

Comments
 (0)