From 409d9ee88ca7569c11f4252feb394dab6896af2d Mon Sep 17 00:00:00 2001 From: jimmy-guzman Date: Fri, 8 Nov 2024 17:41:16 -0600 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20dynamically=20import=20`jes?= =?UTF-8?q?t`=20plugin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/configs/testing.spec.ts | 8 ++++---- src/configs/testing.ts | 13 +++++++------ src/factory.ts | 20 ++++++++++++-------- src/utils.spec.ts | 18 +++++++++++------- src/utils.ts | 20 +++++++------------- 5 files changed, 41 insertions(+), 38 deletions(-) diff --git a/src/configs/testing.spec.ts b/src/configs/testing.spec.ts index 81595ff..d12fc28 100644 --- a/src/configs/testing.spec.ts +++ b/src/configs/testing.spec.ts @@ -2,8 +2,8 @@ import { ALLOWED_VITEST_FUNCS } from "../constants"; import { testingConfig } from "./testing"; describe("testingConfig", () => { - it("should create default config w/ vitest overrides", () => { - const [, vitest] = testingConfig({}, false); + it("should create default config w/ vitest overrides", async () => { + const [, vitest] = await testingConfig({}, false); expect(vitest?.rules).toStrictEqual( expect.objectContaining({ @@ -18,8 +18,8 @@ describe("testingConfig", () => { ); }); - it("should create default config w/o vitest overrides", () => { - const [, jest] = testingConfig({ framework: "jest" }, false); + it("should create default config w/o vitest overrides", async () => { + const [, jest] = await testingConfig({ framework: "jest" }, false); expect(jest?.rules).toStrictEqual( expect.not.objectContaining({ diff --git a/src/configs/testing.ts b/src/configs/testing.ts index b506547..7d897b5 100644 --- a/src/configs/testing.ts +++ b/src/configs/testing.ts @@ -1,15 +1,16 @@ -import jest from "eslint-plugin-jest"; - import type { Rules, TestingOptions } from "../types"; import { ALLOWED_VITEST_FUNCS, GLOB_E2E, GLOB_TESTS } from "../constants"; import { hasJest, hasVitest } from "../has-dep"; import { jestRules } from "../rules/jest"; +import { interopDefault } from "../utils"; -export const testingConfig = ( +export const testingConfig = async ( { framework = "vitest" }: TestingOptions = {}, autoDetect = true, ) => { + const jestPlugin = await interopDefault(import("eslint-plugin-jest")); + const isVitest = autoDetect ? hasVitest() : framework === "vitest"; const isJest = framework === "jest" || (autoDetect && hasJest()); @@ -17,14 +18,14 @@ export const testingConfig = ( { files: GLOB_TESTS, name: "jimmy.codes/testing", - ...jest.configs["flat/recommended"], + ...jestPlugin.configs["flat/recommended"], }, ...(isVitest ? [ { files: GLOB_TESTS, name: "jimmy.codes/testing/vitest", - ...jest.configs["flat/recommended"], + ...jestPlugin.configs["flat/recommended"], rules: { ...jestRules, "jest/no-deprecated-functions": "off", @@ -43,7 +44,7 @@ export const testingConfig = ( { files: GLOB_TESTS, name: "jimmy.codes/testing/jest", - ...jest.configs["flat/recommended"], + ...jestPlugin.configs["flat/recommended"], rules: jestRules, }, ] diff --git a/src/factory.ts b/src/factory.ts index 3bf3b72..fed51a5 100644 --- a/src/factory.ts +++ b/src/factory.ts @@ -26,10 +26,11 @@ import { hasTypescript, } from "./has-dep"; import { - includeTanstackQuery, - includeTestingLibrary, - testingOptions, - typescriptOptions, + addTanstackQuery, + addTestingLibrary, + getReactOptions, + getTestingOptions, + getTypescriptOptions, } from "./utils"; export const jimmyDotCodes = async ( @@ -44,14 +45,17 @@ export const jimmyDotCodes = async ( }: Options = {}, ...moreConfigs: Linter.Config[] | TypedConfigItem[] ) => { + const reactOptions = getReactOptions(react); + const testingOptions = getTestingOptions(testing); + const typescriptOptions = getTypescriptOptions(typescript); const isTypescriptEnabled = typescript || (autoDetect && hasTypescript()); const isReactEnabled = react || (autoDetect && hasReact()); const isTestingEnabled = testing || (autoDetect && hasTesting()); const isAstroEnabled = astro || (autoDetect && hasAstro()); const isTanstackQueryEnabled = - includeTanstackQuery(react) || (autoDetect && hasReactQuery()); + addTanstackQuery(reactOptions) || (autoDetect && hasReactQuery()); const isTestingLibraryEnabled = - includeTestingLibrary(testing) || (autoDetect && hasTestingLibrary()); + addTestingLibrary(testingOptions) || (autoDetect && hasTestingLibrary()); return [ javascriptConfig(), @@ -60,11 +64,11 @@ export const jimmyDotCodes = async ( unicornConfig(), eslintCommentsConfig(), importsConfig({ typescript: isTypescriptEnabled }), - isTypescriptEnabled ? typescriptConfig(typescriptOptions(typescript)) : [], + isTypescriptEnabled ? typescriptConfig(typescriptOptions) : [], isReactEnabled ? await reactConfig() : [], isTanstackQueryEnabled ? await tanstackQuery() : [], isAstroEnabled ? await astroConfig() : [], - isTestingEnabled ? testingConfig(testingOptions(testing), autoDetect) : [], + isTestingEnabled ? await testingConfig(testingOptions, autoDetect) : [], isTestingLibraryEnabled ? await testingLibrary() : [], prettierConfig(), commonjsConfig(), diff --git a/src/utils.spec.ts b/src/utils.spec.ts index 3dfbf0a..8c38d77 100644 --- a/src/utils.spec.ts +++ b/src/utils.spec.ts @@ -1,14 +1,18 @@ -import { reactOptions, testingOptions, typescriptOptions } from "./utils"; +import { + getReactOptions, + getTestingOptions, + getTypescriptOptions, +} from "./utils"; describe("getTypescriptOptions", () => { it("should return default options when a boolean is provided", () => { - expect(typescriptOptions(true)).toStrictEqual({ + expect(getTypescriptOptions(true)).toStrictEqual({ project: "./tsconfig.json", }); }); it("should return override options when an object is provided", () => { expect( - typescriptOptions({ + getTypescriptOptions({ project: "./tsconfig.eslint.json", }), ).toStrictEqual({ @@ -19,13 +23,13 @@ describe("getTypescriptOptions", () => { describe("getTestingOptions", () => { it("should return default options when a boolean is provided", () => { - expect(testingOptions(true)).toStrictEqual({ + expect(getTestingOptions(true)).toStrictEqual({ framework: "vitest", }); }); it("should return override options when an object is provided", () => { expect( - testingOptions({ + getTestingOptions({ framework: "jest", utilities: ["testing-library"], }), @@ -38,11 +42,11 @@ describe("getTestingOptions", () => { describe("getReactOptions", () => { it("should return default options when a boolean is provided", () => { - expect(reactOptions(true)).toStrictEqual({ utilities: [] }); + expect(getReactOptions(true)).toStrictEqual({ utilities: [] }); }); it("should return override options when an object is provided", () => { expect( - reactOptions({ + getReactOptions({ utilities: ["@tanstack/query"], }), ).toStrictEqual({ diff --git a/src/utils.ts b/src/utils.ts index ead2847..b0c31d3 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,6 @@ import type { ReactOptions, TestingOptions, TypescriptOptions } from "./types"; -export const typescriptOptions = (options: boolean | TypescriptOptions) => { +export const getTypescriptOptions = (options: boolean | TypescriptOptions) => { return typeof options === "object" ? options : { @@ -8,7 +8,7 @@ export const typescriptOptions = (options: boolean | TypescriptOptions) => { }; }; -export const testingOptions = (options: boolean | TestingOptions) => { +export const getTestingOptions = (options: boolean | TestingOptions) => { return typeof options === "object" ? options : { @@ -16,22 +16,16 @@ export const testingOptions = (options: boolean | TestingOptions) => { }; }; -export const reactOptions = (options: boolean | ReactOptions) => { +export const getReactOptions = (options: boolean | ReactOptions) => { return typeof options === "object" ? options : { utilities: [] }; }; -export const includeTanstackQuery = (options: boolean | ReactOptions) => { - return ( - typeof options === "object" && - Boolean(options.utilities?.includes("@tanstack/query")) - ); +export const addTanstackQuery = (options: ReactOptions) => { + return Boolean(options.utilities?.includes("@tanstack/query")); }; -export const includeTestingLibrary = (options: boolean | TestingOptions) => { - return ( - typeof options === "object" && - Boolean(options.utilities?.includes("testing-library")) - ); +export const addTestingLibrary = (options: TestingOptions) => { + return Boolean(options.utilities?.includes("testing-library")); }; /* v8 ignore start */