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: validate ignorePatterns constructor option in FlatESLint class #17139

Merged
merged 2 commits into from May 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/eslint/eslint-helpers.js
Expand Up @@ -758,6 +758,9 @@ function processOptions({
if (typeof ignore !== "boolean") {
errors.push("'ignore' must be a boolean.");
}
if (!isNonEmptyString(ignorePatterns) && !isArrayOfNonEmptyString(ignorePatterns) && ignorePatterns !== null) {
errors.push("'ignorePatterns' must be a non-empty string, an array of non-empty strings, or null.");
}
if (typeof overrideConfig !== "object") {
errors.push("'overrideConfig' must be an object or null.");
}
Expand Down
2 changes: 1 addition & 1 deletion lib/eslint/flat-eslint.js
Expand Up @@ -76,7 +76,7 @@ const LintResultCache = require("../cli-engine/lint-result-cache");
* @property {string[]} [fixTypes] Array of rule types to apply fixes for.
* @property {boolean} [globInputPaths] Set to false to skip glob resolution of input file paths to lint (default: true). If false, each input file paths is assumed to be a non-glob path to an existing file.
* @property {boolean} [ignore] False disables all ignore patterns except for the default ones.
* @property {string[]} [ignorePatterns] Ignore file patterns to use in addition to config ignores.
* @property {string|string[]} [ignorePatterns] Ignore file patterns to use in addition to config ignores. These patterns are relative to `cwd`.
* @property {ConfigData} [overrideConfig] Override config object, overrides all configs used with this instance
* @property {boolean|string} [overrideConfigFile] Searches for default config file when falsy;
* doesn't do any config file lookup when `true`; considered to be a config filename
Expand Down
29 changes: 29 additions & 0 deletions tests/lib/eslint/flat-eslint.js
Expand Up @@ -182,6 +182,7 @@ describe("FlatESLint", () => {
fixTypes: ["xyz"],
globInputPaths: "",
ignore: "",
ignorePatterns: "",
overrideConfig: "",
overrideConfigFile: "",
plugins: "",
Expand All @@ -199,6 +200,7 @@ describe("FlatESLint", () => {
"- 'fixTypes' must be an array of any of \"directive\", \"problem\", \"suggestion\", and \"layout\".",
"- 'globInputPaths' must be a boolean.",
"- 'ignore' must be a boolean.",
"- 'ignorePatterns' must be a non-empty string, an array of non-empty strings, or null.",
"- 'overrideConfig' must be an object or null.",
"- 'overrideConfigFile' must be a non-empty string, null, or true.",
"- 'plugins' must be an object or null.",
Expand All @@ -207,6 +209,33 @@ describe("FlatESLint", () => {
);
});

it("should throw readable messages if 'ignorePatterns' is not a non-empty string or an array of non-empty strings.", () => {
const invalidIgnorePatterns = [
() => {},
false,
{},
"",
[[]],
[() => {}],
[false],
[{}],
[""],
["foo", ""],
["foo", "", "bar"],
["foo", false, "bar"]
];

invalidIgnorePatterns.forEach(ignorePatterns => {
assert.throws(
() => new FlatESLint({ ignorePatterns }),
new RegExp(escapeStringRegExp([
"Invalid Options:",
"- 'ignorePatterns' must be a non-empty string, an array of non-empty strings, or null."
].join("\n")), "u")
);
});
});

it("should throw readable messages if 'plugins' option contains empty key", () => {
assert.throws(
() => new FlatESLint({
Expand Down