Skip to content

Commit e702cec

Browse files
committed
impl delete statement
1 parent 0d9813e commit e702cec

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"dependencies": {
55
"babel-types": "^6.26.0",
66
"babylon": "^6.18.0",
7+
"invariant": "^2.2.4",
78
"uglifyjs-webpack-plugin": "^1.2.5",
89
"webpack": "^4.12.0",
910
"webpack-cli": "^3.0.8",

src/ast-transforms.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const bt = require("babel-types");
2+
const invariant = require("invariant");
23
const t = require("./cljs-types");
34
const utils = require("./utils");
45

@@ -36,8 +37,38 @@ const BinaryExpression = (next, ast, opts) => {
3637
]);
3738
};
3839

39-
const UnaryExpression = (next, ast, opts) =>
40-
t.list([t.symbol(utils.normalizeOperator(ast.operator)), next(ast.argument)]);
40+
const DeleteStatement = (next, ast, opts) => {
41+
const { argument } = ast;
42+
43+
invariant(
44+
bt.isMemberExpression(argument),
45+
`Can't transform "delete" for non MemberExpression node`
46+
);
47+
48+
const prop = next(argument.property);
49+
50+
invariant(
51+
prop.value !== undefined || prop.name !== undefined,
52+
`Couldn't infer "delete" key. Should be a symbol or a number`
53+
);
54+
55+
const property =
56+
prop.type === "StringLiteral"
57+
? prop
58+
: prop.type === "NumericLiteral"
59+
? prop
60+
: t.StringLiteral(prop.name);
61+
62+
return t.list([t.symbol("js-delete"), next(argument.object), property]);
63+
};
64+
65+
const UnaryExpression = (next, ast, opts) => {
66+
const { operator, argument } = ast;
67+
if (operator === "delete") {
68+
return DeleteStatement(next, ast, opts);
69+
}
70+
return t.list([t.symbol(utils.normalizeOperator(operator)), next(argument)]);
71+
};
4172

4273
const Identifier = (next, ast, opts) => {
4374
if (opts.isGetter) {

0 commit comments

Comments
 (0)