eslint-plugin-strict-dependencies
Advanced tools
Comparing version 0.0.1 to 0.0.2
{ | ||
"name": "eslint-plugin-strict-dependencies", | ||
"description": "Restrict imports between files according to the configured dependency rules.", | ||
"version": "0.0.1", | ||
"description": "ESlint plugin to define custom module dependency rules.", | ||
"version": "0.0.2", | ||
"repository": { | ||
@@ -9,2 +9,14 @@ "type": "git", | ||
}, | ||
"keywords": [ | ||
"eslint", | ||
"eslintplugin", | ||
"lint", | ||
"rule", | ||
"check", | ||
"import", | ||
"module", | ||
"directory", | ||
"strict", | ||
"dependencies" | ||
], | ||
"license": "MIT", | ||
@@ -15,3 +27,11 @@ "main": "index.js", | ||
"index.js" | ||
] | ||
], | ||
"devDependencies": { | ||
"is-glob": "^4.0.3", | ||
"jest": "^27.2.4", | ||
"micromatch": "^4.0.4" | ||
}, | ||
"scripts": { | ||
"test": "jest --coverage" | ||
} | ||
} |
# eslint-plugin-strict-dependencies | ||
NOTE: eslint-plugin-strict-dependencies uses tsconfig, tsconfig.json must be present. | ||
ESlint plugin to define custom module dependency rules. | ||
NOTE: `eslint-plugin-strict-dependencies` uses tsconfig, tsconfig.json must be present. | ||
## Installation | ||
@@ -19,4 +21,15 @@ | ||
- allowSameModule: `boolean` | ||
- Whether or not the target module itself can import on the target module | ||
- Whether it can be imported by other files in the same directory | ||
### Options | ||
- resolveRelativeImport: `boolean[default = false]` | ||
- Whether to resolve relative import as in the following example | ||
- `src/components/aaa.ts` | ||
```typescript | ||
import bbb from './bbb'; | ||
``` | ||
- `resolveRelativeImport = false`: Resolve as `./bbb` (excluded from lint target) | ||
- `resolveRelativeImport = true`: Resolve as `src/components/bbb`: (included from lint target) | ||
## Usage | ||
@@ -35,4 +48,4 @@ | ||
/** | ||
* example: | ||
* Components only allow dependencies in the following directions | ||
* Example: | ||
* Limit the dependencies in the following directions | ||
* pages -> components/page -> components/ui | ||
@@ -55,3 +68,3 @@ */ | ||
* example: | ||
* Don't import next/router directly, always import it through libs/router. | ||
* Disallow to import `next/router` directly. it should always be imported using `libs/router.ts`. | ||
*/ | ||
@@ -64,3 +77,7 @@ { | ||
], | ||
], | ||
// options | ||
// { | ||
// "resolveRelativeImport": true | ||
// } | ||
] | ||
} | ||
@@ -73,2 +90,2 @@ | ||
MIT | ||
MIT |
@@ -46,2 +46,10 @@ /* eslint-disable */ | ||
}, | ||
{ | ||
type: 'object', | ||
properties: { | ||
resolveRelativeImport: { | ||
type: 'boolean', | ||
} | ||
}, | ||
}, | ||
], | ||
@@ -51,2 +59,4 @@ }, | ||
const dependencies = context.options[0] | ||
const options = context.options.length > 1 ? context.options[1] : {} | ||
const resolveRelativeImport = options.resolveRelativeImport | ||
@@ -56,3 +66,3 @@ function checkImport(node) { | ||
const relativeFilePath = path.relative(process.cwd(), fileFullPath) | ||
const importPath = resolveImportPath(node.source.value) | ||
const importPath = resolveImportPath(node.source.value, resolveRelativeImport ? relativeFilePath : null) | ||
@@ -70,3 +80,3 @@ dependencies | ||
if (!isAllowed) { | ||
context.report(node, `import '${importPath}' is not allowed from ${dependency.module}.`) | ||
context.report(node, `import '${importPath}' is not allowed from ${relativeFilePath}.`) | ||
} | ||
@@ -73,0 +83,0 @@ }) |
@@ -9,3 +9,3 @@ /* eslint-disable */ | ||
*/ | ||
module.exports = (importPath) => { | ||
module.exports = (importPath, relativeFilePath) => { | ||
// { [importAlias: string]: OriginalPath } | ||
@@ -23,3 +23,3 @@ const importAliasMap = {} | ||
// FIXME: このlint ruleではimport先が存在するかチェックしておらず、複数のパスから正しい方を選択できないため[0]固定 | ||
importAliasMap[key] = tsConfig.compilerOptions.paths[key][0] | ||
importAliasMap[key] = tsConfig.compilerOptions.baseUrl ? path.join(tsConfig.compilerOptions.baseUrl, tsConfig.compilerOptions.paths[key][0]) : tsConfig.compilerOptions.paths[key][0] | ||
}) | ||
@@ -31,2 +31,6 @@ } | ||
if (relativeFilePath && (importPath.startsWith('./') || importPath.startsWith('../'))) { | ||
importPath = path.join(path.dirname(relativeFilePath), importPath) | ||
} | ||
return Object.keys(importAliasMap).reduce((resolvedImportPath, key) => { | ||
@@ -33,0 +37,0 @@ // FIXME: use glob module instead of replace('*') |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7945
117
87
3