Skip to content

Commit

Permalink
feat: add check-path-alias custom ESLint rule (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
doprz committed Mar 6, 2024
1 parent 6ba8b68 commit 208103d
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 9 deletions.
5 changes: 3 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"react-prefer-function-component",
"@typescript-eslint",
"simple-import-sort",
"restrict-import-depth",
"custom-utrp",
],
"globals": {
"Atomics": "readonly",
Expand Down Expand Up @@ -208,6 +208,7 @@
"@typescript-eslint/consistent-type-imports": "error",
"simple-import-sort/imports": "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",
},
}
54 changes: 54 additions & 0 deletions custom-eslint-rules/check-path-alias.js
Original file line number Diff line number Diff line change
@@ -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',
});
}
},
};
},
};
1 change: 1 addition & 0 deletions custom-eslint-rules/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
rules: {
'restrict-import-depth': require('./restrict-import-depth'),
'check-path-alias': require('./check-path-alias'),
},
};
4 changes: 2 additions & 2 deletions custom-eslint-rules/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-restrict-import-depth",
"name": "eslint-plugin-custom-utrp",
"version": "0.1.0",
"description": "ESLint plugin to restrict the depth of import statements",
"description": "Custom ESLint rules for UTRP",
"main": "index.js"
}
2 changes: 1 addition & 1 deletion custom-eslint-rules/restrict-import-depth.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = {
if (importPath.startsWith('../../')) {
context.report({
node,
message: 'Importing files more than 2 directories up is not allowed.',
message: 'Importing files more than 2 directories up is not allowed',
});
}
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,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": "link:./custom-eslint-rules",
"eslint-plugin-custom-utrp": "link:./custom-eslint-rules",
"eslint-plugin-simple-import-sort": "^12.0.0",
"eslint-plugin-storybook": "^0.6.15",
"path": "^0.12.7",
Expand Down
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

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

0 comments on commit 208103d

Please sign in to comment.