Skip to content

Commit ad40a7b

Browse files
committed
feat: support delete operation
1 parent c58699b commit ad40a7b

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

src/proxy/array.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ export function proxifyArrayElements<T extends object>(
4242
if (typeof key === "symbol") {
4343
return;
4444
}
45-
const prop = getItem(+key);
45+
const index = +key;
46+
if (Number.isNaN(index)) {
47+
return;
48+
}
49+
const prop = getItem(index);
4650
if (prop) {
4751
return proxify(prop);
4852
}
@@ -51,7 +55,22 @@ export function proxifyArrayElements<T extends object>(
5155
if (typeof key === "symbol") {
5256
return false;
5357
}
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);
5574
return true;
5675
},
5776
ownKeys() {

src/proxy/object.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ export function proxifyObject<T>(node: ESNode): Proxified<T> {
6161
replaceOrAddProp(key, literalToAst(value));
6262
return true;
6363
},
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+
},
6476
ownKeys() {
6577
return node.properties
6678
.map((prop) => {

test/index.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ describe("magicast", () => {
111111
);
112112
});
113113

114-
it("parse, update, generate", () => {
114+
it("function wrapper", () => {
115115
const mod = parseCode(`
116116
export const a: any = { foo: 1}
117117
export default defineConfig({
@@ -145,4 +145,26 @@ describe("magicast", () => {
145145
});"
146146
`);
147147
});
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+
});
148170
});

0 commit comments

Comments
 (0)