Skip to content

Commit aeae198

Browse files
committed
fix: prefix namespace identifier with _$ to avoid conflicts with real identifiers
1 parent f8b8003 commit aeae198

File tree

8 files changed

+93
-83
lines changed

8 files changed

+93
-83
lines changed

src/fake-js.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -695,9 +695,11 @@ export function createFakeJsPlugin({
695695
): Dep {
696696
const sourceText = source.value.replaceAll(/\W/g, '_')
697697
// Use original source if it's already a valid identifier, otherwise use formatted text with index
698-
const localName = isIdentifierName(source.value)
699-
? source.value
700-
: `${sourceText}${getIdentifierIndex(identifierMap, sourceText)}`
698+
const localName = `_$${
699+
isIdentifierName(source.value)
700+
? source.value
701+
: `${sourceText}${getIdentifierIndex(identifierMap, sourceText)}`
702+
}`
701703
let local: t.Identifier | t.TSQualifiedName = t.identifier(localName)
702704

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

tests/__snapshots__/index.test.ts.snap

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -123,72 +123,72 @@ export { Product };"
123123
124124
exports[`deterministic namespace import index 1`] = `
125125
"// a.d.ts
126-
import * as stub_lib from "stub_lib";
126+
import * as _$stub_lib from "stub_lib";
127127
128128
//#region a.d.ts
129-
type MyTypeA = stub_lib.LibType;
129+
type MyTypeA = _$stub_lib.LibType;
130130
//#endregion
131131
export { MyTypeA };
132132
// b.d.ts
133-
import * as stub_lib from "stub_lib";
133+
import * as _$stub_lib from "stub_lib";
134134
135135
//#region b.d.ts
136-
type MyTypeB = stub_lib.LibType;
136+
type MyTypeB = _$stub_lib.LibType;
137137
//#endregion
138138
export { MyTypeB };
139139
// c.d.ts
140-
import * as stub_lib from "stub_lib";
140+
import * as _$stub_lib from "stub_lib";
141141
142142
//#region c.d.ts
143-
type MyTypeC = stub_lib.LibType;
143+
type MyTypeC = _$stub_lib.LibType;
144144
//#endregion
145145
export { MyTypeC };"
146146
`;
147147
148148
exports[`deterministic namespace import index 2`] = `
149149
"// a.d.ts
150-
import * as stub_lib from "stub_lib";
150+
import * as _$stub_lib from "stub_lib";
151151
152152
//#region a.d.ts
153-
type MyTypeA = stub_lib.LibType;
153+
type MyTypeA = _$stub_lib.LibType;
154154
//#endregion
155155
export { MyTypeA };
156156
// b.d.ts
157-
import * as stub_lib from "stub_lib";
157+
import * as _$stub_lib from "stub_lib";
158158
159159
//#region b.d.ts
160-
type MyTypeB = stub_lib.LibType;
160+
type MyTypeB = _$stub_lib.LibType;
161161
//#endregion
162162
export { MyTypeB };
163163
// c.d.ts
164-
import * as stub_lib from "stub_lib";
164+
import * as _$stub_lib from "stub_lib";
165165
166166
//#region c.d.ts
167-
type MyTypeC = stub_lib.LibType;
167+
type MyTypeC = _$stub_lib.LibType;
168168
//#endregion
169169
export { MyTypeC };"
170170
`;
171171
172172
exports[`deterministic namespace import index 3`] = `
173173
"// a.d.ts
174-
import * as stub_lib from "stub_lib";
174+
import * as _$stub_lib from "stub_lib";
175175
176176
//#region a.d.ts
177-
type MyTypeA = stub_lib.LibType;
177+
type MyTypeA = _$stub_lib.LibType;
178178
//#endregion
179179
export { MyTypeA };
180180
// b.d.ts
181-
import * as stub_lib from "stub_lib";
181+
import * as _$stub_lib from "stub_lib";
182182
183183
//#region b.d.ts
184-
type MyTypeB = stub_lib.LibType;
184+
type MyTypeB = _$stub_lib.LibType;
185185
//#endregion
186186
export { MyTypeB };
187187
// c.d.ts
188-
import * as stub_lib from "stub_lib";
188+
import * as _$stub_lib from "stub_lib";
189189
190190
//#region c.d.ts
191-
type MyTypeC = stub_lib.LibType;
191+
type MyTypeC = _$stub_lib.LibType;
192192
//#endregion
193193
export { MyTypeC };"
194194
`;

tests/__snapshots__/tsc.test.ts.snap

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
exports[`tsc > arktype 1`] = `
44
"// arktype.d.ts
5-
import * as arktype_internal_variants_object_ts0 from "arktype/internal/variants/object.ts";
5+
import * as _$arktype_internal_variants_object_ts0 from "arktype/internal/variants/object.ts";
66
77
//#region tests/fixtures/arktype.d.ts
8-
declare const test: arktype_internal_variants_object_ts0.ObjectType<{
8+
declare const test: _$arktype_internal_variants_object_ts0.ObjectType<{
99
test: string;
1010
}, {}>;
1111
//#endregion
@@ -479,7 +479,7 @@ export { Unused };"
479479
480480
exports[`tsc > vue-sfc w/ ts-compiler 1`] = `
481481
"// main.d.ts
482-
import * as _vue_runtime_core0 from "@vue/runtime-core";
482+
import * as _$_vue_runtime_core0 from "@vue/runtime-core";
483483
484484
//#region tests/fixtures/vue-sfc/App.vue.d.ts
485485
type __VLS_Props = {
@@ -490,30 +490,30 @@ declare global {
490490
foo: string;
491491
}
492492
}
493-
declare const __VLS_export: _vue_runtime_core0.DefineComponent<__VLS_Props, {}, {}, {}, {}, _vue_runtime_core0.ComponentOptionsMixin, _vue_runtime_core0.ComponentOptionsMixin, {}, string, _vue_runtime_core0.PublicProps, Readonly<__VLS_Props> & Readonly<{}>, {}, {}, {}, {}, string, _vue_runtime_core0.ComponentProvideOptions, false, {}, any>;
493+
declare const __VLS_export: _$_vue_runtime_core0.DefineComponent<__VLS_Props, {}, {}, {}, {}, _$_vue_runtime_core0.ComponentOptionsMixin, _$_vue_runtime_core0.ComponentOptionsMixin, {}, string, _$_vue_runtime_core0.PublicProps, Readonly<__VLS_Props> & Readonly<{}>, {}, {}, {}, {}, string, _$_vue_runtime_core0.ComponentProvideOptions, false, {}, any>;
494494
declare const _default: typeof __VLS_export;
495495
//#endregion
496496
export { _default as App };"
497497
`;
498498
499499
exports[`tsc > vue-sfc w/ ts-compiler w/ vueCompilerOptions in tsconfig 1`] = `
500500
"// main.d.ts
501-
import * as vue from "vue";
501+
import * as _$vue from "vue";
502502
503503
//#region tests/fixtures/vue-sfc-fallthrough/App.vue.d.ts
504-
declare const __VLS_export: vue.DefineComponent<{
504+
declare const __VLS_export: _$vue.DefineComponent<{
505505
foo: string;
506506
bar?: string | undefined;
507507
innerHTML?: string | undefined;
508-
class?: vue.ClassValue;
509-
style?: vue.StyleValue;
508+
class?: _$vue.ClassValue;
509+
style?: _$vue.StyleValue;
510510
accesskey?: string | undefined;
511511
contenteditable?: (boolean | "true" | "false") | "inherit" | "plaintext-only" | undefined;
512512
contextmenu?: string | undefined;
513513
dir?: string | undefined;
514514
draggable?: (boolean | "true" | "false") | undefined;
515515
enterkeyhint?: "enter" | "done" | "go" | "next" | "previous" | "search" | "send" | undefined;
516-
enterKeyHint?: vue.HTMLAttributes["enterkeyhint"];
516+
enterKeyHint?: _$vue.HTMLAttributes["enterkeyhint"];
517517
hidden?: (boolean | "true" | "false") | "" | "hidden" | "until-found" | undefined;
518518
id?: string | undefined;
519519
inert?: (boolean | "true" | "false") | undefined;
@@ -692,28 +692,28 @@ declare const __VLS_export: vue.DefineComponent<{
692692
onTransitionrun?: ((payload: TransitionEvent) => void) | undefined;
693693
onTransitionstart?: ((payload: TransitionEvent) => void) | undefined;
694694
key?: PropertyKey | undefined;
695-
ref?: vue.VNodeRef | undefined;
695+
ref?: _$vue.VNodeRef | undefined;
696696
ref_for?: boolean | undefined | undefined;
697697
ref_key?: string | undefined | undefined;
698-
onVnodeBeforeMount?: (((vnode: vue.VNode) => void) | ((vnode: vue.VNode) => void)[]) | undefined;
699-
onVnodeMounted?: (((vnode: vue.VNode) => void) | ((vnode: vue.VNode) => void)[]) | undefined;
700-
onVnodeBeforeUpdate?: (((vnode: vue.VNode, oldVNode: vue.VNode) => void) | ((vnode: vue.VNode, oldVNode: vue.VNode) => void)[]) | undefined;
701-
onVnodeUpdated?: (((vnode: vue.VNode, oldVNode: vue.VNode) => void) | ((vnode: vue.VNode, oldVNode: vue.VNode) => void)[]) | undefined;
702-
onVnodeBeforeUnmount?: (((vnode: vue.VNode) => void) | ((vnode: vue.VNode) => void)[]) | undefined;
703-
onVnodeUnmounted?: (((vnode: vue.VNode) => void) | ((vnode: vue.VNode) => void)[]) | undefined;
704-
}, {}, {}, {}, {}, vue.ComponentOptionsMixin, vue.ComponentOptionsMixin, {}, string, vue.PublicProps, Readonly<{
698+
onVnodeBeforeMount?: (((vnode: _$vue.VNode) => void) | ((vnode: _$vue.VNode) => void)[]) | undefined;
699+
onVnodeMounted?: (((vnode: _$vue.VNode) => void) | ((vnode: _$vue.VNode) => void)[]) | undefined;
700+
onVnodeBeforeUpdate?: (((vnode: _$vue.VNode, oldVNode: _$vue.VNode) => void) | ((vnode: _$vue.VNode, oldVNode: _$vue.VNode) => void)[]) | undefined;
701+
onVnodeUpdated?: (((vnode: _$vue.VNode, oldVNode: _$vue.VNode) => void) | ((vnode: _$vue.VNode, oldVNode: _$vue.VNode) => void)[]) | undefined;
702+
onVnodeBeforeUnmount?: (((vnode: _$vue.VNode) => void) | ((vnode: _$vue.VNode) => void)[]) | undefined;
703+
onVnodeUnmounted?: (((vnode: _$vue.VNode) => void) | ((vnode: _$vue.VNode) => void)[]) | undefined;
704+
}, {}, {}, {}, {}, _$vue.ComponentOptionsMixin, _$vue.ComponentOptionsMixin, {}, string, _$vue.PublicProps, Readonly<{
705705
foo: string;
706706
bar?: string | undefined;
707707
innerHTML?: string | undefined;
708-
class?: vue.ClassValue;
709-
style?: vue.StyleValue;
708+
class?: _$vue.ClassValue;
709+
style?: _$vue.StyleValue;
710710
accesskey?: string | undefined;
711711
contenteditable?: (boolean | "true" | "false") | "inherit" | "plaintext-only" | undefined;
712712
contextmenu?: string | undefined;
713713
dir?: string | undefined;
714714
draggable?: (boolean | "true" | "false") | undefined;
715715
enterkeyhint?: "enter" | "done" | "go" | "next" | "previous" | "search" | "send" | undefined;
716-
enterKeyHint?: vue.HTMLAttributes["enterkeyhint"];
716+
enterKeyHint?: _$vue.HTMLAttributes["enterkeyhint"];
717717
hidden?: (boolean | "true" | "false") | "" | "hidden" | "until-found" | undefined;
718718
id?: string | undefined;
719719
inert?: (boolean | "true" | "false") | undefined;
@@ -892,24 +892,24 @@ declare const __VLS_export: vue.DefineComponent<{
892892
onTransitionrun?: ((payload: TransitionEvent) => void) | undefined;
893893
onTransitionstart?: ((payload: TransitionEvent) => void) | undefined;
894894
key?: PropertyKey | undefined;
895-
ref?: vue.VNodeRef | undefined;
895+
ref?: _$vue.VNodeRef | undefined;
896896
ref_for?: boolean | undefined | undefined;
897897
ref_key?: string | undefined | undefined;
898-
onVnodeBeforeMount?: (((vnode: vue.VNode) => void) | ((vnode: vue.VNode) => void)[]) | undefined;
899-
onVnodeMounted?: (((vnode: vue.VNode) => void) | ((vnode: vue.VNode) => void)[]) | undefined;
900-
onVnodeBeforeUpdate?: (((vnode: vue.VNode, oldVNode: vue.VNode) => void) | ((vnode: vue.VNode, oldVNode: vue.VNode) => void)[]) | undefined;
901-
onVnodeUpdated?: (((vnode: vue.VNode, oldVNode: vue.VNode) => void) | ((vnode: vue.VNode, oldVNode: vue.VNode) => void)[]) | undefined;
902-
onVnodeBeforeUnmount?: (((vnode: vue.VNode) => void) | ((vnode: vue.VNode) => void)[]) | undefined;
903-
onVnodeUnmounted?: (((vnode: vue.VNode) => void) | ((vnode: vue.VNode) => void)[]) | undefined;
904-
}> & Readonly<{}>, {}, {}, {}, {}, string, vue.ComponentProvideOptions, false, {}, any>;
898+
onVnodeBeforeMount?: (((vnode: _$vue.VNode) => void) | ((vnode: _$vue.VNode) => void)[]) | undefined;
899+
onVnodeMounted?: (((vnode: _$vue.VNode) => void) | ((vnode: _$vue.VNode) => void)[]) | undefined;
900+
onVnodeBeforeUpdate?: (((vnode: _$vue.VNode, oldVNode: _$vue.VNode) => void) | ((vnode: _$vue.VNode, oldVNode: _$vue.VNode) => void)[]) | undefined;
901+
onVnodeUpdated?: (((vnode: _$vue.VNode, oldVNode: _$vue.VNode) => void) | ((vnode: _$vue.VNode, oldVNode: _$vue.VNode) => void)[]) | undefined;
902+
onVnodeBeforeUnmount?: (((vnode: _$vue.VNode) => void) | ((vnode: _$vue.VNode) => void)[]) | undefined;
903+
onVnodeUnmounted?: (((vnode: _$vue.VNode) => void) | ((vnode: _$vue.VNode) => void)[]) | undefined;
904+
}> & Readonly<{}>, {}, {}, {}, {}, string, _$vue.ComponentProvideOptions, false, {}, any>;
905905
declare const _default: typeof __VLS_export;
906906
//#endregion
907907
export { _default as App };"
908908
`;
909909
910910
exports[`tsc > vue-sfc w/ ts-macro w/ ts-compiler 1`] = `
911911
"// main.d.ts
912-
import * as vue from "vue";
912+
import * as _$vue from "vue";
913913
914914
//#region tests/fixtures/vue-sfc-with-ts-macro/App.vue.d.ts
915915
type __VLS_Props = {
@@ -920,7 +920,7 @@ declare global {
920920
foo: string;
921921
}
922922
}
923-
declare const __VLS_export: vue.DefineComponent<__VLS_Props, {}, {}, {}, {}, vue.ComponentOptionsMixin, vue.ComponentOptionsMixin, {}, string, vue.PublicProps, Readonly<__VLS_Props> & Readonly<{}>, {}, {}, {}, {}, string, vue.ComponentProvideOptions, false, {}, any>;
923+
declare const __VLS_export: _$vue.DefineComponent<__VLS_Props, {}, {}, {}, {}, _$vue.ComponentOptionsMixin, _$vue.ComponentOptionsMixin, {}, string, _$vue.PublicProps, Readonly<__VLS_Props> & Readonly<{}>, {}, {}, {}, {}, string, _$vue.ComponentProvideOptions, false, {}, any>;
924924
declare const _default: typeof __VLS_export;
925925
//#endregion
926926
//#region tests/fixtures/vue-sfc-with-ts-macro/main.d.ts

tests/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ test('deterministic namespace import index', async () => {
587587
expect(results[0]).toBe(results[1])
588588
expect(results[1]).toBe(results[2])
589589
// Valid identifiers (stub_lib) don't need index suffix
590-
expect(results[0]).toContain('import * as stub_lib from "stub_lib"')
590+
expect(results[0]).toContain('import * as _$stub_lib from "stub_lib"')
591591
// Should not have stub_lib0 since each file is independent
592592
expect(results[0]).not.toContain('stub_lib0')
593593
})

tests/rollup-plugin-dts/inline-import-external-namespace/diff.patch

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,24 @@ Index: diff.patch
88
-import * as __foo_bar from 'foo.bar'
99
-import * as _foo_bar from 'foo-bar'
1010
-import * as foo_bar from 'foo_bar'
11-
import * as foo from 'foo'
12-
+import * as foo_bar from 'foo_bar'
13-
+import * as foo_bar0 from 'foo-bar'
14-
+import * as foo_bar1 from 'foo.bar'
15-
+import * as foo_bar2 from 'foo/bar'
11+
-import * as foo from 'foo'
12+
+import * as _$foo from 'foo'
13+
+import * as _$foo_bar from 'foo_bar'
14+
+import * as _$foo_bar0 from 'foo-bar'
15+
+import * as _$foo_bar1 from 'foo.bar'
16+
+import * as _$foo_bar2 from 'foo/bar'
1617
interface Foo {
17-
ns1: foo
18-
ns2: typeof foo
19-
ns3: foo_bar.T
18+
- ns1: foo
19+
- ns2: typeof foo
20+
- ns3: foo_bar.T
2021
- ns4: _foo_bar.T
2122
- ns5: __foo_bar.T
2223
- ns6: ___foo_bar.T
23-
+ ns4: foo_bar0.T
24-
+ ns5: foo_bar1.T
25-
+ ns6: foo_bar2.T
24+
+ ns1: _$foo
25+
+ ns2: typeof _$foo
26+
+ ns3: _$foo_bar.T
27+
+ ns4: _$foo_bar0.T
28+
+ ns5: _$foo_bar1.T
29+
+ ns6: _$foo_bar2.T
2630
}
2731
export { Foo }
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
// index.d.ts
2-
import * as foo from "foo";
3-
import * as foo_bar from "foo_bar";
4-
import * as foo_bar0 from "foo-bar";
5-
import * as foo_bar1 from "foo.bar";
6-
import * as foo_bar2 from "foo/bar";
2+
import * as _$foo from "foo";
3+
import * as _$foo_bar from "foo_bar";
4+
import * as _$foo_bar0 from "foo-bar";
5+
import * as _$foo_bar1 from "foo.bar";
6+
import * as _$foo_bar2 from "foo/bar";
77

88
//#region tests/rollup-plugin-dts/inline-import-external-namespace/index.d.ts
99
interface Foo {
10-
ns1: foo;
11-
ns2: typeof foo;
12-
ns3: foo_bar.T;
13-
ns4: foo_bar0.T;
14-
ns5: foo_bar1.T;
15-
ns6: foo_bar2.T;
10+
ns1: _$foo;
11+
ns2: typeof _$foo;
12+
ns3: _$foo_bar.T;
13+
ns4: _$foo_bar0.T;
14+
ns5: _$foo_bar1.T;
15+
ns6: _$foo_bar2.T;
1616
}
1717
//#endregion
1818
export { Foo };

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ Index: diff.patch
22
===================================================================
33
--- diff.patch
44
+++ diff.patch
5-
@@ -1,7 +1,7 @@
5+
@@ -1,8 +1,8 @@
66
// index.d.ts
77
-import * as rollup from 'rollup'
8-
import * as typescript from 'typescript'
9-
+import * as rollup from 'rollup'
10-
type TypeScript = typeof typescript
8+
-import * as typescript from 'typescript'
9+
-type TypeScript = typeof typescript
10+
+import * as _$typescript from 'typescript'
11+
+import * as _$rollup from 'rollup'
12+
+type TypeScript = typeof _$typescript
1113
interface Test {
12-
rollup: rollup.RollupOptions
14+
- rollup: rollup.RollupOptions
15+
+ rollup: _$rollup.RollupOptions
1316
}
17+
export { Test, TypeScript }
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// index.d.ts
2-
import * as typescript from "typescript";
3-
import * as rollup from "rollup";
2+
import * as _$typescript from "typescript";
3+
import * as _$rollup from "rollup";
44

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

0 commit comments

Comments
 (0)