Skip to content

Commit

Permalink
disallow string value
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic committed May 4, 2023
1 parent a76f18a commit 04ee542
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 38 deletions.
4 changes: 2 additions & 2 deletions lib/eslint/eslint-helpers.js
Expand Up @@ -758,8 +758,8 @@ 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 (!isArrayOfNonEmptyString(ignorePatterns) && ignorePatterns !== null) {
errors.push("'ignorePatterns' must be an array of non-empty strings or null.");
}
if (typeof overrideConfig !== "object") {
errors.push("'overrideConfig' must be an object or null.");
Expand Down
50 changes: 22 additions & 28 deletions 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|string[]} [ignorePatterns] Ignore file patterns to use in addition to config ignores. These patterns are relative to `cwd`.
* @property {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 Expand Up @@ -377,45 +377,39 @@ async function calculateConfigArray(eslint, {
// add in any configured defaults
configs.push(...slots.defaultConfigs);

let allIgnorePatterns = [];

// append command line ignore patterns
if (ignorePatterns) {
if (typeof ignorePatterns === "string") {
allIgnorePatterns.push(ignorePatterns);
} else {
allIgnorePatterns.push(...ignorePatterns);
}
}
if (ignorePatterns && ignorePatterns.length > 0) {

/*
* If the config file basePath is different than the cwd, then
* the ignore patterns won't work correctly. Here, we adjust the
* ignore pattern to include the correct relative path. Patterns
* loaded from ignore files are always relative to the cwd, whereas
* the config file basePath can be an ancestor of the cwd.
*/
if (basePath !== cwd && allIgnorePatterns.length) {
let relativeIgnorePatterns;

const relativeIgnorePath = path.relative(basePath, cwd);
/*
* If the config file basePath is different than the cwd, then
* the ignore patterns won't work correctly. Here, we adjust the
* ignore pattern to include the correct relative path. Patterns
* passed as `ignorePatterns` are relative to the cwd, whereas
* the config file basePath can be an ancestor of the cwd.
*/
if (basePath === cwd) {
relativeIgnorePatterns = ignorePatterns;
} else {

allIgnorePatterns = allIgnorePatterns.map(pattern => {
const negated = pattern.startsWith("!");
const basePattern = negated ? pattern.slice(1) : pattern;
const relativeIgnorePath = path.relative(basePath, cwd);

return (negated ? "!" : "") +
path.posix.join(relativeIgnorePath, basePattern);
});
}
relativeIgnorePatterns = ignorePatterns.map(pattern => {
const negated = pattern.startsWith("!");
const basePattern = negated ? pattern.slice(1) : pattern;

if (allIgnorePatterns.length) {
return (negated ? "!" : "") +
path.posix.join(relativeIgnorePath, basePattern);
});
}

/*
* Ignore patterns are added to the end of the config array
* so they can override default ignores.
*/
configs.push({
ignores: allIgnorePatterns
ignores: relativeIgnorePatterns
});
}

Expand Down
17 changes: 9 additions & 8 deletions tests/lib/eslint/flat-eslint.js
Expand Up @@ -200,7 +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.",
"- 'ignorePatterns' must be 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 @@ -209,12 +209,13 @@ describe("FlatESLint", () => {
);
});

it("should throw readable messages if 'ignorePatterns' is not a non-empty string or an array of non-empty strings.", () => {
it("should throw readable messages if 'ignorePatterns' is not an array of non-empty strings.", () => {
const invalidIgnorePatterns = [
() => {},
false,
{},
"",
"foo",
[[]],
[() => {}],
[false],
Expand All @@ -230,7 +231,7 @@ describe("FlatESLint", () => {
() => new FlatESLint({ ignorePatterns }),
new RegExp(escapeStringRegExp([
"Invalid Options:",
"- 'ignorePatterns' must be a non-empty string, an array of non-empty strings, or null."
"- 'ignorePatterns' must be an array of non-empty strings or null."
].join("\n")), "u")
);
});
Expand Down Expand Up @@ -3578,7 +3579,7 @@ describe("FlatESLint", () => {
const engine = new FlatESLint({
cwd,
overrideConfigFile: true,
ignorePatterns: "!node_modules/package/**"
ignorePatterns: ["!node_modules/package/**"]
});

const result = await engine.isPathIgnored(getFixturePath("ignored-paths", "node_modules", "package", "file.js"));
Expand Down Expand Up @@ -4064,7 +4065,7 @@ describe("FlatESLint", () => {
const engine = new FlatESLint({
overrideConfigFile: true,
cwd: path.join(fixtureDir, "foo", "bar"),
ignorePatterns: "*/**", // ignore all subdirectories of `cwd`
ignorePatterns: ["*/**"], // ignore all subdirectories of `cwd`
overrideConfig: {
rules: {
eqeqeq: "warn"
Expand All @@ -4082,7 +4083,7 @@ describe("FlatESLint", () => {
const engine = new FlatESLint({
overrideConfigFile: true,
cwd: path.join(fixtureDir, "foo", "bar"),
ignorePatterns: "**"
ignorePatterns: ["**"]
});

const results = await engine.lintText("", { warnIgnored: true });
Expand All @@ -4095,7 +4096,7 @@ describe("FlatESLint", () => {
const engine = new FlatESLint({
overrideConfigFile: true,
cwd: getFixturePath(),
ignorePatterns: "passing*",
ignorePatterns: ["passing*"],
overrideConfig: {
rules: {
"no-undef": 2,
Expand Down Expand Up @@ -4209,7 +4210,7 @@ describe("FlatESLint", () => {
it("should ignore messages not related to a rule", async () => {
const engine = new FlatESLint({
overrideConfigFile: true,
ignorePatterns: "ignored.js",
ignorePatterns: ["ignored.js"],
overrideConfig: {
rules: {
"no-var": "warn"
Expand Down

0 comments on commit 04ee542

Please sign in to comment.