Skip to content

Commit

Permalink
Fix validateOptions to report when secondary option object is an em…
Browse files Browse the repository at this point in the history
…pty object or null
  • Loading branch information
ybiquitous committed Jan 20, 2024
1 parent 6a4397c commit 8fd46d3
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/olive-experts-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"stylelint": patch
---

Fixed: `validateOptions` to report when secondary option object is an empty object or null
24 changes: 24 additions & 0 deletions lib/utils/__tests__/validateOptions.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,30 @@ describe('validateOptions for `*-no-*` rule with no valid options', () => {
expect(result.warn).toHaveBeenCalledTimes(0);
});

it('with empty object as `possible`', () => {
validateOptions(result, 'no-dancing', {
possible: {},
actual: undefined,
});
expect(result.warn).toHaveBeenCalledTimes(0);
});

it('with undefined as `possible`', () => {
validateOptions(result, 'no-dancing', {
possible: undefined,
actual: undefined,
});
expect(result.warn).toHaveBeenCalledTimes(0);
});

it('with null as `possible`', () => {
validateOptions(result, 'no-dancing', {
possible: null,
actual: undefined,
});
expect(result.warn).toHaveBeenCalledTimes(0);
});

it('with `possible` left undefined', () => {
validateOptions(result, 'no-dancing', {
actual: undefined,
Expand Down
12 changes: 6 additions & 6 deletions lib/utils/validateOptions.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,23 @@ function validateOptions(result, ruleName, ...optionDescriptions) {
* @param {string} ruleName
* @param {(message: string) => void} complain
*/
function validate(opts, ruleName, complain) {
const possible = opts.possible;
const actual = opts.actual;
const optional = opts.optional;

function validate({ possible, actual, optional }, ruleName, complain) {
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`,
);
}

// `null` means to turn off a rule.
if (actual === null || arrayEqual(actual, [null])) {
return;
}

const nothingPossible =
possible === undefined || (Array.isArray(possible) && possible.length === 0);
possible === undefined ||
possible === null ||
(Array.isArray(possible) && possible.length === 0) ||
(validateTypes.isPlainObject(possible) && Object.keys(possible).length === 0);

if (nothingPossible && actual === true) {
return;
Expand Down
12 changes: 6 additions & 6 deletions lib/utils/validateOptions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,23 @@ export default function validateOptions(result, ruleName, ...optionDescriptions)
* @param {string} ruleName
* @param {(message: string) => void} complain
*/
function validate(opts, ruleName, complain) {
const possible = opts.possible;
const actual = opts.actual;
const optional = opts.optional;

function validate({ possible, actual, optional }, ruleName, complain) {
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`,
);
}

// `null` means to turn off a rule.
if (actual === null || arrayEqual(actual, [null])) {
return;
}

const nothingPossible =
possible === undefined || (Array.isArray(possible) && possible.length === 0);
possible === undefined ||
possible === null ||
(Array.isArray(possible) && possible.length === 0) ||
(isPlainObject(possible) && Object.keys(possible).length === 0);

if (nothingPossible && actual === true) {
return;
Expand Down

0 comments on commit 8fd46d3

Please sign in to comment.