feat: add check-path-alias custom ESLint rule (#123)

This commit is contained in:
doprz
2024-03-02 12:04:36 -06:00
parent 6ba8b68654
commit 208103d708
7 changed files with 65 additions and 9 deletions

View File

@@ -24,7 +24,7 @@
"react-prefer-function-component", "react-prefer-function-component",
"@typescript-eslint", "@typescript-eslint",
"simple-import-sort", "simple-import-sort",
"restrict-import-depth", "custom-utrp",
], ],
"globals": { "globals": {
"Atomics": "readonly", "Atomics": "readonly",
@@ -208,6 +208,7 @@
"@typescript-eslint/consistent-type-imports": "error", "@typescript-eslint/consistent-type-imports": "error",
"simple-import-sort/imports": "error", "simple-import-sort/imports": "error",
"simple-import-sort/exports": "error", "simple-import-sort/exports": "error",
"restrict-import-depth/restrict-import-depth": "error", "custom-utrp/restrict-import-depth": "error",
"custom-utrp/check-path-alias": "error",
}, },
} }

View File

@@ -0,0 +1,54 @@
/**
* @fileoverview Check if imports match the path aliases defined in tsconfig.
*/
'use strict';
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'Check if imports match the path aliases defined in tsconfig',
category: 'Possible Errors',
recommended: true,
},
fixable: null,
schema: [],
},
create: function (context) {
return {
ImportDeclaration(node) {
const importPath = node.source.value;
// Get aliases from tsconfig and check if the import matches any of them
// "paths": {
// "src/*": ["src/*"],
// "@assets/*": ["src/assets/*"],
// "@pages/*": ["src/pages/*"],
// "@public/*": ["public/*"],
// "@shared/*": ["src/shared/*"],
// "@background/*": ["src/pages/background/*"],
// "@views/*": ["src/views/*"]
// }
const tsconfig = require('../tsconfig.json');
const paths = tsconfig.compilerOptions.paths;
let pathList = [];
for (let key in paths) {
paths[key].forEach(value => {
if (key.startsWith('@')) {
pathList.push(value.replace('/*', ''));
}
});
}
if (pathList.some(path => importPath.startsWith(path))) {
context.report({
node,
message: 'Use a path alias here',
});
}
},
};
},
};

View File

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

View File

@@ -1,6 +1,6 @@
{ {
"name": "eslint-plugin-restrict-import-depth", "name": "eslint-plugin-custom-utrp",
"version": "0.1.0", "version": "0.1.0",
"description": "ESLint plugin to restrict the depth of import statements", "description": "Custom ESLint rules for UTRP",
"main": "index.js" "main": "index.js"
} }

View File

@@ -23,7 +23,7 @@ module.exports = {
if (importPath.startsWith('../../')) { if (importPath.startsWith('../../')) {
context.report({ context.report({
node, node,
message: 'Importing files more than 2 directories up is not allowed.', message: 'Importing files more than 2 directories up is not allowed',
}); });
} }
}, },

View File

@@ -94,7 +94,7 @@
"eslint-plugin-react-hooks": "^4.6.0", "eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-prefer-function-component": "^3.3.0", "eslint-plugin-react-prefer-function-component": "^3.3.0",
"eslint-plugin-react-refresh": "^0.4.5", "eslint-plugin-react-refresh": "^0.4.5",
"eslint-plugin-restrict-import-depth": "link:./custom-eslint-rules", "eslint-plugin-custom-utrp": "link:./custom-eslint-rules",
"eslint-plugin-simple-import-sort": "^12.0.0", "eslint-plugin-simple-import-sort": "^12.0.0",
"eslint-plugin-storybook": "^0.6.15", "eslint-plugin-storybook": "^0.6.15",
"path": "^0.12.7", "path": "^0.12.7",

6
pnpm-lock.yaml generated
View File

@@ -198,6 +198,9 @@ devDependencies:
eslint-import-resolver-typescript: eslint-import-resolver-typescript:
specifier: ^3.6.1 specifier: ^3.6.1
version: 3.6.1(@typescript-eslint/parser@6.21.0)(eslint-plugin-import@2.29.1)(eslint@8.56.0) version: 3.6.1(@typescript-eslint/parser@6.21.0)(eslint-plugin-import@2.29.1)(eslint@8.56.0)
eslint-plugin-custom-utrp:
specifier: link:./custom-eslint-rules
version: link:custom-eslint-rules
eslint-plugin-import: eslint-plugin-import:
specifier: ^2.29.1 specifier: ^2.29.1
version: 2.29.1(@typescript-eslint/parser@6.21.0)(eslint-import-resolver-typescript@3.6.1)(eslint@8.56.0) version: 2.29.1(@typescript-eslint/parser@6.21.0)(eslint-import-resolver-typescript@3.6.1)(eslint@8.56.0)
@@ -219,9 +222,6 @@ devDependencies:
eslint-plugin-react-refresh: eslint-plugin-react-refresh:
specifier: ^0.4.5 specifier: ^0.4.5
version: 0.4.5(eslint@8.56.0) version: 0.4.5(eslint@8.56.0)
eslint-plugin-restrict-import-depth:
specifier: link:./custom-eslint-rules
version: link:custom-eslint-rules
eslint-plugin-simple-import-sort: eslint-plugin-simple-import-sort:
specifier: ^12.0.0 specifier: ^12.0.0
version: 12.0.0(eslint@8.56.0) version: 12.0.0(eslint@8.56.0)