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 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
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