Skip to content

Commit

Permalink
feat(rules): no-missing-pkg-files checks browser and types fields
Browse files Browse the repository at this point in the history
Closes #304
  • Loading branch information
boneskull committed Aug 24, 2023
1 parent 79b7a95 commit 2beb84f
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/rules/builtin/no-missing-pkg-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ const noMissingPkgFiles = createRule({
if (opts?.bin !== false) {
fieldsToCheck.push('bin');
}
if (opts?.browser !== false) {
fieldsToCheck.push('browser');
}
if (opts?.types !== false) {
fieldsToCheck.push('types');
}

/**
* @param relativePath Path from package root to file
Expand Down Expand Up @@ -68,6 +74,16 @@ const noMissingPkgFiles = createRule({
.default(true)
.optional()
.describe('Check the "bin" field (if it exists)'),
browser: z
.boolean()
.default(true)
.optional()
.describe('Check the "browser" field (if it exists)'),
types: z
.boolean()
.default(true)
.optional()
.describe('Check the "types" field (if it exists)'),
fields: z
.array(z.string().min(1))
.optional()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "no-missing-pkg-files-types",
"version": "1.0.0",
"browser": "index.browser.js"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "no-missing-pkg-files-types",
"version": "1.0.0",
"types": "index.d.ts"
}
57 changes: 57 additions & 0 deletions test/e2e/rules/no-missing-pkg-files.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ describe('midnight-smoker', function () {
});
});
});

describe('when the "bin" field is a string', function () {
beforeEach(function () {
({ruleConfig, pkg} = setupRuleTest(
Expand Down Expand Up @@ -99,6 +100,62 @@ describe('midnight-smoker', function () {
);
});
});

describe('when the "types" field is a string', function () {
beforeEach(function () {
({ruleConfig, pkg} = setupRuleTest('no-missing-pkg-files-types'));
});

it('should return a RuleFailure', async function () {
await expect(
applyRules(ruleConfig, pkg, ruleCont),
'to be fulfilled with value satisfying',
{
failed: [
{
rule: noMissingPkgFiles.toJSON(),
message:
'File from "types" field unreadable at path: index.d.ts',
context: {
pkgJson: expect.it('to be an object'),
pkgJsonPath: expect.it('to be a string'),
pkgPath: expect.it('to be a string'),
severity: RuleSeverities.ERROR,
},
},
],
},
);
});
});

describe('when the "browser" field is a string', function () {
beforeEach(function () {
({ruleConfig, pkg} = setupRuleTest('no-missing-pkg-files-browser'));
});

it('should return a RuleFailure', async function () {
await expect(
applyRules(ruleConfig, pkg, ruleCont),
'to be fulfilled with value satisfying',
{
failed: [
{
rule: noMissingPkgFiles.toJSON(),
message:
'File from "browser" field unreadable at path: index.browser.js',
context: {
pkgJson: expect.it('to be an object'),
pkgJsonPath: expect.it('to be a string'),
pkgPath: expect.it('to be a string'),
severity: RuleSeverities.ERROR,
},
},
],
},
);
});
});
});

describe('when run with options', function () {
Expand Down

0 comments on commit 2beb84f

Please sign in to comment.