Bogdan Savluk 6ac07a1647
convert @babel/helper-module-transforms to typescript (#12928)
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
2021-03-27 01:40:32 +01:00

23 lines
738 B
TypeScript

import { environmentVisitor } from "@babel/helper-replace-supers";
import traverse from "@babel/traverse";
import * 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(t.unaryExpression("void", t.numericLiteral(0), true));
},
},
]);