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

Commit

Permalink
Added compatibleRegex array
Browse files Browse the repository at this point in the history
  • Loading branch information
pranav300 committed Sep 3, 2021
1 parent dfc30a5 commit b3446c5
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 19 deletions.
4 changes: 2 additions & 2 deletions packages/package-json-lint/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

## Unreleased

## 1.1.0 - (August 31, 2021)

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

## 1.1.0 - (August 31, 2021)

* Added
* Added `projectType` to allow users to flag project as `application`, `module` or `devModule`.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
module.exports = {
create: ({ ruleConfig, projectType, report }) => ({
dependencies: (dependencies) => {
const compatibleVersionRegexList = ['latest', 'master', 'dev', /(\^|<|<=|>|>=|~)\s{0,1}\d/, /^x\.|\.x\.|\.x$/, /[\d.]{0,2}\d\s{0,1}-\s{0,1}[\d.]{0,2}\d/];
if (projectType === 'module' || projectType === 'devModule') {
const messageString = 'no hard coded dependency';
const messageString = 'require-no-hard-coded-dependency-versions';
const currentProblems = Object.keys(dependencies).map(dependencyName => {
const dependencyVersion = dependencies[dependencyName];
if (!dependencyVersion.startsWith('^') && !(ruleConfig.severity.allowList && ruleConfig.severity.allowList.includes(dependencyName))) {
return `${dependencyName}@${dependencyVersion} does not satisfy requirement for ${messageString}`;
const isCompatibleVersion = compatibleVersionRegexList.map(versionRegex => (!!dependencyVersion.match(versionRegex))).includes(true);
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 for ${messageString}:\n ${currentProblems.join('\n ')}`;
const lintMessage = `The dependencies for this project have hard-coded versions that violates the ${messageString} rule:\n ${currentProblems.join('\n ')}`;
report({
lintId: 'require-no-hard-coded-dependency-versions', severity: ruleConfig.severity.severityType, lintMessage, projectType,
lintId: messageString, severity: ruleConfig.severity.severityType, lintMessage, projectType,
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ exports[`require-no-hard-coded-dependency-versions when projectType is applicati
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 for no hard coded dependency:
a@1.0.0 does not satisfy requirement for no hard coded dependency",
"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",
}
Expand All @@ -21,8 +21,8 @@ exports[`require-no-hard-coded-dependency-versions when projectType is devModule
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 for no hard coded dependency:
a@1.0.0 does not satisfy requirement for no hard coded dependency",
"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",
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
const requireNoTerraBasePeerDependencyVersions = require('../../../src/rules/require-no-hard-coded-dependency-versions');
const requireNoHardCodedDependencyVersions = require('../../../src/rules/require-no-hard-coded-dependency-versions');

describe('require-no-hard-coded-dependency-versions', () => {
describe('when projectType is application', () => {
it('succeeds when there are hardcoded dependencies', () => {
const results = requireNoTerraBasePeerDependencyVersions.create({
let results;
requireNoHardCodedDependencyVersions.create({
ruleConfig: {
severity: {
severityType: 'error',
},
},
projectType: 'application',
report: (issues) => {
results = issues;
},
}).dependencies({
a: '1.0.0',
});
expect(results).toMatchSnapshot();
});
it('succeeds when there are no hardcoded dependencies', () => {
const results = requireNoTerraBasePeerDependencyVersions.create({
let results;
requireNoHardCodedDependencyVersions.create({
ruleConfig: {
severity: {
severityType: 'error',
},
},
projectType: 'application',
report: (issues) => {
results = issues;
},
}).dependencies({
a: '^1.0.0',
});
Expand All @@ -33,7 +41,7 @@ describe('require-no-hard-coded-dependency-versions', () => {
describe('when projectType is module', () => {
it('fails when there are hardcoded dependencies', () => {
let results;
requireNoTerraBasePeerDependencyVersions.create({
requireNoHardCodedDependencyVersions.create({
ruleConfig: {
severity: {
severityType: 'error',
Expand All @@ -49,7 +57,8 @@ describe('require-no-hard-coded-dependency-versions', () => {
expect(results).toMatchSnapshot();
});
it('succeeds when there are no hardcoded dependencies', () => {
let results = requireNoTerraBasePeerDependencyVersions.create({
let results;
requireNoHardCodedDependencyVersions.create({
ruleConfig: {
severity: {
severityType: 'error',
Expand All @@ -61,6 +70,15 @@ describe('require-no-hard-coded-dependency-versions', () => {
},
}).dependencies({
a: '^1.0.0',
b: '1.0.0 - 2.9999.9999',
c: '>=1.0.2 <2.1.2',
d: '>1.0.2 <=2.3.4',
e: '<1.0.0 || >=2.3.1 <2.4.5 || >=2.5.2 <3.0.0',
f: '~1.2',
g: 'latest',
h: '~1.2.3',
i: '2.x',
j: '3.3.x',
});
expect(results).toMatchSnapshot();
});
Expand All @@ -69,7 +87,7 @@ describe('require-no-hard-coded-dependency-versions', () => {
describe('when projectType is devModule', () => {
it('fails when there are hardcoded dependencies', () => {
let results;
requireNoTerraBasePeerDependencyVersions.create({
requireNoHardCodedDependencyVersions.create({
ruleConfig: {
severity: {
severityType: 'error',
Expand All @@ -85,23 +103,36 @@ describe('require-no-hard-coded-dependency-versions', () => {
expect(results).toMatchSnapshot();
});
it('succeeds when there are no hardcoded dependencies', () => {
const results = requireNoTerraBasePeerDependencyVersions.create({
let results;
requireNoHardCodedDependencyVersions.create({
ruleConfig: {
severity: {
severityType: 'error',
},
},
projectType: 'devModule',
report: (issues) => {
results = issues;
},
}).dependencies({
a: '^1.0.0',
b: '1.0.0 - 2.9999.9999',
c: '>=1.0.2 <2.1.2',
d: '>1.0.2 <=2.3.4',
e: '<1.0.0 || >=2.3.1 <2.4.5 || >=2.5.2 <3.0.0',
f: '~1.2',
g: 'latest',
h: '~1.2.3',
i: '2.x',
j: '3.3.x',
});
expect(results).toMatchSnapshot();
});
});

it('succeeds when hardcoded dependency is passed in the allowList', () => {
let results;
requireNoTerraBasePeerDependencyVersions.create({
requireNoHardCodedDependencyVersions.create({
ruleConfig: {
severity: {
severityType: 'error',
Expand Down

0 comments on commit b3446c5

Please sign in to comment.