File tree Expand file tree Collapse file tree 3 files changed +56
-3
lines changed Expand file tree Collapse file tree 3 files changed +56
-3
lines changed Original file line number Diff line number Diff line change @@ -42,7 +42,11 @@ export function proxifyArrayElements<T extends object>(
42
42
if ( typeof key === "symbol" ) {
43
43
return ;
44
44
}
45
- const prop = getItem ( + key ) ;
45
+ const index = + key ;
46
+ if ( Number . isNaN ( index ) ) {
47
+ return ;
48
+ }
49
+ const prop = getItem ( index ) ;
46
50
if ( prop ) {
47
51
return proxify ( prop ) ;
48
52
}
@@ -51,7 +55,22 @@ export function proxifyArrayElements<T extends object>(
51
55
if ( typeof key === "symbol" ) {
52
56
return false ;
53
57
}
54
- replaceItem ( + key , literalToAst ( value ) ) ;
58
+ const index = + key ;
59
+ if ( Number . isNaN ( index ) ) {
60
+ return false ;
61
+ }
62
+ replaceItem ( index , literalToAst ( value ) ) ;
63
+ return true ;
64
+ } ,
65
+ deleteProperty ( _ , key ) {
66
+ if ( typeof key === "symbol" ) {
67
+ return false ;
68
+ }
69
+ const index = + key ;
70
+ if ( Number . isNaN ( index ) ) {
71
+ return false ;
72
+ }
73
+ elements [ index ] = literalToAst ( undefined ) ;
55
74
return true ;
56
75
} ,
57
76
ownKeys ( ) {
Original file line number Diff line number Diff line change @@ -61,6 +61,18 @@ export function proxifyObject<T>(node: ESNode): Proxified<T> {
61
61
replaceOrAddProp ( key , literalToAst ( value ) ) ;
62
62
return true ;
63
63
} ,
64
+ deleteProperty ( _ , key ) {
65
+ if ( typeof key !== "string" ) {
66
+ key = String ( key ) ;
67
+ }
68
+ const index = node . properties . findIndex (
69
+ ( prop ) => "key" in prop && "name" in prop . key && prop . key . name === key
70
+ ) ;
71
+ if ( index !== - 1 ) {
72
+ node . properties . splice ( index , 1 ) ;
73
+ }
74
+ return true ;
75
+ } ,
64
76
ownKeys ( ) {
65
77
return node . properties
66
78
. map ( ( prop ) => {
Original file line number Diff line number Diff line change @@ -111,7 +111,7 @@ describe("magicast", () => {
111
111
) ;
112
112
} ) ;
113
113
114
- it ( "parse, update, generate " , ( ) => {
114
+ it ( "function wrapper " , ( ) => {
115
115
const mod = parseCode ( `
116
116
export const a: any = { foo: 1}
117
117
export default defineConfig({
@@ -145,4 +145,26 @@ describe("magicast", () => {
145
145
});"
146
146
` ) ;
147
147
} ) ;
148
+
149
+ it ( "delete property" , ( ) => {
150
+ const mod = parseCode ( `export default { a: 1, b: [1, { foo: 'bar' }] }` ) ;
151
+
152
+ delete mod . exports . default . b [ 1 ] . foo ;
153
+
154
+ expect ( generate ( mod ) ) . toMatchInlineSnapshot (
155
+ '"export default { a: 1, b: [1, {}] };"'
156
+ ) ;
157
+
158
+ delete mod . exports . default . b [ 0 ] ;
159
+ expect ( generate ( mod ) ) . toMatchInlineSnapshot (
160
+ '"export default { a: 1, b: [undefined, {}] };"'
161
+ ) ;
162
+
163
+ delete mod . exports . default . a ;
164
+ expect ( generate ( mod ) ) . toMatchInlineSnapshot ( `
165
+ "export default {
166
+ b: [undefined, {}],
167
+ };"
168
+ ` ) ;
169
+ } ) ;
148
170
} ) ;
You can’t perform that action at this time.
0 commit comments