Skip to content
Permalink
Browse files
Update: lint code block with same extension but different content (#1…
…4227)

* Fix: lint code block with same extension but different content

close #14207

* Chore: apply review suggestions
  • Loading branch information
JounQin committed Apr 13, 2021
1 parent eb29996 commit 41b3570b6c014c534bb3208ed00050fd99842101
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
@@ -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,
@@ -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) {

0 comments on commit 41b3570

Please sign in to comment.