Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
[package-json-lint] Add rule for hard coded dependencies in module an…
Browse files Browse the repository at this point in the history
…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
pranav300 and ShettyAkarsh authored Sep 27, 2021
1 parent a21de7e commit 96aab2c
Show file tree
Hide file tree
Showing 25 changed files with 483 additions and 19 deletions.
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,18 @@
"url": "https://github.com/cerner/terra-toolkit/issues"
},
"package-json-lint": {
"extends": "./packages/package-json-lint-config-terra/package-json-lint.config.js"
"extends": "./packages/package-json-lint-config-terra/package-json-lint.config.js",
"projectType": "devModule",
"rules": {
"require-no-hard-coded-dependency-versions": {
"severityType": "error",
"allowList": [
"eslint-plugin-react",
"axe-core",
"inquirer"
]
}
}
},
"eslintConfig": {
"extends": "./packages/eslint-config-terra/eslint.config.js",
Expand Down
3 changes: 3 additions & 0 deletions packages/jest-config-terra/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

* Changed
* Updated hard coded dependency to compatible dependencies.

## 1.2.0 - (May 11, 2021)

* Added
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-config-terra/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"@jest/reporters": "^25.3.0",
"babel-jest": "^26.6.3",
"identity-obj-proxy": "^3.0.0",
"jest-environment-jsdom": "26.6.2",
"jest-environment-jsdom": "^26.6.2",
"jest-mock": "^26.6.2",
"strip-ansi": "^6.0.0"
},
Expand Down
6 changes: 6 additions & 0 deletions packages/package-json-lint-config-terra/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## Unreleased

* Changed
* Updated `severity` to be an object with `severityType` and `allowList`.

* Added
* Added new rule `require-no-hard-coded-dependency-versions`.

## 1.0.0 - (August 13, 2021)

* Initial stable release
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' },
},
};
3 changes: 3 additions & 0 deletions packages/package-json-lint/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

* Added
* Added new rule `require-no-hard-coded-dependency-versions`.

## 1.1.0 - (August 31, 2021)

* Added
Expand Down
2 changes: 2 additions & 0 deletions packages/package-json-lint/src/rules/index.js
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,
};
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,
};
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,17 @@ const versionSet = [
{ name: 'terra-visually-hidden-text', versionRange: '>2.4.0' },
];

const documentation = {
ruleName: 'require-no-terra-base-peer-dependency-versions',
defaultValue: 'warn',
description: "This rule doesn't allow to use terra packages versions that use terra-base as a peerDependency.",
};

module.exports = {
create: ({ ruleConfig, projectType, report }) => ({
dependencies: (dependencies) => requireVersionSet({
versionSet, dependencies, ruleConfig, projectType, report, lintId: 'require-no-terra-base-peer-dependency-versions', messageString: 'no terra base peer dependencies',
}),
}),
documentation,
};
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,17 @@ const versionSet = [
{ name: 'terra-toolbar', versionRange: '>=1.8.0' },
];

const documentation = {
ruleName: 'require-theme-context-versions',
defaultValue: 'warn',
description: "This rule doesn't allow to use terra packages versions that don't use terra-theme-context as a Dependency.",
};

module.exports = {
create: ({ ruleConfig, projectType, report }) => ({
dependencies: (dependencies) => requireVersionSet({
versionSet, dependencies, ruleConfig, projectType, report, lintId: 'require-theme-context-versions', messageString: 'theming context',
}),
}),
documentation,
};
2 changes: 1 addition & 1 deletion packages/package-json-lint/src/rules/requireVersionSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
return `${name}@${dependencyVersion} does not satisfy range requirement for ${messageString}: ${name}@${versionRange}`;
}
return undefined;
Expand Down
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`;
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
}
`;

Expand Down Expand Up @@ -88,7 +90,9 @@ Object {
terra-toggle@3.5.0 does not satisfy range requirement for no terra base peer dependencies: terra-toggle@>3.5.0
terra-visually-hidden-text@2.4.0 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",
},
}
`;

Expand All @@ -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`;
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ Object {
"lintMessage": "The dependencies for this project do not have the minimum versions required for theming context:
terra-action-footer@^1.0.0 does not satisfy range requirement for theming context: terra-action-footer@>=2.42.0",
"projectType": "module",
"severity": "warning",
"severity": Object {
"severityType": "warning",
},
}
`;

Expand Down Expand Up @@ -87,7 +89,9 @@ Object {
terra-time-input@<4.29.0 does not satisfy range requirement for theming context: terra-time-input@>=4.29.0
terra-toolbar@<1.8.0 does not satisfy range requirement for theming context: terra-toolbar@>=1.8.0",
"projectType": "module",
"severity": "error",
"severity": Object {
"severityType": "error",
},
}
`;

Expand All @@ -96,3 +100,5 @@ exports[`require-theme-context-versions passes when all versions meet the requir
exports[`require-theme-context-versions succeeds when there are dependencies not in the list to check 1`] = `undefined`;

exports[`require-theme-context-versions succeeds when there are no dependencies 1`] = `undefined`;

exports[`require-theme-context-versions succeeds when versions do not meet the required version but package is passed in allowList 1`] = `undefined`;
Loading

0 comments on commit 96aab2c

Please sign in to comment.