forked from sverweij/dependency-cruiser
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(resolve): only read license/ deprecation when used in rule set (s…
…verweij#412) When 'normalising' the options, it determines whether a license check and/ or a deprecation check against external dependencies is necessary. If it isn't necessary skipping these checks saves a significant amount of time (averages over consecutive runs on a 2,6 GHz Quad-Core Intel Core i7 with an SSD with APFS - macOS 10.15.7); - For dependency-cruiser's own scan it saved ~1.5s (for the license check) and ~0.3s (for the deprecation check) on a total run time taking ~3.2s (for 395 modules, 939 dependencies) before these improvements. - For yarn 'berry' it saved ~2s (license check + deprecation check) on a total run time originaly taking ~9.5s (for 745 modules, 1262 dependencies using ts -> ast ) before these improvements.
- Loading branch information
Showing
18 changed files
with
222 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const _get = require("lodash/get"); | ||
const _has = require("lodash/has"); | ||
|
||
/** | ||
* Finds the first rule in the rule set that has name pName, | ||
* and undefined if no such rule exists/ the rule is an 'allowed' | ||
* rule. | ||
* | ||
* (this thing probably belongs in a model-like folder and not in utl) | ||
* | ||
* @param {import("../../types/configuration").IConfiguration} pRuleSet - The rule set to search in | ||
* @param {string} pName - The rule name to look for | ||
* @return {import("../../types/rule-set").IForbiddenRuleType|import("../../types/rule-set").IAllowedRuleType} - a rule (or 'undefined' if nothing found) | ||
*/ | ||
function findRuleByName(pRuleSet, pName) { | ||
return _get(pRuleSet, "forbidden", []).find( | ||
(pForbiddenRule) => pForbiddenRule.name === pName | ||
); | ||
} | ||
|
||
/** | ||
* Returns true if the rule set has a rule that uses the 'license' or | ||
* 'licenseNot' attribute. | ||
* | ||
* Returns false in all other cases | ||
* | ||
* @param {import('../../../types/rule-set').IFlattenedRuleSet} pRuleSet | ||
* @return {boolean} | ||
*/ | ||
function ruleSetHasLicenseRule(pRuleSet) { | ||
return ( | ||
_get(pRuleSet, "forbidden", []).some( | ||
(pRule) => _has(pRule.to, "license") || _has(pRule.to, "licenseNot") | ||
) || | ||
_get(pRuleSet, "allowed", []).some( | ||
(pRule) => _has(pRule.to, "license") || _has(pRule.to, "licenseNot") | ||
) | ||
); | ||
} | ||
function ruleSetHasDeprecationRule(pRuleSet) { | ||
return ( | ||
_get(pRuleSet, "forbidden", []).some((pRule) => | ||
_get(pRule.to, "dependencyTypes", []).includes("deprecated") | ||
) || | ||
_get(pRuleSet, "allowed", []).some((pRule) => | ||
_get(pRule.to, "dependencyTypes", []).includes("deprecated") | ||
) | ||
); | ||
} | ||
|
||
module.exports = { | ||
findRuleByName, | ||
ruleSetHasLicenseRule, | ||
ruleSetHasDeprecationRule, | ||
}; | ||
|
||
// file deepcode ignore valid-jsdoc: deepcode believes this isn't valid, but likely it just isn't privy on imports as types |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* eslint-disable no-unused-expressions */ | ||
const { expect } = require("chai"); | ||
const { | ||
findRuleByName, | ||
ruleSetHasLicenseRule, | ||
ruleSetHasDeprecationRule, | ||
} = require("../../src/graph-utl/rule-set"); | ||
|
||
describe("graph-utl/rule-set - findRuleByName", () => { | ||
const lRuleSet = { | ||
forbidden: [{ name: "a-rule", severity: "warn", from: {}, to: {} }], | ||
}; | ||
|
||
it("returns undefined for null rule set/ null rule name", () => { | ||
expect(findRuleByName(null, null)).to.be.undefined; | ||
}); | ||
it("returns undefined for empty rule set/ null rule name", () => { | ||
expect(findRuleByName({}, null)).to.be.undefined; | ||
}); | ||
it("returns undefined for empty rule set/ non-null rule name", () => { | ||
expect(findRuleByName({}, "non-null-rule-name")).to.be.undefined; | ||
}); | ||
it("returns undefined for undefined rule set/ non-null rule name", () => { | ||
// eslint-disable-next-line no-undefined | ||
expect(findRuleByName(undefined, "non-null-rule-name")).to.be.undefined; | ||
}); | ||
it("returns undefined if the rule is not in there", () => { | ||
// eslint-disable-next-line no-undefined | ||
expect(findRuleByName(lRuleSet, "another-rule")).to.be.undefined; | ||
}); | ||
it("returns the rule if the rule is in there", () => { | ||
expect(findRuleByName(lRuleSet, "a-rule")).to.deep.equal({ | ||
name: "a-rule", | ||
severity: "warn", | ||
from: {}, | ||
to: {}, | ||
}); | ||
}); | ||
}); | ||
|
||
describe("graph-utl/rule-set - ruleSetHasLicenseRule", () => { | ||
it("returns false for an empty rule set", () => { | ||
expect(ruleSetHasLicenseRule({})).to.equal(false); | ||
}); | ||
it("returns false when rule set no rules with license-like attributes", () => { | ||
expect( | ||
ruleSetHasLicenseRule({ | ||
forbidden: [{ from: {}, to: {} }], | ||
allowed: [{ from: {}, to: {} }], | ||
}) | ||
).to.equal(false); | ||
}); | ||
it("returns true when rule set has a forbidden rule with a license attribute", () => { | ||
expect( | ||
ruleSetHasLicenseRule({ | ||
forbidden: [{ from: {}, to: { license: "commercial" } }], | ||
}) | ||
).to.equal(true); | ||
}); | ||
it("returns true when rule set doesn't have a forbidden rule with license like attributes", () => { | ||
expect( | ||
ruleSetHasLicenseRule({ | ||
forbidden: [{ from: {}, to: { licenseNot: "" } }], | ||
}) | ||
).to.equal(true); | ||
}); | ||
it("returns true when rule set has a forbidden rule with a licenseNot attribute", () => { | ||
expect( | ||
ruleSetHasLicenseRule({ | ||
forbidden: [{ from: {}, to: { licenseNot: "MIT" } }], | ||
}) | ||
).to.equal(true); | ||
}); | ||
it("returns true when rule set has an allowed rule with a license attribute", () => { | ||
expect( | ||
ruleSetHasLicenseRule({ | ||
allowed: [{ from: {}, to: { license: "commercial" } }], | ||
}) | ||
).to.equal(true); | ||
}); | ||
it("returns true when rule set has an allowed rule with a licenseNot attribute", () => { | ||
expect( | ||
ruleSetHasLicenseRule({ | ||
allowed: [{ from: {}, to: { licenseNot: "MIT" } }], | ||
}) | ||
).to.equal(true); | ||
}); | ||
}); | ||
|
||
describe("graph-utl/rule-set - ruleSetHasDeprecation", () => { | ||
it("returns false for an empty rule set", () => { | ||
expect(ruleSetHasDeprecationRule({})).to.equal(false); | ||
}); | ||
it("returns false when rule set no rules with license-like attributes", () => { | ||
expect( | ||
ruleSetHasDeprecationRule({ | ||
forbidden: [{ from: {}, to: { dependencyTypes: ["npm"] } }], | ||
allowed: [{ from: {}, to: {} }], | ||
}) | ||
).to.equal(false); | ||
}); | ||
it("returns true when there's a 'forbidden' rule that checks for external module deprecation", () => { | ||
expect( | ||
ruleSetHasDeprecationRule({ | ||
forbidden: [{ from: {}, to: { dependencyTypes: ["deprecated"] } }], | ||
allowed: [{ from: {}, to: {} }], | ||
}) | ||
).to.equal(true); | ||
}); | ||
it("returns true when there's an 'allowed' rule that checks for external module deprecation", () => { | ||
expect( | ||
ruleSetHasDeprecationRule({ | ||
forbidden: [{ from: {}, to: {} }], | ||
allowed: [{ from: {}, to: { dependencyTypes: ["deprecated"] } }], | ||
}) | ||
).to.equal(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.