Skip to content

Commit 6993cba

Browse files
[yaml] Improve error messages when mixing list and key-value syntax (#2610)
1 parent 068119b commit 6993cba

3 files changed

Lines changed: 64 additions & 4 deletions

File tree

‎pkgs/yaml/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## 3.2.0-wip
22

33
* Export `ErrorListener` and `ErrorCollector` from `package:yaml/yaml.dart`.
4+
* Improve error messages when mixing list and key-value syntax.
45

56
## 3.1.4
67

‎pkgs/yaml/lib/src/parser.dart‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,13 @@ class Parser {
389389
return _parseNode(block: true);
390390
}
391391

392-
throw YamlException("While parsing a block collection, expected '-'.",
393-
token.span.start.pointSpan());
392+
final message =
393+
StringBuffer("While parsing a block collection, expected '-'.");
394+
if (token.type == TokenType.value) {
395+
message.write(
396+
' You cannot mix list and key-value syntax in the same collection.');
397+
}
398+
throw YamlException(message.toString(), token.span.start.pointSpan());
394399
}
395400

396401
/// Parses the productions:
@@ -461,8 +466,13 @@ class Parser {
461466
return Event(EventType.mappingEnd, token.span);
462467
}
463468

464-
throw YamlException('Expected a key while parsing a block mapping.',
465-
token.span.start.pointSpan());
469+
final message =
470+
StringBuffer('Expected a key while parsing a block mapping.');
471+
if (token.type == TokenType.blockEntry) {
472+
message.write(
473+
' You cannot mix list and key-value syntax in the same collection.');
474+
}
475+
throw YamlException(message.toString(), token.span.start.pointSpan());
466476
}
467477

468478
/// Parses the productions:

‎pkgs/yaml/test/yaml_test.dart‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,55 @@ void main() {
3030
\tbar"
3131
error'''), throwsA(isNot(tabError)));
3232
});
33+
34+
Matcher yamlError(String message, int line, int column) =>
35+
isA<YamlException>()
36+
.having((e) => e.message, 'message', message)
37+
.having((e) => e.span!.start.line, 'line', line)
38+
.having((e) => e.span!.start.column, 'column', column)
39+
.having((e) => e.span!.length, 'span length', 0);
40+
41+
test('a list entry in a block mapping', () {
42+
expect(
43+
() => loadYaml('linter:\n'
44+
' rules:\n'
45+
' close_sinks: false\n'
46+
' - empty_statements\n'),
47+
throwsA(yamlError(
48+
'Expected a key while parsing a block mapping. '
49+
'You cannot mix list and key-value syntax in the same '
50+
'collection.',
51+
3,
52+
4)));
53+
});
54+
55+
test('a mapping entry in a block sequence', () {
56+
expect(
57+
() => loadYaml('linter:\n'
58+
' rules:\n'
59+
' - empty_statements\n'
60+
' close_sinks: false\n'),
61+
throwsA(yamlError(
62+
"While parsing a block collection, expected '-'. "
63+
'You cannot mix list and key-value syntax in the same '
64+
'collection.',
65+
3,
66+
15)));
67+
});
68+
69+
test('a block scalar where a mapping key is expected', () {
70+
expect(
71+
() => loadYaml('a: 1\n|\n b\n'),
72+
throwsA(yamlError(
73+
'Expected a key while parsing a block mapping.', 1, 0)));
74+
});
75+
76+
test('a block scalar where a list entry is expected', () {
77+
expect(
78+
() => loadYaml('- a\n|\n b\n'),
79+
throwsA(yamlError(
80+
"While parsing a block collection, expected '-'.", 1, 0)));
81+
});
3382
});
3483

3584
group('refuses', () {

0 commit comments

Comments
 (0)