|
1 | 1 | const bt = require("babel-types");
|
| 2 | +const invariant = require("invariant"); |
2 | 3 | const t = require("./cljs-types");
|
3 | 4 | const utils = require("./utils");
|
4 | 5 |
|
@@ -36,8 +37,38 @@ const BinaryExpression = (next, ast, opts) => {
|
36 | 37 | ]);
|
37 | 38 | };
|
38 | 39 |
|
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 | +}; |
41 | 72 |
|
42 | 73 | const Identifier = (next, ast, opts) => {
|
43 | 74 | if (opts.isGetter) {
|
|
0 commit comments