@alloy-js/typescript 0.23.0-dev.9 → 0.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/dist/dev/src/components/ImportStatement.js +42 -10
  3. package/dist/dev/src/components/ImportStatement.js.map +1 -1
  4. package/dist/dev/src/name-conflict-resolver.js +8 -2
  5. package/dist/dev/src/name-conflict-resolver.js.map +1 -1
  6. package/dist/dev/src/symbols/index.js +9 -3
  7. package/dist/dev/src/symbols/index.js.map +1 -1
  8. package/dist/dev/src/symbols/reference.js +19 -9
  9. package/dist/dev/src/symbols/reference.js.map +1 -1
  10. package/dist/dev/src/symbols/ts-module-scope.js +66 -2
  11. package/dist/dev/src/symbols/ts-module-scope.js.map +1 -1
  12. package/dist/dev/test/externals.test.js +17 -13
  13. package/dist/dev/test/externals.test.js.map +1 -1
  14. package/dist/dev/test/imports-reactive.test.js +223 -0
  15. package/dist/dev/test/imports-reactive.test.js.map +1 -0
  16. package/dist/dev/test/imports.test.js +262 -80
  17. package/dist/dev/test/imports.test.js.map +1 -1
  18. package/dist/src/components/ImportStatement.d.ts.map +1 -1
  19. package/dist/src/components/ImportStatement.js +16 -8
  20. package/dist/src/components/ImportStatement.js.map +1 -1
  21. package/dist/src/name-conflict-resolver.d.ts.map +1 -1
  22. package/dist/src/name-conflict-resolver.js +8 -2
  23. package/dist/src/name-conflict-resolver.js.map +1 -1
  24. package/dist/src/symbols/index.d.ts.map +1 -1
  25. package/dist/src/symbols/index.js +9 -3
  26. package/dist/src/symbols/index.js.map +1 -1
  27. package/dist/src/symbols/reference.d.ts.map +1 -1
  28. package/dist/src/symbols/reference.js +13 -3
  29. package/dist/src/symbols/reference.js.map +1 -1
  30. package/dist/src/symbols/ts-module-scope.d.ts +7 -0
  31. package/dist/src/symbols/ts-module-scope.d.ts.map +1 -1
  32. package/dist/src/symbols/ts-module-scope.js +66 -2
  33. package/dist/src/symbols/ts-module-scope.js.map +1 -1
  34. package/dist/test/externals.test.js +5 -1
  35. package/dist/test/externals.test.js.map +1 -1
  36. package/dist/test/imports-reactive.test.d.ts +2 -0
  37. package/dist/test/imports-reactive.test.d.ts.map +1 -0
  38. package/dist/test/imports-reactive.test.js +167 -0
  39. package/dist/test/imports-reactive.test.js.map +1 -0
  40. package/dist/test/imports.test.js +121 -3
  41. package/dist/test/imports.test.js.map +1 -1
  42. package/dist/tsconfig.tsbuildinfo +1 -1
  43. package/docs/api/types/TSModuleScope.md +9 -8
  44. package/package.json +6 -6
  45. package/src/components/ImportStatement.tsx +26 -12
  46. package/src/name-conflict-resolver.ts +8 -2
  47. package/src/symbols/index.ts +18 -3
  48. package/src/symbols/reference.tsx +17 -6
  49. package/src/symbols/ts-module-scope.ts +79 -2
  50. package/temp/api.json +66 -0
  51. package/test/externals.test.tsx +5 -1
  52. package/test/imports-reactive.test.tsx +128 -0
  53. package/test/imports.test.tsx +136 -2
@@ -6,11 +6,12 @@ A lexical scope for TypeScript which represents the top-level scope of a module.
6
6
 
7
7
  Extends [TSLexicalScope](../tslexicalscope/)
8
8
 
