Skip to content

Commit

Permalink
Add invalid rule primary option message for false (#6250)
Browse files Browse the repository at this point in the history
* feat: warn if the primary option is false

* fix(rules): false is invalid as a primary option

Co-authored-by: Gary G <>
  • Loading branch information
Mouvedia committed Aug 11, 2022
1 parent 9f79437 commit 096113e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/__tests__/fixtures/plugin-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const ruleName = 'plugin/async';
const rule = (enabled) => (root, result) => {
const validOptions = stylelint.utils.validateOptions(result, ruleName, {
actual: enabled,
possible: [true, false],
possible: [true],
});

if (!validOptions) {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/selector-no-qualifying-type/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const rule = (primary, secondaryOptions) => {
ruleName,
{
actual: primary,
possible: [true, false],
possible: [true],
},
{
actual: secondaryOptions,
Expand Down
12 changes: 6 additions & 6 deletions lib/utils/__tests__/validateOptions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ describe('validateOptions for primary options', () => {
expect(result.warn.mock.calls[0][0]).toBe('Invalid option value "d" for rule "foo"');
});

it('passing boolean equivalence', () => {
it('passing null equivalence', () => {
validateOptions(result, 'foo', {
possible: [true, false],
actual: false,
possible: [true, null],
actual: null,
});
expect(result.warn).toHaveBeenCalledTimes(0);
});

it('failing boolean equivalence', () => {
it('failing null equivalence', () => {
validateOptions(result, 'foo', {
possible: [true, false],
possible: [true, null],
actual: 'a',
});
expect(result.warn).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -204,7 +204,7 @@ describe('validateOptions for `*-no-*` rule with no valid options', () => {
});
expect(result.warn.mock.calls[0]).toHaveLength(2);
expect(result.warn.mock.calls[0][0]).toBe(
'Unexpected option value "false" for rule "no-dancing"',
'Invalid option value "false" for rule "no-dancing". Are you trying to disable this rule? If so use "null" instead',
);
});

Expand Down
6 changes: 6 additions & 0 deletions lib/utils/validateOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ function validate(opts, ruleName, complain) {
const actual = opts.actual;
const optional = opts.optional;

if (actual === false && !ruleName.startsWith('report')) {
return complain(
`Invalid option value "false" for rule "${ruleName}". Are you trying to disable this rule? If so use "null" instead`,
);
}

if (actual === null || arrayEqual(actual, [null])) {
return;
}
Expand Down

0 comments on commit 096113e

Please sign in to comment.