Skip to content

Commit

Permalink
Merge pull request #326 from javierbrea/require-dependency-node
Browse files Browse the repository at this point in the history
feat: Add require dependency node
  • Loading branch information
javierbrea committed Jan 16, 2024
2 parents 3f7a221 + 256760b commit 268a67c
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 10 deletions.
97 changes: 94 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
module.exports = {
root: true,
env: {
node: true,
es6: true,
},
parserOptions: {
ecmaVersion: 2022,
},
plugins: ["prettier"],
extends: ["prettier"],
plugins: ["prettier", "eslint-plugin-local-rules"],
rules: {
"prettier/prettier": [
"error",
Expand All @@ -19,6 +21,95 @@ module.exports = {
"no-undef": "error",
"no-unused-vars": ["error", { vars: "all", args: "after-used", ignoreRestSiblings: false }],
},
extends: ["prettier"],
root: true,
overrides: [
{
files: ["src/**/*.js"],
settings: {
"boundaries/dependency-nodes": ["require", "import", "dynamic-import", "export"],
"boundaries/elements": [
{
type: "config",
mode: "file",
pattern: ["src/configs/*.js", "(package.json)"],
capture: ["name"],
},
{
type: "constants",
mode: "file",
pattern: "src/constants/*.js",
capture: ["name"],
},
{
type: "core",
mode: "file",
pattern: "src/core/*.js",
capture: ["name"],
},
{
type: "helper",
mode: "file",
pattern: "src/helpers/*.js",
capture: ["name"],
},
{
type: "rule",
mode: "file",
pattern: "src/rules/*.js",
capture: ["name"],
},
{
type: "rule-factory",
mode: "file",
pattern: "src/rules-factories/*.js",
capture: ["name"],
},
{
type: "plugin",
mode: "full",
pattern: ["src/index.js"],
},
],
},
rules: {
"local-rules/boundaries/element-types": [
2,
{
default: "disallow",
rules: [
{
from: "plugin",
allow: ["constants", "config", "rule"],
},
{
from: "config",
allow: ["constants", "config"],
},
{
from: "constants",
allow: ["constants"],
},
{
from: "core",
allow: ["constants", "helper", "core"],
},
{
from: "helper",
allow: ["constants", "helper"],
},
{
from: "rule",
allow: ["constants", "helper", "core", "rule-factory"],
},
{
from: "rule-factory",
allow: ["constants", "helper", "core"],
},
],
},
],
"local-rules/boundaries/no-unknown": [2],
"local-rules/boundaries/no-unknown-files": [2],
},
},
],
};
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
### Removed
### BREAKING CHANGES

## [4.2.0] - 2024-01-16

### Added
- feat: Add `require` dependency node, enabling to analyze dependencies in `require(...)` calls
- chore: Lint code using eslint-plugin-boundaries in its own codebase

## [4.1.0] - 2024-01-13

### Changed
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ In words of Robert C. Martin, _"Software architecture is the art of drawing line

__This plugin ensures that your architecture boundaries are respected by the elements in your project__ checking the folders and files structure and the dependencies between them. __It is not a replacement for [eslint-plugin-import](https://www.npmjs.com/package/eslint-plugin-import), on the contrary, the combination of both plugins is recommended.__

By default, __the plugin works by checking `import` statements, but it is also able to analyze exports, dynamic imports, and can be configured to check any other [AST nodes](https://eslint.org/docs/latest/extend/selectors)__. (_Read the [main rules overview](#main-rules-overview) and [configuration](#configuration) chapters for better comprehension_)
By default, __the plugin works by checking `import` statements, but it is also able to analyze "require", "exports" and dynamic imports, and can be configured to check any other [AST nodes](https://eslint.org/docs/latest/extend/selectors)__. (_Read the [main rules overview](#main-rules-overview) and [configuration](#configuration) chapters for better comprehension_)

## Table of Contents

Expand Down Expand Up @@ -207,6 +207,7 @@ This setting allows to modify built-in default dependency nodes. By default, the

The setting should be an array of the following strings:

* `'require'`: analyze `require` statements.
* `'import'`: analyze `import` statements.
* `'export'`: analyze `export` statements.
* `'dynamic-import'`: analyze [dynamic import](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import) statements.
Expand Down
8 changes: 8 additions & 0 deletions eslint-local-rules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const eslintPluginBoundaries = require("./src/index.js");

module.exports = Object.keys(eslintPluginBoundaries.rules).reduce((rules, ruleName) => {
return {
...rules,
[`boundaries/${ruleName}`]: eslintPluginBoundaries.rules[ruleName],
};
}, {});
17 changes: 15 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-boundaries",
"version": "4.1.0",
"version": "4.2.0",
"description": "Eslint plugin checking architecture boundaries between elements",
"keywords": [
"eslint",
Expand All @@ -21,6 +21,7 @@
],
"main": "index.js",
"scripts": {
"eslint": "eslint",
"lint": "eslint src *.js test",
"lint-staged": "lint-staged",
"test": "jest",
Expand All @@ -45,6 +46,7 @@
"eslint-config-prettier": "9.0.0",
"eslint-import-resolver-typescript": "3.6.1",
"eslint-plugin-prettier": "5.0.1",
"eslint-plugin-local-rules": "2.0.1",
"husky": "8.0.3",
"is-ci": "3.0.1",
"jest": "29.7.0",
Expand Down
4 changes: 2 additions & 2 deletions sonar-project.properties
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
sonar.organization=javierbrea
sonar.projectKey=javierbrea_eslint-plugin-boundaries
sonar.projectVersion=4.1.0
sonar.projectVersion=4.2.0

sonar.javascript.file.suffixes=.js
sonar.sourceEncoding=UTF-8
sonar.exclusions=node_modules/**
sonar.cpd.exclusions=test/**/*
sonar.coverage.exclusions=test/**/*,index.js,stryker.conf.js,jest.config.js,resolver-legacy-alias/**/*
sonar.coverage.exclusions=test/**/*,index.js,stryker.conf.js,jest.config.js,resolver-legacy-alias/**/*,eslint-local-rules.js
sonar.javascript.lcov.reportPaths=coverage/lcov.info
sonar.host.url=https://sonarcloud.io
7 changes: 7 additions & 0 deletions src/constants/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ module.exports = {

VALID_DEPENDENCY_NODE_KINDS: ["value", "type"],
DEFAULT_DEPENDENCY_NODES: {
require: [
// Note: detects "require('source')"
{
selector: "CallExpression[callee.name=require] > Literal",
kind: "value",
},
],
import: [
// Note: detects "import x from 'source'"
{ selector: "ImportDeclaration:not([importKind=type]) > Literal", kind: "value" },
Expand Down
19 changes: 18 additions & 1 deletion test/rules/one-level/element-types-dependency-nodes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const typescriptSettings = {
"boundaries/dependency-nodes": ["import"],
};
const dependencyNodesSettings = {
"boundaries/dependency-nodes": ["import", "export", "dynamic-import"],
"boundaries/dependency-nodes": ["require", "import", "export", "dynamic-import"],
"boundaries/additional-dependency-nodes": [
{
// mock('source')
Expand Down Expand Up @@ -146,6 +146,12 @@ createRuleTester({
code: "mock('helpers/helper-a')",
options,
},
// Components can require helpers
{
filename: absoluteFilePath("components/component-a/ComponentA.js"),
code: "require('helpers/helper-a')",
options,
},
],
invalid: [
// Helpers can't export value from another helper
Expand Down Expand Up @@ -192,6 +198,17 @@ createRuleTester({
},
],
},
// Helpers can't require another helper
{
filename: absoluteFilePath("helpers/helper-a/HelperA.js"),
code: "require('helpers/helper-b')",
options,
errors: [
{
type: "Literal",
},
],
},
],
});

Expand Down

0 comments on commit 268a67c

Please sign in to comment.