Check if data is a function

pull/1057/head
Emil Ajdyna 8 years ago
parent 37f8f8afe3
commit c9ad4a3a28

@ -1,14 +1,12 @@
import { Validator } from '../../'; import { Validator } from '../../';
import { Node } from '../../../interfaces'; import { Node } from '../../../interfaces';
const disallowed = new Set(['Literal', 'ObjectExpression', 'ArrayExpression']); const allowed = new Set(['FunctionExpression', 'ArrowFunctionExpression'])
export default function data(validator: Validator, prop: Node) { export default function data(validator: Validator, prop: Node) {
while (prop.type === 'ParenthesizedExpression') prop = prop.expression; while (prop.type === 'ParenthesizedExpression') prop = prop.expression;
// TODO should we disallow references and expressions as well? if (!allowed.has(prop.value.type)) {
if (disallowed.has(prop.value.type)) {
validator.error(`'data' must be a function`, prop.value.start); validator.error(`'data' must be a function`, prop.value.start);
} }
} }

@ -0,0 +1,8 @@
[{
"message": "'data' must be a function",
"loc": {
"line": 10,
"column": 8
},
"pos": 131
}]

@ -0,0 +1,12 @@
<div></div>
<script>
function data (foo) {
return function () {
return { foo, bar: 'baz' }
}
}
export default {
data: data('hello')
};
</script>

@ -0,0 +1,8 @@
[{
"message": "'data' must be a function",
"loc": {
"line": 6,
"column": 2
},
"pos": 76
}]

@ -0,0 +1,8 @@
<div></div>
<script>
const data = () => ({ foo: 42 })
export default {
data
};
</script>
Loading…
Cancel
Save