Skip to content

Commit 39694c3

Browse files
committed
nicer error messages
1 parent f97036d commit 39694c3

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

src/parse/read/directives.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
import { parseExpressionAt } from 'acorn';
22
import repeat from '../../utils/repeat';
3-
import list from '../../utils/list';
43
import { Parser } from '../index';
54

65
const DIRECTIVES = {
76
Ref: {
87
names: ['ref'],
98
attribute(start, end, type, name) {
109
return { start, end, type, name };
11-
}
10+
},
11+
allowedExpressionTypes: [],
12+
error: 'ref directives cannot have a value'
1213
},
1314

1415
EventHandler: {
1516
names: ['on'],
16-
allowedExpressionTypes: ['CallExpression'],
1717
attribute(start, end, type, name, expression) {
1818
return { start, end, type, name, expression };
19-
}
19+
},
20+
allowedExpressionTypes: ['CallExpression'],
21+
error: 'Expected a method call'
2022
},
2123

2224
Binding: {
2325
names: ['bind'],
24-
allowedExpressionTypes: ['Identifier', 'MemberExpression'],
2526
attribute(start, end, type, name, expression) {
2627
return {
2728
start, end, type, name,
@@ -32,19 +33,22 @@ const DIRECTIVES = {
3233
name,
3334
}
3435
};
35-
}
36+
},
37+
allowedExpressionTypes: ['Identifier', 'MemberExpression'],
38+
error: 'Can only bind to an identifier (e.g. `foo`) or a member expression (e.g. `foo.bar` or `foo[baz]`)'
3639
},
3740

3841
Transition: {
3942
names: ['in', 'out', 'transition'],
40-
allowedExpressionTypes: ['ObjectExpression'],
4143
attribute(start, end, type, name, expression, directiveName) {
4244
return {
4345
start, end, type, name, expression,
4446
intro: directiveName === 'in' || directiveName === 'transition',
4547
outro: directiveName === 'out' || directiveName === 'transition',
4648
};
47-
}
49+
},
50+
allowedExpressionTypes: ['ObjectExpression'],
51+
error: 'Transition argument must be an object literal, e.g. `{ duration: 400 }`'
4852
}
4953
};
5054

@@ -138,12 +142,8 @@ export function readDirective(
138142
}
139143

140144
expression = readExpression(parser, expressionStart, quoteMark);
141-
if (directive.allowedExpressionTypes) {
142-
if (directive.allowedExpressionTypes.indexOf(expression.type) === -1) {
143-
parser.error(`Expected ${list(directive.allowedExpressionTypes)}`, expressionStart);
144-
}
145-
} else {
146-
parser.error(`${directiveName} directives cannot have a value`, expressionStart);
145+
if (directive.allowedExpressionTypes.indexOf(expression.type) === -1) {
146+
parser.error(directive.error, expressionStart);
147147
}
148148
}
149149

test/parser/samples/error-binding-rvalue/error.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"message": "Expected Identifier or MemberExpression",
2+
"message": "Can only bind to an identifier (e.g. `foo`) or a member expression (e.g. `foo.bar` or `foo[baz]`)",
33
"pos": 19,
44
"loc": {
55
"line": 1,

test/parser/samples/error-event-handler/error.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"message": "Expected CallExpression",
2+
"message": "Expected a method call",
33
"loc": {
44
"line": 1,
55
"column": 15

0 commit comments

Comments
 (0)