Last active
February 17, 2024 09:54
-
-
Save haskellcamargo/f16b66c0d1e64638402d9618c9b4b035 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { NodePath } from '@babel/traverse'; | |
/** | |
* Finds dependencies of a declaration and removes the ones that are used exclusively | |
* by this declaration. | |
* | |
* NOTE: this only works for runtime bindings. Type bindings are excluded as I'd need to patch | |
* Babel to keep track of type bindings in the context of a declaration, and ain't nobody got | |
* time for that for now. | |
* | |
* @experimental | |
*/ | |
export function removeDeclarationDeps(path: NodePath) { | |
for (const binding of Object.values(path.scope.bindings)) { | |
const pathsBelongingToDeclaration = binding.referencePaths.filter((referencePath) => | |
referencePath.findParent((parentPath) => parentPath === path), | |
); | |
if (binding.referencePaths.length === pathsBelongingToDeclaration.length) { | |
binding.path.remove(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment