Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/execute/ojs/errors.ts
6458 views
1
/*
2
* errors.ts
3
*
4
* Copyright (C) 2021-2022 Posit Software, PBC
5
*
6
*/
7
8
import { error } from "../../deno_ral/log.ts";
9
import { mappedIndexToLineCol } from "../../core/lib/mapped-text.ts";
10
import { MappedString } from "../../core/lib/text-types.ts";
11
12
export function ojsParseError(
13
// deno-lint-ignore no-explicit-any
14
acornError: any, // we can't use SyntaxError here because acorn injects extra properties
15
ojsSource: MappedString,
16
) {
17
const acornMsg = String(acornError).split("\n")[0].trim().replace(
18
/ *\(\d+:\d+\)$/,
19
"",
20
);
21
22
const { line, column } = mappedIndexToLineCol(ojsSource)(acornError.pos)!;
23
24
const errLines: string[] = [];
25
const ourError = (msg: string) => {
26
if (msg.length) {
27
errLines.push(msg);
28
}
29
};
30
31
const errMsg = `OJS parsing failed on line ${line + 1}, column ${column + 1}`;
32
ourError(errMsg);
33
ourError(acornMsg);
34
if (
35
acornMsg.endsWith("Unexpected character '#'") &&
36
ojsSource.value.slice(acornError.pos, acornError.pos + 2) === "#|"
37
) {
38
ourError("\n(Did you mean to use '//|' instead?)\n");
39
}
40
ourError("----- OJS Source:");
41
const ojsSourceSplit = ojsSource.value.split("\n");
42
for (let i = 0; i < ojsSourceSplit.length; ++i) {
43
ourError(ojsSourceSplit[i]);
44
if (i + 1 === acornError.loc.line) {
45
ourError(" ".repeat(acornError.loc.column) + "^");
46
}
47
}
48
ourError("-----");
49
50
if (errLines.length) {
51
error(errLines.join("\n"));
52
}
53
}
54
55
// FIXME Figure out line numbering story for error reporting
56
export function jsParseError(
57
jsSource: string,
58
errorMessage: string,
59
) {
60
error(
61
"Parse error occurred while parsing the following statement in a Javascript code block.",
62
);
63
error(errorMessage);
64
error("----- Source:");
65
error(`\n${jsSource}`);
66
error("-----");
67
}
68
69