-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.es5.js
129 lines (112 loc) · 4.39 KB
/
main.es5.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _babelGenerator = require('babel-generator');
var _babelGenerator2 = _interopRequireDefault(_babelGenerator);
var _debug = require('debug');
var _debug2 = _interopRequireDefault(_debug);
var _sideEffectsSafe = require('side-effects-safe');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var info = (0, _debug2.default)('remove-props');
/**
* Removes properties matching regex specified as a plugin option.
*/
exports.default = function (_ref) {
var types = _ref.types;
var undef = types.identifier('undefined');
var regex = undefined,
pure = undefined;
var visitor = {
Program: {
// Preprocess options.
enter: function enter(path, state) {
regex = state.opts.regex;
if (!regex) {
throw new TypeError("A regex option is required.");
}
pure = function pure(n) {
return (0, _sideEffectsSafe.pureBabylon)(n, state.opts);
};
}
},
MemberExpression: function MemberExpression(path) {
// obj.prop
var _path$node = path.node;
var computed = _path$node.computed;
var object = _path$node.object;
var property = _path$node.property;
var parentPath = path.parentPath;
var name = null;
if (!computed) {
name = property.name;
} else if (types.isStringLiteral(property)) {
name = property.value;
}
if (regex.test(name)) {
if (types.isAssignmentExpression(parentPath)) {
// obj.prop = value (as an expression, not a declaration).
var grandparentPath = parentPath.parentPath;
var expStmt = types.isExpressionStatement(grandparentPath);
var objectPure = pure(object);
var rightPure = pure(parentPath.node.right);
if (expStmt && objectPure && rightPure) {
info('Removing: ' + source(grandparentPath));
grandparentPath.remove();
} else if (objectPure) {
info('Removing left in: ' + source(parentPath));
parentPath.replaceWith(parentPath.node.right);
} else if (expStmt && rightPure) {
info('Removing right in: ' + source(parentPath));
parentPath.replaceWith(object);
} else if (rightPure) {
// TODO: Replace with a sequence within expressions?
info('Impure object: ' + source(parentPath));
} else {
info('Impure object and value: ' + source(parentPath));
}
} else {
// obj.prop (value use in an expression).
if (pure(object)) {
info('Replacing: ' + source(path) + ' in: ' + ('' + source(parentPath)));
path.replaceWith(undef);
} else {
info('Impure object: ' + source(path));
}
}
}
},
ObjectProperty: function ObjectProperty(path) {
// prop: value
var _path$node2 = path.node;
var computed = _path$node2.computed;
var key = _path$node2.key;
var value = _path$node2.value;
if (computed) {
return;
}
var name = null;
if (types.isIdentifier(key)) {
name = key.name;
} else if (types.isStringLiteral(key)) {
name = key.value;
}
if (regex.test(name)) {
if (pure(value)) {
info('Removing: ' + source(path));
path.remove();
} else {
info('Inpure value: ' + source(path));
}
}
}
};
return { visitor: visitor };
};
/**
* Original or generated code (source for a transformed path may not be
* available).
*/
function source(path) {
return path.getSource() || (0, _babelGenerator2.default)(path.node).code;
}