From 41b3570b6c014c534bb3208ed00050fd99842101 Mon Sep 17 00:00:00 2001 From: JounQin Date: Tue, 13 Apr 2021 13:45:24 +0800 Subject: [PATCH] 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 --- lib/linter/linter.js | 6 ++--- tests/lib/eslint/eslint.js | 48 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/lib/linter/linter.js b/lib/linter/linter.js index adb5c215590..576816b5b7b 100644 --- a/lib/linter/linter.js +++ b/lib/linter/linter.js @@ -1308,9 +1308,9 @@ class Linter { return []; } - // Resolve configuration again if the file extension was changed. - if (configForRecursive && path.extname(blockName) !== originalExtname) { - debug("Resolving configuration again because the file extension was changed."); + // Resolve configuration again if the file content or extension was changed. + if (configForRecursive && (text !== blockText || path.extname(blockName) !== originalExtname)) { + debug("Resolving configuration again because the file content or extension was changed."); return this._verifyWithConfigArray( blockText, configForRecursive, diff --git a/tests/lib/eslint/eslint.js b/tests/lib/eslint/eslint.js index ec7f13cd890..c7783a6c059 100644 --- a/tests/lib/eslint/eslint.js +++ b/tests/lib/eslint/eslint.js @@ -2917,6 +2917,54 @@ describe("ESLint", () => { assert.strictEqual(results[0].messages[0].ruleId, "post-processed"); }); + it("should run processors when calling lintText with processor resolves same extension but different content correctly", async () => { + let count = 0; + + eslint = new ESLint({ + useEslintrc: false, + overrideConfig: { + plugins: ["test-processor"], + overrides: [{ + files: ["**/*.txt/*.txt"], + rules: { + "no-console": 2, + "no-unused-vars": 2 + } + }] + }, + extensions: ["txt"], + ignore: false, + plugins: { + "test-processor": { + processors: { + ".txt": { + preprocess(text) { + count++; + return [ + { + + // it will be run twice, and text will be as-is at the second time, then it will not run third time + text: text.replace("a()", "b()"), + filename: ".txt" + } + ]; + }, + postprocess(messages) { + messages[0][0].ruleId = "post-processed"; + return messages[0]; + } + } + } + } + } + }); + const results = await eslint.lintText("function a() {console.log(\"Test\");}", { filePath: "tests/fixtures/processors/test/test-processor.txt" }); + + assert.strictEqual(count, 2); + assert.strictEqual(results[0].messages[0].message, "'b' is defined but never used."); + assert.strictEqual(results[0].messages[0].ruleId, "post-processed"); + }); + describe("autofixing with processors", () => { const HTML_PROCESSOR = Object.freeze({ preprocess(text) {