* migrate to named babel types imports * perf: transform babel types import to destructuring * fix merge errors * apply plugin to itself
24 lines
803 B
TypeScript
24 lines
803 B
TypeScript
import { environmentVisitor } from "@babel/helper-replace-supers";
|
|
import traverse from "@babel/traverse";
|
|
import { numericLiteral, unaryExpression } from "@babel/types";
|
|
import type * as t from "@babel/types";
|
|
|
|
import type { NodePath, Visitor } from "@babel/traverse";
|
|
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: Visitor = traverse.visitors.merge([
|
|
environmentVisitor,
|
|
{
|
|
ThisExpression(path: NodePath<t.ThisExpression>) {
|
|
path.replaceWith(unaryExpression("void", numericLiteral(0), true));
|
|
},
|
|
},
|
|
]);
|