-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathindex.js
402 lines (337 loc) · 12.3 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/* eslint-disable global-require, import/no-dynamic-require */
// import generate from 'babel-generator';
// console.log(generate(node).code);
import isAnnotatedForRemoval from './isAnnotatedForRemoval'
import isStatelessComponent from './isStatelessComponent'
import remove from './remove'
function isPathReactClass(path, globalOptions) {
const node = path.node
const matchers = globalOptions.classNameMatchers
if (path.matchesPattern('React.Component') || path.matchesPattern('React.PureComponent')) {
return true
}
if (node && (node.name === 'Component' || node.name === 'PureComponent')) {
return true
}
if (node && matchers && matchers.test(node.name)) {
return true
}
return false
}
function isReactClass(superClass, scope, globalOptions) {
if (!superClass.node) {
return false
}
let answer = false
if (isPathReactClass(superClass, globalOptions)) {
answer = true
} else if (superClass.node.name) {
// Check for inheritance
const className = superClass.node.name
const binding = scope.getBinding(className)
if (!binding) {
answer = false
} else {
const bindingSuperClass = binding.path.get('superClass')
if (isPathReactClass(bindingSuperClass, globalOptions)) {
answer = true
}
}
}
return answer
}
function areSetsEqual(set1, set2) {
if (set1 === set2) {
return true
}
if (set1.size !== set2.size) {
return false
}
return !Array.from(set1).some(item => !set2.has(item))
}
function memberExpressionRootIdentifier(path) {
// Traverse up to the parent before the topmost member expression, and then
// traverse back down to find the topmost identifier. It seems like there
// might be a better way to do this.
const parent = path.findParent(p => !p.isMemberExpression())
const { type } = parent.node
let memberExpression
if (type === 'ObjectProperty') {
// The topmost MemberExpression's parent is an object property, so the
// topmost MemberExpression should be the value.
memberExpression = parent.get('value')
}
if (!memberExpression || memberExpression.type !== 'MemberExpression') {
// This case is currently unhandled by this plugin.
return null
}
// We have a topmost MemberExpression now, so we want to traverse down the
// left half untli we no longer see MemberExpressions. This node will give us
// our leftmost identifier.
while (memberExpression.node.object.type === 'MemberExpression') {
memberExpression = memberExpression.get('object')
}
return memberExpression.get('object')
}
export default function(api) {
const { template, types, traverse } = api
const nestedIdentifiers = new Set()
const removedPaths = new WeakSet()
const collectNestedIdentifiers = {
Identifier(path) {
if (path.parent.type === 'MemberExpression') {
// foo.bar
const root = memberExpressionRootIdentifier(path)
if (root) {
nestedIdentifiers.add(root.node.name)
}
return
}
if (
path.parent.type === 'ObjectProperty' &&
(path.parent.key === path.node || path.parent.shorthand)
) {
// { foo: 'bar' }
// { foo }
return
}
nestedIdentifiers.add(path.node.name)
},
}
return {
visitor: {
Program(programPath, state) {
let ignoreFilenames
let classNameMatchers
if (state.opts.ignoreFilenames) {
ignoreFilenames = new RegExp(state.opts.ignoreFilenames.join('|'), 'i')
} else {
ignoreFilenames = undefined
}
if (state.opts.classNameMatchers) {
classNameMatchers = new RegExp(state.opts.classNameMatchers.join('|'))
} else {
classNameMatchers = undefined
}
const globalOptions = {
visitedKey: `transform-react-remove-prop-types${Date.now()}`,
unsafeWrapTemplate: template(
`
if (process.env.NODE_ENV !== "production") {
NODE;
}
`,
{ placeholderPattern: /^NODE$/ }
),
wrapTemplate: ({ LEFT, RIGHT }, options = {}) => {
const { as = 'assignmentExpression' } = options
const right = template.expression(
`
process.env.NODE_ENV !== "production" ? RIGHT : {}
`,
{ placeholderPattern: /^(LEFT|RIGHT)$/ }
)({ RIGHT })
switch (as) {
case 'variableDeclarator':
return types.variableDeclarator(LEFT, right)
case 'assignmentExpression':
return types.assignmentExpression('=', LEFT, right)
default:
throw new Error(`unrecognized template type ${as}`)
}
},
mode: state.opts.mode || 'remove',
ignoreFilenames,
types,
removeImport: state.opts.removeImport || false,
libraries: (state.opts.additionalLibraries || []).concat('prop-types'),
classNameMatchers,
createReactClassName: state.opts.createReactClassName || 'createReactClass',
}
if (state.opts.plugins) {
const pluginsState = state
const pluginsVisitors = state.opts.plugins.map(pluginOpts => {
const pluginName = typeof pluginOpts === 'string' ? pluginOpts : pluginOpts[0]
if (typeof pluginOpts !== 'string') {
pluginsState.opts = {
...pluginsState.opts,
...pluginOpts[1],
}
}
let plugin = require(pluginName)
if (typeof plugin !== 'function') {
plugin = plugin.default
}
return plugin(api).visitor
})
traverse(
programPath.parent,
traverse.visitors.merge(pluginsVisitors),
programPath.scope,
pluginsState,
programPath.parentPath
)
}
// On program start, do an explicit traversal up front for this plugin.
programPath.traverse({
ObjectProperty: {
exit(path) {
const node = path.node
if (node.computed || node.key.name !== 'propTypes') {
return
}
const parent = path.findParent(currentNode => {
if (currentNode.type !== 'CallExpression') {
return false
}
return (
currentNode.get('callee').node.name === globalOptions.createReactClassName ||
(currentNode.get('callee').node.property &&
currentNode.get('callee').node.property.name === 'createClass')
)
})
if (parent) {
path.traverse(collectNestedIdentifiers)
removedPaths.add(path)
remove(path, globalOptions, {
type: 'createClass',
})
}
},
},
// Here to support stage-1 transform-class-properties.
ClassProperty(path) {
const { node, scope } = path
if (node.key.name === 'propTypes') {
const pathClassDeclaration = scope.path
if (isReactClass(pathClassDeclaration.get('superClass'), scope, globalOptions)) {
path.traverse(collectNestedIdentifiers)
removedPaths.add(path)
remove(path, globalOptions, {
type: 'class static',
pathClassDeclaration,
})
}
}
},
AssignmentExpression(path) {
const { node, scope } = path
if (
node.left.computed ||
!node.left.property ||
node.left.property.name !== 'propTypes'
) {
return
}
const forceRemoval = isAnnotatedForRemoval(path.node.left)
if (forceRemoval) {
path.traverse(collectNestedIdentifiers)
removedPaths.add(path)
remove(path, globalOptions, { type: 'assign' })
return
}
const className = node.left.object.name
const binding = scope.getBinding(className)
if (!binding) {
return
}
if (binding.path.isClassDeclaration()) {
const superClass = binding.path.get('superClass')
if (isReactClass(superClass, scope, globalOptions)) {
path.traverse(collectNestedIdentifiers)
removedPaths.add(path)
remove(path, globalOptions, { type: 'assign' })
}
} else if (isStatelessComponent(binding.path)) {
path.traverse(collectNestedIdentifiers)
removedPaths.add(path)
remove(path, globalOptions, { type: 'assign' })
}
},
})
let skippedIdentifiers = 0
const removeNewlyUnusedIdentifiers = {
VariableDeclarator(path) {
// Only consider the top level scope.
if (path.scope.block.type !== 'Program') {
return
}
if (['ObjectPattern', 'ArrayPattern'].includes(path.node.id.type)) {
// Object or Array destructuring, so we will want to capture all
// the names created by the destructuring. This currently doesn't
// work, but would be good to improve. All of the names for
// ObjectPattern can be collected like:
//
// path.node.id.properties.map(prop => prop.value.name);
return
}
const { name } = path.node.id
if (!nestedIdentifiers.has(name)) {
return
}
const { referencePaths } = path.scope.getBinding(name)
// Count the number of referencePaths that are not in the
// removedPaths Set. We need to do this in order to support the wrap
// option, which doesn't actually remove the references.
const hasRemainingReferencePaths = referencePaths.some(referencePath => {
const found = referencePath.find(path2 => removedPaths.has(path2))
return !found
})
if (hasRemainingReferencePaths) {
// There are still references to this identifier, so we need to
// skip over it for now.
skippedIdentifiers += 1
return
}
removedPaths.add(path)
nestedIdentifiers.delete(name)
path.get('init').traverse(collectNestedIdentifiers)
remove(path, globalOptions, { type: 'declarator' })
},
}
let lastNestedIdentifiers = new Set()
while (
!areSetsEqual(nestedIdentifiers, lastNestedIdentifiers) &&
nestedIdentifiers.size > 0 &&
skippedIdentifiers < nestedIdentifiers.size
) {
lastNestedIdentifiers = new Set(nestedIdentifiers)
skippedIdentifiers = 0
programPath.scope.crawl()
programPath.traverse(removeNewlyUnusedIdentifiers)
}
if (globalOptions.removeImport) {
if (globalOptions.mode === 'remove') {
programPath.scope.crawl()
programPath.traverse({
ImportDeclaration(path) {
const { source, specifiers } = path.node
const found = globalOptions.libraries.some(library => {
if (library instanceof RegExp) {
return library.test(source.value)
}
return source.value === library
})
if (!found) {
return
}
const haveUsedSpecifiers = specifiers.some(specifier => {
const importedIdentifierName = specifier.local.name
const { referencePaths } = path.scope.getBinding(importedIdentifierName)
return referencePaths.length > 0
})
if (!haveUsedSpecifiers) {
path.remove()
}
},
})
} else {
throw new Error(
'transform-react-remove-prop-type: removeImport = true and mode != "remove" can not be used at the same time.'
)
}
}
},
},
}
}