From 5af3f8c74537a25b1a96a9b117d50e8d650f1fce Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Sat, 25 Feb 2023 14:26:23 +0100 Subject: [PATCH] refactor: break down test files --- src/error.ts | 4 +- src/proxy/imports.ts | 2 + test/array.test.ts | 56 ++++++++++ test/errors.test.ts | 2 +- test/format.test.ts | 2 +- test/function-call.test.ts | 41 +++++++ test/{basic.test.ts => general.test.ts} | 137 +----------------------- test/object.test.ts | 53 +++++++++ 8 files changed, 157 insertions(+), 140 deletions(-) create mode 100644 test/array.test.ts create mode 100644 test/function-call.test.ts rename test/{basic.test.ts => general.test.ts} (50%) create mode 100644 test/object.test.ts diff --git a/src/error.ts b/src/error.ts index 43c5ece..5da6aa9 100644 --- a/src/error.ts +++ b/src/error.ts @@ -15,9 +15,9 @@ export class MagicastError extends Error { this.rawMessage = message; this.options = options; - if (options?.ast && options?.code) { + if (options?.ast && options?.code && options.ast.loc) { // construct a code frame point to the start of the ast node - const { line, column } = options.ast.loc!.start; + const { line, column } = options.ast.loc.start; const lines = options.code.split("\n"); const start = Math.max(0, line - 3); const end = Math.min(lines.length, line + 3); diff --git a/src/proxy/imports.ts b/src/proxy/imports.ts index ae2166a..5ed3214 100644 --- a/src/proxy/imports.ts +++ b/src/proxy/imports.ts @@ -28,6 +28,7 @@ export function creatImportProxy( root: Program ): ProxifiedImportItem { if (_importProxyCache.has(specifier)) { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion return _importProxyCache.get(specifier)!; } const proxy = createProxy( @@ -114,6 +115,7 @@ export function creatImportProxy( export function createImportsProxy( root: Program, + // eslint-disable-next-line @typescript-eslint/no-unused-vars mod: ProxifiedModule ): ProxifiedImportsMap { // TODO: cache diff --git a/test/array.test.ts b/test/array.test.ts new file mode 100644 index 0000000..5f5449a --- /dev/null +++ b/test/array.test.ts @@ -0,0 +1,56 @@ +import { describe, expect, it } from "vitest"; +import { parseCode } from "../src"; +import { generate } from "./_utils"; + +describe("array", () => { + it("array operations", () => { + const mod = parseCode(`export default [1, 2, 3, 4, 5]`); + + expect(mod.exports.default.length).toBe(5); + expect(mod.exports.default.includes(5)).toBe(true); + expect(mod.exports.default.includes(6)).toBe(false); + + const deleted = mod.exports.default.splice(1, 3, { foo: "bar" }, "bar"); + + expect(deleted).toEqual([2, 3, 4]); + + expect(generate(mod)).toMatchInlineSnapshot( + ` + "export default [ + 1, + { + foo: \\"bar\\", + }, + \\"bar\\", + 5, + ];" + ` + ); + + const foundIndex = mod.exports.default.findIndex( + (item) => item.foo === "bar" + ); + const found = mod.exports.default.find((item) => item.foo === "bar"); + + expect(foundIndex).toBe(1); + expect(found).toMatchInlineSnapshot(` + { + "foo": "bar", + } + `); + }); + + it("array should be iterable", () => { + const mod = parseCode(` + export const config = { + array: ['a'] + } + `); + const arr = [...mod.exports.config.array]; + expect(arr).toMatchInlineSnapshot(` + [ + "a", + ] + `); + }); +}); diff --git a/test/errors.test.ts b/test/errors.test.ts index e75998b..91376d1 100644 --- a/test/errors.test.ts +++ b/test/errors.test.ts @@ -2,7 +2,7 @@ import { expect, describe, it } from "vitest"; import { parseCode } from "../src"; import { generate } from "./_utils"; -describe("magicast", () => { +describe("errors", () => { it("ternary", () => { const mod = parseCode( ` diff --git a/test/format.test.ts b/test/format.test.ts index 340ce4b..b818adf 100644 --- a/test/format.test.ts +++ b/test/format.test.ts @@ -1,7 +1,7 @@ import { expect, it, describe } from "vitest"; import { CodeFormatOptions, detectCodeFormat } from "../src"; -describe("magicast:style", () => { +describe("format", () => { const cases: Array<{ name: string; code: string; diff --git a/test/function-call.test.ts b/test/function-call.test.ts new file mode 100644 index 0000000..2871369 --- /dev/null +++ b/test/function-call.test.ts @@ -0,0 +1,41 @@ +import { describe, expect, it } from "vitest"; +import { parseCode } from "../src"; +import { generate } from "./_utils"; + +describe("function-calls", () => { + it("function wrapper", () => { + const mod = parseCode(` + export const a: any = { foo: 1} + export default defineConfig({ + // Modules + modules: ["a"] + }) + `); + + expect(mod.exports.a.foo).toBe(1); + expect(mod.exports.default.$type).toBe("function-call"); + expect(mod.exports.default.$callee).toBe("defineConfig"); + expect(mod.exports.default.$args).toMatchInlineSnapshot(` + [ + { + "modules": [ + "a", + ], + }, + ] + `); + + const options = mod.exports.default.$args[0]; + + options.modules ||= []; + options.modules.push("b"); + + expect(generate(mod)).toMatchInlineSnapshot(` + "export const a: any = { foo: 1 }; + export default defineConfig({ + // Modules + modules: [\\"a\\", \\"b\\"], + });" + `); + }); +}); diff --git a/test/basic.test.ts b/test/general.test.ts similarity index 50% rename from test/basic.test.ts rename to test/general.test.ts index 1a21b7f..daf4022 100644 --- a/test/basic.test.ts +++ b/test/general.test.ts @@ -2,7 +2,7 @@ import { expect, it, describe } from "vitest"; import { generateCode, parseCode } from "../src"; import { generate } from "./_utils"; -describe("magicast", () => { +describe("general", () => { it("basic object and array", () => { const mod = parseCode(`export default { a: 1, b: { c: {} } }`); @@ -107,42 +107,6 @@ describe("magicast", () => { ); }); - it("function wrapper", () => { - const mod = parseCode(` - export const a: any = { foo: 1} - export default defineConfig({ - // Modules - modules: ["a"] - }) - `); - - expect(mod.exports.a.foo).toBe(1); - expect(mod.exports.default.$type).toBe("function-call"); - expect(mod.exports.default.$callee).toBe("defineConfig"); - expect(mod.exports.default.$args).toMatchInlineSnapshot(` - [ - { - "modules": [ - "a", - ], - }, - ] - `); - - const options = mod.exports.default.$args[0]; - - options.modules ||= []; - options.modules.push("b"); - - expect(generate(mod)).toMatchInlineSnapshot(` - "export const a: any = { foo: 1 }; - export default defineConfig({ - // Modules - modules: [\\"a\\", \\"b\\"], - });" - `); - }); - it("delete property", () => { const mod = parseCode(`export default { a: 1, b: [1, { foo: 'bar' }] }`); @@ -165,43 +129,6 @@ describe("magicast", () => { `); }); - it("array operations", () => { - const mod = parseCode(`export default [1, 2, 3, 4, 5]`); - - expect(mod.exports.default.length).toBe(5); - expect(mod.exports.default.includes(5)).toBe(true); - expect(mod.exports.default.includes(6)).toBe(false); - - const deleted = mod.exports.default.splice(1, 3, { foo: "bar" }, "bar"); - - expect(deleted).toEqual([2, 3, 4]); - - expect(generate(mod)).toMatchInlineSnapshot( - ` - "export default [ - 1, - { - foo: \\"bar\\", - }, - \\"bar\\", - 5, - ];" - ` - ); - - const foundIndex = mod.exports.default.findIndex( - (item) => item.foo === "bar" - ); - const found = mod.exports.default.find((item) => item.foo === "bar"); - - expect(foundIndex).toBe(1); - expect(found).toMatchInlineSnapshot(` - { - "foo": "bar", - } - `); - }); - it("should preserve code styles", () => { const mod = parseCode(` export const config = { @@ -215,66 +142,4 @@ describe("magicast", () => { }" `); }); - - it("array should be iterable", () => { - const mod = parseCode(` - export const config = { - array: ['a'] - } - `); - const arr = [...mod.exports.config.array]; - expect(arr).toMatchInlineSnapshot(` - [ - "a", - ] - `); - }); - - it("object property", () => { - const mod = parseCode( - ` -export default { - foo: { - ['a']: 1, - ['a-b']: 2, - foo() {} - } -} - `.trim() - ); - - expect(mod.exports.default.foo.a).toBe(1); - expect(mod.exports.default.foo["a-b"]).toBe(2); - expect(Object.keys(mod.exports.default.foo)).toMatchInlineSnapshot(` - [ - "a", - "a-b", - "foo", - ] - `); - - mod.exports.default.foo["a-b-c"] = 3; - - expect(Object.keys(mod.exports.default.foo)).toMatchInlineSnapshot(` - [ - "a", - "a-b", - "foo", - "a-b-c", - ] - `); - - mod.exports.default.foo["a-b"] = "updated"; - - expect(generate(mod)).toMatchInlineSnapshot(` - "export default { - foo: { - [\\"a\\"]: 1, - [\\"a-b\\"]: \\"updated\\", - foo() {}, - \\"a-b-c\\": 3, - }, - };" - `); - }); }); diff --git a/test/object.test.ts b/test/object.test.ts new file mode 100644 index 0000000..2e5d024 --- /dev/null +++ b/test/object.test.ts @@ -0,0 +1,53 @@ +import { describe, expect, it } from "vitest"; +import { parseCode } from "../src"; +import { generate } from "./_utils"; + +describe("object", () => { + it("object property", () => { + const mod = parseCode( + ` +export default { + foo: { + ['a']: 1, + ['a-b']: 2, + foo() {} + } +} + `.trim() + ); + + expect(mod.exports.default.foo.a).toBe(1); + expect(mod.exports.default.foo["a-b"]).toBe(2); + expect(Object.keys(mod.exports.default.foo)).toMatchInlineSnapshot(` + [ + "a", + "a-b", + "foo", + ] + `); + + mod.exports.default.foo["a-b-c"] = 3; + + expect(Object.keys(mod.exports.default.foo)).toMatchInlineSnapshot(` + [ + "a", + "a-b", + "foo", + "a-b-c", + ] + `); + + mod.exports.default.foo["a-b"] = "updated"; + + expect(generate(mod)).toMatchInlineSnapshot(` + "export default { + foo: { + [\\"a\\"]: 1, + [\\"a-b\\"]: \\"updated\\", + foo() {}, + \\"a-b-c\\": 3, + }, + };" + `); + }); +});