Fix local rules

This commit is contained in:
Alex
2024-08-08 02:59:28 -04:00
parent 25695599aa
commit afb93c4971
9 changed files with 41 additions and 36 deletions

6
dist/index.d.ts vendored
View File

@ -32,16 +32,16 @@ export type RuleEntry<Options> = RuleLevel | [
]; ];
export interface LocalRuleOptions { export interface LocalRuleOptions {
/** Bans import from the specifier '.' and '..' and replaces it with '.+/index' */ /** 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 * Enforce template literal expressions to be of `string` type
* @see [restrict-template-expressions](https://typescript-eslint.io/rules/restrict-template-expressions) * @see [restrict-template-expressions](https://typescript-eslint.io/rules/restrict-template-expressions)
*/ */
"rules/restrict-template-expressions": RuleEntry<{ "custom/restrict-template-expressions": RuleEntry<{
allow: string[]; allow: string[];
}>; }>;
/** Ban assignment of empty object literals `{}` and replace them with `Object.create(null)` */ /** 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>; export type RuleOptions = Rules & Partial<LocalRuleOptions>;
export interface CustomRule { export interface CustomRule {

12
dist/package.json vendored
View File

@ -1,6 +1,6 @@
{ {
"name": "@aet/eslint-rules", "name": "@aet/eslint-rules",
"version": "1.0.1-beta.20", "version": "1.0.1-beta.22",
"license": "UNLICENSED", "license": "UNLICENSED",
"bin": { "bin": {
"eslint-install": "install.js", "eslint-install": "install.js",
@ -17,10 +17,10 @@
"@eslint-community/eslint-utils": "^4.4.0", "@eslint-community/eslint-utils": "^4.4.0",
"@tanstack/eslint-plugin-query": "^5.51.15", "@tanstack/eslint-plugin-query": "^5.51.15",
"@types/eslint": "^9.6.0", "@types/eslint": "^9.6.0",
"@typescript-eslint/eslint-plugin": "^8.0.0", "@typescript-eslint/eslint-plugin": "^8.0.1",
"@typescript-eslint/parser": "^8.0.0", "@typescript-eslint/parser": "^8.0.1",
"@typescript-eslint/type-utils": "^8.0.0", "@typescript-eslint/type-utils": "^8.0.1",
"@typescript-eslint/utils": "^8.0.0", "@typescript-eslint/utils": "^8.0.1",
"@eslint-react/eslint-plugin": "1.7.1", "@eslint-react/eslint-plugin": "1.7.1",
"aria-query": "^5.3.0", "aria-query": "^5.3.0",
"axe-core": "^4.10.0", "axe-core": "^4.10.0",
@ -35,7 +35,7 @@
"eslint-module-utils": "^2.8.1", "eslint-module-utils": "^2.8.1",
"eslint-plugin-es-x": "^8.0.0", "eslint-plugin-es-x": "^8.0.0",
"eslint-plugin-import-x": "^3.1.0", "eslint-plugin-import-x": "^3.1.0",
"eslint-plugin-jsdoc": "^48.11.0", "eslint-plugin-jsdoc": "^50.0.0",
"eslint-plugin-react-refresh": "^0.4.9", "eslint-plugin-react-refresh": "^0.4.9",
"eslint-plugin-unicorn": "^55.0.0", "eslint-plugin-unicorn": "^55.0.0",
"esprima": "^4.0.1", "esprima": "^4.0.1",

Submodule packages/eslint-import-resolver-typescript updated: c9b5626ee6...3dfad602a0

Submodule packages/eslint-plugin-n updated: 5aad5f1c41...6744257b43

View File

@ -1,10 +1,9 @@
import * as fs from 'node:fs'; import * as fs from 'node:fs';
import { resolve } from 'node:path'; import { resolve } from 'node:path';
import { storybook } from './index';
import { react, reactRefresh } from './presets/react'; import { react, reactRefresh } from './presets/react';
import { tailwind } from './presets/tailwind'; import { tailwind } from './presets/tailwind';
import { jsdoc } from './presets/jsdoc'; import { jsdoc } from './presets/jsdoc';
import { reactQuery } from './presets/misc'; import { storybook, reactQuery } from './presets/misc';
import { testingLibrary } from './presets/testing-library'; import { testingLibrary } from './presets/testing-library';
import type { Middleware } from './middleware'; import type { Middleware } from './middleware';

View File

@ -9,7 +9,7 @@ import type { Middleware, MiddlewareConfig, MiddlewareFunctions } from './middle
import { importTypeScript } from './presets/typescript'; import { importTypeScript } from './presets/typescript';
import { unicorn } from './presets/unicorn'; import { unicorn } from './presets/unicorn';
import { eslintRules } from './presets/eslint'; import { eslintRules } from './presets/eslint';
import { local } from './presets/local'; import { custom } from './presets/custom';
import { error, warn, off } from './constants'; import { error, warn, off } from './constants';
import { checkEnv } from './env'; import { checkEnv } from './env';
@ -41,14 +41,14 @@ type RuleEntry<Options> = RuleLevel | [RuleLevel, Partial<Options>];
export interface LocalRuleOptions { export interface LocalRuleOptions {
/** Bans import from the specifier '.' and '..' and replaces it with '.+/index' */ /** 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 * Enforce template literal expressions to be of `string` type
* @see [restrict-template-expressions](https://typescript-eslint.io/rules/restrict-template-expressions) * @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)` */ /** 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>; export type RuleOptions = Rules & Partial<LocalRuleOptions>;
@ -121,25 +121,27 @@ export function extendConfig(
customRuleFiles, customRuleFiles,
parserOptions, parserOptions,
middlewares: _middlewares = [], middlewares: _middlewares = [],
// @ts-expect-error
localRules: _,
...rest ...rest
} = of; } = of;
let plugins: Plugin[] = [..._plugins]; let plugins: Plugin[] = [..._plugins];
let extend: Extends[] = ensureArray(_extends); let extend: Extends[] = ensureArray(_extends);
if (customRuleFiles != null) {
plugins.push('local');
}
const middlewares: Middleware[] = uniq([ const middlewares: Middleware[] = uniq([
importTypeScript, importTypeScript,
unicorn, unicorn,
local, custom,
...(auto ? checkEnv() : []), ...(auto ? checkEnv() : []),
..._middlewares, ..._middlewares,
]); ]);
const result: MiddlewareConfig = { const result: MiddlewareConfig = {
root: true, root: true,
plugins: unique('rules', plugins), plugins: unique('custom', plugins),
env: { node: true, browser: true, es2023: true }, env: { node: true, browser: true, es2023: true },
reportUnusedDisableDirectives: true, reportUnusedDisableDirectives: true,
parserOptions: { parserOptions: {

View File

@ -1,6 +1,6 @@
import type { ESLint } from 'eslint'; import type { ESLint } from 'eslint';
import * as fs from 'node:fs'; 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 { glob } from 'fast-glob';
import { parseModule } from 'esprima'; import { parseModule } from 'esprima';
import query from 'esquery'; import query from 'esquery';
@ -9,6 +9,7 @@ import type { Node, Property } from 'estree';
// https://github.com/gulpjs/interpret // https://github.com/gulpjs/interpret
const transpilers = [ const transpilers = [
'esbuild-register', 'esbuild-register',
'tsx',
'ts-node/register/transpile-only', 'ts-node/register/transpile-only',
'@swc/register', '@swc/register',
'sucrase/register', 'sucrase/register',
@ -64,7 +65,10 @@ function main() {
if (!customRuleFiles?.length) return; if (!customRuleFiles?.length) return;
tryRequire(); 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 module = unwrapDefault(require(file));
const name = module.name ?? basename(file, extname(file)); const name = module.name ?? basename(file, extname(file));
plugin.rules![name] = module; plugin.rules![name] = module;

12
src/presets/custom.ts Normal file
View 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);
});

View File

@ -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);
});