@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 138 lines 5.58 KB
Raw
1 import { describe, expect, it } from "@alchemy.run/test";
2 import { collectNamespaceImportEdits } from "@/Bundle/NamespaceImportPlugin";
3
4 const isAlchemy = (specifier: string) => /^(alchemy|@alchemy\.run\/[^/]+)(\/|$)/.test(specifier);
5
6 /** Applies the plugin's edits to `code`, or returns `null` when it leaves the file alone. */
7 const rewrite = async (code: string, filename = "/proj/src/api.ts") => {
8 const edits = await collectNamespaceImportEdits(code, filename, isAlchemy);
9 if (edits.length === 0) return null;
10 let out = code;
11 for (const edit of [...edits].sort((a, b) => b.start - a.start)) {
12 out = out.slice(0, edit.start) + edit.text + out.slice(edit.end);
13 }
14 return out;
15 };
16
17 describe("collectNamespaceImportEdits", { tags: ["unit", "local"] }, () => {
18 it("rewrites static member access into named imports", async () => {
19 const code = [
20 `import * as AWS from "@alchemy.run/aws";`,
21 `export default class Api extends AWS.Lambda.Function<Api>()("Api", {}) {}`,
22 `export const layer = AWS.S3.GetObjectHttp;`,
23 `export const lambda = AWS.Lambda;`,
24 ].join("\n");
25 expect(await rewrite(code)).toBe(
26 [
27 `import { Lambda as AWS$Lambda, S3 as AWS$S3 } from "@alchemy.run/aws";`,
28 `export default class Api extends AWS$Lambda.Function<Api>()("Api", {}) {}`,
29 `export const layer = AWS$S3.GetObjectHttp;`,
30 `export const lambda = AWS$Lambda;`,
31 ].join("\n"),
32 );
33 });
34
35 it("imports members only read in types as type-only", async () => {
36 const code = [
37 `import * as AWS from "@alchemy.run/aws";`,
38 `export const fn = (props: AWS.Lambda.FunctionProps) => AWS.S3.Bucket(props);`,
39 ].join("\n");
40 expect(await rewrite(code)).toBe(
41 [
42 `import { type Lambda as AWS$Lambda, S3 as AWS$S3 } from "@alchemy.run/aws";`,
43 `export const fn = (props: AWS$Lambda.FunctionProps) => AWS$S3.Bucket(props);`,
44 ].join("\n"),
45 );
46 });
47
48 it("rewrites a member read both as a value and as a type as a value import", async () => {
49 const code = [
50 `import * as AWS from "@alchemy.run/aws";`,
51 `export const region: AWS.Region = AWS.Region;`,
52 ].join("\n");
53 expect(await rewrite(code)).toBe(
54 [
55 `import { Region as AWS$Region } from "@alchemy.run/aws";`,
56 `export const region: AWS$Region = AWS$Region;`,
57 ].join("\n"),
58 );
59 });
60
61 it("imports members read in interface heritage and implements clauses as type-only", async () => {
62 const code = [
63 `import * as Bundle from "alchemy/Bundle/Bundle";`,
64 `export interface Options extends Bundle.BundleConfig {}`,
65 `export class Plugin implements Bundle.Plugin {}`,
66 `export const build = Bundle.build;`,
67 ].join("\n");
68 expect(await rewrite(code)).toBe(
69 [
70 `import { type BundleConfig as Bundle$BundleConfig, type Plugin as Bundle$Plugin, build as Bundle$build } from "alchemy/Bundle/Bundle";`,
71 `export interface Options extends Bundle$BundleConfig {}`,
72 `export class Plugin implements Bundle$Plugin {}`,
73 `export const build = Bundle$build;`,
74 ].join("\n"),
75 );
76 });
77
78 it("imports members read in a type query as type-only", async () => {
79 const code = [
80 `import * as AWS from "@alchemy.run/aws";`,
81 `export type Fn = typeof AWS.Lambda.Function;`,
82 ].join("\n");
83 expect(await rewrite(code)).toBe(
84 [
85 `import { type Lambda as AWS$Lambda } from "@alchemy.run/aws";`,
86 `export type Fn = typeof AWS$Lambda.Function;`,
87 ].join("\n"),
88 );
89 });
90
91 it("picks an alias that doesn't collide with existing names", async () => {
92 const code = [
93 `import * as AWS from "@alchemy.run/aws";`,
94 `const AWS$Lambda = 1;`,
95 `export const f = [AWS.Lambda, AWS$Lambda];`,
96 ].join("\n");
97 expect(await rewrite(code)).toContain(
98 `import { Lambda as AWS$Lambda$ } from "@alchemy.run/aws";`,
99 );
100 });
101
102 it("leaves packages outside the list alone", async () => {
103 const code = `import * as Effect from "effect/Effect";\nexport const x = Effect.void;\n`;
104 expect(await rewrite(code)).toBeNull();
105 });
106
107 for (const [reason, use] of [
108 ["the namespace is passed around", `export const all = AWS;`],
109 ["the namespace is spread", `export const all = { ...AWS };`],
110 ["a member is computed", `export const fn = AWS["Lambda"];`],
111 ["a local binding shadows the name", `export const f = (AWS: any) => AWS.Lambda;`],
112 ["the namespace is queried as a type", `export type All = typeof AWS;`],
113 ["the namespace is re-exported", `export { AWS };`],
114 ["a TypeScript import alias reads it", `import L = AWS.Lambda;`],
115 ]) {
116 it(`leaves the import alone when ${reason}`, async () => {
117 const code = `import * as AWS from "@alchemy.run/aws";\nexport const ok = AWS.Lambda;\n${use}\n`;
118 expect(await rewrite(code)).toBeNull();
119 });
120 }
121
122 it("leaves the import alone when the namespace is used in JSX", async () => {
123 const code = `import * as UI from "@alchemy.run/ui";\nexport const el = <UI.Button />;\n`;
124 expect(await rewrite(code, "/proj/src/app.tsx")).toBeNull();
125 });
126
127 it("leaves type-only namespace imports alone", async () => {
128 const code = `import type * as AWS from "@alchemy.run/aws";\nexport type P = AWS.Lambda.FunctionProps;\n`;
129 expect(await rewrite(code)).toBeNull();
130 });
131
132 it("rewrites JavaScript files", async () => {
133 const code = `import * as Alchemy from "alchemy";\nexport default Alchemy.Stack;\n`;
134 expect(await rewrite(code, "/proj/node_modules/app/dist/index.js")).toBe(
135 `import { Stack as Alchemy$Stack } from "alchemy";\nexport default Alchemy$Stack;\n`,
136 );
137 });
138 });