Skip to content

Commit

Permalink
feat: close #4
Browse files Browse the repository at this point in the history
  • Loading branch information
Bluzzi committed Apr 30, 2024
1 parent 600dff1 commit b34bdb9
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 27 deletions.
11 changes: 8 additions & 3 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { eslintConfig } from "./dist/index.js";

export default eslintConfig({
typescript: { tsconfigPath: `./tsconfig.json` },
});
export default eslintConfig(
{
typescript: { tsconfigPath: `./tsconfig.json` },
},
{
rules: { "ts/no-explicit-any": "off" },
},
);
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
"lint:fix": "eslint . --fix",
"ts:check": "tsc --noEmit"
},
"peerDependencies": {
"eslint": ">=9.0.0"
},
"dependencies": {
"@eslint/js": "^9.1.1",
"@stylistic/eslint-plugin": "^1.7.2",
Expand Down
9 changes: 6 additions & 3 deletions scripts/typegen.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import type { Awaitable, TypedFlatConfigItem } from "#/types/type";
import { flatConfigsToRulesDTS } from "eslint-typegen/core";
import { builtinRules } from "eslint/use-at-your-own-risk";
import { javascript, node, stylistic, typescript } from "../src";
import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { cwd } from "node:process";
import { javascript } from "#/configs/javascript";
import { typescript } from "#/configs/typescript";
import { stylistic } from "#/configs/stylistic";
import { node } from "#/configs/node";

/**
* Combine array and non-array configs into a single array.
*/
export async function combine(...configs: Awaitable<TypedFlatConfigItem | TypedFlatConfigItem[]>[]): Promise<TypedFlatConfigItem[]> {
export const combine = async (...configs: Awaitable<TypedFlatConfigItem | TypedFlatConfigItem[]>[]): Promise<TypedFlatConfigItem[]> => {
const resolved = await Promise.all(configs);
return resolved.flat();
}
};

const configs = await combine(
{ plugins: { "": { rules: Object.fromEntries(builtinRules.entries()) } } },
Expand Down
4 changes: 2 additions & 2 deletions src/configs/ignore/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { TypedFlatConfigItem } from "#/types/type";
import { configName } from "#/utils/naming";

export function ignore(): TypedFlatConfigItem[] {
export const ignore = (): TypedFlatConfigItem[] => {
return [
{
name: configName("ignore", "rules"),
Expand Down Expand Up @@ -41,4 +41,4 @@ export function ignore(): TypedFlatConfigItem[] {
],
},
];
}
};
5 changes: 2 additions & 3 deletions src/configs/javascript/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { configName } from "#/utils/naming";
import globals from "globals";
import js from "@eslint/js";

export function javascript(): TypedFlatConfigItem[] {
export const javascript = (): TypedFlatConfigItem[] => {
const recommendedRules = js.configs.recommended.rules;

return [{
Expand Down Expand Up @@ -80,7 +80,6 @@ export function javascript(): TypedFlatConfigItem[] {
"no-useless-constructor": "error",
"no-useless-return": "error",
"no-var": "error",
"no-void": "error", // TODO: not sure about it (conflict with TS rules?)
"prefer-arrow-callback": "error",
"prefer-const": "error",
"prefer-object-has-own": "error",
Expand All @@ -93,4 +92,4 @@ export function javascript(): TypedFlatConfigItem[] {
"yoda": "error",
},
}];
}
};
4 changes: 2 additions & 2 deletions src/configs/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { TypedFlatConfigItem } from "#/types/type";
import { nodePlugin } from "#/utils/extension";
import { configName } from "#/utils/naming";

export function node(): TypedFlatConfigItem[] {
export const node = (): TypedFlatConfigItem[] => {
return [
{
name: configName("node", "rules"),
Expand All @@ -16,4 +16,4 @@ export function node(): TypedFlatConfigItem[] {
},
},
];
}
};
4 changes: 2 additions & 2 deletions src/configs/stylistic/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { TypedFlatConfigItem } from "#/types/type";
import { antfuPlugin, stylisticPlugin } from "#/utils/extension";
import { configName } from "#/utils/naming";

export function stylistic({ indent = 2, quotes = "double", semi = true }: ParamsStylistic = {}): TypedFlatConfigItem[] {
export const stylistic = ({ indent = 2, quotes = "double", semi = true }: ParamsStylistic = {}): TypedFlatConfigItem[] => {
const config = stylisticPlugin.configs.customize({ pluginName: "style", indent, quotes, semi });

return [{
Expand All @@ -29,4 +29,4 @@ export function stylistic({ indent = 2, quotes = "double", semi = true }: Params
"antfu/consistent-list-newline": "error",
},
}];
}
};
6 changes: 2 additions & 4 deletions src/configs/typescript/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { typescriptParser, typescriptPlugin } from "#/utils/extension";
import { configName, renameRules } from "#/utils/naming";
import { cwd } from "node:process";

export function typescript({ tsconfigPath }: ParamsTS = {}): TypedFlatConfigItem[] {
export const typescript = ({ tsconfigPath }: ParamsTS = {}): TypedFlatConfigItem[] => {
const isTypeChecked = Boolean(tsconfigPath);

const recommendedRules = isTypeChecked
Expand Down Expand Up @@ -49,8 +49,6 @@ export function typescript({ tsconfigPath }: ParamsTS = {}): TypedFlatConfigItem
"ts/explicit-member-accessibility": "error",
"ts/explicit-module-boundary-types": "error",
"ts/no-invalid-void-type": "off", // TODO: for undefined generics types (temporary?)
"no-magic-numbers": "off",
"ts/no-magic-numbers": "error",
"ts/prefer-enum-initializers": "error",
"ts/prefer-find": "error",
"ts/prefer-readonly": "error",
Expand All @@ -63,4 +61,4 @@ export function typescript({ tsconfigPath }: ParamsTS = {}): TypedFlatConfigItem
},
},
];
}
};
10 changes: 5 additions & 5 deletions src/utils/factory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConfigNames, OptionsConfig, TypedFlatConfigItem } from "#/types/type";
import type { Awaitable, ConfigNames, OptionsConfig, TypedFlatConfigItem } from "#/types/type";
import type { Linter } from "eslint";
import { isPackageExists } from "local-pkg";
import { FlatConfigComposer } from "eslint-flat-config-utils";
Expand All @@ -9,10 +9,10 @@ import { node } from "#/configs/node";
import { logger } from "#/utils/logger";
import { ignore } from "#/configs/ignore";

