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

Fix: support absolute filename in preprocessor #14205

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 6 additions & 1 deletion lib/linter/linter.js
Expand Up @@ -1300,7 +1300,12 @@ class Linter {
}

const blockText = block.text;
const blockName = path.join(filename, `${i}_${block.filename}`);
const blockFilename = block.filename;
const blockName = path.isAbsolute(blockFilename)
? blockFilename
: path.join(filename, `${i}${blockFilename.startsWith(".")
? blockFilename
: `_${blockFilename}`}`);

// Skip this block if filtered.
if (!filterCodeBlock(blockName, blockText)) {
Expand Down
40 changes: 40 additions & 0 deletions tests/lib/eslint/eslint.js
Expand Up @@ -2917,6 +2917,46 @@ describe("ESLint", () => {
assert.strictEqual(results[0].messages[0].ruleId, "post-processed");
});

it("should work with absolute filename for preprocessor", async () => {
eslint = new ESLint({
useEslintrc: false,
overrideConfig: {
plugins: ["test-processor"],
rules: {
"no-console": 2,
"no-unused-vars": 2
}
},
extensions: ["js", "txt"],
ignore: false,
plugins: {
"test-processor": {
processors: {
".txt": {
preprocess(text, filename) {
return [
{
text: text.replace("a()", "b()"),
filename
}
];
},
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(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) {
Expand Down