Skip to content

Commit

Permalink
Tolerate missing optional dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
cressie176 committed May 29, 2024
1 parent 23578ca commit f379c1b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/PackageJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = class PackageJson {

forEachDependencySet(fn) {
['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies'].forEach((key) => {
fn(this.#source[key] || {});
fn(this.#source[key] || {}, key);
});
}
};
5 changes: 3 additions & 2 deletions src/rules/latest.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ const rule = {
const { text } = context.getSourceCode();
const packageJson = new PackageJson(text);

packageJson.forEachDependencySet((dependencies) => {
packageJson.forEachDependencySet((dependencies, set) => {
Object.keys(dependencies).forEach((dependency) => {

if (!packages.includes(dependency)) return;
if (isUrl(dependencies[dependency])) return;

const installed = getInstalledPackage(npmOptions, dependency);
if (!installed || installed.missing) {
if (!installed?.version || installed.missing) {
if (set === 'optionalDependencies') return;
context.report({ node, messageId: 'missing', data: { dependency } });
return;
}
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/latest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
"lodash": "latest",
"oubliette": "1.0.4",
"zunit": "latest"
},
"optionalDependencies": {
"missing": "1.0.0"
}
}
20 changes: 19 additions & 1 deletion test/latest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,31 @@ describe('latest', () => {
expect(results[0]).toHaveProperty('warningCount', 0);
});

it('should tolerate missing optional packages', async () => {
const results = await createLinter({
'tech-radar/latest': [
'error',
{
packages: [
'missing',
],
cwd,
},
],
}).lintFiles('package.json');

expect(results).toHaveLength(1);
expect(results[0]).toHaveProperty('errorCount', 0);
expect(results[0]).toHaveProperty('warningCount', 0);
});

describe('latest-missing', () => {

beforeEach(() => {
cwd = path.resolve(__dirname, 'fixtures', 'latest-missing');
});

it('should report missing packages', async () => {
it('should report missing, mandatory packages', async () => {
const results = await createLinter({
'tech-radar/latest': [
'error',
Expand Down

0 comments on commit f379c1b

Please sign in to comment.