Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
JounQin and mdjermanovic committed Jul 8, 2022
1 parent 49ca3f0 commit ca83178
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 3 deletions.
53 changes: 50 additions & 3 deletions lib/linter/linter.js
Expand Up @@ -1510,7 +1510,31 @@ class Linter {
options.filterCodeBlock ||
(blockFilename => blockFilename.endsWith(".js"));
const originalExtname = path.extname(filename);
const messageLists = preprocess(text, filenameToExpose).map((block, i) => {

let blocks;

try {
blocks = preprocess(text, filenameToExpose);
} catch (ex) {

// If the message includes a leading line number, strip it:
const message = `Preprocessing error: ${ex.message.replace(/^line \d+:/iu, "").trim()}`;

debug("%s\n%s", message, ex.stack);

return [
{
ruleId: null,
fatal: true,
severity: 2,
message,
line: ex.lineNumber,
column: ex.column
}
];
}

const messageLists = blocks.map((block, i) => {
debug("A code block was found: %o", block.filename || "(unnamed)");

// Keep the legacy behavior.
Expand Down Expand Up @@ -1788,13 +1812,36 @@ class Linter {
const physicalFilename = options.physicalFilename || filenameToExpose;
const text = ensureText(textOrSourceCode);
const preprocess = options.preprocess || (rawText => [rawText]);

const postprocess = options.postprocess || (messagesList => messagesList.flat());
const filterCodeBlock =
options.filterCodeBlock ||
(blockFilename => blockFilename.endsWith(".js"));
const originalExtname = path.extname(filename);
const messageLists = preprocess(text, filenameToExpose).map((block, i) => {

let blocks;

try {
blocks = preprocess(text, filenameToExpose);
} catch (ex) {

// If the message includes a leading line number, strip it:
const message = `Preprocessing error: ${ex.message.replace(/^line \d+:/iu, "").trim()}`;

debug("%s\n%s", message, ex.stack);

return [
{
ruleId: null,
fatal: true,
severity: 2,
message,
line: ex.lineNumber,
column: ex.column
}
];
}

const messageLists = blocks.map((block, i) => {
debug("A code block was found: %o", block.filename || "(unnamed)");

// Keep the legacy behavior.
Expand Down
56 changes: 56 additions & 0 deletions tests/lib/linter/linter.js
Expand Up @@ -6660,6 +6660,31 @@ var a = "test2";
assert.strictEqual(preprocess.calledOnce, true);
assert.deepStrictEqual(preprocess.args[0], [code, filename]);
});

it("should catch preprocess error.", () => {
const code = "foo";
const preprocess = sinon.spy(() => {
throw Object.assign(new SyntaxError("Invalid syntax"), {
lineNumber: 1,
column: 1
});
});

const messages = linter.verify(code, {}, { filename, preprocess });

assert.strictEqual(preprocess.calledOnce, true);
assert.deepStrictEqual(preprocess.args[0], [code, filename]);
assert.deepStrictEqual(messages, [
{
ruleId: null,
fatal: true,
severity: 2,
message: "Preprocessing error: Invalid syntax",
line: 1,
column: 1
}
]);
});
});

describe("postprocessors", () => {
Expand Down Expand Up @@ -15158,6 +15183,37 @@ var a = "test2";
assert.strictEqual(preprocess.calledOnce, true);
assert.deepStrictEqual(preprocess.args[0], [code, filename]);
});

it("should catch preprocess error.", () => {
const code = "foo";
const preprocess = sinon.spy(() => {
throw Object.assign(new SyntaxError("Invalid syntax"), {
lineNumber: 1,
column: 1
});
});

const configs = createFlatConfigArray([
extraConfig
]);

configs.normalizeSync();

const messages = linter.verify(code, configs, { filename, preprocess });

assert.strictEqual(preprocess.calledOnce, true);
assert.deepStrictEqual(preprocess.args[0], [code, filename]);
assert.deepStrictEqual(messages, [
{
ruleId: null,
fatal: true,
severity: 2,
message: "Preprocessing error: Invalid syntax",
line: 1,
column: 1
}
]);
});
});

describe("postprocessors", () => {
Expand Down

0 comments on commit ca83178

Please sign in to comment.