From 9b3dfd95f4f149d91897c9461378330adabcea2b Mon Sep 17 00:00:00 2001 From: doprz <52579214+doprz@users.noreply.github.com> Date: Mon, 26 Feb 2024 00:44:51 -0600 Subject: [PATCH] feat: add custom ESLint rule restrict-import-depth --- .eslintrc | 10 +++++- custom-eslint-rules/index.js | 5 +++ custom-eslint-rules/package.json | 6 ++++ custom-eslint-rules/restrict-import-depth.js | 35 ++++++++++++++++++++ package.json | 1 + pnpm-lock.yaml | 9 +++++ 6 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 custom-eslint-rules/index.js create mode 100644 custom-eslint-rules/package.json create mode 100644 custom-eslint-rules/restrict-import-depth.js diff --git a/.eslintrc b/.eslintrc index 1896e834e..9ee44bdf3 100644 --- a/.eslintrc +++ b/.eslintrc @@ -18,7 +18,14 @@ "@unocss", "prettier", ], - "plugins": ["import", "jsdoc", "react-prefer-function-component", "@typescript-eslint", "simple-import-sort"], + "plugins": [ + "import", + "jsdoc", + "react-prefer-function-component", + "@typescript-eslint", + "simple-import-sort", + "restrict-import-depth", + ], "globals": { "Atomics": "readonly", "SharedArrayBuffer": "readonly", @@ -201,5 +208,6 @@ "@typescript-eslint/consistent-type-imports": "error", "simple-import-sort/imports": "error", "simple-import-sort/exports": "error", + "restrict-import-depth/restrict-import-depth": "error", }, } diff --git a/custom-eslint-rules/index.js b/custom-eslint-rules/index.js new file mode 100644 index 000000000..cc3efd138 --- /dev/null +++ b/custom-eslint-rules/index.js @@ -0,0 +1,5 @@ +module.exports = { + rules: { + 'restrict-import-depth': require('./restrict-import-depth'), + }, +}; diff --git a/custom-eslint-rules/package.json b/custom-eslint-rules/package.json new file mode 100644 index 000000000..ba81fc2c2 --- /dev/null +++ b/custom-eslint-rules/package.json @@ -0,0 +1,6 @@ +{ + "name": "eslint-plugin-restrict-import-depth", + "version": "0.1.0", + "description": "ESLint plugin to restrict the depth of import statements", + "main": "index.js" +} \ No newline at end of file diff --git a/custom-eslint-rules/restrict-import-depth.js b/custom-eslint-rules/restrict-import-depth.js new file mode 100644 index 000000000..a8b8453b2 --- /dev/null +++ b/custom-eslint-rules/restrict-import-depth.js @@ -0,0 +1,35 @@ +/** + * @fileoverview Custom ESLint rule to restrict imports from accessing files more than 2 directories up. + */ + +'use strict'; + +module.exports = { + meta: { + type: 'problem', + docs: { + description: 'Restrict imports from accessing files more than 2 directories up.', + category: 'Best Practices', + recommended: true, + }, + fixable: null, + schema: [], + }, + + create: function (context) { + return { + ImportDeclaration(node) { + const importPath = node.source.value; + if (importPath.startsWith('../')) { + const depth = importPath.match(/\.\.\//g).length; + if (depth > 2) { + context.report({ + node, + message: 'Importing files more than 2 directories up is not allowed.', + }); + } + } + }, + }; + }, +}; diff --git a/package.json b/package.json index 561b42f4a..d76dd9ec4 100644 --- a/package.json +++ b/package.json @@ -93,6 +93,7 @@ "eslint-plugin-react-hooks": "^4.6.0", "eslint-plugin-react-prefer-function-component": "^3.3.0", "eslint-plugin-react-refresh": "^0.4.5", + "eslint-plugin-restrict-import-depth": "file:custom-eslint-rules", "eslint-plugin-simple-import-sort": "^12.0.0", "eslint-plugin-storybook": "^0.6.15", "husky": "^9.0.11", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4c0869b9e..8930a292f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -212,6 +212,9 @@ devDependencies: eslint-plugin-react-refresh: specifier: ^0.4.5 version: 0.4.5(eslint@8.56.0) + eslint-plugin-restrict-import-depth: + specifier: file:custom-eslint-rules + version: file:custom-eslint-rules eslint-plugin-simple-import-sort: specifier: ^12.0.0 version: 12.0.0(eslint@8.56.0) @@ -13255,6 +13258,12 @@ packages: engines: {node: '>=12.20'} dev: true + file:custom-eslint-rules: + resolution: {directory: custom-eslint-rules, type: directory} + name: eslint-plugin-restrict-import-depth + version: 0.1.0 + dev: true + settings: autoInstallPeers: true excludeLinksFromLockfile: false