forked from sveltejs/svelte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomputed.js
32 lines (26 loc) · 1.07 KB
/
computed.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import checkForDupes from '../utils/checkForDupes.js';
import checkForComputedKeys from '../utils/checkForComputedKeys.js';
const isFunctionExpression = {
FunctionExpression: true,
ArrowFunctionExpression: true
};
export default function computed ( validator, prop ) {
if ( prop.value.type !== 'ObjectExpression' ) {
validator.error( `The 'computed' property must be an object literal`, prop.start );
return;
}
checkForDupes( validator, prop.value.properties );
checkForComputedKeys( validator, prop.value.properties );
prop.value.properties.forEach( computation => {
if ( !isFunctionExpression[ computation.value.type ] ) {
validator.error( `Computed properties can be function expressions or arrow function expressions`, computation.value.start );
return;
}
computation.value.params.forEach( param => {
const valid = param.type === 'Identifier' || param.type === 'AssignmentPattern' && param.left.type === 'Identifier';
if ( !valid ) {
validator.error( `Computed properties cannot use destructuring in function parameters`, param.start );
}
});
});
}