eslint-plugin-welldone
Advanced tools
Comparing version
const path = require('path'); | ||
const fs = require('fs'); | ||
const findUp = require('find-up'); | ||
const minimatch = require('minimatch'); | ||
@@ -37,3 +38,3 @@ const {isRelative} = require('../utils'); | ||
function isCorrectModuleImportPath(importRequest, {modulesMapping, moduleInnerPaths}) { | ||
function isCorrectModuleImportPath(importRequest, {modulesMapping, moduleInnerPaths, ignoreInnerPathsForModules}) { | ||
const targetModuleName = getModuleNameFromImportRequest(importRequest); | ||
@@ -44,2 +45,6 @@ if (!modulesMapping[targetModuleName]) { | ||
if (ignoreInnerPathsForModules.includes(targetModuleName)) { | ||
return true; | ||
} | ||
const importPath = getModuleImportPathPart(importRequest); | ||
@@ -50,9 +55,6 @@ return !importPath || moduleInnerPaths.includes(importPath); | ||
const defaultOptions = {}; | ||
const defaultModulesLevels = { | ||
'app': 3, | ||
'common': 1, | ||
'shared': 1, | ||
}; | ||
const defaultModulesLevels = {'common': 1, 'shared': 1, 'app': 3}; | ||
const defaultMiddleModulesLevel = 2; | ||
const defaultModuleInnerPaths = []; | ||
const defaultIgnoreInnerPathsForModules = ['common', 'shared'] | ||
@@ -73,2 +75,5 @@ module.exports = { | ||
"properties": { | ||
"glob": { | ||
"type": "string" | ||
}, | ||
"modulesLevels": { | ||
@@ -87,2 +92,9 @@ "type": "object", | ||
} | ||
}, | ||
"ignoreInnerPathsForModules": { | ||
"type": "array", | ||
"items": [], | ||
"additionalItems": { | ||
"type": "string" | ||
} | ||
} | ||
@@ -96,12 +108,20 @@ } | ||
const sourceFilePath = context.getFilename(); | ||
const sourceFileRelativePath = sourceFilePath.replace(context.getCwd(), ''); | ||
const closestPkgToSource = findUp.sync('package.json', {cwd: sourceFilePath, type: 'file'}); | ||
const modulesMapping = getModulesMappingForPkg(closestPkgToSource); | ||
const { | ||
modulesLevels = defaultModulesLevels, | ||
middleModulesLevel = defaultMiddleModulesLevel, | ||
moduleInnerPaths = defaultModuleInnerPaths | ||
moduleInnerPaths = defaultModuleInnerPaths, | ||
ignoreInnerPathsForModules = defaultIgnoreInnerPathsForModules, | ||
glob, | ||
} = context.options[0] || defaultOptions; | ||
const shouldLint = !glob || minimatch(sourceFileRelativePath, glob); | ||
if (!shouldLint) { | ||
return {}; | ||
} | ||
const closestPkgToSource = findUp.sync('package.json', {cwd: sourceFilePath, type: 'file'}); | ||
const modulesMapping = getModulesMappingForPkg(closestPkgToSource); | ||
return { | ||
@@ -122,3 +142,3 @@ 'Program > ImportDeclaration'(node) { | ||
if (!isCorrectModuleImportPath(importRequest, {modulesMapping, moduleInnerPaths})) { | ||
if (!isCorrectModuleImportPath(importRequest, {modulesMapping, moduleInnerPaths, ignoreInnerPathsForModules})) { | ||
context.report({ | ||
@@ -125,0 +145,0 @@ node, |
{ | ||
"name": "eslint-plugin-welldone", | ||
"description": "Custom eslint rules helpful for our needs", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"engines": { | ||
@@ -20,2 +20,3 @@ "node": ">=10.0.0" | ||
"find-up": "^4.1.0", | ||
"minimatch": "^3.0.4", | ||
"requireindex": "^1.2.0" | ||
@@ -22,0 +23,0 @@ }, |
@@ -36,3 +36,3 @@ # eslint-plugin-welldone | ||
"rules": { | ||
"welldone/rule-name": 2 | ||
"welldone/modules-engagement": "error", | ||
} | ||
@@ -42,4 +42,31 @@ } | ||
these are the possible options for the `welldone/modules-engagement` rule: | ||
```json | ||
{ | ||
"rules": { | ||
"welldone/modules-engagement": ["error", { | ||
// provide a glob to only lint certain paths. F.E: | ||
// glob": "/packages/!(common-package)/**/!(*.stories|*.test).js" | ||
"glob": null, | ||
// Levels to enforce imports between modules | ||
"modulesLevels": {'common': 1, 'shared': 1, 'app': 3}, | ||
// Default level for other modules | ||
"middleModulesLevel": 2, | ||
// Allow importing from inner paths in modules. F.E: | ||
// "moduleInnerPaths": ['/components'] | ||
"moduleInnerPaths": [], | ||
// Array of modules that can be used not only from their root path | ||
"ignoreInnerPathsForModules": ['common', 'shared'] | ||
}], | ||
} | ||
} | ||
``` | ||
## Supported Rules | ||
- Fill in provided rules here |
9795
20.29%204
9.68%71
61.36%3
50%+ Added
+ Added
+ Added
+ Added
+ Added