The notable thing here is that inside the propTypes assignment, a
MemberExpression is used instead of just an identifier. However, in our
visitor for collecting nested identifiers, we special-cased Identifiers
that have a MemberExpression parent to not be collected. This was to
prevent the bar portion of foo.bar from being collected. However, in
this case, we actually want the foo part of foo.bar to be collected
for possible additional cleanup, so we need to add a little more logic
to make this happen.
To solve this, I added a function that takes a path and traverses up the
tree until it finds the first non-MemberExpression, and then traverses
down the left side of that tree until it finds the root identifier. This
should be the foo in foo.bar.baz, for instance.
It seems likely that there is a better more built-in Babel way to do
this, but I was unable to find anything to point me in that direction so
I rolled my own.
In our codebase, I noticed that we had the following pattern that was not getting properly cleaned up:
The notable thing here is that inside the propTypes assignment, a MemberExpression is used instead of just an identifier. However, in our visitor for collecting nested identifiers, we special-cased Identifiers that have a MemberExpression parent to not be collected. This was to prevent the
bar
portion offoo.bar
from being collected. However, in this case, we actually want thefoo
part offoo.bar
to be collected for possible additional cleanup, so we need to add a little more logic to make this happen.To solve this, I added a function that takes a path and traverses up the tree until it finds the first non-MemberExpression, and then traverses down the left side of that tree until it finds the root identifier. This should be the
foo
infoo.bar.baz
, for instance.It seems likely that there is a better more built-in Babel way to do this, but I was unable to find anything to point me in that direction so I rolled my own.