This repository has been archived by the owner on May 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[package-json-lint] Add rule for hard coded dependencies in module an…
…d devModule. (#704) * added new rule for hard coded dependencies * minor change * updated projectType to devModule for toolkit * Added jest tests * minor change * Added allowList to the severity object * Added compatibleRegex array * minor changes * Added documentation for package-json-lint * updated changelog * minor changes * minor chnages * Update Rules.2.tool.mdx Removed extension from the import. * minorChanges * Update packages/terra-functional-testing/CHANGELOG.md Co-authored-by: Akarsh Shetty <[email protected]> * updated condition to use semver.clean Co-authored-by: Akarsh Shetty <[email protected]>
- Loading branch information
1 parent
a21de7e
commit 96aab2c
Showing
25 changed files
with
483 additions
and
19 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
5 changes: 3 additions & 2 deletions
5
packages/package-json-lint-config-terra/package-json-lint.config.js
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
module.exports = { | ||
rules: { | ||
'require-no-terra-base-peer-dependency-versions': 'warn', | ||
'require-theme-context-versions': 'warn', | ||
'require-no-hard-coded-dependency-versions': { severityType: 'error' }, | ||
'require-no-terra-base-peer-dependency-versions': { severityType: 'warn' }, | ||
'require-theme-context-versions': { severityType: 'warn' }, | ||
}, | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
const requireThemeContextVersions = require('./require-theme-context-versions'); | ||
const requireNoTerraBasePeerDependencyVersions = require('./require-no-terra-base-peer-dependency-versions'); | ||
const requireNoHardCodedDependencyVersions = require('./require-no-hard-coded-dependency-versions'); | ||
|
||
module.exports = { | ||
'require-no-terra-base-peer-dependency-versions': requireNoTerraBasePeerDependencyVersions, | ||
'require-theme-context-versions': requireThemeContextVersions, | ||
'require-no-hard-coded-dependency-versions': requireNoHardCodedDependencyVersions, | ||
}; |
33 changes: 33 additions & 0 deletions
33
packages/package-json-lint/src/rules/require-no-hard-coded-dependency-versions.js
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,33 @@ | ||
const semver = require('semver'); | ||
|
||
const documentation = { | ||
ruleName: 'require-no-hard-coded-dependency-versions', | ||
defaultValue: 'error', | ||
description: "This rule doesn't allow any hard-coded dependencies to be passed in the package.json. Only applies for module and devModule.", | ||
}; | ||
|
||
module.exports = { | ||
create: ({ ruleConfig, projectType, report }) => ({ | ||
dependencies: (dependencies) => { | ||
if (projectType === 'module' || projectType === 'devModule') { | ||
const messageString = 'require-no-hard-coded-dependency-versions'; | ||
const currentProblems = Object.keys(dependencies).map(dependencyName => { | ||
const dependencyVersion = dependencies[dependencyName]; | ||
const isCompatibleVersion = semver.clean(dependencyVersion) === null; | ||
if (!isCompatibleVersion && !(ruleConfig.severity.allowList && ruleConfig.severity.allowList.includes(dependencyName))) { | ||
return `${dependencyName}@${dependencyVersion} does not satisfy requirement for the ${messageString} rule.`; | ||
} | ||
return undefined; | ||
}).filter(problem => !!problem); | ||
|
||
if (currentProblems.length) { | ||
const lintMessage = `The dependencies for this project have hard-coded versions that violates the ${messageString} rule:\n ${currentProblems.join('\n ')}`; | ||
report({ | ||
lintId: messageString, severity: ruleConfig.severity.severityType, lintMessage, projectType, | ||
}); | ||
} | ||
} | ||
}, | ||
}), | ||
documentation, | ||
}; |
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
31 changes: 31 additions & 0 deletions
31
...int/tests/jest/rules/__snapshots__/require-no-hard-coded-dependency-versions.test.js.snap
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,31 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`require-no-hard-coded-dependency-versions succeeds when hardcoded dependency is passed in the allowList 1`] = `undefined`; | ||
|
||
exports[`require-no-hard-coded-dependency-versions when projectType is application succeeds when there are hardcoded dependencies 1`] = `undefined`; | ||
|
||
exports[`require-no-hard-coded-dependency-versions when projectType is application succeeds when there are no hardcoded dependencies 1`] = `undefined`; | ||
|
||
exports[`require-no-hard-coded-dependency-versions when projectType is devModule fails when there are hardcoded dependencies 1`] = ` | ||
Object { | ||
"lintId": "require-no-hard-coded-dependency-versions", | ||
"lintMessage": "The dependencies for this project have hard-coded versions that violates the require-no-hard-coded-dependency-versions rule: | ||
a@1.0.0 does not satisfy requirement for the require-no-hard-coded-dependency-versions rule.", | ||
"projectType": "devModule", | ||
"severity": "error", | ||
} | ||
`; | ||
|
||
exports[`require-no-hard-coded-dependency-versions when projectType is devModule succeeds when there are no hardcoded dependencies 1`] = `undefined`; | ||
|
||
exports[`require-no-hard-coded-dependency-versions when projectType is module fails when there are hardcoded dependencies 1`] = ` | ||
Object { | ||
"lintId": "require-no-hard-coded-dependency-versions", | ||
"lintMessage": "The dependencies for this project have hard-coded versions that violates the require-no-hard-coded-dependency-versions rule: | ||
a@1.0.0 does not satisfy requirement for the require-no-hard-coded-dependency-versions rule.", | ||
"projectType": "module", | ||
"severity": "error", | ||
} | ||
`; | ||
|
||
exports[`require-no-hard-coded-dependency-versions when projectType is module succeeds when there are no hardcoded dependencies 1`] = `undefined`; |
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.