Skip to content

Commit

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

* Update dependency-audit.ts

* Update dependency-audit.ts
  • Loading branch information
tclindner authored Mar 19, 2022
1 parent 85a8424 commit 59dd6cb
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 66 deletions.
16 changes: 12 additions & 4 deletions src/rules/no-git-dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import {PackageJson} from 'type-fest';
import {doVersContainGitRepository} from '../validators/dependency-audit';
import {auditDependenciesForGitRepositoryVersion} from '../validators/dependency-audit';
import {LintIssue} from '../lint-issue';
import {RuleType} from '../types/rule-type';
import {Severity} from '../types/severity';

const lintId = 'no-git-dependencies';
const nodeName = 'dependencies';
const message = 'You are using dependencies from git repository. Please use dependencies from npm.';

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 => {
if (packageJsonData.hasOwnProperty(nodeName) && doVersContainGitRepository(packageJsonData, nodeName, config)) {
return new LintIssue(lintId, severity, nodeName, message);
const auditResult = auditDependenciesForGitRepositoryVersion(packageJsonData, nodeName, config);

if (packageJsonData.hasOwnProperty(nodeName) && auditResult.hasGitRepositoryVersions) {
return new LintIssue(
lintId,
severity,
nodeName,
`You are using ${nodeName} from git repository. Please use ${nodeName} from npm. Invalid ${nodeName} include: ${auditResult.dependenciesWithGitRepositoryVersion.join(
', '
)}`
);
}

return null;
Expand Down
16 changes: 12 additions & 4 deletions src/rules/no-git-devDependencies.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import {PackageJson} from 'type-fest';
import {doVersContainGitRepository} from '../validators/dependency-audit';
import {auditDependenciesForGitRepositoryVersion} from '../validators/dependency-audit';
import {LintIssue} from '../lint-issue';
import {RuleType} from '../types/rule-type';
import {Severity} from '../types/severity';

const lintId = 'no-git-devDependencies';
const nodeName = 'devDependencies';
const message = 'You are using devDependencies from git repository. Please use devDependencies from npm.';

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 => {
if (packageJsonData.hasOwnProperty(nodeName) && doVersContainGitRepository(packageJsonData, nodeName, config)) {
return new LintIssue(lintId, severity, nodeName, message);
const auditResult = auditDependenciesForGitRepositoryVersion(packageJsonData, nodeName, config);

if (packageJsonData.hasOwnProperty(nodeName) && auditResult.hasGitRepositoryVersions) {
return new LintIssue(
lintId,
severity,
nodeName,
`You are using ${nodeName} from git repository. Please use ${nodeName} from npm. Invalid devDependencies include: ${auditResult.dependenciesWithGitRepositoryVersion.join(
', '
)}`
);
}

return null;
Expand Down
57 changes: 44 additions & 13 deletions src/validators/dependency-audit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,12 @@ export const doVersContainInvalidRange = (
export interface AbsoluteVersionCheckerResult {
onlyAbsoluteVersionDetected: boolean;
dependenciesChecked: number;
dependenciesWithAbsoluteVersion: string[];
dependenciesWithoutAbsoluteVersion: string[];
}

/**
* Determines whether or not all dependency versions are absolut
* Determines whether or not all dependency versions are absolute
* @param {object} packageJsonData Valid JSON
* @param {string} nodeName Name of a node in the package.json file
* @param {object} config Rule configuration
Expand All @@ -218,6 +220,8 @@ const absoluteVersionChecker = (
const firstCharOfStr = 0;
let onlyAbsoluteVersionDetected = true;
let dependenciesChecked = 0;
const dependenciesWithAbsoluteVersion = [];
const dependenciesWithoutAbsoluteVersion = [];

// eslint-disable-next-line no-restricted-syntax
for (const dependencyName in packageJsonData[nodeName]) {
Expand All @@ -236,6 +240,9 @@ const absoluteVersionChecker = (
dependencyVersion.indexOf('*') !== notFound
) {
onlyAbsoluteVersionDetected = false;
dependenciesWithoutAbsoluteVersion.push(dependencyName);
} else {
dependenciesWithAbsoluteVersion.push(dependencyName);
}

dependenciesChecked += 1;
Expand All @@ -244,6 +251,8 @@ const absoluteVersionChecker = (
return {
onlyAbsoluteVersionDetected,
dependenciesChecked,
dependenciesWithAbsoluteVersion,
dependenciesWithoutAbsoluteVersion,
};
};

Expand All @@ -263,10 +272,10 @@ export const areVersionsAbsolute = (packageJsonData: PackageJson | any, nodeName

/**
* Determines whether or not all dependency versions are absolut
* @param {object} packageJsonData Valid JSON
* @param {string} nodeName Name of a node in the package.json file
* @param {object} config Rule configuration
* @return {boolean} False if the package has an non-absolute version. True if it is not or the node is missing.
* @param packageJsonData Valid JSON
* @param nodeName Name of a node in the package.json file
* @param config Rule configuration
* @return False if the package has an non-absolute version. True if it is not or the node is missing.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const doVersContainNonAbsolute = (packageJsonData: PackageJson | any, nodeName: string, config: any): boolean => {
Expand Down Expand Up @@ -318,15 +327,30 @@ const isGitRepositoryUrl = (version: string): boolean => {
return match;
};

export interface AuditDependenciesForGitRepositoryVersionResponse {
hasGitRepositoryVersions: boolean;
dependenciesWithGitRepositoryVersion: string[];
dependenciesWithoutGitRepositoryVersion: string[];
}

/**
* Determines whether or not dependency versions are git repository
* @param {object} packageJsonData Valid JSON
* @param {string} nodeName Name of a node in the package.json file
* @param {object} config Rule configuration
* @return {boolean} True if the package has an git repo.
* @param packageJsonData Valid JSON
* @param nodeName Name of a node in the package.json file
* @param config Rule configuration
* @return True if the package has an git repo.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const doVersContainGitRepository = (packageJsonData: PackageJson | any, nodeName: string, config: any): boolean => {
export const auditDependenciesForGitRepositoryVersion = (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
packageJsonData: PackageJson | any,
nodeName: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
config: any
): AuditDependenciesForGitRepositoryVersionResponse => {
let hasGitRepositoryVersions = false;
const dependenciesWithGitRepositoryVersion = [];
const dependenciesWithoutGitRepositoryVersion = [];

// eslint-disable-next-line no-restricted-syntax
for (const dependencyName in packageJsonData[nodeName]) {
if (hasExceptions(config) && config.exceptions.includes(dependencyName)) {
Expand All @@ -337,11 +361,18 @@ export const doVersContainGitRepository = (packageJsonData: PackageJson | any, n
const dependencyVersion = packageJsonData[nodeName][dependencyName];

if (isGitRepositoryUrl(dependencyVersion) || isGithubRepositoryShortcut(dependencyVersion)) {
return true;
hasGitRepositoryVersions = true;
dependenciesWithGitRepositoryVersion.push(dependencyName);
} else {
dependenciesWithoutGitRepositoryVersion.push(dependencyName);
}
}

return false;
return {
hasGitRepositoryVersions,
dependenciesWithGitRepositoryVersion,
dependenciesWithoutGitRepositoryVersion,
};
};

export interface AuditDependenciesForArchiveUrlVersionResponse {
Expand Down
22 changes: 11 additions & 11 deletions test/unit/rules/no-git-dependencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('no-git-dependencies Unit Tests', () => {
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('dependencies');
expect(response.lintMessage).toStrictEqual(
'You are using dependencies from git repository. Please use dependencies from npm.'
'You are using dependencies from git repository. Please use dependencies from npm. Invalid dependencies include: my-module'
);
});
});
Expand All @@ -40,7 +40,7 @@ describe('no-git-dependencies Unit Tests', () => {
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('dependencies');
expect(response.lintMessage).toStrictEqual(
'You are using dependencies from git repository. Please use dependencies from npm.'
'You are using dependencies from git repository. Please use dependencies from npm. Invalid dependencies include: my-module'
);
});
});
Expand All @@ -58,7 +58,7 @@ describe('no-git-dependencies Unit Tests', () => {
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('dependencies');
expect(response.lintMessage).toStrictEqual(
'You are using dependencies from git repository. Please use dependencies from npm.'
'You are using dependencies from git repository. Please use dependencies from npm. Invalid dependencies include: my-module'
);
});
});
Expand All @@ -76,7 +76,7 @@ describe('no-git-dependencies Unit Tests', () => {
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('dependencies');
expect(response.lintMessage).toStrictEqual(
'You are using dependencies from git repository. Please use dependencies from npm.'
'You are using dependencies from git repository. Please use dependencies from npm. Invalid dependencies include: my-module'
);
});
});
Expand All @@ -94,7 +94,7 @@ describe('no-git-dependencies Unit Tests', () => {
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('dependencies');
expect(response.lintMessage).toStrictEqual(
'You are using dependencies from git repository. Please use dependencies from npm.'
'You are using dependencies from git repository. Please use dependencies from npm. Invalid dependencies include: my-module'
);
});
});
Expand All @@ -112,7 +112,7 @@ describe('no-git-dependencies Unit Tests', () => {
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('dependencies');
expect(response.lintMessage).toStrictEqual(
'You are using dependencies from git repository. Please use dependencies from npm.'
'You are using dependencies from git repository. Please use dependencies from npm. Invalid dependencies include: my-module'
);
});
});
Expand All @@ -130,7 +130,7 @@ describe('no-git-dependencies Unit Tests', () => {
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('dependencies');
expect(response.lintMessage).toStrictEqual(
'You are using dependencies from git repository. Please use dependencies from npm.'
'You are using dependencies from git repository. Please use dependencies from npm. Invalid dependencies include: my-module'
);
});
});
Expand All @@ -148,7 +148,7 @@ describe('no-git-dependencies Unit Tests', () => {
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('dependencies');
expect(response.lintMessage).toStrictEqual(
'You are using dependencies from git repository. Please use dependencies from npm.'
'You are using dependencies from git repository. Please use dependencies from npm. Invalid dependencies include: my-module'
);
});
});
Expand All @@ -166,7 +166,7 @@ describe('no-git-dependencies Unit Tests', () => {
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('dependencies');
expect(response.lintMessage).toStrictEqual(
'You are using dependencies from git repository. Please use dependencies from npm.'
'You are using dependencies from git repository. Please use dependencies from npm. Invalid dependencies include: my-module'
);
});
});
Expand All @@ -184,7 +184,7 @@ describe('no-git-dependencies Unit Tests', () => {
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('dependencies');
expect(response.lintMessage).toStrictEqual(
'You are using dependencies from git repository. Please use dependencies from npm.'
'You are using dependencies from git repository. Please use dependencies from npm. Invalid dependencies include: my-module'
);
});
});
Expand All @@ -202,7 +202,7 @@ describe('no-git-dependencies Unit Tests', () => {
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('dependencies');
expect(response.lintMessage).toStrictEqual(
'You are using dependencies from git repository. Please use dependencies from npm.'
'You are using dependencies from git repository. Please use dependencies from npm. Invalid dependencies include: my-module'
);
});
});
Expand Down
22 changes: 11 additions & 11 deletions test/unit/rules/no-git-devDependencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('no-git-devDependencies Unit Tests', () => {
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('devDependencies');
expect(response.lintMessage).toStrictEqual(
'You are using devDependencies from git repository. Please use devDependencies from npm.'
'You are using devDependencies from git repository. Please use devDependencies from npm. Invalid devDependencies include: my-module'
);
});
});
Expand All @@ -40,7 +40,7 @@ describe('no-git-devDependencies Unit Tests', () => {
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('devDependencies');
expect(response.lintMessage).toStrictEqual(
'You are using devDependencies from git repository. Please use devDependencies from npm.'
'You are using devDependencies from git repository. Please use devDependencies from npm. Invalid devDependencies include: my-module'
);
});
});
Expand All @@ -58,7 +58,7 @@ describe('no-git-devDependencies Unit Tests', () => {
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('devDependencies');
expect(response.lintMessage).toStrictEqual(
'You are using devDependencies from git repository. Please use devDependencies from npm.'
'You are using devDependencies from git repository. Please use devDependencies from npm. Invalid devDependencies include: my-module'
);
});
});
Expand All @@ -76,7 +76,7 @@ describe('no-git-devDependencies Unit Tests', () => {
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('devDependencies');
expect(response.lintMessage).toStrictEqual(
'You are using devDependencies from git repository. Please use devDependencies from npm.'
'You are using devDependencies from git repository. Please use devDependencies from npm. Invalid devDependencies include: my-module'
);
});
});
Expand All @@ -94,7 +94,7 @@ describe('no-git-devDependencies Unit Tests', () => {
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('devDependencies');
expect(response.lintMessage).toStrictEqual(
'You are using devDependencies from git repository. Please use devDependencies from npm.'
'You are using devDependencies from git repository. Please use devDependencies from npm. Invalid devDependencies include: my-module'
);
});
});
Expand All @@ -112,7 +112,7 @@ describe('no-git-devDependencies Unit Tests', () => {
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('devDependencies');
expect(response.lintMessage).toStrictEqual(
'You are using devDependencies from git repository. Please use devDependencies from npm.'
'You are using devDependencies from git repository. Please use devDependencies from npm. Invalid devDependencies include: my-module'
);
});
});
Expand All @@ -130,7 +130,7 @@ describe('no-git-devDependencies Unit Tests', () => {
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('devDependencies');
expect(response.lintMessage).toStrictEqual(
'You are using devDependencies from git repository. Please use devDependencies from npm.'
'You are using devDependencies from git repository. Please use devDependencies from npm. Invalid devDependencies include: my-module'
);
});
});
Expand All @@ -148,7 +148,7 @@ describe('no-git-devDependencies Unit Tests', () => {
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('devDependencies');
expect(response.lintMessage).toStrictEqual(
'You are using devDependencies from git repository. Please use devDependencies from npm.'
'You are using devDependencies from git repository. Please use devDependencies from npm. Invalid devDependencies include: my-module'
);
});
});
Expand All @@ -166,7 +166,7 @@ describe('no-git-devDependencies Unit Tests', () => {
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('devDependencies');
expect(response.lintMessage).toStrictEqual(
'You are using devDependencies from git repository. Please use devDependencies from npm.'
'You are using devDependencies from git repository. Please use devDependencies from npm. Invalid devDependencies include: my-module'
);
});
});
Expand All @@ -184,7 +184,7 @@ describe('no-git-devDependencies Unit Tests', () => {
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('devDependencies');
expect(response.lintMessage).toStrictEqual(
'You are using devDependencies from git repository. Please use devDependencies from npm.'
'You are using devDependencies from git repository. Please use devDependencies from npm. Invalid devDependencies include: my-module'
);
});
});
Expand All @@ -202,7 +202,7 @@ describe('no-git-devDependencies Unit Tests', () => {
expect(response.severity).toStrictEqual('error');
expect(response.node).toStrictEqual('devDependencies');
expect(response.lintMessage).toStrictEqual(
'You are using devDependencies from git repository. Please use devDependencies from npm.'
'You are using devDependencies from git repository. Please use devDependencies from npm. Invalid devDependencies include: my-module'
);
});
});
Expand Down
Loading

0 comments on commit 59dd6cb

Please sign in to comment.