Skip to content

Commit

Permalink
getting lint packages refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
kanthesha committed Jan 22, 2024
1 parent 555ec7a commit 8a9c710
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 19 deletions.
53 changes: 52 additions & 1 deletion src/rules/package-lint-dependencies-conform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ describe('Rule: package-lint-dependencies-conform', () => {
expect(result).toStrictEqual({
passed: false,
failures: [
{ message: 'eslint version is "1.0.0", when it should be "1.1.0".' },
{ message: '`eslint` is "1.0.0", when it should be "1.1.0".' },
],
});
});
Expand Down Expand Up @@ -152,4 +152,55 @@ describe('Rule: package-lint-dependencies-conform', () => {
});
});
});

it("passes if the there're no lint related dependencies in the template's package.json", async () => {
await withinSandbox(async (sandbox) => {
const template = buildMetaMaskRepository({
shortname: 'template',
directoryPath: path.join(sandbox.directoryPath, 'template'),
});
await writeFile(
path.join(template.directoryPath, 'package.json'),
JSON.stringify({
packageManager: 'a',
engines: { node: 'test' },
devDependencies: {
'@metamask/test-config-foo': '1.0.0',
},
}),
);
const project = buildMetaMaskRepository({
shortname: 'project',
directoryPath: path.join(sandbox.directoryPath, 'project'),
});
await writeFile(
path.join(project.directoryPath, 'package.json'),
JSON.stringify({
packageManager: 'a',
engines: { node: 'test' },
devDependencies: {
'@metamask/eslint-config-foo': '1.0.0',
'@typescript-eslint/foo': '1.0.0',
eslint: '1.0.0',
'eslint-plugin-foo': '1.0.0',
'eslint-config-foo': '1.0.0',
prettier: '1.0.0',
'prettier-plugin-foo': '1.0.0',
'prettier-config-foo': '1.0.0',
},
}),
);

const result = await packageLintDependenciesConform.execute({
template,
project,
pass,
fail,
});

expect(result).toStrictEqual({
passed: true,
});
});
});
});
40 changes: 22 additions & 18 deletions src/rules/package-lint-dependencies-conform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,23 @@ async function lintPackageConform(
templateDependencies: Record<string, string>,
projectDependencies: Record<string, string>,
): Promise<RuleExecutionFailure[]> {
const templateLintPackageNames =
getTemplateLintPackageNames(templateDependencies);
const templateLintPackages = getTemplateLintPackages(templateDependencies);
const failures: RuleExecutionFailure[] = [];
for (const lintPackage of templateLintPackageNames) {
const projectPackageVersion = projectDependencies[lintPackage];
const templatePackageVersion = templateDependencies[lintPackage] as string;
for (const [templatePackageName, templatePackageVersion] of Object.entries(
templateLintPackages,
)) {
const projectPackageVersion = projectDependencies[templatePackageName];
if (!projectPackageVersion) {
failures.push({
message: `\`package.json\` should list \`"${lintPackage}": "${templatePackageVersion}"\` in \`devDependencies\`, but does not.`,
message: `\`package.json\` should list \`"${templatePackageName}": "${templatePackageVersion}"\` in \`devDependencies\`, but does not.`,
});

continue;
}

if (projectPackageVersion !== templatePackageVersion) {
failures.push({
message: `${lintPackage} version is "${projectPackageVersion}", when it should be "${templatePackageVersion}".`,
message: `\`${templatePackageName}\` is "${projectPackageVersion}", when it should be "${templatePackageVersion}".`,
});
}
}
Expand All @@ -64,25 +64,29 @@ async function lintPackageConform(
}

/**
* Extracts array of lint package names from template's package.json.
* Extracts the records of lint package name and version from template's package.json.
*
* @param templateDependencies - The record of lint package name and version.
* @returns The array of lint package names.
* @returns The records of lint package name and version.
*/
function getTemplateLintPackageNames(
function getTemplateLintPackages(
templateDependencies: Record<string, string>,
): string[] {
): Record<string, string> {
const requiredPackagePatterns: RegExp[] = [
/^@metamask\/eslint-config-[^/]+$/u,
/^@typescript-eslint\/[^/]+$/u,
/^eslint(-[^/]+)?$/u,
/^prettier(-[^/]+)?$/u,
];

const templatePackageNames = Object.keys(templateDependencies);
return templatePackageNames.filter((packageName) => {
return requiredPackagePatterns.some((pattern) => {
return packageName.match(pattern);
});
});
return Object.entries(templateDependencies).reduce<Record<string, string>>(
(packages, [packageName, packageVersion]) => {
if (
requiredPackagePatterns.some((pattern) => packageName.match(pattern))
) {
return { ...packages, [packageName]: packageVersion };
}
return packages;
},
{},
);
}

0 comments on commit 8a9c710

Please sign in to comment.