Skip to content

Commit

Permalink
fix: Ensure config keys are printed for config errors
Browse files Browse the repository at this point in the history
fixes #17960
  • Loading branch information
nzakas committed Jan 10, 2024
1 parent 40be60e commit 52ceb8b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"@eslint-community/regexpp": "^4.6.1",
"@eslint/eslintrc": "^3.0.0",
"@eslint/js": "9.0.0-alpha.0",
"@humanwhocodes/config-array": "^0.11.13",
"@humanwhocodes/config-array": "^0.11.14",
"@humanwhocodes/module-importer": "^1.0.1",
"@nodelib/fs.walk": "^1.2.8",
"ajv": "^6.12.4",
Expand Down
15 changes: 15 additions & 0 deletions tests/bin/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,21 @@ describe("bin/eslint.js", () => {
});
});

// https://github.com/eslint/eslint/issues/17960
it("should include key information in the error message when there is an invalid config", () => {

// The error message should include the key name
const config = path.join(__dirname, "../fixtures/bin/eslint.config-invalid-key.js");
const child = runESLint(["--config", config, "conf", "tools"]);
const exitCodeAssertion = assertExitCode(child, 2);
const outputAssertion = getOutput(child).then(output => {
assert.include(output.stderr, "Key \"linterOptions\": Key \"reportUnusedDisableDirectives\"");
});

return Promise.all([exitCodeAssertion, outputAssertion]);

});

it("prints the error message pointing to line of code", () => {
const invalidConfig = path.join(__dirname, "../fixtures/bin/eslint.config.js");
const child = runESLint(["--no-ignore", "-c", invalidConfig]);
Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/bin/eslint.config-invalid-key.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = [{
linterOptions: {
reportUnusedDisableDirectives: "banana"
}
}];
25 changes: 24 additions & 1 deletion tests/lib/config/flat-config-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,7 @@ describe("FlatConfigArray", () => {
reportUnusedDisableDirectives: {}
}
}
], /Expected one of: "error", "warn", "off", 0, 1, 2, or a boolean./u);
], /Key "linterOptions": Key "reportUnusedDisableDirectives": Expected one of: "error", "warn", "off", 0, 1, 2, or a boolean./u);
});

it("should merge two objects when second object has overrides", () => assertMergedResult([
Expand Down Expand Up @@ -2264,4 +2264,27 @@ describe("FlatConfigArray", () => {
});

});

describe("invalid options", () => {

it("should report key when an invalid option is found during merge", () => {
const configs = new FlatConfigArray([
{
rules: {
}
},
{
linterOptions: {
reportUnusedDisableDirectives: "foo"
}
}
]);

configs.normalizeSync();

assert.throws(() => {
configs.getConfig("foo.js");
}, /Key "linterOptions": Key "reportUnusedDisableDirectives":/u);
});
});
});

0 comments on commit 52ceb8b

Please sign in to comment.