Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix validateOptions to report when secondary option object is an empty object or null #7476

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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