-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
166 lines (152 loc) · 4.41 KB
/
index.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// @flow
/* eslint no-continue: off, func-names: off */
import { addSideEffect } from '@babel/helper-module-imports';
import type { NodePath } from '@babel/traverse';
// https://astexplorer.net/#/gist/a6acab67ec110ce0ebcfbbee7521de2a/779ae92132ef8c26e0b8f37e96ec83ab176d33f3
export default function failExplicit({ types: t }: Object) {
return {
visitor: {
Program: {
exit(path: NodePath) {
addSideEffect(path, 'safe-access-check');
}
},
/**
* Used by safePropertyAccess
*
* http://2ality.com/2015/12/babel-commonjs.html
*
* BUG:
* var _safeAccessCheck = require('safe-access-check');
* var _safeAccessCheck2 = interopRequireDefault(_safeAccessCheck)
* _safeAccessCheck2.safePropertyAccess()
*
* @TODO: Enforce that call to safeCoerce() and safePropertyAccess()
* ALWAYS come after the import
*/
MemberExpression(path: Object, state: Object) {
// Exit if safeCoerceCheck is disabled
if (state.opts.safePropertyAccess === false) {
return;
}
if (t.isAssignmentExpression(path.parent)) {
if (path.parentKey === 'left') {
return;
}
}
// Abort if immediate parent is logical expression (`||` or `&&`)
if (state.opts.enforceLogicExpressionCheck !== false) {
if (t.isLogicalExpression(path.parent)) {
return;
}
}
// @TODO @HACK: Temporarily prevent wrapping callExpression's
// Reason of bug is unknown
if (t.isCallExpression(path.parent)) {
return;
}
let object = path.node;
const items = [];
let id;
while (t.isMemberExpression(object)) {
if (object.computed === false) {
// @HACK: Hardcode edge case
if (object.property.name === '__esModule') {
return;
}
items.push(t.stringLiteral(object.property.name));
} else {
items.push(object.property);
}
object = object.object; // eslint-disable-line
id = object;
}
if (t.isCallExpression(id)) {
if (id && id.callee) {
if (id.callee.name === 'require') {
return;
}
}
}
if (!id) {
return;
}
if (!id.name) {
if (!id.callee) {
return;
}
} else if (!id.callee) {
if (!id.name) {
return;
}
}
try {
if (!(id.name || id.callee.name)) {
return;
}
} catch (e) {
return;
}
try {
path.replaceWith(
t.callExpression(
t.memberExpression(
t.identifier('global'),
t.identifier('safePropertyAccess')
),
[
t.arrayExpression(items.reverse()),
t.identifier(id.name || id.callee.name)
]
)
);
} catch (error) {
throw new Error(
[
'This is an issue with "babel-plugin-fail-explicit"',
`Line "${path.node.loc.start}" in "${state.file.opts.filename}"`,
error
].join('. ')
);
}
},
/**
* Used by safeCoerce
* @TODO: Support BinaryExpression|AssignmentExpression|UnaryExpression
*/
'BinaryExpression|AssignmentExpression': function(
path: NodePath,
state: Object
) {
// Exit if safeCoerce is disabled
if (state.opts.safeCoerce === false) {
return;
}
if (
path.node.operator === '===' ||
path.node.operator === '==' ||
path.node.operator === '=' ||
path.node.operator === '!=' ||
path.node.operator === '!==' ||
path.node.operator === 'instanceof' ||
path.node.operator === 'in'
) {
return;
}
path.replaceWith(
t.callExpression(
t.memberExpression(
t.identifier('global'),
t.identifier('safeCoerce')
),
[
path.node.left,
t.stringLiteral(path.node.operator),
path.node.right
]
)
);
}
}
};
}