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
[package-json-lint] Add rule for hard coded dependencies in module and devModule. #704
Merged
Merged
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
e33f311
added new rule for hard coded dependencies
pranav300 6c60eb4
minor change
pranav300 cc6d1e0
updated projectType to devModule for toolkit
pranav300 841d4f9
Added jest tests
pranav300 ad34194
minor change
pranav300 dfc30a5
Added allowList to the severity object
pranav300 b3446c5
Added compatibleRegex array
pranav300 2786855
Merge branch 'main' into add-rule-for-hard-coded-dependencies
pranav300 80c4eb1
minor changes
pranav300 0095a1f
Added documentation for package-json-lint
pranav300 bc28dc4
updated changelog
pranav300 1f1cc1b
minor changes
pranav300 2b4a8d3
minor chnages
pranav300 f7e974f
Update Rules.2.tool.mdx
pranav300 d6206c3
minorChanges
pranav300 a3783ca
Merge branch 'add-rule-for-hard-coded-dependencies' of https://github…
pranav300 d828bdd
Update packages/terra-functional-testing/CHANGELOG.md
pranav300 bce14c4
updated condition to use semver.clean
pranav300 74b9ac7
Merge branch 'main' into add-rule-for-hard-coded-dependencies
pranav300 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ module.exports = ({ | |
}) => { | ||
const currentProblems = versionSet.map(({ name, versionRange }) => { | ||
const dependencyVersion = dependencies[name]; | ||
if (dependencyVersion && !semver.intersects(dependencyVersion, versionRange)) { | ||
if (dependencyVersion && !semver.intersects(dependencyVersion, versionRange) && !(ruleConfig.severity.allowList && ruleConfig.severity.allowList.includes(name))) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as we add more rules will this if continue to grow? Is there a way we could clean this up? |
||
return `${name}@${dependencyVersion} does not satisfy range requirement for ${messageString}: ${name}@${versionRange}`; | ||
} | ||
return undefined; | ||
|
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: | ||
[email protected] 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: | ||
[email protected] 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,9 @@ Object { | |
"lintMessage": "The dependencies for this project do not have the minimum versions required for no terra base peer dependencies: | ||
terra-action-footer@^1.0.0 does not satisfy range requirement for no terra base peer dependencies: terra-action-footer@>2.5.0", | ||
"projectType": "module", | ||
"severity": "warning", | ||
"severity": Object { | ||
"severityType": "warning", | ||
}, | ||
} | ||
`; | ||
|
||
|
@@ -88,7 +90,9 @@ Object { | |
[email protected] does not satisfy range requirement for no terra base peer dependencies: terra-toggle@>3.5.0 | ||
[email protected] does not satisfy range requirement for no terra base peer dependencies: terra-visually-hidden-text@>2.4.0", | ||
"projectType": "devModule", | ||
"severity": "error", | ||
"severity": Object { | ||
"severityType": "error", | ||
}, | ||
} | ||
`; | ||
|
||
|
@@ -97,3 +101,5 @@ exports[`require-no-terra-base-peer-dependency-versions passes when all versions | |
exports[`require-no-terra-base-peer-dependency-versions succeeds when there are dependencies not in the list to check 1`] = `undefined`; | ||
|
||
exports[`require-no-terra-base-peer-dependency-versions succeeds when there are no dependencies 1`] = `undefined`; | ||
|
||
exports[`require-no-terra-base-peer-dependency-versions succeeds when versions do not meet the required version but package is passed in allowList 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could not tell you why i locked this down, or if it was a mistake. 🤷♂️