1
1
import checkForDupes from '../utils/checkForDupes' ;
2
2
import checkForComputedKeys from '../utils/checkForComputedKeys' ;
3
- import checkForValidIdentifiers from '../utils/checkForValidIdentifiers' ;
3
+ import getName from '../../../utils/getName' ;
4
+ import isValidIdentifier from '../../../utils/isValidIdentifier' ;
5
+ import reservedNames from '../../../utils/reservedNames' ;
4
6
import { Validator } from '../../' ;
5
7
import { Node } from '../../../interfaces' ;
6
8
import walkThroughTopFunctionScope from '../../../utils/walkThroughTopFunctionScope' ;
@@ -21,9 +23,25 @@ export default function computed(validator: Validator, prop: Node) {
21
23
22
24
checkForDupes ( validator , prop . value . properties ) ;
23
25
checkForComputedKeys ( validator , prop . value . properties ) ;
24
- checkForValidIdentifiers ( validator , prop . value . properties ) ;
25
26
26
27
prop . value . properties . forEach ( ( computation : Node ) => {
28
+ const name = getName ( computation . key ) ;
29
+
30
+ if ( ! isValidIdentifier ( name ) ) {
31
+ const suggestion = name . replace ( / [ ^ _ $ a - z 0 - 9 ] / ig, '_' ) . replace ( / ^ \d / , '_$&' ) ;
32
+ validator . error (
33
+ `Computed property name '${ name } ' is invalid — must be a valid identifier such as ${ suggestion } ` ,
34
+ computation . start
35
+ ) ;
36
+ }
37
+
38
+ if ( reservedNames . has ( name ) ) {
39
+ validator . error (
40
+ `Computed property name '${ name } ' is invalid — cannot be a JavaScript reserved word` ,
41
+ computation . start
42
+ ) ;
43
+ }
44
+
27
45
if ( ! isFunctionExpression . has ( computation . value . type ) ) {
28
46
validator . error (
29
47
`Computed properties can be function expressions or arrow function expressions` ,
0 commit comments