feat: add custom ESLint rule restrict-import-depth (#110)

This commit is contained in:
doprz
2024-02-27 23:52:45 -06:00
parent 87799d8f02
commit 8c069b7ad3
6 changed files with 65 additions and 1 deletions

View File

@@ -0,0 +1,5 @@
module.exports = {
rules: {
'restrict-import-depth': require('./restrict-import-depth'),
},
};

View File

@@ -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"
}

View File

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