Skip to content

Commit

Permalink
fix(graph-utl): lets findRuleByName also search in 'required' rules (#…
Browse files Browse the repository at this point in the history
…931)

## Description

- lets findRuleByName also search in 'required' rules

## Motivation and Context

fixes #930 

## How Has This Been Tested?

- [x] green ci
- [x] additional automated non-regression test

## Screenshots

With this rule added to dependency-cruiser's own config
```javascript
// ...
  "required": [
    {
      "name": "something-required",
      "comment": "Foo bar baz quux garply waldo fred plugh xyzzy thud",
      "severity": "warn",
      "module": {
        "path": "^src/meta.cjs$"
      },
      "to": {
        "path": "^test/kannie"
      }
    }
  ],
// ...
```

... `npx depcruise --progress -T err-long` shows this:


```
  warn something-required: src/meta.cjs
    Foo bar baz quux garply waldo fred plugh xyzzy thud


✘ 1 dependency violations (0 errors, 1 warnings). 545 modules, 1325 dependencies cruised.
⚠ 7 known violations ignored. Run with --no-ignore-known to see them.
```

## Types of changes

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] Documentation only change
- [ ] Refactor (non-breaking change which fixes an issue without
changing functionality)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
  • Loading branch information
sverweij committed Apr 13, 2024
1 parent 9a523b4 commit 15d692e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/graph-utl/rule-set.mjs
Expand Up @@ -10,9 +10,10 @@
* @return {import("../../types/rule-set").IForbiddenRuleType|undefined} - a rule (or 'undefined' if nothing found)
*/
export function findRuleByName(pRuleSet, pName) {
return (pRuleSet?.forbidden ?? []).find(
(pForbiddenRule) => pForbiddenRule.name === pName,
const lNamedRules = (pRuleSet?.forbidden ?? []).concat(
pRuleSet?.required ?? [],
);
return lNamedRules.find((pRule) => pRule.name === pName);
}

function ruleHasALicenseLikeAttribute(pRule) {
Expand Down
21 changes: 21 additions & 0 deletions test/graph-utl/rule-set.spec.mjs
Expand Up @@ -34,6 +34,27 @@ describe("[U] graph-utl/rule-set - findRuleByName", () => {
to: {},
});
});
it("also finds 'required' types rules, provided they're named", () => {
const lRuleSetWithRequiredRule = {
...lRuleSet,
required: [
{
name: "some-required-rule",
comment: "heide does hok schapen",
severity: "warn",
from: {},
to: {},
},
],
};
deepEqual(findRuleByName(lRuleSetWithRequiredRule, "some-required-rule"), {
name: "some-required-rule",
comment: "heide does hok schapen",
severity: "warn",
from: {},
to: {},
});
});
});

describe("[U] graph-utl/rule-set - ruleSetHasLicenseRule", () => {
Expand Down

0 comments on commit 15d692e

Please sign in to comment.