mirror of https://github.com/sveltejs/svelte
parent
37f8f8afe3
commit
c9ad4a3a28
@ -1,14 +1,12 @@
|
||||
import { Validator } from '../../';
|
||||
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) {
|
||||
while (prop.type === 'ParenthesizedExpression') prop = prop.expression;
|
||||
|
||||
// TODO should we disallow references and expressions as well?
|
||||
|
||||
if (disallowed.has(prop.value.type)) {
|
||||
if (!allowed.has(prop.value.type)) {
|
||||
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…
Reference in new issue