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

Commit

Permalink
added a function findUnnecessaryDependency
Browse files Browse the repository at this point in the history
  • Loading branch information
pranav300 committed Oct 5, 2021
1 parent cceb840 commit d8f833e
Showing 1 changed file with 31 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,14 @@ const devDependencySet = [
'@cerner/jest-config-terra',
'@cerner/stylelint-config-terra',
'@cerner/terra-aggregate-translations',
'@cerner/terra-application',
'@cerner/terra-cli',
'@cerner/terra-dev-site',
'@cerner/terra-functional-testing',
'@cerner/terra-open-source-scripts',
'@cerner/webpack-config-terra',
'@octokit/core',
'bufferutil',
'chai',
'check-installed-dependencies',
'commander',
'core-js',
'csvtojson',
'danger',
Expand All @@ -41,10 +38,6 @@ const devDependencySet = [
'ky',
'lerna',
'link-parent-bin',
'lodash.groupby',
'lodash.map',
'lodash.mapkeys',
'lodash.mapvalues',
'memory-fs',
'mocha',
'nock',
Expand All @@ -58,10 +51,6 @@ const devDependencySet = [
'strip-ansi',
'stylelint',
'svgo',
'terra-application',
'terra-button',
'terra-disclosure-manager',
'terra-enzyme-intl',
'utf-8-validate',
'webpack',
'webpack-cli',
Expand All @@ -70,55 +59,46 @@ const devDependencySet = [
'yargs',
];

function findUnnecessaryDependency(rule, dependencies, dependencyType) {
const { ruleConfig, projectType, report } = rule;
const messageString = 'require-no-unnecessary-dependency';
if (projectType !== 'devModule') {
const currentProblems = Object.keys(dependencies).map((dependency) => {
const dependencyVersion = dependencies[dependency];
if (devDependencySet.includes(dependency) && !(ruleConfig.severity.allowList && ruleConfig.severity.allowList.includes(dependency))) {
return `${dependency}@${dependencyVersion} does not satisfy requirement for ${messageString} rule.`;
}
return undefined;
}).filter(problem => !!problem);

if (currentProblems.length) {
let lintMessage;
if (dependencyType === 'dependency') {
lintMessage = `This project has unnecessary dependencies that violates the ${messageString} rule:\n ${currentProblems.join('\n ')}`;
} else if (dependencyType === 'peer') {
lintMessage = `This project has unnecessary peerDependencies that violates the ${messageString} rule:\n ${currentProblems.join('\n ')}`;
}
report({
lintId: messageString,
severity: ruleConfig.severity.severityType,
lintMessage,
projectType,
});
}
}
}

module.exports = {
create: ({
ruleConfig,
projectType,
report,
}) => ({
dependencies: (dependencies) => {
const messageString = 'require-no-unnecessary-dependency';
if (projectType !== 'devModule') {
const currentProblems = Object.keys(dependencies).map((dependency) => {
const dependencyVersion = dependencies[dependency];
if (devDependencySet.includes(dependency) && !(ruleConfig.severity.allowList && ruleConfig.severity.allowList.includes(dependency))) {
return `${dependency}@${dependencyVersion} does not satisfy requirement for ${messageString} rule.`;
}
return undefined;
}).filter(problem => !!problem);

if (currentProblems.length) {
const lintMessage = `This project has unnecessary dependencies that violates the ${messageString} rule:\n ${currentProblems.join('\n ')}`;
report({
lintId: messageString,
severity: ruleConfig.severity.severityType,
lintMessage,
projectType,
});
}
}
findUnnecessaryDependency({ ruleConfig, projectType, report }, dependencies, 'dependency');
},
peerDependencies: (peerDependencies) => {
const messageString = 'require-no-unnecessary-dependency';
if (projectType !== 'devModule') {
const currentProblems = Object.keys(peerDependencies).map((dependency) => {
const dependencyVersion = peerDependencies[dependency];
if (devDependencySet.includes(dependency) && !(ruleConfig.severity.allowList && ruleConfig.severity.allowList.includes(dependency))) {
return `${dependency}@${dependencyVersion} does not satisfy requirement for ${messageString} rule.`;
}
return undefined;
}).filter(problem => !!problem);

if (currentProblems.length) {
const lintMessage = `This project has unnecessary peerDependencies that violates the ${messageString} rule:\n ${currentProblems.join('\n ')}`;
report({
lintId: messageString,
severity: ruleConfig.severity.severityType,
lintMessage,
projectType,
});
}
}
findUnnecessaryDependency({ ruleConfig, projectType, report }, peerDependencies, 'peer');
},
}),
};

0 comments on commit d8f833e

Please sign in to comment.