eslint-plugin-boundaries 2.6.0 → 2.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -259,7 +259,7 @@ Some rules require extra configuration, and it has to be defined in each specifi
259
259
 
260
260
  #### Main format of rules options
261
261
 
262
- The docs of each rule contains an specification of their own options, but __the main rules share the format in which the options have to be defined__. The format described here is valid for options of [`element-types`](docs/rules/element-types.md), [`external`](docs/rules/external.md) and [`entry-point`](docs/rules/entry-point.md) rules, __except the `message` properties, which for the moment are only supported in the [`element-types`](docs/rules/element-types.md) rule settings.
262
+ The docs of each rule contains an specification of their own options, but __the main rules share the format in which the options have to be defined__. The format described here is valid for options of [`element-types`](docs/rules/element-types.md), [`external`](docs/rules/external.md) and [`entry-point`](docs/rules/entry-point.md) rules, __except the `message` property, which for the moment is only supported in the [`element-types`](docs/rules/element-types.md) and [`entry-point`](docs/rules/entry-point.md) rules settings.
263
263
 
264
264
  Options set an "allow/disallow" value by default, and provide an array of rules. Each matching rule will override the default value and the value returned by previous matching rules. So, the final result of the options, once processed for each case, will be "allow" or "disallow", and this value will be applied by the plugin rule in the correspondant way, making it to produce an eslint error or not.
265
265
 
@@ -300,7 +300,7 @@ Remember that:
300
300
 
301
301
  * __`from/target`__: `<element matchers>` Depending of the rule to which the options are for, the rule will be applied only if the file being analized matches with this element matcher (`from`), or the dependency being imported matches with this element matcher (`target`).
302
302
  * __`disallow/allow`__: `<value matchers>` If the plugin rule target matches with this, then the result of the rule will be "disallow/allow". Each rule will require a type of value here depending of what it is checking. In the case of the `element-types` rule, for example, another `<element matcher>` has to be provided in order to check the type of the local dependency.
