From 208103d7081abcf5b31bf36335320aaf7d213464 Mon Sep 17 00:00:00 2001 From: doprz <52579214+doprz@users.noreply.github.com> Date: Sat, 2 Mar 2024 12:04:36 -0600 Subject: [PATCH] feat: add check-path-alias custom ESLint rule (#123) --- .eslintrc | 5 +- custom-eslint-rules/check-path-alias.js | 54 ++++++++++++++++++++ custom-eslint-rules/index.js | 1 + custom-eslint-rules/package.json | 4 +- custom-eslint-rules/restrict-import-depth.js | 2 +- package.json | 2 +- pnpm-lock.yaml | 6 +-- 7 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 custom-eslint-rules/check-path-alias.js diff --git a/.eslintrc b/.eslintrc index 9ee44bdf..79d1cd5a 100644 --- a/.eslintrc +++ b/.eslintrc @@ -24,7 +24,7 @@ "react-prefer-function-component", "@typescript-eslint", "simple-import-sort", - "restrict-import-depth", + "custom-utrp", ], "globals": { "Atomics": "readonly", @@ -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", }, } diff --git a/custom-eslint-rules/check-path-alias.js b/custom-eslint-rules/check-path-alias.js new file mode 100644 index 00000000..2993e07d --- /dev/null +++ b/custom-eslint-rules/check-path-alias.js @@ -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', + }); + } + }, + }; + }, +}; diff --git a/custom-eslint-rules/index.js b/custom-eslint-rules/index.js index cc3efd13..411a2643 100644 --- a/custom-eslint-rules/index.js +++ b/custom-eslint-rules/index.js @@ -1,5 +1,6 @@ module.exports = { rules: { 'restrict-import-depth': require('./restrict-import-depth'), + 'check-path-alias': require('./check-path-alias'), }, }; diff --git a/custom-eslint-rules/package.json b/custom-eslint-rules/package.json index ba81fc2c..98d5169b 100644 --- a/custom-eslint-rules/package.json +++ b/custom-eslint-rules/package.json @@ -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" } \ No newline at end of file diff --git a/custom-eslint-rules/restrict-import-depth.js b/custom-eslint-rules/restrict-import-depth.js index 3ded655d..93544cbc 100644 --- a/custom-eslint-rules/restrict-import-depth.js +++ b/custom-eslint-rules/restrict-import-depth.js @@ -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', }); } }, diff --git a/package.json b/package.json index 7fac5b69..d157285c 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6d782c43..14ceda5e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -198,6 +198,9 @@ devDependencies: eslint-import-resolver-typescript: specifier: ^3.6.1 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: 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) @@ -219,9 +222,6 @@ devDependencies: eslint-plugin-react-refresh: specifier: ^0.4.5 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: specifier: ^12.0.0 version: 12.0.0(eslint@8.56.0)