Skip to content

Commit 5af3f8c

Browse files
committed
refactor: break down test files
1 parent 4a286e2 commit 5af3f8c

File tree

8 files changed

+157
-140
lines changed

8 files changed

+157
-140
lines changed

src/error.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ export class MagicastError extends Error {
1515
this.rawMessage = message;
1616
this.options = options;
1717

18-
if (options?.ast && options?.code) {
18+
if (options?.ast && options?.code && options.ast.loc) {
1919
// construct a code frame point to the start of the ast node
20-
const { line, column } = options.ast.loc!.start;
20+
const { line, column } = options.ast.loc.start;
2121
const lines = options.code.split("\n");
2222
const start = Math.max(0, line - 3);
2323
const end = Math.min(lines.length, line + 3);

src/proxy/imports.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export function creatImportProxy(
2828
root: Program
2929
): ProxifiedImportItem {
3030
if (_importProxyCache.has(specifier)) {
31+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
3132
return _importProxyCache.get(specifier)!;
3233
}
3334
const proxy = createProxy(
@@ -114,6 +115,7 @@ export function creatImportProxy(
114115

115116
export function createImportsProxy(
116117
root: Program,
118+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
117119
mod: ProxifiedModule
118120
): ProxifiedImportsMap {
119121
// TODO: cache

test/array.test.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { describe, expect, it } from "vitest";
2+
import { parseCode } from "../src";
3+
import { generate } from "./_utils";
4+
5+
describe("array", () => {
6+
it("array operations", () => {
7+
const mod = parseCode(`export default [1, 2, 3, 4, 5]`);
8+
9+
expect(mod.exports.default.length).toBe(5);
10+
expect(mod.exports.default.includes(5)).toBe(true);
11+
expect(mod.exports.default.includes(6)).toBe(false);
12+
13+
const deleted = mod.exports.default.splice(1, 3, { foo: "bar" }, "bar");
14+
15+
expect(deleted).toEqual([2, 3, 4]);
16+
17+
expect(generate(mod)).toMatchInlineSnapshot(
18+
`
19+
"export default [
20+
1,
21+
{
22+
foo: \\"bar\\",
23+
},
24+
\\"bar\\",
25+
5,
26+
];"
27+
`
28+
);
29+
30+
const foundIndex = mod.exports.default.findIndex(
31+
(item) => item.foo === "bar"
32+
);
33+
const found = mod.exports.default.find((item) => item.foo === "bar");
34+
35+
expect(foundIndex).toBe(1);
36+
expect(found).toMatchInlineSnapshot(`
37+
{
38+
"foo": "bar",
39+
}
40+
`);
41+
});
42+
43+
it("array should be iterable", () => {
44+
const mod = parseCode(`
45+
export const config = {
46+
array: ['a']
47+
}
48+
`);
49+
const arr = [...mod.exports.config.array];
50+
expect(arr).toMatchInlineSnapshot(`
51+
[
52+
"a",
53+
]
54+
`);
55+
});
56+
});

test/errors.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { expect, describe, it } from "vitest";
22
import { parseCode } from "../src";
33
import { generate } from "./_utils";
44

5-
describe("magicast", () => {
5+
describe("errors", () => {
66
it("ternary", () => {
77
const mod = parseCode(
88
`

test/format.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect, it, describe } from "vitest";
22
import { CodeFormatOptions, detectCodeFormat } from "../src";
33

4-
describe("magicast:style", () => {
4+
describe("format", () => {
55
const cases: Array<{
66
name: string;
77
code: string;

test/function-call.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { describe, expect, it } from "vitest";
2+
import { parseCode } from "../src";
3+
import { generate } from "./_utils";
4+
5+
describe("function-calls", () => {
6+
it("function wrapper", () => {
7+
const mod = parseCode(`
8+
export const a: any = { foo: 1}
9+
export default defineConfig({
10+
// Modules
11+
modules: ["a"]
12+
})
13+
`);
14+
15+
expect(mod.exports.a.foo).toBe(1);
16+
expect(mod.exports.default.$type).toBe("function-call");
17+
expect(mod.exports.default.$callee).toBe("defineConfig");
18+
expect(mod.exports.default.$args).toMatchInlineSnapshot(`
19+
[
20+
{
21+
"modules": [
22+
"a",
23+
],
24+
},
25+
]
26+
`);
27+
28+
const options = mod.exports.default.$args[0];
29+
30+
options.modules ||= [];
31+
options.modules.push("b");
32+
33+
expect(generate(mod)).toMatchInlineSnapshot(`
34+
"export const a: any = { foo: 1 };
35+
export default defineConfig({
36+
// Modules
37+
modules: [\\"a\\", \\"b\\"],
38+
});"
39+
`);
40+
});
41+
});

test/basic.test.ts renamed to test/general.test.ts

Lines changed: 1 addition & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { expect, it, describe } from "vitest";
22
import { generateCode, parseCode } from "../src";
33
import { generate } from "./_utils";
44

5-
describe("magicast", () => {
5+
describe("general", () => {
66
it("basic object and array", () => {
77
const mod = parseCode(`export default { a: 1, b: { c: {} } }`);
88

@@ -107,42 +107,6 @@ describe("magicast", () => {
107107
);
108108
});
109109

110-
it("function wrapper", () => {
111-
const mod = parseCode(`
112-
export const a: any = { foo: 1}
113-
export default defineConfig({
114-
// Modules
115-
modules: ["a"]
116-
})
117-
`);
118-
119-
expect(mod.exports.a.foo).toBe(1);
120-
expect(mod.exports.default.$type).toBe("function-call");
121-
expect(mod.exports.default.$callee).toBe("defineConfig");
122-
expect(mod.exports.default.$args).toMatchInlineSnapshot(`
123-
[
124-
{
125-
"modules": [
126-
"a",
127-
],
128-
},
129-
]
130-
`);
131-
132-
const options = mod.exports.default.$args[0];
133-
134-
options.modules ||= [];
135-
options.modules.push("b");
136-
137-
expect(generate(mod)).toMatchInlineSnapshot(`
138-
"export const a: any = { foo: 1 };
139-
export default defineConfig({
140-
// Modules
141-
modules: [\\"a\\", \\"b\\"],
142-
});"
143-
`);
144-
});
145-
146110
it("delete property", () => {
147111
const mod = parseCode(`export default { a: 1, b: [1, { foo: 'bar' }] }`);
148112

@@ -165,43 +129,6 @@ describe("magicast", () => {
165129
`);
166130
});
167131

168-
it("array operations", () => {
169-
const mod = parseCode(`export default [1, 2, 3, 4, 5]`);
170-
171-
expect(mod.exports.default.length).toBe(5);
172-
expect(mod.exports.default.includes(5)).toBe(true);
173-
expect(mod.exports.default.includes(6)).toBe(false);
174-
175-
const deleted = mod.exports.default.splice(1, 3, { foo: "bar" }, "bar");
176-
177-
expect(deleted).toEqual([2, 3, 4]);
178-
179-
expect(generate(mod)).toMatchInlineSnapshot(
180-
`
181-
"export default [
182-
1,
183-
{
184-
foo: \\"bar\\",
185-
},
186-
\\"bar\\",
187-
5,
188-
];"
189-
`
190-
);
191-
192-
const foundIndex = mod.exports.default.findIndex(
193-
(item) => item.foo === "bar"
194-
);
195-
const found = mod.exports.default.find((item) => item.foo === "bar");
196-
197-
expect(foundIndex).toBe(1);
198-
expect(found).toMatchInlineSnapshot(`
199-
{
200-
"foo": "bar",
201-
}
202-
`);
203-
});
204-
205132
it("should preserve code styles", () => {
206133
const mod = parseCode(`
207134
export const config = {
@@ -215,66 +142,4 @@ describe("magicast", () => {
215142
}"
216143
`);
217144
});
218-
219-
it("array should be iterable", () => {
220-
const mod = parseCode(`
221-
export const config = {
222-
array: ['a']
223-
}
224-
`);
225-
const arr = [...mod.exports.config.array];
226-
expect(arr).toMatchInlineSnapshot(`
227-
[
228-
"a",
229-
]
230-
`);
231-
});
232-
233-
it("object property", () => {
234-
const mod = parseCode(
235-
`
236-
export default {
237-
foo: {
238-
['a']: 1,
239-
['a-b']: 2,
240-
foo() {}
241-
}
242-
}
243-
`.trim()
244-
);
245-
246-
expect(mod.exports.default.foo.a).toBe(1);
247-
expect(mod.exports.default.foo["a-b"]).toBe(2);
248-
expect(Object.keys(mod.exports.default.foo)).toMatchInlineSnapshot(`
249-
[
250-
"a",
251-
"a-b",
252-
"foo",
253-
]
254-
`);
255-
256-
mod.exports.default.foo["a-b-c"] = 3;
257-
258-
expect(Object.keys(mod.exports.default.foo)).toMatchInlineSnapshot(`
259-
[
260-
"a",
261-
"a-b",
262-
"foo",
263-
"a-b-c",
264-
]
265-
`);
266-
267-
mod.exports.default.foo["a-b"] = "updated";
268-
269-
expect(generate(mod)).toMatchInlineSnapshot(`
270-
"export default {
271-
foo: {
272-
[\\"a\\"]: 1,
273-
[\\"a-b\\"]: \\"updated\\",
274-
foo() {},
275-
\\"a-b-c\\": 3,
276-
},
277-
};"
278-
`);
279-
});
280145
});

test/object.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { describe, expect, it } from "vitest";
2+
import { parseCode } from "../src";
3+
import { generate } from "./_utils";
4+
5+
describe("object", () => {
6+
it("object property", () => {
7+
const mod = parseCode(
8+
`
9+
export default {
10+
foo: {
11+
['a']: 1,
12+
['a-b']: 2,
13+
foo() {}
14+
}
15+
}
16+
`.trim()
17+
);
18+
19+
expect(mod.exports.default.foo.a).toBe(1);
20+
expect(mod.exports.default.foo["a-b"]).toBe(2);
21+
expect(Object.keys(mod.exports.default.foo)).toMatchInlineSnapshot(`
22+
[
23+
"a",
24+
"a-b",
25+
"foo",
26+
]
27+
`);
28+
29+
mod.exports.default.foo["a-b-c"] = 3;
30+
31+
expect(Object.keys(mod.exports.default.foo)).toMatchInlineSnapshot(`
32+
[
33+
"a",
34+
"a-b",
35+
"foo",
36+
"a-b-c",
37+
]
38+
`);
39+
40+
mod.exports.default.foo["a-b"] = "updated";
41+
42+
expect(generate(mod)).toMatchInlineSnapshot(`
43+
"export default {
44+
foo: {
45+
[\\"a\\"]: 1,
46+
[\\"a-b\\"]: \\"updated\\",
47+
foo() {},
48+
\\"a-b-c\\": 3,
49+
},
50+
};"
51+
`);
52+
});
53+
});

0 commit comments

Comments
 (0)