303
- * __`message`__: `<string>` Optional. If the rule results in an error, the plugin will return this message.
303
+ * __`message`__: `<string>` Optional. If the rule results in an error, the plugin will return this message instead of the default one. Read [error messages](#error-messages) for further info.
304
304
 
305
305
  > Tip: Properties `from/target` and `disallow/allow` can receive a single matcher, or an array of matchers.
306
306
 
@@ -317,7 +317,7 @@ Elements matchers used in the rules options can have the next formats:
317
317
 
318
318
  The plugin returns a different default message for each rule, check the documentation of each one for further info. But some rules support defining custom messages in their configuration, as seen in ["Main format of rules options"](#main-format-of-rules-options).
319
319
 
320
- When defining custom messages, it is possible to provide information about the type or captured properties of the current file or dependency. Use `${file.PROPERTY}` or `${dependency.PROPERTY}`, and it will be replaced by the correspondent captured value from the file or the dependency:
320
+ When defining custom messages, it is possible to provide information about the current file or dependency. Use `${file.PROPERTY}` or `${dependency.PROPERTY}`, and it will be replaced by the correspondent captured value from the file or the dependency:
321
321
 
322
322
  ```jsonc
323
323
  {
@@ -327,6 +327,13 @@ When defining custom messages, it is possible to provide information about the t
327
327
  }
328
328
  ```
329
329
 
330
+ Available properties in error templates both from `file` or `dependency` are:
331
+
332
+ * `type`: Element's type.
333
+ * `internalPath`: File path being analyzed or imported. Relative to the element's root path.
334
+ * ...All captured properties are also available
335
+
336
+
330
337
  > Tip: Read ["Global settings"](#global-settings) for further info about how to capture values from elements.
331
338
 
332
339
  ##### Advanced example of a rule configuration
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-boundaries",
3
- "version": "2.6.0",
3
+ "version": "2.7.0",
4
4
  "description": "Eslint plugin checking architecture boundaries between elements",
5
5
  "keywords": [
6
6
  "eslint",
@@ -39,13 +39,13 @@
39
39
  },
40
40
  "devDependencies": {
41
41
  "cross-env": "7.0.3",
42
- "eslint": "8.2.0",
42
+ "eslint": "8.3.0",
43
43
  "eslint-config-prettier": "8.3.0",
44
44
  "eslint-plugin-prettier": "4.0.0",
45
45
  "husky": "7.0.4",
46
46
  "is-ci": "3.0.1",
47
47
  "jest": "27.3.1",
48
- "lint-staged": "11.2.6",
48
+ "lint-staged": "12.1.2",
49
49
  "prettier": "2.4.1"
50
50
  },
51
51
  "lint-staged": {
@@ -82,7 +82,7 @@ function ruleElementMessage(elementPatterns, elementCapturedValues) {
82
82
  function customErrorMessage(message, file, dependency) {
83
83
  return replaceObjectValuesInTemplates(
84
84
  replaceObjectValuesInTemplates(message, { ...file.capturedValues, type: file.type }, "file"),
85
- { ...dependency.capturedValues, type: dependency.type },
85
+ { ...dependency.capturedValues, type: dependency.type, internalPath: dependency.internalPath },
86
86
  "dependency"
87
87
  );
88
88
  }
@@ -8,6 +8,7 @@ const {
8
8
  isMatchElementKey,
9
9
  elementRulesAllowDependency,
10
10
  } = require("../helpers/rules");
11
+ const { customErrorMessage, ruleElementMessage, elementMessage } = require("../helpers/messages");
11
12
 
12
13
  function isMatchElementInternalPath(elementInfo, matcher, options) {
13
14
  return isMatchElementKey(elementInfo, matcher, options, "internalPath");
@@ -20,7 +21,23 @@ function elementRulesAllowEntryPoint(element, dependency, options) {
20
21
  options,
21
22
  isMatch: isMatchElementInternalPath,
22
23
  rulesMainKey: "target",
23
- }).result;
24
+ });
25
+ }
26
+
27
+ function errorMessage(ruleData, file, dependency) {
28
+ const ruleReport = ruleData.ruleReport;
29
+ if (ruleReport.message) {
30
+ return customErrorMessage(ruleReport.message, file, dependency);
31
+ }
32
+ if (ruleReport.isDefault) {
33
+ return `No rule allows the entry point '${
34
+ dependency.internalPath
35
+ }' in dependencies ${elementMessage(dependency)}`;
36
+ }
37
+ return `The entry point '${dependency.internalPath}' is not allowed in ${ruleElementMessage(
38
+ ruleReport.element,
39
+ dependency.capturedValues
40
+ )}. Disallowed in rule ${ruleReport.index + 1}`;
24
41
  }
25
42
 
26
43
  module.exports = dependencyRule(
@@ -29,20 +46,20 @@ module.exports = dependencyRule(
29
46
  description: `Check entry point used for each element type`,
30
47
  schema: rulesOptionsSchema({
31
48
  rulesMainKey: "target",
49
+ customMessage: true,
50
+ customRuleMessage: true,
32
51
  }),
33
52
  },
34
53
  function ({ dependency, file, node, context, options }) {
35
- if (
36
- !dependency.isIgnored &&
37
- dependency.type &&
38
- !dependency.isInternal &&
39
- !elementRulesAllowEntryPoint(file, dependency, options)
40
- ) {
41
- context.report({
42
- message: `Entry point '${dependency.internalPath}' is not allowed in '${dependency.type}'`,
43
- node: node,
44
- ...dependencyLocation(node, context),
45
- });
54
+ if (!dependency.isIgnored && dependency.type && !dependency.isInternal) {
55
+ const ruleData = elementRulesAllowEntryPoint(file, dependency, options);
56
+ if (!ruleData.result) {
57
+ context.report({
58
+ message: errorMessage(ruleData, file, dependency),
59
+ node: node,
60
+ ...dependencyLocation(node, context),
61
+ });
62
+ }
46
63
  }
47
64
  },
48
65
  {