9
- | | | |
10
- | --------------- | -------------------------------------------------------------------------------------------------------- | - |
11
- | addImport | (targetSymbol: TSOutputSymbol, targetModule: TSModuleScope, options: AddImportOptions) => TSOutputSymbol | |
12
- | debugInfo | Record\<string, unknown> | |
13
- | exportedSymbols | Map<[Refkey](../../../core/types/refkey/), [TSOutputSymbol](../tsoutputsymbol/)> | |
14
- | getAllSymbols | () => Set\<TSOutputSymbol> | |
15
- | importedModules | [ImportRecords](../importrecords/) | |
16
- | importedSymbols | Map<[TSOutputSymbol](../tsoutputsymbol/), [TSOutputSymbol](../tsoutputsymbol/)> | |
9
+ | | | |
10
+ | --------------- | -------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
11
+ | addImport | (targetSymbol: TSOutputSymbol, targetModule: TSModuleScope, options: AddImportOptions) => TSOutputSymbol | |
12
+ | debugInfo | Record\<string, unknown> | |
13
+ | exportedSymbols | Map<[Refkey](../../../core/types/refkey/), [TSOutputSymbol](../tsoutputsymbol/)> | |
14
+ | getAllSymbols | () => Set\<TSOutputSymbol> | |
15
+ | importedModules | [ImportRecords](../importrecords/) | |
16
+ | importedSymbols | Map<[TSOutputSymbol](../tsoutputsymbol/), [TSOutputSymbol](../tsoutputsymbol/)> | |
17
+ | removeImport | (targetSymbol: TSOutputSymbol, options: AddImportOptions) => void | Decrement the refcount for an import previously registered via addImport. When the last live consumer goes away, the local import symbol is deleted, which removes it from the module’s name spaces and triggers name-conflict-resolver reconciliation. |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alloy-js/typescript",
3
- "version": "0.23.0-dev.9",
3
+ "version": "0.23.0",
4
4
  "description": "",
