@@ -14,6 +14,11 @@ export const toArrow: Command = {
14
14
let rangeStart = fn . range [ 0 ]
15
15
const rangeEnd = fn . range [ 1 ]
16
16
17
+ const parent = fn . parent
18
+
19
+ if ( parent . type === 'Property' && parent . kind !== 'init' )
20
+ return ctx . reportError ( `Cannot convert ${ parent . kind } ter property to arrow function` )
21
+
17
22
ctx . removeComment ( )
18
23
ctx . report ( {
19
24
node : fn ,
@@ -42,24 +47,25 @@ export const toArrow: Command = {
42
47
}
43
48
44
49
// For object methods
45
- else if ( fn . parent . type === 'Property' ) {
46
- rangeStart = fn . parent . range [ 0 ]
47
- textName = ctx . getTextOf ( fn . parent . key )
48
- final = `${ textName } : ${ final } `
50
+ else if ( parent . type === 'Property' ) {
51
+ rangeStart = parent . range [ 0 ]
52
+ textName = ctx . getTextOf ( parent . key )
53
+ final = `${ parent . computed ? `[ ${ textName } ]` : textName } : ${ final } `
49
54
}
50
55
51
56
// For class methods
52
- else if ( fn . parent . type === 'MethodDefinition' ) {
53
- rangeStart = fn . parent . range [ 0 ]
54
- textName = ctx . getTextOf ( fn . parent . key )
55
- final = `${ textName } = ${ final } `
57
+ else if ( parent . type === 'MethodDefinition' ) {
58
+ rangeStart = parent . range [ 0 ]
59
+ textName = ctx . getTextOf ( parent . key )
60
+ final = `${ [
61
+ parent . accessibility ,
62
+ parent . static && 'static' ,
63
+ parent . override && 'override' ,
64
+ parent . computed ? `[${ textName } ]` : textName ,
65
+ parent . optional && '?' ,
66
+ ] . filter ( Boolean ) . join ( ' ' ) } = ${ final } `
56
67
}
57
68
58
- // console.log({
59
- // final,
60
- // original: code.slice(rangeStart, rangeEnd),
61
- // p: fn.parent.type,
62
- // })
63
69
return fixer . replaceTextRange ( [ rangeStart , rangeEnd ] , final )
64
70
} ,
65
71
} )
0 commit comments