Skip to content

Commit

Permalink
Improve output for no-*-version-* rules (#612)
Browse files Browse the repository at this point in the history
* Improve output for no-*-version-* rules

* Update dependency-audit.ts
  • Loading branch information
tclindner authored Mar 20, 2022
1 parent de681de commit c833dfb
Show file tree
Hide file tree
Showing 15 changed files with 128 additions and 57 deletions.
18 changes: 11 additions & 7 deletions src/rules/no-caret-version-dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import {PackageJson} from 'type-fest';
import {doVersContainInvalidRange} from '../validators/dependency-audit';
import {auditDependenciesForInvalidRange} from '../validators/dependency-audit';
import {LintIssue} from '../lint-issue';
import {RuleType} from '../types/rule-type';
import {Severity} from '../types/severity';

const lintId = 'no-caret-version-dependencies';
const nodeName = 'dependencies';
const message = 'You are using an invalid version range. Please do not use ^.';

export const ruleType = RuleType.OptionalObject;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const lint = (packageJsonData: PackageJson | any, severity: Severity, config: any): LintIssue | null => {
const rangeSpecifier = '^';
const auditResult = auditDependenciesForInvalidRange(packageJsonData, nodeName, rangeSpecifier, config);

if (
packageJsonData.hasOwnProperty(nodeName) &&
doVersContainInvalidRange(packageJsonData, nodeName, rangeSpecifier, config)
) {
return new LintIssue(lintId, severity, nodeName, message);
if (packageJsonData.hasOwnProperty(nodeName) && auditResult.hasInvalidRangeVersions) {
return new LintIssue(
lintId,
severity,
nodeName,
`You are using an invalid version range. Please do not use ^. Invalid ${nodeName} include: ${auditResult.dependenciesWithInvalidVersionRange.join(
', '
)}`
);
}

return null;
Expand Down
18 changes: 11 additions & 7 deletions src/rules/no-caret-version-devDependencies.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import {PackageJson} from 'type-fest';
import {doVersContainInvalidRange} from '../validators/dependency-audit';
import {auditDependenciesForInvalidRange} from '../validators/dependency-audit';
import {LintIssue} from '../lint-issue';
import {RuleType} from '../types/rule-type';
import {Severity} from '../types/severity';

const lintId = 'no-caret-version-devDependencies';
const nodeName = 'devDependencies';
const message = 'You are using an invalid version range. Please do not use ^.';

export const ruleType = RuleType.OptionalObject;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const lint = (packageJsonData: PackageJson | any, severity: Severity, config: any): LintIssue | null => {
const rangeSpecifier = '^';
const auditResult = auditDependenciesForInvalidRange(packageJsonData, nodeName, rangeSpecifier, config);

if (
packageJsonData.hasOwnProperty(nodeName) &&
doVersContainInvalidRange(packageJsonData, nodeName, rangeSpecifier, config)
) {
return new LintIssue(lintId, severity, nodeName, message);
if (packageJsonData.hasOwnProperty(nodeName) && auditResult.hasInvalidRangeVersions) {
return new LintIssue(
lintId,
severity,
nodeName,
`You are using an invalid version range. Please do not use ^. Invalid ${nodeName} include: ${auditResult.dependenciesWithInvalidVersionRange.join(
', '
)}`
);
}

return null;
Expand Down
18 changes: 11 additions & 7 deletions src/rules/no-tilde-version-dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import {PackageJson} from 'type-fest';
import {doVersContainInvalidRange} from '../validators/dependency-audit';
import {auditDependenciesForInvalidRange} from '../validators/dependency-audit';
import {LintIssue} from '../lint-issue';
import {RuleType} from '../types/rule-type';
import {Severity} from '../types/severity';

const lintId = 'no-tilde-version-dependencies';
const nodeName = 'dependencies';
const message = 'You are using an invalid version range. Please do not use ~.';

export const ruleType = RuleType.OptionalObject;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const lint = (packageJsonData: PackageJson | any, severity: Severity, config: any): LintIssue | null => {
const rangeSpecifier = '~';
const auditResult = auditDependenciesForInvalidRange(packageJsonData, nodeName, rangeSpecifier, config);

if (
packageJsonData.hasOwnProperty(nodeName) &&
doVersContainInvalidRange(packageJsonData, nodeName, rangeSpecifier, config)
) {
return new LintIssue(lintId, severity, nodeName, message);
if (packageJsonData.hasOwnProperty(nodeName) && auditResult.hasInvalidRangeVersions) {
return new LintIssue(
lintId,
severity,
nodeName,
`You are using an invalid version range. Please do not use ~. Invalid ${nodeName} include: ${auditResult.dependenciesWithInvalidVersionRange.join(
', '
)}`
);
}

return null;
Expand Down
18 changes: 11 additions & 7 deletions src/rules/no-tilde-version-devDependencies.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import {PackageJson} from 'type-fest';
import {doVersContainInvalidRange} from '../validators/dependency-audit';
import {auditDependenciesForInvalidRange} from '../validators/dependency-audit';
import {LintIssue} from '../lint-issue';
import {RuleType} from '../types/rule-type';
import {Severity} from '../types/severity';

const lintId = 'no-tilde-version-devDependencies';
const nodeName = 'devDependencies';
const message = 'You are using an invalid version range. Please do not use ~.';

export const ruleType = RuleType.OptionalObject;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const lint = (packageJsonData: PackageJson | any, severity: Severity, config: any): LintIssue | null => {
const rangeSpecifier = '~';
const auditResult = auditDependenciesForInvalidRange(packageJsonData, nodeName, rangeSpecifier, config);

if (
packageJsonData.hasOwnProperty(nodeName) &&
doVersContainInvalidRange(packageJsonData, nodeName, rangeSpecifier, config)
) {
return new LintIssue(lintId, severity, nodeName, message);
if (packageJsonData.hasOwnProperty(nodeName) && auditResult.hasInvalidRangeVersions) {
return new LintIssue(
lintId,
severity,
nodeName,
`You are using an invalid version range. Please do not use ~. Invalid ${nodeName} include: ${auditResult.dependenciesWithInvalidVersionRange.join(
', '
)}`
);
}

return null;
Expand Down
43 changes: 31 additions & 12 deletions src/validators/dependency-audit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,28 +157,40 @@ export const areVersRangesValid = (
return rangesValid;
};

export interface AuditDependenciesForInvalidRangeResponse {
hasInvalidRangeVersions: boolean;
dependenciesWithInvalidVersionRange: string[];
dependenciesWithoutInvalidVersionRange: string[];
}

/**
* Determines if any dependencies have a version string that starts with the specified invalid range
* @param {object} packageJsonData Valid JSON
* @param {string} nodeName Name of a node in the package.json file
* @param {string} rangeSpecifier A version range specifier
* @param {object} config Rule configuration
* @return {Boolean} True if any dependencies versions start with the invalid range, false if they don't.
* @param packageJsonData Valid JSON
* @param nodeName Name of a node in the package.json file
* @param rangeSpecifier A version range specifier
* @param config Rule configuration
* @return True if any dependencies versions start with the invalid range, false if they don't.
*/
export const doVersContainInvalidRange = (
export const auditDependenciesForInvalidRange = (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
packageJsonData: PackageJson | any,
nodeName: string,
rangeSpecifier: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
config: any
): boolean => {
): AuditDependenciesForInvalidRangeResponse => {
let hasInvalidRangeVersions = false;
const dependenciesWithInvalidVersionRange = [];
const dependenciesWithoutInvalidVersionRange = [];

if (!packageJsonData.hasOwnProperty(nodeName)) {
return false;
return {
hasInvalidRangeVersions,
dependenciesWithInvalidVersionRange,
dependenciesWithoutInvalidVersionRange,
};
}

let containsInvalidVersion = false;

// eslint-disable-next-line no-restricted-syntax
for (const dependencyName in packageJsonData[nodeName]) {
if (hasExceptions(config) && config.exceptions.includes(dependencyName)) {
Expand All @@ -189,11 +201,18 @@ export const doVersContainInvalidRange = (
const dependencyVersion = packageJsonData[nodeName][dependencyName];

if (doesVersStartsWithRange(dependencyVersion, rangeSpecifier)) {
containsInvalidVersion = true;
hasInvalidRangeVersions = true;
dependenciesWithInvalidVersionRange.push(dependencyName);
} else {
dependenciesWithoutInvalidVersionRange.push(dependencyName);
}
}

return containsInvalidVersion;
return {
hasInvalidRangeVersions,
dependenciesWithInvalidVersionRange,
dependenciesWithoutInvalidVersionRange,
};
};

export interface AbsoluteVersionCheckerResult {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/linter/linter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ describe('linter Unit Tests', () => {
'no-caret-version-dependencies',
Severity.Error,
'dependencies',
'You are using an invalid version range. Please do not use ^.'
'You are using an invalid version range. Please do not use ^. Invalid dependencies include: myModule'
);
const expected = {
errorCount: 1,
Expand Down Expand Up @@ -520,7 +520,7 @@ describe('linter Unit Tests', () => {
'no-caret-version-dependencies',
Severity.Error,
'dependencies',
'You are using an invalid version range. Please do not use ^.'
'You are using an invalid version range. Please do not use ^. Invalid dependencies include: myModule'
);
const expected = {
errorCount: 1,
Expand Down
4 changes: 3 additions & 1 deletion test/unit/rules/no-caret-version-dependencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ describe('no-caret-version-dependencies Unit Tests', () => {
expect(response.lintId).toStrictEqual('no-caret-version-dependencies');
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('dependencies');
expect(response.lintMessage).toStrictEqual('You are using an invalid version range. Please do not use ^.');
expect(response.lintMessage).toStrictEqual(
'You are using an invalid version range. Please do not use ^. Invalid dependencies include: npm-package-json-lint'
);
});
});

Expand Down
4 changes: 3 additions & 1 deletion test/unit/rules/no-caret-version-devDependencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ describe('no-caret-version-devDependencies Unit Tests', () => {
expect(response.lintId).toStrictEqual('no-caret-version-devDependencies');
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('devDependencies');
expect(response.lintMessage).toStrictEqual('You are using an invalid version range. Please do not use ^.');
expect(response.lintMessage).toStrictEqual(
'You are using an invalid version range. Please do not use ^. Invalid devDependencies include: npm-package-json-lint'
);
});
});

Expand Down
4 changes: 3 additions & 1 deletion test/unit/rules/no-tilde-version-dependencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ describe('no-tilde-version-dependencies Unit Tests', () => {
expect(response.lintId).toStrictEqual('no-tilde-version-dependencies');
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('dependencies');
expect(response.lintMessage).toStrictEqual('You are using an invalid version range. Please do not use ~.');
expect(response.lintMessage).toStrictEqual(
'You are using an invalid version range. Please do not use ~. Invalid dependencies include: npm-package-json-lint'
);
});
});

Expand Down
4 changes: 3 additions & 1 deletion test/unit/rules/no-tilde-version-devDependencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ describe('no-tilde-version-devDependencies Unit Tests', () => {
expect(response.lintId).toStrictEqual('no-tilde-version-devDependencies');
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('devDependencies');
expect(response.lintMessage).toStrictEqual('You are using an invalid version range. Please do not use ~.');
expect(response.lintMessage).toStrictEqual(
'You are using an invalid version range. Please do not use ~. Invalid devDependencies include: npm-package-json-lint'
);
});
});

Expand Down
46 changes: 35 additions & 11 deletions test/unit/validators/dependency-audit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ describe('dependency-audit Unit Tests', () => {
});
});

describe('doVersContainInvalidRange method', () => {
describe('auditDependenciesForInvalidRange method', () => {
describe('when the node does not exist in the package.json file', () => {
test('false should be returned', () => {
const packageJson = {
Expand All @@ -568,9 +568,13 @@ describe('dependency-audit Unit Tests', () => {
'gulp-npm-package-json-lint': '^2.0.0-rc1',
},
};
const response = dependencyAudit.doVersContainInvalidRange(packageJson, 'devDependencies', '~', {});
const response = dependencyAudit.auditDependenciesForInvalidRange(packageJson, 'devDependencies', '~', {});

expect(response).toBe(false);
expect(response).toStrictEqual({
hasInvalidRangeVersions: false,
dependenciesWithInvalidVersionRange: [],
dependenciesWithoutInvalidVersionRange: [],
});
});
});

Expand All @@ -583,9 +587,13 @@ describe('dependency-audit Unit Tests', () => {
'gulp-npm-package-json-lint': '^2.0.0-rc1',
},
};
const response = dependencyAudit.doVersContainInvalidRange(packageJson, 'dependencies', '~', {});
const response = dependencyAudit.auditDependenciesForInvalidRange(packageJson, 'dependencies', '~', {});

expect(response).toBe(true);
expect(response).toStrictEqual({
hasInvalidRangeVersions: true,
dependenciesWithInvalidVersionRange: ['grunt-npm-package-json-lint'],
dependenciesWithoutInvalidVersionRange: ['npm-package-json-lint', 'gulp-npm-package-json-lint'],
});
});
});

Expand All @@ -598,9 +606,17 @@ describe('dependency-audit Unit Tests', () => {
'gulp-npm-package-json-lint': '^2.0.0-rc1',
},
};
const response = dependencyAudit.doVersContainInvalidRange(packageJson, 'dependencies', '~', {});
const response = dependencyAudit.auditDependenciesForInvalidRange(packageJson, 'dependencies', '~', {});

expect(response).toBe(false);
expect(response).toStrictEqual({
hasInvalidRangeVersions: false,
dependenciesWithInvalidVersionRange: [],
dependenciesWithoutInvalidVersionRange: [
'npm-package-json-lint',
'grunt-npm-package-json-lint',
'gulp-npm-package-json-lint',
],
});
});
});

Expand All @@ -613,11 +629,15 @@ describe('dependency-audit Unit Tests', () => {
'gulp-npm-package-json-lint': '^2.0.0-rc1',
},
};
const response = dependencyAudit.doVersContainInvalidRange(packageJson, 'dependencies', '~', {
const response = dependencyAudit.auditDependenciesForInvalidRange(packageJson, 'dependencies', '~', {
exceptions: ['npm-package-json-lint', 'grunt-npm-package-json-lint'],
});

expect(response).toBe(false);
expect(response).toStrictEqual({
hasInvalidRangeVersions: false,
dependenciesWithInvalidVersionRange: [],
dependenciesWithoutInvalidVersionRange: ['gulp-npm-package-json-lint'],
});
});
});

Expand All @@ -630,9 +650,13 @@ describe('dependency-audit Unit Tests', () => {
'gulp-npm-package-json-lint': '^2.0.0-rc1',
},
};
const response = dependencyAudit.doVersContainInvalidRange(packageJson, 'dependencies', '~', {});
const response = dependencyAudit.auditDependenciesForInvalidRange(packageJson, 'dependencies', '~', {});

expect(response).toBe(true);
expect(response).toStrictEqual({
hasInvalidRangeVersions: true,
dependenciesWithInvalidVersionRange: ['npm-package-json-lint', 'grunt-npm-package-json-lint'],
dependenciesWithoutInvalidVersionRange: ['gulp-npm-package-json-lint'],
});
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,5 @@ With exceptions

## History

* Improved messaging when an invalid configuration is detected in version 6.3.0
* Introduced in version 3.2.0
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,5 @@ With exceptions

## History

* Improved messaging when an invalid configuration is detected in version 6.3.0
* Introduced in version 3.2.0
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,5 @@ With exceptions

## History

* Improved messaging when an invalid configuration is detected in version 6.3.0
* Introduced in version 3.2.0
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,5 @@ With exceptions

## History

* Improved messaging when an invalid configuration is detected in version 6.3.0
* Introduced in version 3.2.0

0 comments on commit c833dfb

Please sign in to comment.