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

feat: catch preprocess errors #16105

Merged
merged 3 commits into from Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 27 additions & 1 deletion lib/linter/linter.js
Expand Up @@ -1510,7 +1510,33 @@ 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 [
{
success: false,
error: {
ruleId: null,
fatal: true,
severity: 2,
message,
line: ex.lineNumber,
column: ex.column
}
}
JounQin marked this conversation as resolved.
Show resolved Hide resolved
];
}

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

// Keep the legacy behavior.
Expand Down
17 changes: 17 additions & 0 deletions tests/lib/linter/linter.js
Expand Up @@ -6660,6 +6660,23 @@ 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
});
});

linter.verify(code, {});
const sourceCode = linter.getSourceCode();
JounQin marked this conversation as resolved.
Show resolved Hide resolved

assert.throw(() => linter.verify(sourceCode, {}, { filename, preprocess }), "Invalid syntax");
JounQin marked this conversation as resolved.
Show resolved Hide resolved
assert.strictEqual(preprocess.calledOnce, true);
assert.deepStrictEqual(preprocess.args[0], [code, filename]);
});
});

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