Skip to content

Commit

Permalink
refactor(projects)!: support different overrides in one config
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Jul 31, 2024
1 parent cd14568 commit 683f060
Show file tree
Hide file tree
Showing 26 changed files with 283 additions and 95 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,6 @@ interface Options {
type RuleBaseOptions<T = NonNullable<unknown>> = T & {
/** The glob patterns to lint */
files?: string[];
/** Override rules */
overrides?: PartialEslintFlatRules;
};

type VueOptions = RuleBaseOptions<{
Expand Down
8 changes: 4 additions & 4 deletions README.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ interface Options {
* 被忽略的 glob
*/
ignores: string[];
/**
* 覆盖的规则
*/
overrides?: FlatConfigItem['rules'];
/**
* 默认的Prettier配置
*
Expand Down Expand Up @@ -146,10 +150,6 @@ type RuleBaseOptions<T = NonNullable<unknown>> = T & {
* 需要被检测的文件
*/
files?: string[];
/**
* 覆盖的规则
*/
overrides?: PartialEslintFlatRules;
};

type VueOptions = RuleBaseOptions<{
Expand Down
2 changes: 1 addition & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default defineConfig({
solid: { files: ['**/*solid.tsx'] },
svelte: true,
astro: true,
unocss: false,
unocss: true,
formatter: {
html: true,
css: true,
Expand Down
7 changes: 1 addition & 6 deletions example/demo.astro
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@ const { title } = Astro.props as Props;
<style is:global>
:root {
--accent: 124, 58, 237;
--accent-gradient: linear-gradient(
45deg,
rgb(var(--accent)),
#da62c4 30%,
white 60%
);
--accent-gradient: linear-gradient(45deg, rgb(var(--accent)), #da62c4 30%, white 60%);
}
html {
height: 100%;
Expand Down
2 changes: 1 addition & 1 deletion example/demo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defineOptions({
</script>

<template>
<div class="text-12px text-#fff font-bold"></div>
<div class="flex text-12px text-#fff font-bold"></div>
</template>

<style scoped></style>
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
"@eslint/js": "9.8.0",
"@typescript-eslint/eslint-plugin": "7.18.0",
"@typescript-eslint/parser": "7.18.0",
"eslint-config-flat-gitignore": "0.1.8",
"eslint-config-prettier": "9.1.0",
"eslint-parser-plain": "0.1.0",
"eslint-plugin-import-x": "3.1.0",
Expand All @@ -145,7 +146,9 @@
"@types/prompts": "2.4.9",
"@types/react": "18.3.3",
"@types/react-dom": "18.3.0",
"@unocss/eslint-config": "0.61.8",
"@unocss/core": "^0.61.9",
"@unocss/eslint-config": "0.61.9",
"@unocss/preset-uno": "^0.61.9",
"astro": "4.12.3",
"eslint": "9.8.0",
"eslint-plugin-astro": "1.2.3",
Expand Down
117 changes: 92 additions & 25 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions src/configs/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { createTsRules } from './typescript';

export async function createAstroConfig(
options?: RequiredRuleBaseOptions,
prettierRules: PartialPrettierExtendedOptions = {}
prettierRules: PartialPrettierExtendedOptions = {},
overrides: Record<string, string> = {}
) {
if (!options) return [];

const { files, overrides } = options;
const { files } = options;

await ensurePackages(['eslint-plugin-astro', 'astro-eslint-parser', 'prettier-plugin-astro']);

Expand Down
16 changes: 16 additions & 0 deletions src/configs/gitignore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { interopDefault } from '../shared';
import type { BaseOptions, FlatConfigItem } from '../types';

export async function createGitignoreRule(options?: BaseOptions['gitignore']) {
if (!options) return [];

const configs: FlatConfigItem[] = [];

const configItem = await interopDefault(import('eslint-config-flat-gitignore')).then(r => [
r(typeof options !== 'boolean' ? options : { strict: false })
]);

configs.push(...configItem);

return configs;
}
7 changes: 4 additions & 3 deletions src/configs/import.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { FlatConfigItem } from '../types';
import { interopDefault } from '../shared';
import type { FlatConfigItem } from '../types';

export async function createImportConfig() {
export async function createImportConfig(overrides: Record<string, string> = {}) {
const pluginImport = await interopDefault(import('eslint-plugin-import-x'));

const configs: FlatConfigItem[] = [
Expand All @@ -22,7 +22,8 @@ export async function createImportConfig() {
groups: ['builtin', 'external', 'internal', 'parent', 'sibling', 'index', 'object'],
pathGroups: [{ group: 'internal', pattern: '{{@,~}/,#}**' }]
}
]
],
...overrides
}
}
];
Expand Down
1 change: 1 addition & 0 deletions src/configs/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './gitignore';
export * from './javascript';
export * from './node';
export * from './import';
Expand Down
5 changes: 3 additions & 2 deletions src/configs/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import jsRules from '@eslint/js';
import { GLOB_SRC, GLOB_SRC_EXT, GLOB_TESTS } from '../constants/glob';
import type { FlatConfigItem } from '../types';

export function createJsConfig() {
export function createJsConfig(overrides: Record<string, string> = {}) {
const js: FlatConfigItem[] = [
{
languageOptions: {
Expand Down Expand Up @@ -179,7 +179,8 @@ export function createJsConfig() {
}
}
],
'unicode-bom': ['error', 'never']
'unicode-bom': ['error', 'never'],
...overrides
}
},
{
Expand Down
5 changes: 3 additions & 2 deletions src/configs/node.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { interopDefault } from '../shared';
import type { FlatConfigItem } from '../types';

export async function createNodeConfig() {
export async function createNodeConfig(overrides: Record<string, string> = {}) {
const pluginNode = await interopDefault(import('eslint-plugin-n'));

const configs: FlatConfigItem[] = [
Expand All @@ -17,7 +17,8 @@ export async function createNodeConfig() {
'n/no-path-concat': 'error',
'n/prefer-global/buffer': ['error', 'never'],
'n/prefer-global/process': ['error', 'never'],
'n/process-exit-as-throw': 'error'
'n/process-exit-as-throw': 'error',
...overrides
}
}
];
Expand Down
13 changes: 8 additions & 5 deletions src/configs/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { isPackageExists } from 'local-pkg';
import { ensurePackages, interopDefault } from '../shared';
import type { FlatConfigItem, RequiredRuleBaseOptions } from '../types';

export async function createReactConfig(options?: RequiredRuleBaseOptions) {
export async function createReactConfig(options?: RequiredRuleBaseOptions, overrides: Record<string, string> = {}) {
if (!options) return [];

const { files, overrides } = options;
const { files } = options;

await ensurePackages(['eslint-plugin-react', 'eslint-plugin-react-hooks', 'eslint-plugin-react-refresh']);

Expand Down Expand Up @@ -62,10 +62,13 @@ export async function createReactConfig(options?: RequiredRuleBaseOptions) {
return configs;
}

export async function createReactNativeConfig(options?: RequiredRuleBaseOptions) {
export async function createReactNativeConfig(
options?: RequiredRuleBaseOptions,
overrides: Record<string, string> = {}
) {
if (!options) return [];

const { files, overrides } = options;
const { files } = options;

await ensurePackages(['eslint-plugin-react-native']);

Expand All @@ -91,7 +94,7 @@ export async function createReactNativeConfig(options?: RequiredRuleBaseOptions)
},
rules: {
...pluginReactNative.configs.all.rules,
...overrides
...(overrides as any)
}
}
];
Expand Down
6 changes: 3 additions & 3 deletions src/configs/solid.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ensurePackages, interopDefault } from '../shared';
import type { FlatConfigItem, RequiredRuleBaseOptions } from '../types';

export async function createSolidConfig(options?: RequiredRuleBaseOptions) {
export async function createSolidConfig(options?: RequiredRuleBaseOptions, overrides: Record<string, string> = {}) {
if (!options) return [];

const { files, overrides } = options;
const { files } = options;

await ensurePackages(['eslint-plugin-solid']);

Expand All @@ -27,7 +27,7 @@ export async function createSolidConfig(options?: RequiredRuleBaseOptions) {
},
rules: {
...pluginSolid.configs.typescript.rules,
...overrides
...(overrides as any)
}
}
];
Expand Down
5 changes: 3 additions & 2 deletions src/configs/svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { createTsRules } from './typescript';

export async function createSvelteConfig(
options?: RequiredRuleBaseOptions,
prettierRules: PartialPrettierExtendedOptions = {}
prettierRules: PartialPrettierExtendedOptions = {},
overrides: Record<string, string> = {}
) {
if (!options) return [];

const { files, overrides } = options;
const { files } = options;

await ensurePackages(['eslint-plugin-svelte', 'svelte-eslint-parser', 'prettier-plugin-svelte']);

Expand Down
5 changes: 3 additions & 2 deletions src/configs/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export async function createTsRules(): Promise<FlatConfigItem['rules']> {
return tsRules as unknown as FlatConfigItem['rules'];
}

export async function createTsConfig() {
export async function createTsConfig(overrides: Record<string, string> = {}) {
const [pluginTs, parserTs] = await Promise.all([
interopDefault(import('@typescript-eslint/eslint-plugin')),
interopDefault(import('@typescript-eslint/parser'))
Expand All @@ -69,7 +69,8 @@ export async function createTsConfig() {
'@typescript-eslint': pluginTs
},
rules: {
...tsRules
...tsRules,
...(overrides as any)
}
},
{
Expand Down
5 changes: 3 additions & 2 deletions src/configs/unicorn.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { interopDefault } from '../shared';
import type { FlatConfigItem } from '../types';

export async function createUnicornConfig() {
export async function createUnicornConfig(overrides: Record<string, string> = {}) {
const pluginUnicorn = await interopDefault(import('eslint-plugin-unicorn'));

const configs: FlatConfigItem[] = [
Expand All @@ -22,7 +22,8 @@ export async function createUnicornConfig() {
'unicorn/prefer-number-properties': 'error',
'unicorn/prefer-string-starts-ends-with': 'error',
'unicorn/prefer-type-error': 'error',
'unicorn/throw-new-error': 'error'
'unicorn/throw-new-error': 'error',
...overrides
}
}
];
Expand Down
20 changes: 17 additions & 3 deletions src/configs/unocss.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import { ensurePackages, interopDefault } from '../shared';
import type { FlatConfigItem } from '../types';

export async function createUnocssConfig(enable?: boolean) {
export async function createUnocssConfig(enable?: boolean, overrides: Record<string, string> = {}) {
if (!enable) return [];

await ensurePackages(['@unocss/eslint-config']);

const unocss = await interopDefault(import('@unocss/eslint-config/flat'));
const pluginUnocss = await interopDefault(import('@unocss/eslint-config/flat'));

return [unocss as FlatConfigItem];
const configs: FlatConfigItem[] = [
{
plugins: {
unocss: pluginUnocss.plugins.unocss
},
rules: {
'unocss/order': 'warn',
'unocss/order-attributify': 'off',
'unocss/blocklist': 'off',
...overrides
}
}
];

return configs;
}
4 changes: 2 additions & 2 deletions src/configs/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { ensurePackages, interopDefault } from '../shared';
import type { FlatConfigItem, RequiredVueOptions } from '../types';
import { createTsRules } from './typescript';

export async function createVueConfig(options?: RequiredVueOptions) {
export async function createVueConfig(options?: RequiredVueOptions, overrides: Record<string, string> = {}) {
if (!options) return [];

const { version, files, overrides } = options;
const { version, files } = options;

await ensurePackages(['eslint-plugin-vue', 'vue-eslint-parser']);

Expand Down
Loading

0 comments on commit 683f060

Please sign in to comment.