feat: add custom ESLint rule restrict-import-depth (#110)
This commit is contained in:
5
custom-eslint-rules/index.js
Normal file
5
custom-eslint-rules/index.js
Normal file
@@ -0,0 +1,5 @@
|
||||
module.exports = {
|
||||
rules: {
|
||||
'restrict-import-depth': require('./restrict-import-depth'),
|
||||
},
|
||||
};
|
||||
6
custom-eslint-rules/package.json
Normal file
6
custom-eslint-rules/package.json
Normal 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"
|
||||
}
|
||||
35
custom-eslint-rules/restrict-import-depth.js
Normal file
35
custom-eslint-rules/restrict-import-depth.js
Normal 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.',
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user