eslint-plugin-path-alias
Advanced tools
@@ -1,1 +0,1 @@ | ||
var M=Object.create;var m=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var U=Object.getOwnPropertyNames;var I=Object.getPrototypeOf,C=Object.prototype.hasOwnProperty;var $=(e,t)=>{for(var r in t)m(e,r,{get:t[r],enumerable:!0})},h=(e,t,r,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of U(t))!C.call(e,s)&&s!==r&&m(e,s,{get:()=>t[s],enumerable:!(i=T(t,s))||i.enumerable});return e};var z=(e,t,r)=>(r=e!=null?M(I(e)):{},h(t||!e||!e.__esModule?m(r,"default",{value:e,enumerable:!0}):r,e)),D=e=>h(m({},"__esModule",{value:!0}),e);var q={};$(q,{default:()=>N});module.exports=D(q);var y="eslint-plugin-path-alias",b="1.1.0";var l=require("node:path"),A=z(require("nanomatch"));function x(e){return`https://github/com/msfragala/eslint-plugin-path-alias/blob/master/docs/rules/${e}.md`}var R=require("get-tsconfig"),a=require("node:path"),j=require("read-package-up");function w(e){if(e.options[0]?.paths)return J(e);let t=(0,R.getTsconfig)(e.filename);if(t?.config?.compilerOptions?.paths)return S(t);let r=(0,j.readPackageUpSync)({cwd:(0,a.dirname)(e.filename)});if(r?.packageJson?.imports)return L(r)}function L(e){let t=new Map,r=e.packageJson.imports??{},i=(0,a.dirname)(e.path);return Object.entries(r).forEach(([s,n])=>{if(!n||typeof n!="string")return;let o=(0,a.resolve)(i,n);t.set(s,[o])}),t}function S(e){let t=new Map,r=e?.config?.compilerOptions?.paths??{},i=(0,a.dirname)(e.path);return e.config.compilerOptions?.baseUrl&&(i=(0,a.resolve)((0,a.dirname)(e.path),e.config.compilerOptions.baseUrl)),Object.entries(r).forEach(([s,n])=>{s=s.replace(/\/\*$/,""),n=n.map(o=>(0,a.resolve)(i,o.replace(/\/\*$/,""))),t.set(s,n)}),t}function J(e){let t=new Map,r=e.options[0]?.paths??{};return Object.entries(r).forEach(([i,s])=>{if(!s||typeof s!="string")return;if(s.startsWith("/")){t.set(i,[s]);return}let n=(0,a.resolve)(e.cwd,s);t.set(i,[n])}),t}var E={meta:{type:"suggestion",docs:{description:"Ensure imports use path aliases whenever possible vs. relative paths",url:x("no-relative")},fixable:"code",schema:[{type:"object",properties:{exceptions:{type:"array",items:{type:"string"}},paths:{type:"object"}},additionalProperties:!1}],messages:{shouldUseAlias:"Import should use path alias instead of relative path"}},create(e){let t=e.options[0]?.exceptions,r=e.filename,i=w(e);return i?.size?{ImportExpression(s){if(s.source.type!=="Literal"||typeof s.source.value!="string")return;let n=s.source.raw,o=s.source.value;if(!/^(\.?\.\/)/.test(o))return;let c=(0,l.resolve)((0,l.dirname)(r),o);if(k(c,t))return;let p=P(c,i);p&&e.report({node:s,messageId:"shouldUseAlias",data:{alias:p},fix(f){let d=O(c,p,i.get(p)),g=n.replace(o,d);return f.replaceText(s.source,g)}})},ImportDeclaration(s){if(typeof s.source.value!="string")return;let n=s.source.value;if(!/^(\.?\.\/)/.test(n))return;let o=(0,l.resolve)((0,l.dirname)(r),n),c=k(o,t),u=P(o,i);c||u&&e.report({node:s,messageId:"shouldUseAlias",data:{alias:u},fix(p){let f=s.source.raw,d=O(o,u,i.get(u)),g=f.replace(n,d);return p.replaceText(s.source,g)}})}}:{}}};function P(e,t){return Array.from(t.keys()).find(r=>t.get(r).some(s=>e.indexOf(s)===0))}function k(e,t){if(!t)return!1;let r=(0,l.basename)(e);return(0,A.default)(r,t).includes(r)}function O(e,t,r){for(let i of r)if(e.indexOf(i)===0)return e.replace(i,t)}var N={meta:{name:y,version:b},rules:{"no-relative":E}}; | ||
var C=Object.create;var f=Object.defineProperty;var I=Object.getOwnPropertyDescriptor;var U=Object.getOwnPropertyNames;var S=Object.getPrototypeOf,F=Object.prototype.hasOwnProperty;var $=(e,t)=>{for(var r in t)f(e,r,{get:t[r],enumerable:!0})},y=(e,t,r,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of U(t))!F.call(e,s)&&s!==r&&f(e,s,{get:()=>t[s],enumerable:!(i=I(t,s))||i.enumerable});return e};var b=(e,t,r)=>(r=e!=null?C(S(e)):{},y(t||!e||!e.__esModule?f(r,"default",{value:e,enumerable:!0}):r,e)),D=e=>y(f({},"__esModule",{value:!0}),e);var N={};$(N,{default:()=>J});module.exports=D(N);var h="eslint-plugin-path-alias",v="2.0.0";var l=require("path"),M=b(require("nanomatch"));function j(e){return`https://github/com/msfragala/eslint-plugin-path-alias/blob/master/docs/rules/${e}.md`}var R=require("get-tsconfig"),a=require("path"),w=b(require("find-pkg")),O=require("fs");function P(e){if(e.options[0]?.paths)return z(e);let t=e.getFilename?.()??e.filename,r=(0,R.getTsconfig)(t);if(r?.config?.compilerOptions?.paths)return q(r);let i=w.default.sync((0,a.dirname)(t));if(!i)return;let s=JSON.parse((0,O.readFileSync)(i).toString());if(s?.imports)return L(s,i)}function L(e,t){let r=new Map,i=e.imports??{},s=(0,a.dirname)(t);return Object.entries(i).forEach(([o,n])=>{if(!n||typeof n!="string")return;let p=(0,a.resolve)(s,n);r.set(o,[p])}),r}function q(e){let t=new Map,r=e?.config?.compilerOptions?.paths??{},i=(0,a.dirname)(e.path);return e.config.compilerOptions?.baseUrl&&(i=(0,a.resolve)((0,a.dirname)(e.path),e.config.compilerOptions.baseUrl)),Object.entries(r).forEach(([s,o])=>{s=s.replace(/\/\*$/,""),o=o.map(n=>(0,a.resolve)(i,n.replace(/\/\*$/,""))),t.set(s,o)}),t}function z(e){let t=new Map,r=e.options[0]?.paths??{};return Object.entries(r).forEach(([i,s])=>{if(!s||typeof s!="string")return;if(s.startsWith("/")){t.set(i,[s]);return}let o=e.getCwd?.()??e.cwd,n=(0,a.resolve)(o,s);t.set(i,[n])}),t}var T={meta:{type:"suggestion",docs:{description:"Ensure imports use path aliases whenever possible vs. relative paths",url:j("no-relative")},fixable:"code",schema:[{type:"object",properties:{exceptions:{type:"array",items:{type:"string"}},paths:{type:"object"}},additionalProperties:!1}],messages:{shouldUseAlias:"Import should use path alias instead of relative path"}},create(e){let t=e.options[0]?.exceptions,r=e.getFilename?.()??e.filename,i=P(e);return i?.size?{ImportExpression(s){if(s.source.type!=="Literal"||typeof s.source.value!="string")return;let o=s.source.raw,n=s.source.value;if(!/^(\.?\.\/)/.test(n))return;let p=(0,l.resolve)((0,l.dirname)(r),n);if(A(p,t))return;let c=k(p,i);c&&e.report({node:s,messageId:"shouldUseAlias",data:{alias:c},fix(m){let g=E(p,c,i.get(c)),d=o.replace(n,g);return m.replaceText(s.source,d)}})},ImportDeclaration(s){if(typeof s.source.value!="string")return;let o=s.source.value;if(!/^(\.?\.\/)/.test(o))return;let n=(0,l.resolve)((0,l.dirname)(r),o),p=A(n,t),u=k(n,i);p||u&&e.report({node:s,messageId:"shouldUseAlias",data:{alias:u},fix(c){let m=s.source.raw,g=E(n,u,i.get(u)),d=m.replace(o,g);return c.replaceText(s.source,d)}})}}:{}}};function k(e,t){return Array.from(t.keys()).find(r=>t.get(r).some(s=>e.indexOf(s)===0))}function A(e,t){if(!t)return!1;let r=(0,l.basename)(e);return(0,M.default)(r,t).includes(r)}function E(e,t,r){for(let i of r)if(e.indexOf(i)===0)return e.replace(i,t)}var J={name:h,version:v,meta:{name:h,version:v},rules:{"no-relative":T}}; |
{ | ||
"name": "eslint-plugin-path-alias", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"main": "./lib/index.js", | ||
@@ -21,8 +21,8 @@ "repository": "https://github.com/msfragala/eslint-plugin-path-alias.git", | ||
"peerDependencies": { | ||
"eslint": "^9" | ||
"eslint": "^8.0.0" | ||
}, | ||
"dependencies": { | ||
"find-pkg": "^2.0.0", | ||
"get-tsconfig": "^4.7.5", | ||
"nanomatch": "^1.2.13", | ||
"read-package-up": "^11.0.0" | ||
"nanomatch": "^1.2.13" | ||
}, | ||
@@ -29,0 +29,0 @@ "devDependencies": { |
# eslint-plugin-path-alias | ||
Enforces usage of path aliases where available instead of relative paths. This helps ensure consistency in how modules are imported across a codebase. | ||
Enforces usage of path aliases where available instead of relative paths. This helps ensure consistency in how modules are imported across your codebase. | ||
## Install | ||
## Installation | ||
@@ -13,2 +13,8 @@ Using npm: | ||
Using pnpm: | ||
``` | ||
pnpm add --save-dev eslint-plugin-path-alias | ||
``` | ||
Using Yarn: | ||
@@ -20,8 +26,13 @@ | ||
## Examples | ||
Examples of **incorrect** code for this rule: | ||
```js | ||
// In src/lib/speak.js | ||
// Path alias: @/lib ➝ src/lib | ||
// Path alias: @/constants ➝ src/constants | ||
// src/lib/speak.js | ||
// With the following path aliases: | ||
// - @/lib ➝ src/lib | ||
// - @/constants ➝ src/constants | ||
import foo from "./greet"; // Should use "@/lib" | ||
@@ -34,5 +45,8 @@ import bar from "../constants/hello.i18n.js"; // Sould use "@/constants" | ||
```js | ||
// In src/lib/speak.js | ||
// Path alias: @/lib ➝ src/lib | ||
// Path alias: @/constants ➝ src/constants | ||
// src/lib/speak.js | ||
// With the following path aliases: | ||
// - @/lib ➝ src/lib | ||
// - @/constants ➝ src/constants | ||
import foo from "@/lib/greet"; | ||
@@ -43,4 +57,38 @@ import bar from "@/constants/hello.i18n.js"; | ||
## Options | ||
## Configuration | ||
You can define your path aliases as options to the `path-alias/no-relative` rule: | ||
```js | ||
import pathAlias from 'eslint-plugin-path-alias'; | ||
import { resolve } from 'node:path'; | ||
export default [ | ||
{ | ||
plugins: { | ||
'path-alias': pathAlias, | ||
}, | ||
rules: { | ||
'path-alias/no-relative': ['error', { | ||
paths: { | ||
// It's recommended to resolve path alias directories as | ||
// relative paths will be resolved relative to cwd. This | ||
// may cause unexpected behavior in monorepo setups | ||
'@': resolve(import.meta.dirname, './src'), | ||
}, | ||
}], | ||
}, | ||
}, | ||
]; | ||
``` | ||
### tsconfig.json | ||
If no `paths` options is provided to the rule, this plugin will attempt to find the nearest `tsconfig.json` and infer path aliases from the [`paths` option](https://www.typescriptlang.org/tsconfig/#paths) there. | ||
### package.json | ||
If no paths are founded in either the rule or a `tsconfig.json`, this plugin will attempt to find the nearest `package.json` and infer path aliases from the [`imports` field](https://nodejs.org/api/packages.html#imports) there. For now, conditional imports are not supported | ||
### `exceptions` | ||
@@ -76,4 +124,4 @@ | ||
## When Not To Use It | ||
If you are using `require()` to import modules. This rule currently only supports ES modules. | ||
## Notes | ||
- Does not validate imports using path aliases. Try using `import/no-unresolved` from [eslint-plugin-import](https://github.com/import-js/eslint-plugin-import) for that | ||
- Does not work with CommonJS imports via `require()` |
Sorry, the diff of this file is not supported yet
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
10659
17.08%26
4%123
64%0
-100%3
200%