From df60550598f39b296008ddd63bf1d83647ec08ac Mon Sep 17 00:00:00 2001 From: Alex <8125011+alex-kinokon@users.noreply.github.com> Date: Mon, 28 Aug 2023 10:31:19 -0400 Subject: [PATCH] Inline eslint-import-resolver-typescript --- .gitignore | 7 ++--- build.sh | 8 +++-- .../index.d.ts | 0 .../index.d.ts | 0 .../index.d.ts | 0 .../{react => eslint-plugin-react}/index.d.ts | 0 dist/index.d.ts | 2 +- dist/package.json | 5 ++-- esbuild.ts | 30 ++++++++++++------- package.json | 8 ++--- .../ExhaustiveDeps.ts | 0 .../RulesOfHooks.ts | 0 .../eslint-plugin-react-hooks}/index.ts | 0 .../eslint-plugin-react-hooks}/package.json | 0 .../eslint-plugin-react-hooks}/types.d.ts | 0 patch/eslint-import-resolver-typescript.patch | 14 +++++++++ pull.sh | 7 +++-- save_patch.sh | 5 ++-- src/index.ts | 20 ++----------- src/redirect.ts | 22 ++++++++++++++ 20 files changed, 80 insertions(+), 48 deletions(-) rename dist/{import => eslint-plugin-import}/index.d.ts (100%) rename dist/{jsx-a11y => eslint-plugin-jsx-a11y}/index.d.ts (100%) rename dist/{react-hooks => eslint-plugin-react-hooks}/index.d.ts (100%) rename dist/{react => eslint-plugin-react}/index.d.ts (100%) rename {eslint-plugin-react-hooks => packages/eslint-plugin-react-hooks}/ExhaustiveDeps.ts (100%) rename {eslint-plugin-react-hooks => packages/eslint-plugin-react-hooks}/RulesOfHooks.ts (100%) rename {eslint-plugin-react-hooks => packages/eslint-plugin-react-hooks}/index.ts (100%) rename {eslint-plugin-react-hooks => packages/eslint-plugin-react-hooks}/package.json (100%) rename {eslint-plugin-react-hooks => packages/eslint-plugin-react-hooks}/types.d.ts (100%) create mode 100644 patch/eslint-import-resolver-typescript.patch create mode 100644 src/redirect.ts diff --git a/.gitignore b/.gitignore index 28f2efa..56e74a7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,5 @@ -eslint-plugin-import -eslint-plugin-jsx-a11y -eslint-plugin-react -eslint-plugin-n -jsx-ast-utils +/packages/* +!/packages/eslint-plugin-react-hooks /react dist/**/*.js diff --git a/build.sh b/build.sh index 6592a5c..83bfc11 100755 --- a/build.sh +++ b/build.sh @@ -1,13 +1,15 @@ #!/bin/bash ./src/build-local-rules.ts -./esbuild.ts npx tsc ./src/local/index.ts --outdir ./dist/local --target ESNext --module CommonJS --esModuleInterop >/dev/null npx tsc ./src/index.ts --outdir ./dist --declaration --target ESNext --module CommonJS --esModuleInterop >/dev/null +rm -r dist/redirect.* +./esbuild.ts +sed -i '' '/import.*redirect.*;/d' dist/index.d.ts type() { - npx dts-bundle-generator "./eslint-plugin-$1/$2" \ + npx dts-bundle-generator "./packages/eslint-plugin-$1/$2" \ -o "./dist/$1/index.d.ts" \ - --project "./eslint-plugin-$1/tsconfig.json" \ + --project "./packages/eslint-plugin-$1/tsconfig.json" \ --no-check } diff --git a/dist/import/index.d.ts b/dist/eslint-plugin-import/index.d.ts similarity index 100% rename from dist/import/index.d.ts rename to dist/eslint-plugin-import/index.d.ts diff --git a/dist/jsx-a11y/index.d.ts b/dist/eslint-plugin-jsx-a11y/index.d.ts similarity index 100% rename from dist/jsx-a11y/index.d.ts rename to dist/eslint-plugin-jsx-a11y/index.d.ts diff --git a/dist/react-hooks/index.d.ts b/dist/eslint-plugin-react-hooks/index.d.ts similarity index 100% rename from dist/react-hooks/index.d.ts rename to dist/eslint-plugin-react-hooks/index.d.ts diff --git a/dist/react/index.d.ts b/dist/eslint-plugin-react/index.d.ts similarity index 100% rename from dist/react/index.d.ts rename to dist/eslint-plugin-react/index.d.ts diff --git a/dist/index.d.ts b/dist/index.d.ts index 54c8a33..5d01b0f 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -4,4 +4,4 @@ declare module 'eslint-define-config/src/rules/react/no-unknown-property.d.ts' { extends: ('next' | 'emotion')[]; } } -export declare function extendConfig({ plugins, settings, rules, extends: _extends, overrides, ...rest }: ESLintConfig): ESLintConfig; +export declare function extendConfig({ plugins, settings, rules, extends: _extends, overrides, ...rest }?: ESLintConfig): ESLintConfig; diff --git a/dist/package.json b/dist/package.json index ebc3542..b10905e 100644 --- a/dist/package.json +++ b/dist/package.json @@ -1,6 +1,6 @@ { "name": "@aet/eslint-rules", - "version": "0.0.1-beta.33", + "version": "0.0.1-beta.35", "license": "UNLICENSED", "peerDependencies": { "esbin": "^0.0.2", @@ -17,12 +17,13 @@ "debug": "^4.3.4", "doctrine": "^3.0.0", "emoji-regex": "^10.2.1", + "enhanced-resolve": "^5.15.0", "eslint-config-prettier": "9.0.0", "eslint-define-config": "^1.23.0", "eslint-import-resolver-node": "^0.3.9", - "eslint-import-resolver-typescript": "^3.6.0", "eslint-module-utils": "^2.8.0", "estraverse": "^5.3.0", + "get-tsconfig": "^4.7.0", "is-core-module": "^2.13.0", "is-glob": "^4.0.3", "language-tags": "^1.0.9", diff --git a/esbuild.ts b/esbuild.ts index b665d9b..3b40ef5 100755 --- a/esbuild.ts +++ b/esbuild.ts @@ -152,8 +152,8 @@ export const babelPlugin: Plugin = { .replaceAll("require('object.fromentries/polyfill')()", 'Object.fromEntries'); if ( - path.includes('eslint-plugin-import/src/rules/') || - path.includes('eslint-plugin-import/config/') + path.includes('packages/eslint-plugin-import/src/rules/') || + path.includes('packages/eslint-plugin-import/config/') ) { source = source.replace('\nmodule.exports = {', '\nexport default {'); } @@ -202,10 +202,12 @@ const plugins: Plugin[] = [ name: 'alias', setup(build) { build.onResolve({ filter: /^jsx-ast-utils$/ }, () => ({ - path: resolve('./jsx-ast-utils/src/index.js'), + path: resolve('./packages/jsx-ast-utils/src/index.js'), })); build.onResolve({ filter: /^jsx-ast-utils\/.+$/ }, ({ path }) => ({ - path: resolve('./jsx-ast-utils/', path.slice('jsx-ast-utils/'.length)) + '.js', + path: + resolve('./packages/jsx-ast-utils/', path.slice('jsx-ast-utils/'.length)) + + '.js', })); }, }, @@ -234,7 +236,13 @@ if (process.env.DEBUG) { }); } -async function main(entry: string, outfile: string) { +async function main( + entry: string, + outfile = entry + .replace('./packages/', './dist/') + .replace('src/', '') + .replace('.ts', '.js'), +) { await esbuild.build({ entryPoints: [entry], outfile, @@ -251,9 +259,11 @@ async function main(entry: string, outfile: string) { }); } -main('./eslint-plugin-react/index.js', './dist/react/index.js'); -main('./eslint-plugin-import/src/index.js', './dist/import/index.js'); -main('./eslint-plugin-jsx-a11y/src/index.js', './dist/jsx-a11y/index.js'); -main('./eslint-plugin-react-hooks/index.ts', './dist/react-hooks/index.js'); -main('./eslint-plugin-n/lib/index.js', './dist/n/index.js'); +main('./packages/eslint-plugin-react/index.js'); +main('./packages/eslint-plugin-import/src/index.js'); +main('./packages/eslint-plugin-jsx-a11y/src/index.js'); +main('./packages/eslint-plugin-react-hooks/index.ts'); +main('./packages/eslint-plugin-n/lib/index.js'); +main('./packages/eslint-import-resolver-typescript/src/index.ts'); main('./src/rules/index.ts', './dist/rules/index.js'); +main('./src/index.ts', './dist/index.js'); diff --git a/package.json b/package.json index a0c8cf1..9254de8 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ }, "private": true, "devDependencies": { - "@babel/core": "^7.22.10", + "@babel/core": "^7.22.11", "@babel/plugin-transform-flow-strip-types": "^7.22.5", "@babel/preset-env": "^7.22.10", "@types/babel-plugin-macros": "^3.1.0", @@ -15,7 +15,7 @@ "@types/estree": "^1.0.1", "@types/estree-jsx": "^1.0.0", "@types/lodash": "^4.14.197", - "@types/node": "^20.5.4", + "@types/node": "^20.5.7", "@typescript-eslint/types": "^6.4.1", "babel-plugin-macros": "^3.1.0", "dts-bundle-generator": "^8.0.1", @@ -23,7 +23,7 @@ "esbuild": "0.19.2", "esbuild-plugin-alias": "^0.2.1", "esbuild-register": "3.4.2", - "eslint": "8.47.0", + "eslint": "8.48.0", "eslint-config-prettier": "9.0.0", "eslint-define-config": "^1.23.0", "eslint-plugin-import": "^2.28.1", @@ -34,7 +34,7 @@ "minimatch": "^9.0.3", "picocolors": "^1.0.0", "prettier": "^3.0.2", - "typescript": "5.1.6" + "typescript": "5.2.2" }, "prettier": { "arrowParens": "avoid", diff --git a/eslint-plugin-react-hooks/ExhaustiveDeps.ts b/packages/eslint-plugin-react-hooks/ExhaustiveDeps.ts similarity index 100% rename from eslint-plugin-react-hooks/ExhaustiveDeps.ts rename to packages/eslint-plugin-react-hooks/ExhaustiveDeps.ts diff --git a/eslint-plugin-react-hooks/RulesOfHooks.ts b/packages/eslint-plugin-react-hooks/RulesOfHooks.ts similarity index 100% rename from eslint-plugin-react-hooks/RulesOfHooks.ts rename to packages/eslint-plugin-react-hooks/RulesOfHooks.ts diff --git a/eslint-plugin-react-hooks/index.ts b/packages/eslint-plugin-react-hooks/index.ts similarity index 100% rename from eslint-plugin-react-hooks/index.ts rename to packages/eslint-plugin-react-hooks/index.ts diff --git a/eslint-plugin-react-hooks/package.json b/packages/eslint-plugin-react-hooks/package.json similarity index 100% rename from eslint-plugin-react-hooks/package.json rename to packages/eslint-plugin-react-hooks/package.json diff --git a/eslint-plugin-react-hooks/types.d.ts b/packages/eslint-plugin-react-hooks/types.d.ts similarity index 100% rename from eslint-plugin-react-hooks/types.d.ts rename to packages/eslint-plugin-react-hooks/types.d.ts diff --git a/patch/eslint-import-resolver-typescript.patch b/patch/eslint-import-resolver-typescript.patch new file mode 100644 index 0000000..b890c54 --- /dev/null +++ b/patch/eslint-import-resolver-typescript.patch @@ -0,0 +1,14 @@ +diff --git a/package.json b/package.json +index a905ca5..4979e41 100644 +--- a/package.json ++++ b/package.json +@@ -62,8 +62,7 @@ + "typecov": "type-coverage" + }, + "peerDependencies": { +- "eslint": "*", +- "eslint-plugin-import": "*" ++ "eslint": "*" + }, + "dependencies": { + "debug": "^4.3.4", diff --git a/pull.sh b/pull.sh index b910806..ea9ca74 100755 --- a/pull.sh +++ b/pull.sh @@ -1,11 +1,12 @@ #!/bin/bash pull() { echo "🚛 Pulling $1" - (cd "$1" && git pull) + (cd "packages/$1" && git pull) } +pull eslint-import-resolver-typescript pull eslint-plugin-import pull eslint-plugin-jsx-a11y -pull jsx-ast-utils -pull eslint-plugin-react pull eslint-plugin-n +pull eslint-plugin-react +pull jsx-ast-utils diff --git a/save_patch.sh b/save_patch.sh index d6792cc..fcb7ee9 100755 --- a/save_patch.sh +++ b/save_patch.sh @@ -1,10 +1,11 @@ #!/bin/bash sync() ( - cd "$1" && git diff > "../patch/$1.patch" + cd "packages/$1" && git diff > "../../patch/$1.patch" ) +sync eslint-import-resolver-typescript sync eslint-plugin-import sync eslint-plugin-jsx-a11y -sync eslint-plugin-react sync eslint-plugin-n +sync eslint-plugin-react sync jsx-ast-utils diff --git a/src/index.ts b/src/index.ts index f01eeda..1c3fa65 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ -import Module from 'module'; import type { ESLintConfig } from 'eslint-define-config'; +import './redirect'; // @ts-expect-error const { name } = (0, require)('./package.json'); @@ -24,7 +24,7 @@ export function extendConfig({ extends: _extends, overrides, ...rest -}: ESLintConfig): ESLintConfig { +}: ESLintConfig = {}): ESLintConfig { const hasReact = plugins?.includes('react'); const hasUnicorn = plugins?.includes('unicorn'); const hasReactRefresh = plugins?.includes('react-refresh'); @@ -200,19 +200,3 @@ export function extendConfig({ return result; } - -const _resolveFilename = (Module as any)._resolveFilename; -(Module as any)._resolveFilename = function (module: string, ...args: any[]) { - switch (module) { - case 'eslint-plugin-import': - case 'eslint-plugin-jsx-a11y': - case 'eslint-plugin-local': - case 'eslint-plugin-react': - case 'eslint-plugin-react-hooks': - case 'eslint-plugin-rules': - case 'eslint-plugin-n': - module = `${name}/${module.slice(14)}`; - } - - return _resolveFilename(module, ...args); -}; diff --git a/src/redirect.ts b/src/redirect.ts new file mode 100644 index 0000000..def3580 --- /dev/null +++ b/src/redirect.ts @@ -0,0 +1,22 @@ +import Module from 'module'; +// @ts-expect-error +const { name } = (0, require)('./package.json'); + +const _resolveFilename = (Module as any)._resolveFilename; +const alias = new Set([ + 'eslint-import-resolver-typescript', + 'eslint-plugin-import', + 'eslint-plugin-jsx-a11y', + 'eslint-plugin-local', + 'eslint-plugin-n', + 'eslint-plugin-react-hooks', + 'eslint-plugin-react', + 'eslint-plugin-rules', +]); + +(Module as any)._resolveFilename = function (module: string, ...args: any[]) { + if (alias.has(module)) { + module = `${name}/${module}`; + } + return _resolveFilename(module, ...args); +};