Skip to content

Commit

Permalink
Potential solution: boolean applyDefaultOptions param for runRules
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaKGoldberg committed Feb 15, 2024
1 parent 0dc4810 commit adb7972
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
20 changes: 18 additions & 2 deletions lib/linter/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -950,13 +950,27 @@ function createRuleListeners(rule, ruleContext) {
* @param {LanguageOptions} languageOptions The options for parsing the code.
* @param {Object} settings The settings that were enabled in the config
* @param {string} filename The reported filename of the code
* @param {boolean} applyDefaultOptions If true, apply rules' meta.defaultOptions in computing their config options.
* @param {boolean} disableFixes If true, it doesn't make `fix` properties.
* @param {string | undefined} cwd cwd of the cli
* @param {string} physicalFilename The full path of the file on disk without any code block information
* @param {Function} ruleFilter A predicate function to filter which rules should be executed.
* @returns {LintMessage[]} An array of reported problems
*/
function runRules(sourceCode, configuredRules, ruleMapper, parserName, languageOptions, settings, filename, disableFixes, cwd, physicalFilename, ruleFilter) {
function runRules(
sourceCode,
configuredRules,
ruleMapper,
parserName,
languageOptions,
settings,
filename,
applyDefaultOptions,
disableFixes,
cwd,
physicalFilename,
ruleFilter
) {
const emitter = createEmitter();
const nodeQueue = [];
let currentNode = sourceCode.ast;
Expand Down Expand Up @@ -1024,7 +1038,7 @@ function runRules(sourceCode, configuredRules, ruleMapper, parserName, languageO
Object.create(sharedTraversalContext),
{
id: ruleId,
options: getRuleOptions(configuredRules[ruleId], rule.meta?.defaultOptions),
options: getRuleOptions(configuredRules[ruleId], applyDefaultOptions && rule.meta?.defaultOptions),
report(...args) {

/*
Expand Down Expand Up @@ -1411,6 +1425,7 @@ class Linter {
languageOptions,
settings,
options.filename,
true,
options.disableFixes,
slots.cwd,
providedOptions.physicalFilename,
Expand Down Expand Up @@ -1843,6 +1858,7 @@ class Linter {
languageOptions,
settings,
options.filename,
false,
options.disableFixes,
slots.cwd,
providedOptions.physicalFilename,
Expand Down
4 changes: 2 additions & 2 deletions tests/lib/linter/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6763,7 +6763,7 @@ var a = "test2";
});

describe("options", () => {
it("rules should apply meta.defaultOptions on top of schema defaults", () => {
it("rules should apply meta.defaultOptions and ignore schema defaults", () => {
linter.defineRule("my-rule", {
meta: {
defaultOptions: [{
Expand Down Expand Up @@ -6803,7 +6803,7 @@ var a = "test2";

assert.deepStrictEqual(
JSON.parse(messages[0].message),
{ inBoth: "from-default-options", inDefaultOptions: "from-default-options", inSchema: "from-schema" }
{ inBoth: "from-default-options", inDefaultOptions: "from-default-options" }
);
});
});
Expand Down

0 comments on commit adb7972

Please sign in to comment.