@@ -14,7 +14,7 @@ export const transformAST: ASTTransformation = (context) => {
14
14
return j . Identifier . check ( node ) && node . name === 'Vue'
15
15
}
16
16
17
- const setCalls = root
17
+ const setOrDeleteCalls = root
18
18
. find ( j . CallExpression , ( n : N . CallExpression ) => {
19
19
if (
20
20
! j . MemberExpression . check ( n . callee ) ||
@@ -23,11 +23,16 @@ export const transformAST: ASTTransformation = (context) => {
23
23
return false
24
24
}
25
25
26
- if ( n . callee . property . name === 'set' && isVue ( n . callee . object ) ) {
26
+ const propName = n . callee . property . name
27
+
28
+ if (
29
+ ( propName === 'set' || propName === 'delete' ) &&
30
+ isVue ( n . callee . object )
31
+ ) {
27
32
return true
28
33
}
29
34
30
- if ( n . callee . property . name === '$set' ) {
35
+ if ( propName === '$set' || propName === '$delete ') {
31
36
// we need the path & scope to check if the object is `this`
32
37
// so leave it to the filter function
33
38
return true
@@ -36,8 +41,11 @@ export const transformAST: ASTTransformation = (context) => {
36
41
return false
37
42
} )
38
43
. filter ( ( path ) => {
39
- // @ts -ignore
40
- if ( path . node . callee . property . name !== '$set' ) {
44
+ const prop = ( path . node . callee as N . MemberExpression )
45
+ . property as N . Identifier
46
+
47
+ // only the object of `.$set` and `.$delete` is pending for check
48
+ if ( prop . name !== '$set' && prop . name !== '$delete' ) {
41
49
return true
42
50
}
43
51
@@ -68,22 +76,33 @@ export const transformAST: ASTTransformation = (context) => {
68
76
return false
69
77
} )
70
78
71
- setCalls . replaceWith ( ( { node } ) => {
72
- if (
73
- node . arguments . length !== 3 ||
74
- node . arguments . some ( ( arg ) => j . SpreadElement . check ( arg ) )
75
- ) {
79
+ setOrDeleteCalls . replaceWith ( ( { node } ) => {
80
+ if ( node . arguments . some ( ( arg ) => j . SpreadElement . check ( arg ) ) ) {
76
81
// TODO: add a comment to inform the user that this kind of usage can't be transformed
77
82
return node
78
83
}
79
84
80
- return j . assignmentExpression (
81
- '=' ,
82
- // @ts -ignore
83
- j . memberExpression ( node . arguments [ 0 ] , node . arguments [ 1 ] , true ) ,
84
- // @ts -ignore
85
- node . arguments [ 2 ]
86
- )
85
+ const prop = ( node . callee as N . MemberExpression ) . property as N . Identifier
86
+ if ( prop . name === '$set' || prop . name === 'set' ) {
87
+ return j . assignmentExpression (
88
+ '=' ,
89
+ // @ts -ignore
90
+ j . memberExpression ( node . arguments [ 0 ] , node . arguments [ 1 ] , true ) ,
91
+ // @ts -ignore
92
+ node . arguments [ 2 ]
93
+ )
94
+ }
95
+
96
+ if ( prop . name === '$delete' || prop . name === 'delete' ) {
97
+ return j . unaryExpression (
98
+ 'delete' ,
99
+ // @ts -ignore
100
+ j . memberExpression ( node . arguments [ 0 ] , node . arguments [ 1 ] , true )
101
+ )
102
+ }
103
+
104
+ // unreachable branch
105
+ return node
87
106
} )
88
107
}
89
108
0 commit comments