eslint-plugin-welldone
Advanced tools
Comparing version 0.2.2 to 0.3.0
@@ -27,13 +27,13 @@ const path = require('path'); | ||
getModulesMappingForPkg(pkg) { | ||
if (!cachedModulesForPkg[pkg]) { | ||
const pkgDirName = path.dirname(pkg) | ||
const pathWithSrc = path.join(pkgDirName, 'src'); | ||
const modulesPath = fs.existsSync(pathWithSrc) ? pathWithSrc : pkgDirName; | ||
cachedModulesForPkg[pkg] = fs.readdirSync(modulesPath).reduce((result, moduleName) => { | ||
result[moduleName] = path.join(modulesPath, moduleName); | ||
getModulesMappingForPkg({modulesDir}) { | ||
if (!cachedModulesForPkg[modulesDir]) { | ||
if (!fs.existsSync(modulesDir)) { | ||
return; | ||
} | ||
cachedModulesForPkg[modulesDir] = fs.readdirSync(modulesDir).reduce((result, moduleName) => { | ||
result[moduleName] = path.join(modulesDir, moduleName); | ||
return result; | ||
}, {}); | ||
} | ||
return cachedModulesForPkg[pkg]; | ||
return cachedModulesForPkg[modulesDir]; | ||
}, | ||
@@ -40,0 +40,0 @@ |
@@ -15,12 +15,18 @@ const path = require('path'); | ||
function isRelativeModuleImport(sourceFilePath, importRequest, {modulesMapping}) { | ||
function isRelativeModuleImport(sourceFilePath, importRequest, {modulesDir, modulesMapping}) { | ||
const relativeBackImport = importRequest.startsWith('..'); | ||
if (!relativeBackImport) { | ||
return; | ||
return false; | ||
} | ||
const importedPath = path.resolve(sourceFilePath, importRequest); | ||
const targetModuleName = getModuleNameFromPath(sourceFilePath, {modulesMapping}); | ||
const importRequestParts = importRequest.split('/') | ||
const twoDotsOfRequest = importRequestParts.filter(part => part === '..').join('/'); | ||
const twoDotsOfRequestResolved = path.resolve(path.dirname(sourceFilePath), twoDotsOfRequest); | ||
return !!targetModuleName; | ||
const resolvedFromAboveAModule = ( | ||
twoDotsOfRequestResolved === modulesDir || | ||
!twoDotsOfRequestResolved.includes(modulesDir) | ||
); | ||
return resolvedFromAboveAModule; | ||
} | ||
@@ -56,2 +62,3 @@ | ||
const defaultMiddleModulesLevel = 2; | ||
const defaultModulesPath = 'src'; | ||
const defaultModuleInnerPaths = []; | ||
@@ -77,2 +84,5 @@ const defaultIgnoreInnerPathsForModules = ['common', 'shared'] | ||
}, | ||
"modulesPath": { | ||
"type": "string" | ||
}, | ||
"modulesLevels": { | ||
@@ -113,2 +123,3 @@ "type": "object", | ||
ignoreInnerPathsForModules = defaultIgnoreInnerPathsForModules, | ||
modulesPath = defaultModulesPath, | ||
glob, | ||
@@ -122,4 +133,8 @@ } = context.options[0] || defaultOptions; | ||
const closestPkgToSource = findUp.sync('package.json', {cwd: sourceFilePath, type: 'file'}); | ||
const modulesMapping = getModulesMappingForPkg(closestPkgToSource); | ||
const closestPkgDir = findUp.sync('package.json', {cwd: sourceFilePath, type: 'file'}).replace(/package.json$/, ''); | ||
const modulesDir = path.join(closestPkgDir, modulesPath); | ||
const modulesMapping = getModulesMappingForPkg({modulesDir}); | ||
if (!modulesMapping) { | ||
return {}; | ||
} | ||
@@ -131,3 +146,3 @@ return { | ||
if (isRelative(importRequest)) { | ||
if (isRelativeModuleImport(sourceFilePath, importRequest, {modulesMapping})) { | ||
if (isRelativeModuleImport(sourceFilePath, importRequest, {modulesMapping, modulesDir})) { | ||
context.report({node, message: 'Import between modules should be absolute. expected: `import {x} from \'module\'`'}); | ||
@@ -134,0 +149,0 @@ } |
{ | ||
"name": "eslint-plugin-welldone", | ||
"description": "Custom eslint rules helpful for our needs", | ||
"version": "0.2.2", | ||
"version": "0.3.0", | ||
"engines": { | ||
@@ -6,0 +6,0 @@ "node": ">=10.0.0" |
@@ -41,4 +41,14 @@ # eslint-plugin-welldone | ||
these are the possible options for the `welldone/modules-engagement` rule: | ||
## Plugins | ||
### Rules of Modules Engagement | ||
([`welldone/modules-engagement`](./rules/modules-engagement)) | ||
This rule enforces Welldone's standard file structure. | ||
[More details about the rule](https://welldone-software.gitbook.io/welldone-file-structure/modules/modules#rules-of-engagement) | ||
can be found here. | ||
### config: | ||
These are the possible configs and their defaults for the rule: | ||
```json5 | ||
@@ -52,2 +62,5 @@ { | ||
// path of modules under package.json's | ||
"modulesPath": "/src", | ||
// Levels to enforce imports between modules | ||
@@ -54,0 +67,0 @@ "modulesLevels": {'common': 1, 'shared': 1, 'app': 3}, |
10737
218
84