Skip to content

Commit 2dd6769

Browse files
cscheidjjallaire
authored andcommitted
improve error message for bad yaml with expr tags. Closes #1949
1 parent 77a7be9 commit 2dd6769

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

news/changelog-1.1.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
## Code Blocks
3838

3939
- Support `filename` attribute for attaching a file name header to code blocks
40+
- Improve YAML parse error messages in `r` code blocks using `!expr` YAML (#1949)
4041

4142
## OJS
4243

src/core/lib/yaml-intelligence/annotated-yaml.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { AnnotatedParse, JSONValue } from "../yaml-schema/types.ts";
1111
import {
1212
asMappedString,
1313
mappedIndexToLineCol,
14+
mappedLines,
1415
MappedString,
1516
} from "../mapped-text.ts";
1617
import { getTreeSitterSync } from "./parsing.ts";
@@ -19,6 +20,7 @@ import { load as jsYamlParse } from "../external/js-yaml.js";
1920

2021
import { QuartoJSONSchema } from "./js-yaml-schema.ts";
2122
import { createSourceContext } from "../yaml-validation/errors.ts";
23+
import { tidyverseInfo } from "../errors.ts";
2224

2325
// deno-lint-ignore no-explicit-any
2426
type TreeSitterParse = any;
@@ -64,10 +66,13 @@ export function readAnnotatedYamlFromMappedString(
6466
try {
6567
return buildJsYamlAnnotation(mappedSource);
6668
} catch (e) {
69+
// FIXME we should convert this to a TidyverseError
70+
// for now, we just use the util functions.
6771
const m = e.stack.split("\n")[0].match(/^.+ \((\d+):(\d+)\)$/);
6872
if (m) {
6973
const f = lineColToIndex(mappedSource.value);
70-
const offset = f({ line: Number(m[1]) - 1, column: Number(m[2] - 1) });
74+
const location = { line: Number(m[1]) - 1, column: Number(m[2] - 1) };
75+
const offset = f(location);
7176
const { originalString } = mappedSource.map(offset, true)!;
7277
const filename = originalString.fileName!;
7378
const f2 = mappedIndexToLineCol(mappedSource);
@@ -80,6 +85,22 @@ export function readAnnotatedYamlFromMappedString(
8085
column + 1
8186
})\n${sourceContext}`;
8287
e.message = e.stack;
88+
if (
89+
mappedLines(mappedSource)[location.line].value.indexOf("!expr") !==
90+
-1 &&
91+
e.reason.match(/bad indentation of a mapping entry/)
92+
) {
93+
e.message = `${e.message}\n${
94+
tidyverseInfo(
95+
"YAML tags like !expr must be followed by YAML strings.",
96+
)
97+
}\n${
98+
tidyverseInfo(
99+
"Is it possible you need to quote the value you passed to !expr ?",
100+
)
101+
}`;
102+
}
103+
83104
e.stack = "";
84105
}
85106
throw e;

src/resources/editor/tools/vs-code.mjs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19893,6 +19893,10 @@ function mappedIndexToLineCol(eitherText) {
1989319893
return indexToLineCol(originalString.value)(index);
1989419894
};
1989519895
}
19896+
function mappedLines(str2, keepNewLines = false) {
19897+
const lines2 = rangedLines(str2.value, keepNewLines);
19898+
return lines2.map((v) => mappedString(str2, [v.range]));
19899+
}
1989619900

1989719901
// parsing.ts
1989819902
var _parser;
@@ -25509,7 +25513,8 @@ function readAnnotatedYamlFromMappedString(mappedSource2) {
2550925513
const m = e.stack.split("\n")[0].match(/^.+ \((\d+):(\d+)\)$/);
2551025514
if (m) {
2551125515
const f = lineColToIndex(mappedSource2.value);
25512-
const offset = f({ line: Number(m[1]) - 1, column: Number(m[2] - 1) });
25516+
const location = { line: Number(m[1]) - 1, column: Number(m[2] - 1) };
25517+
const offset = f(location);
2551325518
const { originalString } = mappedSource2.map(offset, true);
2551425519
const filename = originalString.fileName;
2551525520
const f2 = mappedIndexToLineCol(mappedSource2);
@@ -25521,6 +25526,11 @@ function readAnnotatedYamlFromMappedString(mappedSource2) {
2552125526
e.stack = `${e.reason} (${filename}, ${line + 1}:${column + 1})
2552225527
${sourceContext}`;
2552325528
e.message = e.stack;
25529+
if (mappedLines(mappedSource2)[location.line].value.indexOf("!expr") !== -1 && e.reason.match(/bad indentation of a mapping entry/)) {
25530+
e.message = `${e.message}
25531+
${tidyverseInfo("YAML tags like !expr must be followed by YAML strings.")}
25532+
${tidyverseInfo("Is it possible you need to quote the value you passed to !expr ?")}`;
25533+
}
2552425534
e.stack = "";
2552525535
}
2552625536
throw e;

src/resources/editor/tools/yaml/web-worker.js

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)