Huáng Jùnliàng f697e7995d Transform class static block (#12143)
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
Co-authored-by: Brian Ng <bng412@gmail.com>
2020-10-14 21:16:24 +02:00

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));
},
},
]);