Skip to content

Commit f02d168

Browse files
authored
Fix validateOptions to report when secondary option object is an empty object or null (#7476)
1 parent a889974 commit f02d168

File tree

4 files changed

+41
-12
lines changed

4 files changed

+41
-12
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"stylelint": patch
3+
---
4+
5+
Fixed: `validateOptions` to report when secondary option object is an empty object or null

lib/utils/__tests__/validateOptions.test.mjs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,30 @@ describe('validateOptions for `*-no-*` rule with no valid options', () => {
181181
expect(result.warn).toHaveBeenCalledTimes(0);
182182
});
183183

184+
it('with empty object as `possible`', () => {
185+
validateOptions(result, 'no-dancing', {
186+
possible: {},
187+
actual: undefined,
188+
});
189+
expect(result.warn).toHaveBeenCalledTimes(0);
190+
});
191+
192+
it('with undefined as `possible`', () => {
193+
validateOptions(result, 'no-dancing', {
194+
possible: undefined,
195+
actual: undefined,
196+
});
197+
expect(result.warn).toHaveBeenCalledTimes(0);
198+
});
199+
200+
it('with null as `possible`', () => {
201+
validateOptions(result, 'no-dancing', {
202+
possible: null,
203+
actual: undefined,
204+
});
205+
expect(result.warn).toHaveBeenCalledTimes(0);
206+
});
207+
184208
it('with `possible` left undefined', () => {
185209
validateOptions(result, 'no-dancing', {
186210
actual: undefined,

lib/utils/validateOptions.cjs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,23 @@ function validateOptions(result, ruleName, ...optionDescriptions) {
4545
* @param {string} ruleName
4646
* @param {(message: string) => void} complain
4747
*/
48-
function validate(opts, ruleName, complain) {
49-
const possible = opts.possible;
50-
const actual = opts.actual;
51-
const optional = opts.optional;
52-
48+
function validate({ possible, actual, optional }, ruleName, complain) {
5349
if (actual === false && !ruleName.startsWith('report')) {
5450
return complain(
5551
`Invalid option value "false" for rule "${ruleName}". Are you trying to disable this rule? If so use "null" instead`,
5652
);
5753
}
5854

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

6360
const nothingPossible =
64-
possible === undefined || (Array.isArray(possible) && possible.length === 0);
61+
possible === undefined ||
62+
possible === null ||
63+
(Array.isArray(possible) && possible.length === 0) ||
64+
(validateTypes.isPlainObject(possible) && Object.keys(possible).length === 0);
6565

6666
if (nothingPossible && actual === true) {
6767
return;

lib/utils/validateOptions.mjs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,23 @@ export default function validateOptions(result, ruleName, ...optionDescriptions)
4141
* @param {string} ruleName
4242
* @param {(message: string) => void} complain
4343
*/
44-
function validate(opts, ruleName, complain) {
45-
const possible = opts.possible;
46-
const actual = opts.actual;
47-
const optional = opts.optional;
48-
44+
function validate({ possible, actual, optional }, ruleName, complain) {
4945
if (actual === false && !ruleName.startsWith('report')) {
5046
return complain(
5147
`Invalid option value "false" for rule "${ruleName}". Are you trying to disable this rule? If so use "null" instead`,
5248
);
5349
}
5450

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

5956
const nothingPossible =
60-
possible === undefined || (Array.isArray(possible) && possible.length === 0);
57+
possible === undefined ||
58+
possible === null ||
59+
(Array.isArray(possible) && possible.length === 0) ||
60+
(isPlainObject(possible) && Object.keys(possible).length === 0);
6161

6262
if (nothingPossible && actual === true) {
6363
return;

0 commit comments

Comments
 (0)