export async function eslintConfig(
export const eslintConfig = async (
options: OptionsConfig = {},
...userConfigs: (TypedFlatConfigItem | TypedFlatConfigItem[] | FlatConfigComposer<any, any> | Linter.FlatConfig[])[]
): FlatConfigComposer<TypedFlatConfigItem, ConfigNames> {
...userConfigs: Awaitable<TypedFlatConfigItem | TypedFlatConfigItem[] | FlatConfigComposer<any, any> | Linter.FlatConfig[]>[]
): Promise<FlatConfigComposer<TypedFlatConfigItem, ConfigNames>> => {
const configs: (TypedFlatConfigItem[])[] = [];

const enabled = {
Expand Down Expand Up @@ -53,4 +53,4 @@ export async function eslintConfig(
});

return composer;
}
};
7 changes: 4 additions & 3 deletions src/utils/naming.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const configName = (name: string, category: "rules" | "parsers" | "plugins") => `we-use/${name}/${category}`;
export const configName = (name: string, category: "rules" | "parsers" | "plugins"): string => `we-use/${name}/${category}`;

/**
* Rename plugin prefixes in a rule object.
Expand All @@ -18,14 +18,15 @@ export const configName = (name: string, category: "rules" | "parsers" | "plugin
* }]
* ```
*/
export function renameRules(rules: Record<string, any>, map: Record<string, string>) {
export const renameRules = (rules: Record<string, any>, map: Record<string, string>): Record<string, string> => {
return Object.fromEntries(
Object.entries(rules)
.map(([key, value]) => {
for (const [from, to] of Object.entries(map)) {
if (key.startsWith(`${from}/`)) return [to + key.slice(from.length), value];
}

return [key, value];
}),
);
}
};

0 comments on commit b34bdb9

Please sign in to comment.