Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve flat config errors for invalid rule options and severit…
…ies (#17140) * feat: improve flat config errors for invalid rule options and severities * Apply suggestions from code review Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com> * Updated error messages and tests per review * Account for undefined value in printing * More general truthiness check --------- Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com>
- Loading branch information
1 parent
f5574dc
commit 5db7808
Showing
6 changed files
with
107 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"use strict"; | ||
|
||
const { stringifyValueForError } = require("./shared"); | ||
|
||
module.exports = function({ ruleId, value }) { | ||
return ` | ||
Configuration for rule "${ruleId}" is invalid. Each rule must have a severity ("off", 0, "warn", 1, "error", or 2) and may be followed by additional options for the rule. | ||
You passed '${stringifyValueForError(value, 4)}', which doesn't contain a valid severity. | ||
If you're attempting to configure rule options, perhaps you meant: | ||
"${ruleId}": ["error", ${stringifyValueForError(value, 8)}] | ||
See https://eslint.org/docs/latest/use/configure/rules#using-configuration-files for configuring rules. | ||
`.trimStart(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"use strict"; | ||
|
||
const { stringifyValueForError } = require("./shared"); | ||
|
||
module.exports = function({ ruleId, value }) { | ||
return ` | ||
Configuration for rule "${ruleId}" is invalid. Expected severity of "off", 0, "warn", 1, "error", or 2. | ||
You passed '${stringifyValueForError(value, 4)}'. | ||
See https://eslint.org/docs/latest/use/configure/rules#using-configuration-files for configuring rules. | ||
`.trimStart(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* @fileoverview Shared utilities for error messages. | ||
* @author Josh Goldberg | ||
*/ | ||
|
||
"use strict"; | ||
|
||
/** | ||
* Converts a value to a string that may be printed in errors. | ||
* @param {any} value The invalid value. | ||
* @param {number} indentation How many spaces to indent | ||
* @returns {string} The value, stringified. | ||
*/ | ||
function stringifyValueForError(value, indentation) { | ||
return value ? JSON.stringify(value, null, 4).replace(/\n/gu, `\n${" ".repeat(indentation)}`) : `${value}`; | ||
} | ||
|
||
module.exports = { stringifyValueForError }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters