Fix local rules
This commit is contained in:
@ -1,10 +1,9 @@
|
||||
import * as fs from 'node:fs';
|
||||
import { resolve } from 'node:path';
|
||||
import { storybook } from './index';
|
||||
import { react, reactRefresh } from './presets/react';
|
||||
import { tailwind } from './presets/tailwind';
|
||||
import { jsdoc } from './presets/jsdoc';
|
||||
import { reactQuery } from './presets/misc';
|
||||
import { storybook, reactQuery } from './presets/misc';
|
||||
import { testingLibrary } from './presets/testing-library';
|
||||
import type { Middleware } from './middleware';
|
||||
|
||||
|
18
src/index.ts
18
src/index.ts
@ -9,7 +9,7 @@ import type { Middleware, MiddlewareConfig, MiddlewareFunctions } from './middle
|
||||
import { importTypeScript } from './presets/typescript';
|
||||
import { unicorn } from './presets/unicorn';
|
||||
import { eslintRules } from './presets/eslint';
|
||||
import { local } from './presets/local';
|
||||
import { custom } from './presets/custom';
|
||||
import { error, warn, off } from './constants';
|
||||
import { checkEnv } from './env';
|
||||
|
||||
@ -41,14 +41,14 @@ type RuleEntry<Options> = RuleLevel | [RuleLevel, Partial<Options>];
|
||||
|
||||
export interface LocalRuleOptions {
|
||||
/** Bans import from the specifier '.' and '..' and replaces it with '.+/index' */
|
||||
'rules/no-import-dot': RuleEntry<unknown>;
|
||||
'custom/no-import-dot': RuleEntry<unknown>;
|
||||
/**
|
||||
* Enforce template literal expressions to be of `string` type
|
||||
* @see [restrict-template-expressions](https://typescript-eslint.io/rules/restrict-template-expressions)
|
||||
*/
|
||||
'rules/restrict-template-expressions': RuleEntry<{ allow: string[] }>;
|
||||
'custom/restrict-template-expressions': RuleEntry<{ allow: string[] }>;
|
||||
/** Ban assignment of empty object literals `{}` and replace them with `Object.create(null)` */
|
||||
'rules/no-empty-object-literal': RuleEntry<unknown>;
|
||||
'custom/no-empty-object-literal': RuleEntry<unknown>;
|
||||
}
|
||||
|
||||
export type RuleOptions = Rules & Partial<LocalRuleOptions>;
|
||||
@ -121,25 +121,27 @@ export function extendConfig(
|
||||
customRuleFiles,
|
||||
parserOptions,
|
||||
middlewares: _middlewares = [],
|
||||
// @ts-expect-error
|
||||
localRules: _,
|
||||
...rest
|
||||
} = of;
|
||||
|
||||
let plugins: Plugin[] = [..._plugins];
|
||||
let extend: Extends[] = ensureArray(_extends);
|
||||
|
||||
if (customRuleFiles != null) {
|
||||
plugins.push('local');
|
||||
}
|
||||
|
||||
const middlewares: Middleware[] = uniq([
|
||||
importTypeScript,
|
||||
unicorn,
|
||||
local,
|
||||
custom,
|
||||
...(auto ? checkEnv() : []),
|
||||
..._middlewares,
|
||||
]);
|
||||
|
||||
const result: MiddlewareConfig = {
|
||||
root: true,
|
||||
plugins: unique('rules', plugins),
|
||||
plugins: unique('custom', plugins),
|
||||
env: { node: true, browser: true, es2023: true },
|
||||
reportUnusedDisableDirectives: true,
|
||||
parserOptions: {
|
||||
|
@ -1,6 +1,6 @@
|
||||
import type { ESLint } from 'eslint';
|
||||
import * as fs from 'node:fs';
|
||||
import { basename, extname, resolve } from 'node:path';
|
||||
import { basename, extname, isAbsolute, resolve } from 'node:path';
|
||||
import { glob } from 'fast-glob';
|
||||
import { parseModule } from 'esprima';
|
||||
import query from 'esquery';
|
||||
@ -9,6 +9,7 @@ import type { Node, Property } from 'estree';
|
||||
// https://github.com/gulpjs/interpret
|
||||
const transpilers = [
|
||||
'esbuild-register',
|
||||
'tsx',
|
||||
'ts-node/register/transpile-only',
|
||||
'@swc/register',
|
||||
'sucrase/register',
|
||||
@ -64,7 +65,10 @@ function main() {
|
||||
if (!customRuleFiles?.length) return;
|
||||
|
||||
tryRequire();
|
||||
for (const file of glob.sync(customRuleFiles)) {
|
||||
for (let file of glob.sync(customRuleFiles)) {
|
||||
if (!isAbsolute(file)) {
|
||||
file = resolve(rootDir, file);
|
||||
}
|
||||
const module = unwrapDefault(require(file));
|
||||
const name = module.name ?? basename(file, extname(file));
|
||||
plugin.rules![name] = module;
|
||||
|
12
src/presets/custom.ts
Normal file
12
src/presets/custom.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import type { LocalRuleOptions } from '../index';
|
||||
import { error } from '../constants';
|
||||
import { defineMiddleware } from '../middleware';
|
||||
|
||||
const customRules: Partial<LocalRuleOptions> = {
|
||||
'custom/no-import-dot': error,
|
||||
'custom/restrict-template-expressions': error,
|
||||
};
|
||||
|
||||
export const custom = defineMiddleware((_, { addRules }) => {
|
||||
addRules(customRules);
|
||||
});
|
@ -1,12 +0,0 @@
|
||||
import type { LocalRuleOptions } from '../index';
|
||||
import { error } from '../constants';
|
||||
import { defineMiddleware } from '../middleware';
|
||||
|
||||
const localRules: Partial<LocalRuleOptions> = {
|
||||
'rules/no-import-dot': error,
|
||||
'rules/restrict-template-expressions': error,
|
||||
};
|
||||
|
||||
export const local = defineMiddleware((_, { addRules }) => {
|
||||
addRules(localRules);
|
||||
});
|
Reference in New Issue
Block a user