Skip to content

Commit

Permalink
feat: ✨ dynamically import astro plugin and parser
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmy-guzman committed Nov 8, 2024
1 parent f6667f1 commit 1d982ef
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 44 deletions.
6 changes: 4 additions & 2 deletions scripts/typegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import fs from "node:fs/promises";
import { builtinRules } from "eslint/use-at-your-own-risk";
import { flatConfigsToRulesDTS } from "eslint-typegen/core";

import config from "../src";
import jimmyDotCodes from "../src";

const configs = await jimmyDotCodes();

const ruleDts = await flatConfigsToRulesDTS(
[
Expand All @@ -15,7 +17,7 @@ const ruleDts = await flatConfigsToRulesDTS(
},
},
// @ts-expect-error TODO: config types don't seem to match
...config(),
...configs,
],
{
includeAugmentation: false,
Expand Down
9 changes: 6 additions & 3 deletions src/configs/astro.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import parserAstro from "astro-eslint-parser";
import pluginAstro from "eslint-plugin-astro";
import jsxA11y from "eslint-plugin-jsx-a11y";
import globals from "globals";
import { configs, parser as parserTs } from "typescript-eslint";
Expand All @@ -8,9 +6,14 @@ import type { TypedConfigItem } from "../types";

import { GLOB_ASTRO } from "../constants";

export const astroConfig = () => {
export const astroConfig = async () => {
const files = [GLOB_ASTRO];

const [pluginAstro, parserAstro] = await Promise.all([
import("eslint-plugin-astro"),
import("astro-eslint-parser"),
]);

return [
{
files,
Expand Down
80 changes: 43 additions & 37 deletions src/factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,52 +15,54 @@ describe("jimmyDotCodes", () => {
"prettier",
"ignores",
"javascript",
])("should create configuration w/ %s", (input) => {
expect(jimmyDotCodes({ autoDetect: false })).toStrictEqual(
])("should create configuration w/ %s", async (input) => {
await expect(jimmyDotCodes({ autoDetect: false })).resolves.toStrictEqual(
expect.arrayContaining([
expect.objectContaining({ name: `jimmy.codes/${input}` }),
]),
);
});
});

it("should create configuration w/ typescript", () => {
expect(
it("should create configuration w/ typescript", async () => {
await expect(
jimmyDotCodes({ autoDetect: false, typescript: true }),
).toStrictEqual(
).resolves.toStrictEqual(
expect.arrayContaining([
expect.objectContaining({ name: "jimmy.codes/typescript" }),
expect.objectContaining({ name: "jimmy.codes/imports/typescript" }),
]),
);
});

it("should create configuration w/ react", () => {
expect(jimmyDotCodes({ autoDetect: false, react: true })).toStrictEqual(
it("should create configuration w/ react", async () => {
await expect(
jimmyDotCodes({ autoDetect: false, react: true }),
).resolves.toStrictEqual(
expect.arrayContaining([
expect.objectContaining({ name: "jimmy.codes/react" }),
]),
);
});

it("should create configuration w/ react & @tanstack/query", () => {
expect(
it("should create configuration w/ react & @tanstack/query", async () => {
await expect(
jimmyDotCodes({
autoDetect: false,
react: { utilities: ["@tanstack/query"] },
}),
).toStrictEqual(
).resolves.toStrictEqual(
expect.arrayContaining([
expect.objectContaining({ name: "jimmy.codes/react" }),
expect.objectContaining({ name: "jimmy.codes/react/query" }),
]),
);
});

it("should create configuration w/ jest", () => {
expect(
it("should create configuration w/ jest", async () => {
await expect(
jimmyDotCodes({ autoDetect: false, testing: { framework: "jest" } }),
).toStrictEqual(
).resolves.toStrictEqual(
expect.arrayContaining([
expect.objectContaining({ name: "jimmy.codes/testing" }),
expect.objectContaining({ name: "jimmy.codes/testing/disabled" }),
Expand All @@ -70,8 +72,10 @@ describe("jimmyDotCodes", () => {
);
});

it("should create configuration w/ vitest", () => {
expect(jimmyDotCodes({ autoDetect: false, testing: true })).toStrictEqual(
it("should create configuration w/ vitest", async () => {
await expect(
jimmyDotCodes({ autoDetect: false, testing: true }),
).resolves.toStrictEqual(
expect.arrayContaining([
expect.objectContaining({ name: "jimmy.codes/testing" }),
expect.objectContaining({ name: "jimmy.codes/testing/disabled" }),
Expand All @@ -81,14 +85,14 @@ describe("jimmyDotCodes", () => {
);
});

it("should create configuration w/ jest & react & testing library", () => {
expect(
it("should create configuration w/ jest & react & testing library", async () => {
await expect(
jimmyDotCodes({
autoDetect: false,
react: true,
testing: { framework: "jest", utilities: ["testing-library"] },
}),
).toStrictEqual(
).resolves.toStrictEqual(
expect.arrayContaining([
expect.objectContaining({ name: "jimmy.codes/testing" }),
expect.objectContaining({ name: "jimmy.codes/testing/disabled" }),
Expand All @@ -104,14 +108,14 @@ describe("jimmyDotCodes", () => {
);
});

it("should create configuration w/ vitest & react & testing library", () => {
expect(
it("should create configuration w/ vitest & react & testing library", async () => {
await expect(
jimmyDotCodes({
autoDetect: false,
react: true,
testing: { utilities: ["testing-library"] },
}),
).toStrictEqual(
).resolves.toStrictEqual(
expect.arrayContaining([
expect.objectContaining({ name: "jimmy.codes/testing" }),
expect.objectContaining({ name: "jimmy.codes/testing/disabled" }),
Expand All @@ -127,8 +131,10 @@ describe("jimmyDotCodes", () => {
);
});

it("should create configuration w/ astro", () => {
expect(jimmyDotCodes({ astro: true, autoDetect: false })).toStrictEqual(
it("should create configuration w/ astro", async () => {
await expect(
jimmyDotCodes({ astro: true, autoDetect: false }),
).resolves.toStrictEqual(
expect.arrayContaining([
expect.objectContaining({
name: "jimmy.codes/astro",
Expand All @@ -144,12 +150,12 @@ describe("jimmyDotCodes", () => {
});

describe("autoDetect", () => {
it("should include typescript when auto detection is enabled", () => {
it("should include typescript when auto detection is enabled", async () => {
vi.mocked(isPackageExists).mockImplementation((name) => {
return name === "typescript";
});

expect(jimmyDotCodes({ autoDetect: true })).toStrictEqual(
await expect(jimmyDotCodes({ autoDetect: true })).resolves.toStrictEqual(
expect.arrayContaining([
expect.objectContaining({ name: "jimmy.codes/typescript" }),
expect.not.objectContaining({ name: "jimmy.codes/testing" }),
Expand All @@ -164,12 +170,12 @@ describe("jimmyDotCodes", () => {
);
});

it("should include react when auto detection is enabled", () => {
it("should include react when auto detection is enabled", async () => {
vi.mocked(isPackageExists).mockImplementation((name) => {
return name === "react";
});

expect(jimmyDotCodes({ autoDetect: true })).toStrictEqual(
await expect(jimmyDotCodes({ autoDetect: true })).resolves.toStrictEqual(
expect.arrayContaining([
expect.not.objectContaining({ name: "jimmy.codes/typescript" }),
expect.not.objectContaining({ name: "jimmy.codes/testing" }),
Expand All @@ -184,13 +190,13 @@ describe("jimmyDotCodes", () => {
);
});

it("should include react-query when auto detection is enabled", () => {
it("should include react-query when auto detection is enabled", async () => {
vi.mocked(isPackageExists).mockImplementation((name) => {
// eslint-disable-next-line jest/no-conditional-in-test -- this condition is only for the mock.
return name === "react" || name === "@tanstack/react-query";
});

expect(jimmyDotCodes({ autoDetect: true })).toStrictEqual(
await expect(jimmyDotCodes({ autoDetect: true })).resolves.toStrictEqual(
expect.arrayContaining([
expect.not.objectContaining({ name: "jimmy.codes/typescript" }),
expect.not.objectContaining({ name: "jimmy.codes/testing" }),
Expand All @@ -205,12 +211,12 @@ describe("jimmyDotCodes", () => {
);
});

it("should include vitest when auto detection is enabled", () => {
it("should include vitest when auto detection is enabled", async () => {
vi.mocked(isPackageExists).mockImplementation((name) => {
return name === "vitest";
});

expect(jimmyDotCodes({ autoDetect: true })).toStrictEqual(
await expect(jimmyDotCodes({ autoDetect: true })).resolves.toStrictEqual(
expect.arrayContaining([
expect.not.objectContaining({ name: "jimmy.codes/typescript" }),
expect.objectContaining({ name: "jimmy.codes/testing" }),
Expand All @@ -225,12 +231,12 @@ describe("jimmyDotCodes", () => {
);
});

it("should include jest when auto detection is enabled", () => {
it("should include jest when auto detection is enabled", async () => {
vi.mocked(isPackageExists).mockImplementation((name) => {
return name === "jest";
});

expect(jimmyDotCodes({ autoDetect: true })).toStrictEqual(
await expect(jimmyDotCodes({ autoDetect: true })).resolves.toStrictEqual(
expect.arrayContaining([
expect.not.objectContaining({ name: "jimmy.codes/typescript" }),
expect.objectContaining({ name: "jimmy.codes/testing" }),
Expand All @@ -245,13 +251,13 @@ describe("jimmyDotCodes", () => {
);
});

it("should include test-library when auto detection is enabled", () => {
it("should include test-library when auto detection is enabled", async () => {
vi.mocked(isPackageExists).mockImplementation((name) => {
// eslint-disable-next-line jest/no-conditional-in-test -- this condition is only for the mock.
return name === "@testing-library/react" || name === "vitest";
});

expect(jimmyDotCodes({ autoDetect: true })).toStrictEqual(
await expect(jimmyDotCodes({ autoDetect: true })).resolves.toStrictEqual(
expect.arrayContaining([
expect.not.objectContaining({ name: "jimmy.codes/typescript" }),
expect.objectContaining({ name: "jimmy.codes/testing" }),
Expand All @@ -266,12 +272,12 @@ describe("jimmyDotCodes", () => {
);
});

it("should include astro when auto detection is enabled", () => {
it("should include astro when auto detection is enabled", async () => {
vi.mocked(isPackageExists).mockImplementation((name) => {
return name === "astro";
});

expect(jimmyDotCodes({ autoDetect: true })).toStrictEqual(
await expect(jimmyDotCodes({ autoDetect: true })).resolves.toStrictEqual(
expect.arrayContaining([
expect.objectContaining({
name: "jimmy.codes/astro",
Expand Down
4 changes: 2 additions & 2 deletions src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { unicornConfig } from "./configs/unicorn";
import { hasAstro, hasReact, hasTesting, hasTypescript } from "./has-dep";
import { reactOptions, testingOptions, typescriptOptions } from "./utils";

export const jimmyDotCodes = (
export const jimmyDotCodes = async (
{
astro = false,
autoDetect = true,
Expand All @@ -44,7 +44,7 @@ export const jimmyDotCodes = (
importsConfig({ typescript: isTypescriptEnabled }),
isTypescriptEnabled ? typescriptConfig(typescriptOptions(typescript)) : [],
isReactEnabled ? reactConfig(reactOptions(react), autoDetect) : [],
isAstroEnabled ? astroConfig() : [],
isAstroEnabled ? await astroConfig() : [],
isTestingEnabled ? testingConfig(testingOptions(testing), autoDetect) : [],
prettierConfig(),
commonjsConfig(),
Expand Down

0 comments on commit 1d982ef

Please sign in to comment.