Skip to content

Commit 41b3570

Browse files
authored
Update: lint code block with same extension but different content (#14227)
* Fix: lint code block with same extension but different content close #14207 * Chore: apply review suggestions
1 parent eb29996 commit 41b3570

2 files changed

Lines changed: 51 additions & 3 deletions

File tree

lib/linter/linter.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,9 +1308,9 @@ class Linter {
13081308
return [];
13091309
}
13101310

1311-
// Resolve configuration again if the file extension was changed.
1312-
if (configForRecursive && path.extname(blockName) !== originalExtname) {
1313-
debug("Resolving configuration again because the file extension was changed.");
1311+
// Resolve configuration again if the file content or extension was changed.
1312+
if (configForRecursive && (text !== blockText || path.extname(blockName) !== originalExtname)) {
1313+
debug("Resolving configuration again because the file content or extension was changed.");
13141314
return this._verifyWithConfigArray(
13151315
blockText,
13161316
configForRecursive,

tests/lib/eslint/eslint.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2917,6 +2917,54 @@ describe("ESLint", () => {
29172917
assert.strictEqual(results[0].messages[0].ruleId, "post-processed");
29182918
});
29192919

2920+
it("should run processors when calling lintText with processor resolves same extension but different content correctly", async () => {
2921+
let count = 0;
2922+
2923+
eslint = new ESLint({
2924+
useEslintrc: false,
2925+
overrideConfig: {
2926+
plugins: ["test-processor"],
2927+
overrides: [{
2928+
files: ["**/*.txt/*.txt"],
2929+
rules: {
2930+
"no-console": 2,
2931+
"no-unused-vars": 2
2932+
}
2933+
}]
2934+
},
2935+
extensions: ["txt"],
2936+
ignore: false,
2937+
plugins: {
2938+
"test-processor": {
2939+
processors: {
2940+
".txt": {
2941+
preprocess(text) {
2942+
count++;
2943+
return [
2944+
{
2945+
2946+
// it will be run twice, and text will be as-is at the second time, then it will not run third time
2947+
text: text.replace("a()", "b()"),
2948+
filename: ".txt"
2949+
}
2950+
];
2951+
},
2952+
postprocess(messages) {
2953+
messages[0][0].ruleId = "post-processed";
2954+
return messages[0];
2955+
}
2956+
}
2957+
}
2958+
}
2959+
}
2960+
});
2961+
const results = await eslint.lintText("function a() {console.log(\"Test\");}", { filePath: "tests/fixtures/processors/test/test-processor.txt" });
2962+
2963+
assert.strictEqual(count, 2);
2964+
assert.strictEqual(results[0].messages[0].message, "'b' is defined but never used.");
2965+
assert.strictEqual(results[0].messages[0].ruleId, "post-processed");
2966+
});
2967+
29202968
describe("autofixing with processors", () => {
29212969
const HTML_PROCESSOR = Object.freeze({
29222970
preprocess(text) {

0 commit comments

Comments
 (0)