nicer error messages

pull/1245/head
Rich Harris 7 years ago
parent f97036dd39
commit 39694c31c3

@ -1,6 +1,5 @@
import { parseExpressionAt } from 'acorn'; import { parseExpressionAt } from 'acorn';
import repeat from '../../utils/repeat'; import repeat from '../../utils/repeat';
import list from '../../utils/list';
import { Parser } from '../index'; import { Parser } from '../index';
const DIRECTIVES = { const DIRECTIVES = {
@ -8,20 +7,22 @@ const DIRECTIVES = {
names: ['ref'], names: ['ref'],
attribute(start, end, type, name) { attribute(start, end, type, name) {
return { start, end, type, name }; return { start, end, type, name };
} },
allowedExpressionTypes: [],
error: 'ref directives cannot have a value'
}, },
EventHandler: { EventHandler: {
names: ['on'], names: ['on'],
allowedExpressionTypes: ['CallExpression'],
attribute(start, end, type, name, expression) { attribute(start, end, type, name, expression) {
return { start, end, type, name, expression }; return { start, end, type, name, expression };
} },
allowedExpressionTypes: ['CallExpression'],
error: 'Expected a method call'
}, },
Binding: { Binding: {
names: ['bind'], names: ['bind'],
allowedExpressionTypes: ['Identifier', 'MemberExpression'],
attribute(start, end, type, name, expression) { attribute(start, end, type, name, expression) {
return { return {
start, end, type, name, start, end, type, name,
@ -32,19 +33,22 @@ const DIRECTIVES = {
name, name,
} }
}; };
} },
allowedExpressionTypes: ['Identifier', 'MemberExpression'],
error: 'Can only bind to an identifier (e.g. `foo`) or a member expression (e.g. `foo.bar` or `foo[baz]`)'
}, },
Transition: { Transition: {
names: ['in', 'out', 'transition'], names: ['in', 'out', 'transition'],
allowedExpressionTypes: ['ObjectExpression'],
attribute(start, end, type, name, expression, directiveName) { attribute(start, end, type, name, expression, directiveName) {
return { return {
start, end, type, name, expression, start, end, type, name, expression,
intro: directiveName === 'in' || directiveName === 'transition', intro: directiveName === 'in' || directiveName === 'transition',
outro: directiveName === 'out' || directiveName === 'transition', outro: directiveName === 'out' || directiveName === 'transition',
}; };
} },
allowedExpressionTypes: ['ObjectExpression'],
error: 'Transition argument must be an object literal, e.g. `{ duration: 400 }`'
} }
}; };
@ -138,12 +142,8 @@ export function readDirective(
} }
expression = readExpression(parser, expressionStart, quoteMark); expression = readExpression(parser, expressionStart, quoteMark);
if (directive.allowedExpressionTypes) {
if (directive.allowedExpressionTypes.indexOf(expression.type) === -1) { if (directive.allowedExpressionTypes.indexOf(expression.type) === -1) {
parser.error(`Expected ${list(directive.allowedExpressionTypes)}`, expressionStart); parser.error(directive.error, expressionStart);
}
} else {
parser.error(`${directiveName} directives cannot have a value`, expressionStart);
} }
} }

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

@ -1,5 +1,5 @@
{ {
"message": "Expected CallExpression", "message": "Expected a method call",
"loc": { "loc": {
"line": 1, "line": 1,
"column": 15 "column": 15

Loading…
Cancel
Save