Skip to content

Commit ca83178

Browse files
feat: catch preprocess errors (#16105)
* feat: catch preprocess error for clearer message * Update lib/linter/linter.js Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com> * refactor: code reviews Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
1 parent 49ca3f0 commit ca83178

File tree

2 files changed

+106
-3
lines changed

2 files changed

+106
-3
lines changed

‎lib/linter/linter.js‎

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,7 +1510,31 @@ class Linter {
15101510
options.filterCodeBlock ||
15111511
(blockFilename => blockFilename.endsWith(".js"));
15121512
const originalExtname = path.extname(filename);
1513-
const messageLists = preprocess(text, filenameToExpose).map((block, i) => {
1513+
1514+
let blocks;
1515+
1516+
try {
1517+
blocks = preprocess(text, filenameToExpose);
1518+
} catch (ex) {
1519+
1520+
// If the message includes a leading line number, strip it:
1521+
const message = `Preprocessing error: ${ex.message.replace(/^line \d+:/iu, "").trim()}`;
1522+
1523+
debug("%s\n%s", message, ex.stack);
1524+
1525+
return [
1526+
{
1527+
ruleId: null,
1528+
fatal: true,
1529+
severity: 2,
1530+
message,
1531+
line: ex.lineNumber,
1532+
column: ex.column
1533+
}
1534+
];
1535+
}
1536+
1537+
const messageLists = blocks.map((block, i) => {
15141538
debug("A code block was found: %o", block.filename || "(unnamed)");
15151539

15161540
// Keep the legacy behavior.
@@ -1788,13 +1812,36 @@ class Linter {
17881812
const physicalFilename = options.physicalFilename || filenameToExpose;
17891813
const text = ensureText(textOrSourceCode);
17901814
const preprocess = options.preprocess || (rawText => [rawText]);
1791-
17921815
const postprocess = options.postprocess || (messagesList => messagesList.flat());
17931816
const filterCodeBlock =
17941817
options.filterCodeBlock ||
17951818
(blockFilename => blockFilename.endsWith(".js"));
17961819
const originalExtname = path.extname(filename);
1797-
const messageLists = preprocess(text, filenameToExpose).map((block, i) => {
1820+
1821+
let blocks;
1822+
1823+
try {
1824+
blocks = preprocess(text, filenameToExpose);
1825+
} catch (ex) {
1826+
1827+
// If the message includes a leading line number, strip it:
1828+
const message = `Preprocessing error: ${ex.message.replace(/^line \d+:/iu, "").trim()}`;
1829+
1830+
debug("%s\n%s", message, ex.stack);
1831+
1832+
return [
1833+
{
1834+
ruleId: null,
1835+
fatal: true,
1836+
severity: 2,
1837+
message,
1838+
line: ex.lineNumber,
1839+
column: ex.column
1840+
}
1841+
];
1842+
}
1843+
1844+
const messageLists = blocks.map((block, i) => {
17981845
debug("A code block was found: %o", block.filename || "(unnamed)");
17991846

18001847
// Keep the legacy behavior.

‎tests/lib/linter/linter.js‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6660,6 +6660,31 @@ var a = "test2";
66606660
assert.strictEqual(preprocess.calledOnce, true);
66616661
assert.deepStrictEqual(preprocess.args[0], [code, filename]);
66626662
});
6663+
6664+
it("should catch preprocess error.", () => {
6665+
const code = "foo";
6666+
const preprocess = sinon.spy(() => {
6667+
throw Object.assign(new SyntaxError("Invalid syntax"), {
6668+
lineNumber: 1,
6669+
column: 1
6670+
});
6671+
});
6672+
6673+
const messages = linter.verify(code, {}, { filename, preprocess });
6674+
6675+
assert.strictEqual(preprocess.calledOnce, true);
6676+
assert.deepStrictEqual(preprocess.args[0], [code, filename]);
6677+
assert.deepStrictEqual(messages, [
6678+
{
6679+
ruleId: null,
6680+
fatal: true,
6681+
severity: 2,
6682+
message: "Preprocessing error: Invalid syntax",
6683+
line: 1,
6684+
column: 1
6685+
}
6686+
]);
6687+
});
66636688
});
66646689

66656690
describe("postprocessors", () => {
@@ -15158,6 +15183,37 @@ var a = "test2";
1515815183
assert.strictEqual(preprocess.calledOnce, true);
1515915184
assert.deepStrictEqual(preprocess.args[0], [code, filename]);
1516015185
});
15186+
15187+
it("should catch preprocess error.", () => {
15188+
const code = "foo";
15189+
const preprocess = sinon.spy(() => {
15190+
throw Object.assign(new SyntaxError("Invalid syntax"), {
15191+
lineNumber: 1,
15192+
column: 1
15193+
});
15194+
});
15195+
15196+
const configs = createFlatConfigArray([
15197+
extraConfig
15198+
]);
15199+
15200+
configs.normalizeSync();
15201+
15202+
const messages = linter.verify(code, configs, { filename, preprocess });
15203+
15204+
assert.strictEqual(preprocess.calledOnce, true);
15205+
assert.deepStrictEqual(preprocess.args[0], [code, filename]);
15206+
assert.deepStrictEqual(messages, [
15207+
{
15208+
ruleId: null,
15209+
fatal: true,
15210+
severity: 2,
15211+
message: "Preprocessing error: Invalid syntax",
15212+
line: 1,
15213+
column: 1
15214+
}
15215+
]);
15216+
});
1516115217
});
1516215218

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

0 commit comments

Comments
 (0)