Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Custom ESLint Rules #110

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
},
}
5 changes: 5 additions & 0 deletions custom-eslint-rules/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
rules: {
'restrict-import-depth': require('./restrict-import-depth'),
},
};
6 changes: 6 additions & 0 deletions custom-eslint-rules/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
35 changes: 35 additions & 0 deletions custom-eslint-rules/restrict-import-depth.js
Original file line number Diff line number Diff line change
@@ -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.',
});
}
}
},
};
},
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading