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 {
/** 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<{
"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>;
export interface CustomRule {

12
dist/package.json vendored
View File

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

@ -1 +1 @@
Subproject commit c9b5626ee69bd529c7e391e40928a4fb28dce179
Subproject commit 3dfad602a05b4b3812a4d3fc681051932f86e838

@ -1 +1 @@
Subproject commit 5aad5f1c419b3143ffb9356bd299fc50dc576ee5
Subproject commit 6744257b43560181412a76eadeb7de564b886ad4

View File

@ -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';

View File

@ -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: {

View File

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