Skip to content

Commit 5fd211d

Browse files
authored
test: processors can return subpaths (#19425)
1 parent 0636cab commit 5fd211d

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo bar baz

tests/lib/eslint/eslint.js

+69
Original file line numberDiff line numberDiff line change
@@ -3876,6 +3876,75 @@ describe("ESLint", () => {
38763876

38773877
});
38783878

3879+
// https://github.com/eslint/markdown/blob/main/rfcs/configure-file-name-from-block-meta.md#name-uniqueness
3880+
it("should allow processors to return filenames with a slash and treat them as subpaths", async () => {
3881+
eslint = new ESLint({
3882+
flags,
3883+
overrideConfigFile: true,
3884+
overrideConfig: [
3885+
{
3886+
plugins: {
3887+
test: {
3888+
processors: {
3889+
txt: {
3890+
preprocess(input) {
3891+
return input.split(" ").map((text, index) => ({
3892+
filename: `example-${index}/a.js`,
3893+
text
3894+
}));
3895+
},
3896+
postprocess(messagesList) {
3897+
return messagesList.flat();
3898+
}
3899+
}
3900+
},
3901+
rules: {
3902+
"test-rule": {
3903+
meta: {},
3904+
create(context) {
3905+
return {
3906+
Identifier(node) {
3907+
context.report({
3908+
node,
3909+
message: `filename: ${context.filename} physicalFilename: ${context.physicalFilename} identifier: ${node.name}`
3910+
});
3911+
}
3912+
};
3913+
}
3914+
}
3915+
}
3916+
}
3917+
}
3918+
},
3919+
{
3920+
files: ["**/*.txt"],
3921+
processor: "test/txt"
3922+
},
3923+
{
3924+
files: ["**/a.js"],
3925+
rules: {
3926+
"test/test-rule": "error"
3927+
}
3928+
}
3929+
],
3930+
cwd: path.join(fixtureDir, "..")
3931+
});
3932+
const filename = getFixturePath("processors", "test", "test-subpath.txt");
3933+
const [result] = await eslint.lintFiles([filename]);
3934+
3935+
assert.strictEqual(result.messages.length, 3);
3936+
3937+
assert.strictEqual(result.messages[0].ruleId, "test/test-rule");
3938+
assert.strictEqual(result.messages[0].message, `filename: ${path.join(filename, "0_example-0", "a.js")} physicalFilename: ${filename} identifier: foo`);
3939+
assert.strictEqual(result.messages[1].ruleId, "test/test-rule");
3940+
assert.strictEqual(result.messages[1].message, `filename: ${path.join(filename, "1_example-1", "a.js")} physicalFilename: ${filename} identifier: bar`);
3941+
assert.strictEqual(result.messages[2].ruleId, "test/test-rule");
3942+
assert.strictEqual(result.messages[2].message, `filename: ${path.join(filename, "2_example-2", "a.js")} physicalFilename: ${filename} identifier: baz`);
3943+
3944+
assert.strictEqual(result.suppressedMessages.length, 0);
3945+
3946+
});
3947+
38793948
describe("autofixing with processors", () => {
38803949
const HTML_PROCESSOR = Object.freeze({
38813950
preprocess(text) {

tests/lib/linter/linter.js

+49
Original file line numberDiff line numberDiff line change
@@ -16945,6 +16945,55 @@ var a = "test2";
1694516945
}
1694616946
]);
1694716947
});
16948+
16949+
// https://github.com/eslint/markdown/blob/main/rfcs/configure-file-name-from-block-meta.md#name-uniqueness
16950+
it("should allow preprocessor to return filenames with a slash and treat them as subpaths.", () => {
16951+
const problems = linter.verify(
16952+
"foo bar baz",
16953+
[
16954+
{
16955+
files: [filename],
16956+
processor: {
16957+
preprocess(input) {
16958+
return input.split(" ").map(text => ({
16959+
filename: "example/block.js",
16960+
text
16961+
}));
16962+
},
16963+
postprocess(messagesList) {
16964+
return messagesList.flat();
16965+
}
16966+
}
16967+
},
16968+
extraConfig,
16969+
{
16970+
files: ["**/block.js"],
16971+
rules: {
16972+
"test/report-original-text": "error"
16973+
}
16974+
}
16975+
],
16976+
{
16977+
filename
16978+
}
16979+
);
16980+
const suppressedMessages = linter.getSuppressedMessages();
16981+
16982+
assert.strictEqual(problems.length, 3);
16983+
assert.deepStrictEqual(problems.map(problem => problem.message), ["foo", "bar", "baz"]);
16984+
16985+
assert.strictEqual(suppressedMessages.length, 0);
16986+
16987+
// filename
16988+
assert.strictEqual(receivedFilenames.length, 3);
16989+
assert.match(receivedFilenames[0], /^filename\.js[/\\]0_example[/\\]block\.js/u);
16990+
assert.match(receivedFilenames[1], /^filename\.js[/\\]1_example[/\\]block\.js/u);
16991+
assert.match(receivedFilenames[2], /^filename\.js[/\\]2_example[/\\]block\.js/u);
16992+
16993+
// physical filename
16994+
assert.strictEqual(receivedPhysicalFilenames.length, 3);
16995+
assert.strictEqual(receivedPhysicalFilenames.every(name => name === filename), true);
16996+
});
1694816997
});
1694916998

1695016999
describe("postprocessors", () => {

0 commit comments

Comments
 (0)