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

feat: Implement SourceCode#applyInlineConfig() #17351

Merged
merged 29 commits into from Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
3a43264
Implement SourceCode#applyInlineConfig()
nzakas May 4, 2023
398a115
Fix lint errors
nzakas Jul 7, 2023
056cced
Add tests for forbidden methods
nzakas Jul 10, 2023
5ff2a9f
Clean up naming
nzakas Jul 10, 2023
dc94b59
Add more tests
nzakas Jul 13, 2023
3cf661a
Fix last test
nzakas Jul 13, 2023
7e02709
Fixing some bugs -- still tests failing
nzakas Jul 18, 2023
1895168
Further refactoring and bug fixes
nzakas Jul 20, 2023
d579475
Fix test for Node.js 19
nzakas Jul 20, 2023
e3e0282
Forgot to save the file
nzakas Jul 20, 2023
10b5ecb
Remove proxy; update RuleTester/FlatRuleTester
nzakas Aug 30, 2023
81b75eb
Clean up
nzakas Sep 6, 2023
24d417a
Use WeakSet to track method calls
nzakas Sep 11, 2023
c681c11
Update tests/lib/rule-tester/rule-tester.js
nzakas Sep 13, 2023
1af148b
Update tests/lib/rule-tester/rule-tester.js
nzakas Sep 13, 2023
b4ba86d
Re-add tests for FlatRuleTester
nzakas Sep 13, 2023
2180336
Apply feedback
nzakas Sep 15, 2023
271f8ba
Cleanup tests
nzakas Sep 18, 2023
0fc3fe6
Update lib/source-code/source-code.js
nzakas Sep 19, 2023
645265d
Update lib/linter/linter.js
nzakas Sep 19, 2023
41f91df
Fix inline config problems in flat config mode
nzakas Sep 19, 2023
6b8e656
Update JSON parse error message
nzakas Sep 19, 2023
5c9f32f
Fix JSON parse message again
nzakas Sep 19, 2023
36baf92
Update lib/source-code/source-code.js
nzakas Sep 22, 2023
3969b41
Update lib/linter/linter.js
nzakas Sep 22, 2023
22a16a6
Update lib/linter/linter.js
nzakas Sep 22, 2023
ff145b0
Update tests/lib/linter/linter.js
nzakas Sep 22, 2023
14edf42
Apply feedback
nzakas Sep 22, 2023
5a8363f
Merge branch 'get-inline-config' of github.com:eslint/eslint into get…
nzakas Sep 22, 2023
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
59 changes: 2 additions & 57 deletions lib/linter/linter.js
Expand Up @@ -494,13 +494,10 @@ function getDirectiveComments(sourceCode, ruleMapper, warnInlineConfig) {
* where reporting is disabled or enabled and merges them with reporting config.
* @param {SourceCode} sourceCode The SourceCode object to get comments from.
* @param {function(string): {create: Function}} ruleMapper A map from rule IDs to defined rules
* @returns {{configuredRules: Object, enabledGlobals: {value:string,comment:Token}[], exportedVariables: Object, problems: LintMessage[], disableDirectives: DisableDirective[]}}
* @returns {{problems: LintMessage[], disableDirectives: DisableDirective[]}}
* A collection of the directive comments that were found, along with any problems that occurred when parsing
*/
function getDirectiveCommentsForFlatConfig(sourceCode, ruleMapper) {
const configuredRules = {};
const enabledGlobals = Object.create(null);
const exportedVariables = {};
const problems = [];
const disableDirectives = [];

Expand Down Expand Up @@ -546,63 +543,11 @@ function getDirectiveCommentsForFlatConfig(sourceCode, ruleMapper) {
break;
}

/*
* case "eslint": {
* const parseResult = commentParser.parseJsonConfig(directiveValue, comment.loc);
*/

/*
* if (parseResult.success) {
* Object.keys(parseResult.config).forEach(name => {
* const rule = ruleMapper(name);
* const ruleValue = parseResult.config[name];
*/

/*
* if (!rule) {
* problems.push(createLintingProblem({ ruleId: name, loc: comment.loc }));
* return;
* }
*/

/*
* try {
* validator.validateRuleOptions(rule, name, ruleValue);
* } catch (err) {
* problems.push(createLintingProblem({
* ruleId: name,
* message: err.message,
* loc: comment.loc
* }));
*/

/*
* // do not apply the config, if found invalid options.
* return;
* }
*/

/*
* configuredRules[name] = ruleValue;
* });
* } else {
* problems.push(parseResult.error);
* }
*/

/*
* break;
* }
*/

// no default
}
});

return {
configuredRules,
enabledGlobals,
exportedVariables,
problems,
disableDirectives
};
Expand Down Expand Up @@ -1782,7 +1727,7 @@ class Linter {
}
}

