Skip to content

Commit fb726cf

Browse files
committed
fix: improve identifier naming
closes #59
1 parent 4367980 commit fb726cf

File tree

4 files changed

+14
-11
lines changed

4 files changed

+14
-11
lines changed

src/fake-js.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function createFakeJsPlugin({
4242
sourcemap,
4343
}: Pick<OptionsResolved, 'dtsInput' | 'sourcemap'>): Plugin {
4444
let symbolIdx = 0
45-
let identifierIdx = 0
45+
const identifierMap: Record<string, number> = Object.create(null)
4646
const symbolMap = new Map<number /* symbol id */, SymbolInfo>()
4747
const commentsMap = new Map<string /* filename */, t.Comment[]>()
4848

@@ -158,7 +158,7 @@ export function createFakeJsPlugin({
158158
} else if ('id' in decl && decl.id) {
159159
let binding = decl.id
160160
binding = sideEffect
161-
? t.identifier(`_${identifierIdx++}`)
161+
? t.identifier(`_${getIdentifierIndex('')}`)
162162
: (binding as t.Identifier)
163163
bindings.push(binding)
164164
} else {
@@ -332,8 +332,11 @@ export function createFakeJsPlugin({
332332
return result
333333
}
334334

335-
function getIdentifierIndex() {
336-
return identifierIdx++
335+
function getIdentifierIndex(name: string) {
336+
if (name in identifierMap) {
337+
return identifierMap[name]++
338+
}
339+
return (identifierMap[name] = 0)
337340
}
338341

339342
function registerSymbol(info: SymbolInfo) {
@@ -423,7 +426,7 @@ export function createFakeJsPlugin({
423426
): Dep {
424427
const sourceText = source.value.replaceAll(/\W/g, '_')
425428
let local: t.Identifier | t.TSQualifiedName = t.identifier(
426-
`${sourceText}${getIdentifierIndex()}`,
429+
`${sourceText}${getIdentifierIndex(sourceText)}`,
427430
)
428431

429432
if (namespaceStmts.has(source.value)) {

tests/__snapshots__/tsc.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export { Unused };"
132132

133133
exports[`tsc > vue-sfc w/ ts-compiler 1`] = `
134134
"// main.d.ts
135-
import * as vue1 from "vue";
135+
import * as vue0 from "vue";
136136
137137
//#region tests/fixtures/vue-sfc/App.vue.d.ts
138138
type __VLS_Props = {
@@ -143,7 +143,7 @@ declare global {
143143
foo: string;
144144
}
145145
}
146-
declare const _default: vue1.DefineComponent<__VLS_Props, {}, {}, {}, {}, vue1.ComponentOptionsMixin, vue1.ComponentOptionsMixin, {}, string, vue1.PublicProps, Readonly<__VLS_Props> & Readonly<{}>, {}, {}, {}, {}, string, vue1.ComponentProvideOptions, false, {}, any>;
146+
declare const _default: vue0.DefineComponent<__VLS_Props, {}, {}, {}, {}, vue0.ComponentOptionsMixin, vue0.ComponentOptionsMixin, {}, string, vue0.PublicProps, Readonly<__VLS_Props> & Readonly<{}>, {}, {}, {}, {}, string, vue0.ComponentProvideOptions, false, {}, any>;
147147
//#endregion
148148
export { _default as App };"
149149
`;

tests/rollup-plugin-dts/inline-import-typeof-members/known-diff.patch

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ Index: diff.patch
88
-import * as typescript from 'typescript'
99
-type TypeScript = typeof typescript
1010
+import * as typescript0 from 'typescript'
11-
+import * as rollup1 from 'rollup'
11+
+import * as rollup0 from 'rollup'
1212
+type TypeScript = typeof typescript0
1313
interface Test {
1414
- rollup: rollup.RollupOptions
15-
+ rollup: rollup1.RollupOptions
15+
+ rollup: rollup0.RollupOptions
1616
}
1717
export { Test, TypeScript }
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// index.d.ts
22
import * as typescript0 from "typescript";
3-
import * as rollup1 from "rollup";
3+
import * as rollup0 from "rollup";
44

55
//#region tests/rollup-plugin-dts/inline-import-typeof-members/index.d.ts
66
type TypeScript = typeof typescript0;
77
interface Test {
8-
rollup: rollup1.RollupOptions;
8+
rollup: rollup0.RollupOptions;
99
}
1010
//#endregion
1111
export { Test, TypeScript };

0 commit comments

Comments
 (0)