5
5
  "repository": {
6
6
  "type": "git",
@@ -30,18 +30,18 @@
30
30
  "author": "brian.terlson@microsoft.com",
31
31
  "license": "MIT",
32
32
  "dependencies": {
33
- "@alloy-js/core": "~0.22.0 || >= 0.23.0-dev.15",
34
33
  "change-case": "^5.4.4",
35
- "pathe": "^2.0.3"
34
+ "pathe": "^2.0.3",
35
+ "@alloy-js/core": "~0.23.0"
36
36
  },
37
37
  "devDependencies": {
38
- "@alloy-js/cli": "~0.22.0 || >= 0.23.0-dev.5",
39
- "@alloy-js/rollup-plugin": "~0.1.0 || >= 0.1.1-dev.2",
40
38
  "@microsoft/api-extractor": "~7.52.8",
41
39
  "@rollup/plugin-typescript": "^12.1.2",
42
40
  "concurrently": "^9.2.0",
43
41
  "typescript": "^5.8.3",
44
- "vitest": "3.2.4"
42
+ "vitest": "3.2.4",
43
+ "@alloy-js/cli": "~0.23.0",
44
+ "@alloy-js/rollup-plugin": "~0.1.1"
45
45
  },
46
46
  "type": "module",
47
47
  "scripts": {
@@ -102,20 +102,34 @@ export function ImportStatement(props: ImportStatementProps) {
102
102
  if (allNamedImportsAreTypes) {
103
103
  parts.push("type ");
104
104
  }
105
- parts.push("{ ");
106
105
  parts.push(
107
- mapJoin(
108
- () => namedImportSymbols,
109
- (nis) => (
110
- <ImportBinding
111
- importedSymbol={nis}
112
- inTypeImport={allNamedImportsAreTypes}
113
- />
114
- ),
115
- { joiner: ", " },
116
- ),
106
+ <group>
107
+ {"{"}
108
+ <indent>
109
+ <line />
110
+ {mapJoin(
111
+ () => namedImportSymbols,
112
+ (nis) => (
113
+ <ImportBinding
114
+ importedSymbol={nis}
115
+ inTypeImport={allNamedImportsAreTypes}
116
+ />
117
+ ),
118
+ {
119
+ joiner: (
120
+ <>
121
+ {","}
122
+ <line />
123
+ </>
124
+ ),
125
+ },
126
+ )}
127
+ <ifBreak>{","}</ifBreak>
128
+ </indent>
129
+ <line />
130
+ {"}"}
131
+ </group>,
117
132
  );
118
- parts.push(" }");
119
133
  }
120
134
  parts.push(` from "${props.path}";`);
121
135
 
@@ -9,12 +9,18 @@ export function tsNameConflictResolver(name: string, symbols: OutputSymbol[]) {
9
9
  (s) => s.tsFlags & TSSymbolFlags.LocalImportSymbol,
10
10
  );
11
11
  let nameCount = 1;
12
+ if (goodNamedSymbols.length >= 1) {
13
+ // The first "good" symbol owns the plain name. Clear any prior
14
+ // deconflict rename so survivors reclaim the original name when a
15
+ // collision is resolved.
16
+ goodNamedSymbols[0].deconflictedName = undefined;
17
+ }
12
18
  if (goodNamedSymbols.length > 1) {
13
19
  for (const sym of goodNamedSymbols.slice(1)) {
14
- sym.name = name + "_" + nameCount++;
20
+ sym.deconflictedName = name + "_" + nameCount++;
15
21
  }
16
22
  }
17
23
  for (const sym of badNamedSymbols) {
18
- sym.name = name + "_" + nameCount++;
24
+ sym.deconflictedName = name + "_" + nameCount++;
19
25
  }
20
26
  }
@@ -28,7 +28,12 @@ export function createTypeAndValueSymbol(
28
28
  const scope = useLexicalScope();
29
29
  const spaces = scope ? [scope.types, scope.values] : [];
30
30
  const binder = options.binder ?? scope?.binder;
31
- return createSymbol(TSOutputSymbol, name, spaces, { ...options, binder });
31
+ const symbol = createSymbol(TSOutputSymbol, name, spaces, {
32
+ ...options,
33
+ binder,
34
+ });
35
+ onCleanup(() => symbol.delete());
36
+ return symbol;
32
37
  }
33
38
 
34
39
  export function createTypeSymbol(
@@ -43,7 +48,12 @@ export function createTypeSymbol(
43
48
  }
44
49
  const spaces = scope ? [scope.types] : [];
45
50
  const binder = options.binder ?? scope?.binder;
46
- return createSymbol(TSOutputSymbol, name, spaces, { ...options, binder });
51
+ const symbol = createSymbol(TSOutputSymbol, name, spaces, {
52
+ ...options,
53
+ binder,
54
+ });
55
+ onCleanup(() => symbol.delete());
56
+ return symbol;
47
57
  }
48
58
 
49
59
  export function createValueSymbol(
@@ -58,7 +68,12 @@ export function createValueSymbol(
58
68
  }
59
69
  const spaces = scope ? [scope.values] : [];
60
70
  const binder = options.binder ?? scope?.binder;
61
- return createSymbol(TSOutputSymbol, name, spaces, { ...options, binder });
71
+ const symbol = createSymbol(TSOutputSymbol, name, spaces, {
72
+ ...options,
73
+ binder,
74
+ });
75
+ onCleanup(() => symbol.delete());
76
+ return symbol;
62
77
  }
63
78
 
64
79
  export function createTransientValueSymbol() {
@@ -3,6 +3,7 @@ import {
3
3
  MemberResolutionContext,
4
4
  MemberResolver,
5
5
  memo,
6
+ onCleanup,
6
7
  Refkey,
7
8
  resolve,
8
9
  unresolvedRefkey,
@@ -65,8 +66,15 @@ export function ref(
65
66
  for (const module of sourcePackage.exportedSymbols.values()) {
66
67
  for (const refkey of lexicalDeclaration.refkeys) {
67
68
  if (module.exportedSymbols.has(refkey)) {
69
+ const scope = sourceFile!.scope;
70
+ const targetModule = module;
68
71
  localSymbol = untrack(() =>
69
- sourceFile!.scope.addImport(lexicalDeclaration, module, {
72
+ scope.addImport(lexicalDeclaration, targetModule, {
73
+ type: options?.type,
74
+ }),
75
+ );
76
+ onCleanup(() =>
77
+ scope.removeImport(lexicalDeclaration, {
70
78
  type: options?.type,
71
79
  }),
72
80
  );
@@ -81,12 +89,15 @@ export function ref(
81
89
  );
82
90
  }
83
91
  } else if (targetLocation === "module") {
92
+ const scope = sourceFile!.scope;
93
+ const targetModule = pathDown[0] as TSModuleScope;
84
94
  localSymbol = untrack(() =>
85
- sourceFile!.scope.addImport(
86
- lexicalDeclaration,
87
- pathDown[0] as TSModuleScope,
88
- { type: options?.type },
89
- ),
95
+ scope.addImport(lexicalDeclaration, targetModule, {
96
+ type: options?.type,
97
+ }),
98
+ );
99
+ onCleanup(() =>
100
+ scope.removeImport(lexicalDeclaration, { type: options?.type }),
90
101
  );
91
102
  }
92
103
 
@@ -17,6 +17,16 @@ export interface AddImportOptions {
17
17
  type?: boolean;
18
18
  }
19
19
 
20
+ interface ImportEntry {
21
+ local: TSOutputSymbol;
22
+ target: TSOutputSymbol;
23
+ module: TSModuleScope;
24
+ // Number of live `ref()` consumers that added this symbol as a value import.
25
+ valueRefCount: number;
26
+ // Number of live `ref()` consumers that added this symbol as a type-only import.
27
+ typeRefCount: number;
28
+ }
29
+
20
30
  /**
21
31
  * A lexical scope for TypeScript which represents the top-level scope of a
22
32
  * module. Manages the module's imported and exported symbols.
@@ -40,6 +50,11 @@ export class TSModuleScope extends TSLexicalScope {
40
50
  return this.#importedModules;
41
51
  }
42
52
 
53
+ // Internal refcount + kind bookkeeping keyed by the target (source-of-truth)
54
+ // symbol. Used by {@link removeImport} to know when to actually delete the
55
+ // local import symbol and associated records.
56
+ #importEntries: Map<TSOutputSymbol, ImportEntry> = new Map();
57
+
43
58
  addImport(
44
59
  targetSymbol: TSOutputSymbol,
45
60
  targetModule: TSModuleScope,
@@ -47,8 +62,14 @@ export class TSModuleScope extends TSLexicalScope {
47
62
  ) {
48
63
  const existing = this.importedSymbols.get(targetSymbol);
49
64
  if (existing) {
50
- if (!options?.type && existing.isTypeSymbol) {
51
- existing.spaces.push(this.values);
65
+ const entry = this.#importEntries.get(targetSymbol)!;
66
+ if (options?.type) {
67
+ entry.typeRefCount++;
68
+ } else {
69
+ entry.valueRefCount++;
70
+ if (existing.isTypeSymbol && !existing.isValueSymbol) {
71
+ existing.spaces.push(this.values);
72
+ }
52
73
  }
53
74
  return existing;
54
75
  }
@@ -89,10 +110,66 @@ export class TSModuleScope extends TSLexicalScope {
89
110
  local: localSymbol,
90
111
  target: targetSymbol,
91
112
  });
113
+ this.#importEntries.set(targetSymbol, {
114
+ local: localSymbol,
115
+ target: targetSymbol,
116
+ module: targetModule,
117
+ valueRefCount: options?.type ? 0 : 1,
118
+ typeRefCount: options?.type ? 1 : 0,
119
+ });
92
120
 
93
121
  return localSymbol;
94
122
  }
95
123
 
124
+ /**
125
+ * Decrement the refcount for an import previously registered via
126
+ * {@link TSModuleScope.addImport | addImport}. When the last live consumer goes away, the local import
127
+ * symbol is deleted, which removes it from the module's name spaces and
128
+ * triggers name-conflict-resolver reconciliation.
129
+ */
130
+ removeImport(targetSymbol: TSOutputSymbol, options?: AddImportOptions) {
131
+ const entry = this.#importEntries.get(targetSymbol);
132
+ if (!entry) {
133
+ return;
134
+ }
135
+
136
+ if (options?.type) {
137
+ if (entry.typeRefCount > 0) entry.typeRefCount--;
138
+ } else {
139
+ if (entry.valueRefCount > 0) entry.valueRefCount--;
140
+ // If there are no more value consumers but type consumers remain, demote
141
+ // the local symbol back to being type-only by removing it from the values
142
+ // space.
143
+ if (entry.valueRefCount === 0 && entry.typeRefCount > 0) {
144
+ if (entry.local.isValueSymbol && entry.local.isTypeSymbol) {
145
+ this.values.delete(entry.local);
146
+ }
147
+ }
148
+ }
149
+
150
+ if (entry.valueRefCount > 0 || entry.typeRefCount > 0) {
151
+ return;
152
+ }
153
+
154
+ // No more live references: tear it all down.
155
+ entry.local.delete();
156
+ this.#importedSymbols.delete(targetSymbol);
157
+ this.#importEntries.delete(targetSymbol);
158
+
159
+ const moduleSet = this.#importedModules.get(entry.module);
160
+ if (moduleSet) {
161
+ for (const imp of moduleSet) {
162
+ if (imp.target === targetSymbol) {
163
+ moduleSet.delete(imp);
164
+ break;
165
+ }
166
+ }
167
+ if (moduleSet.size === 0) {
168
+ this.#importedModules.delete(entry.module);
169
+ }
170
+ }
171
+ }
172
+
96
173
  getAllSymbols() {
97
174
  const allSymbols = new Set<TSOutputSymbol>();
98
175
  for (const symbol of this.values) {
package/temp/api.json CHANGED
@@ -14941,6 +14941,72 @@
14941
14941
  "isStatic": false,
14942
14942
  "isProtected": false,
14943
14943
  "isAbstract": false
14944
+ },
14945
+ {
14946
+ "kind": "Method",
14947
+ "canonicalReference": "@alloy-js/typescript!TSModuleScope#removeImport:member(1)",
14948
+ "docComment": "/**\n * Decrement the refcount for an import previously registered via\n * {@link TSModuleScope.addImport | addImport}. When the last live consumer goes away, the local import\n * symbol is deleted, which removes it from the module's name spaces and\n * triggers name-conflict-resolver reconciliation.\n */\n",
14949
+ "excerptTokens": [
14950
+ {
14951
+ "kind": "Content",
14952
+ "text": "removeImport(targetSymbol: "
14953
+ },
14954
+ {
14955
+ "kind": "Reference",
14956
+ "text": "TSOutputSymbol",
14957
+ "canonicalReference": "@alloy-js/typescript!TSOutputSymbol:class"
14958
+ },
14959
+ {
14960
+ "kind": "Content",
14961
+ "text": ", options?: "
14962
+ },
14963
+ {
14964
+ "kind": "Reference",
14965
+ "text": "AddImportOptions",
14966
+ "canonicalReference": "@alloy-js/typescript!AddImportOptions:interface"
14967
+ },
14968
+ {
14969
+ "kind": "Content",
14970
+ "text": "): "
14971
+ },
14972
+ {
14973
+ "kind": "Content",
14974
+ "text": "void"
14975
+ },
14976
+ {
14977
+ "kind": "Content",
14978
+ "text": ";"
14979
+ }
14980
+ ],
14981
+ "isStatic": false,
14982
+ "returnTypeTokenRange": {
14983
+ "startIndex": 5,
14984
+ "endIndex": 6
14985
+ },
14986
+ "releaseTag": "Public",
14987
+ "isProtected": false,
14988
+ "overloadIndex": 1,
14989
+ "parameters": [
14990
+ {
14991
+ "parameterName": "targetSymbol",
14992
+ "parameterTypeTokenRange": {
14993
+ "startIndex": 1,
14994
+ "endIndex": 2
14995
+ },
14996
+ "isOptional": false
14997
+ },
14998
+ {
14999
+ "parameterName": "options",
15000
+ "parameterTypeTokenRange": {
15001
+ "startIndex": 3,
15002
+ "endIndex": 4
15003
+ },
15004
+ "isOptional": true
15005
+ }
15006
+ ],
15007
+ "isOptional": false,
15008
+ "isAbstract": false,
15009
+ "name": "removeImport"
14944
15010
  }
14945
15011
  ],
14946
15012
  "extendsTokenRange": {
@@ -175,7 +175,11 @@ it("can import static members", () => {
175
175
  </Output>,
176
176
  ).toRenderTo({
177
177
  "index.ts": `
178
- import { noMembers, server, simpleName } from "@modelcontextprotocol/sdk/server/index.js";
178
+ import {
179
+ noMembers,
180
+ server,
181
+ simpleName,
182
+ } from "@modelcontextprotocol/sdk/server/index.js";
179
183
 
180
184
  function foo() {
181
185
  server();
@@ -0,0 +1,128 @@
1
+ import {
2
+ Output,
3
+ printTree,
4
+ ref,
5
+ refkey,
6
+ renderTree,
7
+ Show,
8
+ } from "@alloy-js/core";
9
+ import "@alloy-js/core/testing";
10
+ import { expect, it } from "vitest";
11
+ import * as ts from "../src/components/index.js";
12
+ import { createPackage, PackageDirectory, SourceFile } from "../src/index.js";
13
+
14
+ const testLib = () =>
15
+ createPackage({
16
+ name: "testLib",
17
+ version: "1.0.0",
18
+ descriptor: {
19
+ ".": { named: ["foo"] },
20
+ },
21
+ });
22
+
23
+ function sourceFileText(printed: string): string {
24
+ // Strip the surrounding package.json / tsconfig.json blobs and keep the
25
+ // text from index.ts onwards. The file content follows the last tsconfig
26
+ // closing brace.
27
+ const marker = "\n}";
28
+ const idx = printed.lastIndexOf(marker);
29
+ return idx === -1 ? printed : printed.slice(idx + marker.length);
30
+ }
31
+
32
+ it("removes an import when the reference is removed", () => {
33
+ const lib = testLib();
34
+ const showIt = ref(true);
35
+
36
+ const tree = renderTree(
37
+ <Output externals={[lib]}>
38
+ <PackageDirectory path="." name="test" version="1.0.0">
39
+ <SourceFile path="index.ts">
40
+ <Show when={showIt.value}>x = {lib.foo};</Show>
41
+ </SourceFile>
42
+ </PackageDirectory>
43
+ </Output>,
44
+ );
45
+
46
+ const initial = sourceFileText(printTree(tree));
47
+ showIt.value = false;
48
+ const off = sourceFileText(printTree(tree));
49
+ showIt.value = true;
50
+ const back = sourceFileText(printTree(tree));
51
+
52
+ // With no live reference, the import line should not be in the file.
53
+ expect(off).not.toMatch(/import .* from "testLib"/);
54
+ // After re-showing, we should be back to the initial output.
55
+ expect(back).toEqual(initial);
56
+ });
57
+
58
+ it("does not leak name-conflict aliases across re-renders", () => {
59
+ const lib = testLib();
60
+ const showLocal = ref(true);
61
+ const fooRk = refkey("local-foo");
62
+
63
+ const tree = renderTree(
64
+ <Output externals={[lib]}>
65
+ <PackageDirectory path="." name="test" version="1.0.0">
66
+ <SourceFile path="index.ts">
67
+ <Show when={showLocal.value}>
68
+ <ts.VarDeclaration const name="foo" refkey={fooRk}>
69
+ 1
70
+ </ts.VarDeclaration>
71
+ ;
72
+ </Show>
73
+ use = {lib.foo};
74
+ </SourceFile>
75
+ </PackageDirectory>
76
+ </Output>,
77
+ );
78
+
79
+ sourceFileText(printTree(tree));
80
+ showLocal.value = false;
81
+ const off = sourceFileText(printTree(tree));
82
+ showLocal.value = true;
83
+ const back = sourceFileText(printTree(tree));
84
+ showLocal.value = false;
85
+ const off2 = sourceFileText(printTree(tree));
86
+ showLocal.value = true;
87
+ const back2 = sourceFileText(printTree(tree));
88
+
89
+ // While the local is hidden, the import should be the plain name, not an
90
+ // alias — the conflict is gone.
91
+ expect(off).toMatch(/import \{ foo \} from "testLib"/);
92
+ expect(off2).toMatch(/import \{ foo \} from "testLib"/);
93
+ // Off snapshots after repeated toggles must be identical (no drift).
94
+ expect(off2).toEqual(off);
95
+ // Back-on snapshots after repeated toggles must be identical too — no
96
+ // `foo_3`/`foo_4` drift.
97
+ expect(back2).toEqual(back);
98
+ // No generated name should drift beyond the expected `_2` suffix.
99
+ expect(back).not.toMatch(/foo_3|foo_4/);
100
+ expect(back2).not.toMatch(/foo_3|foo_4/);
101
+ });
102
+
103
+ it("removes only one ref count at a time", () => {
104
+ const lib = testLib();
105
+ const showA = ref(true);
106
+ const showB = ref(true);
107
+
108
+ const tree = renderTree(
109
+ <Output externals={[lib]}>
110
+ <PackageDirectory path="." name="test" version="1.0.0">
111
+ <SourceFile path="index.ts">
112
+ <Show when={showA.value}>a = {lib.foo};</Show>
113
+ <Show when={showB.value}>b = {lib.foo};</Show>
114
+ </SourceFile>
115
+ </PackageDirectory>
116
+ </Output>,
117
+ );
118
+
119
+ // Hide only one of the two references — the import must still be present.
120
+ showA.value = false;
121
+ const oneHidden = sourceFileText(printTree(tree));
122
+ expect(oneHidden).toMatch(/import \{ foo \} from "testLib"/);
123
+
124
+ // Hide the second — now the import should go away.
125
+ showB.value = false;
126
+ const bothHidden = sourceFileText(printTree(tree));
127
+ expect(bothHidden).not.toMatch(/import .* from "testLib"/);
128
+ });