const commentDirectives = options.allowInlineConfig
const commentDirectives = options.allowInlineConfig && !options.warnInlineConfig
? getDirectiveCommentsForFlatConfig(
sourceCode,
ruleId => getRuleFromConfig(ruleId, config)
Expand Down
188 changes: 188 additions & 0 deletions tests/lib/linter/linter.js
Expand Up @@ -2023,6 +2023,46 @@ describe("Linter", () => {

assert.strictEqual(suppressedMessages.length, 0);
});

it("should apply valid configuration even if there is an invalid configuration present", () => {
const code = [
"/* eslint no-unused-vars: [ */ // <-- this one is invalid JSON",
"/* eslint no-undef: [\"error\"] */ // <-- this one is fine, and thus should apply",
"foo(); // <-- expected no-undef error here"
].join("\n");

const messages = linter.verify(code);
const suppressedMessages = linter.getSuppressedMessages();

assert.deepStrictEqual(
messages,
[
{
severity: 2,
fatal: true,
ruleId: null,
message: "Failed to parse JSON from ' \"no-unused-vars\": [': Unexpected token } in JSON at position 21",
line: 1,
column: 1,
nodeType: null
},
{
severity: 2,
ruleId: "no-undef",
message: "'foo' is not defined.",
messageId: "undef",
line: 3,
column: 1,
endLine: 3,
endColumn: 4,
nodeType: "Identifier"
}
]
);

assert.strictEqual(suppressedMessages.length, 0);
});

});

describe("when evaluating code with comments to disable rules", () => {
Expand Down Expand Up @@ -4779,6 +4819,59 @@ var a = "test2";
});
});

describe("warnInlineConfig option", () => {
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved

it("should report both a rule violation and a warning about inline config", () => {
const code = [
"/* eslint-disable */ // <-- this should be inline config warning",
"foo(); // <-- this should be no-undef error"
].join("\n");
const config = {
rules: {
"no-undef": 2
},
noInlineConfig: true
};

const messages = linter.verify(code, config, {
filename,
warnInlineConfig: true
});
const suppressedMessages = linter.getSuppressedMessages();

assert.strictEqual(messages.length, 2);
assert.deepStrictEqual(
messages,
[
{
ruleId: null,
message: "'/*eslint-disable*/' has no effect because you have 'noInlineConfig' setting in your config.",
line: 1,
column: 1,
endLine: 1,
endColumn: 21,
severity: 1,
nodeType: null
},
{
ruleId: "no-undef",
messageId: "undef",
message: "'foo' is not defined.",
line: 2,
endLine: 2,
column: 1,
endColumn: 4,
severity: 2,
nodeType: "Identifier"
}
]
);

assert.strictEqual(suppressedMessages.length, 0);
});
});


describe("when evaluating code with comments to change config when allowInlineConfig is disabled", () => {
it("should not report a violation", () => {
const code = [
Expand Down Expand Up @@ -12013,6 +12106,46 @@ describe("Linter with FlatConfigArray", () => {

assert.strictEqual(suppressedMessages.length, 0);
});

it("should apply valid configuration even if there is an invalid configuration present", () => {
const code = [
"/* eslint no-unused-vars: [ */ // <-- this one is invalid JSON",
"/* eslint no-undef: [\"error\"] */ // <-- this one is fine, and thus should apply",
"foo(); // <-- expected no-undef error here"
].join("\n");

const messages = linter.verify(code);
const suppressedMessages = linter.getSuppressedMessages();

assert.deepStrictEqual(
messages,
[
{
severity: 2,
fatal: true,
ruleId: null,
message: "Failed to parse JSON from ' \"no-unused-vars\": [': Unexpected token } in JSON at position 21",
line: 1,
column: 1,
nodeType: null
},
{
severity: 2,
ruleId: "no-undef",
message: "'foo' is not defined.",
messageId: "undef",
line: 3,
column: 1,
endLine: 3,
endColumn: 4,
nodeType: "Identifier"
}
]
);

assert.strictEqual(suppressedMessages.length, 0);
});

});

describe("when evaluating code with comments to disable rules", () => {
Expand Down Expand Up @@ -14093,6 +14226,61 @@ var a = "test2";

});


describe("warnInlineConfig option", () => {
nzakas marked this conversation as resolved.
Show resolved Hide resolved

it("should report both a rule violation and a warning about inline config", () => {
const code = [
"/* eslint-disable */ // <-- this should be inline config warning",
"foo(); // <-- this should be no-undef error"
].join("\n");
const config = {
rules: {
"no-undef": 2
},
linterOptions: {
noInlineConfig: true
}
};

const messages = linter.verify(code, config, {
filename,
warnInlineConfig: true
});
const suppressedMessages = linter.getSuppressedMessages();

assert.strictEqual(messages.length, 2);
assert.deepStrictEqual(
messages,
[
{
ruleId: null,
message: "'/* eslint-disable */' has no effect because you have 'noInlineConfig' setting in your config.",
line: 1,
column: 1,
endLine: 1,
endColumn: 21,
severity: 1,
nodeType: null
},
{
ruleId: "no-undef",
messageId: "undef",
message: "'foo' is not defined.",
line: 2,
endLine: 2,
column: 1,
endColumn: 4,
severity: 2,
nodeType: "Identifier"
}
]
);

assert.strictEqual(suppressedMessages.length, 0);
});
});

describe("reportUnusedDisableDirectives option", () => {
it("reports problems for unused eslint-disable comments", () => {
const messages = linter.verify("/* eslint-disable */", {}, { reportUnusedDisableDirectives: true });
Expand Down