Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com> Co-authored-by: Brian Ng <bng412@gmail.com>
22 lines
643 B
JavaScript
22 lines
643 B
JavaScript
import { environmentVisitor } from "@babel/helper-replace-supers";
|
|
import traverse from "@babel/traverse";
|
|
import * as t from "@babel/types";
|
|
|
|
export default function rewriteThis(programPath: NodePath) {
|
|
// Rewrite "this" to be "undefined".
|
|
traverse(programPath.node, { ...rewriteThisVisitor, noScope: true });
|
|
}
|
|
|
|
/**
|
|
* A visitor to walk the tree, rewriting all `this` references in the top-level scope to be
|
|
* `void 0` (undefined).
|
|
*/
|
|
const rewriteThisVisitor = traverse.visitors.merge([
|
|
environmentVisitor,
|
|
{
|
|
ThisExpression(path) {
|
|
path.replaceWith(t.unaryExpression("void", t.numericLiteral(0), true));
|
|
},
|
|
},
|
|
]);
|