Skip to content

Commit a59584f

Browse files
authored
Avoid removeNewlyUnusedIdentifiers when encountering destructuring (#161)
In 0.14.6, I ran into the following error when updating in a project: > Cannot read property 'referencePaths' of undefined at > VariableDeclarator, lib/index.js:292:56 I have so far been unable to reproduce this in our test suite, but I can reproduce it in a local repo by using `npm link`. I wonder if this could be a weird interaction of multiple babel plugins. It seems that in the case I am encountering, the VariableDeclarator has an id of type ObjectPattern, which means that there is some object destructuring happening. We should ideally take care to clean up the destructuring in the same way that we clean up other variable usage, but I think that will be a little more complicated than what I have time for right now, so as a quick fix, I am escaping from this function in this case. In my local testing, this prevents the plugin from crashing and produces the same result as before.
1 parent fb3c061 commit a59584f

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/index.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,22 @@ export default function(api) {
288288
let skippedIdentifiers = 0
289289
const removeNewlyUnusedIdentifiers = {
290290
VariableDeclarator(path) {
291-
if (!nestedIdentifiers.has(path.node.id.name)) {
291+
if (path.node.id.type === 'ObjectPattern') {
292+
// Object destructuring, so we will want to capture all the names
293+
// created by the destructuring. This currently doesn't work, but
294+
// would be good to improve. All of the names can be collected
295+
// like:
296+
//
297+
// path.node.id.properties.map(prop => prop.value.name);
292298
return
293299
}
300+
const { name } = path.node.id
294301

295-
const { referencePaths } = path.scope.getBinding(path.node.id.name)
302+
if (!nestedIdentifiers.has(name)) {
303+
return
304+
}
305+
306+
const { referencePaths } = path.scope.getBinding(name)
296307

297308
// Count the number of referencePaths that are not in the
298309
// removedPaths Set. We need to do this in order to support the wrap
@@ -310,7 +321,7 @@ export default function(api) {
310321
}
311322

312323
removedPaths.add(path)
313-
nestedIdentifiers.delete(path.node.id.name)
324+
nestedIdentifiers.delete(name)
314325
path.get('init').traverse(collectNestedIdentifiers)
315326
remove(path, globalOptions, { type: 'declarator' })
316327
},

0 commit comments

Comments
 (0)