Prevent module transforms from rewriting `this` to `void 0` when found in the values of private class properties. Add tests to prevent regressions.
24 lines
559 B
JavaScript
24 lines
559 B
JavaScript
export default function rewriteThis(programPath: NodePath) {
|
|
// Rewrite "this" to be "undefined".
|
|
programPath.traverse(rewriteThisVisitor);
|
|
}
|
|
|
|
/**
|
|
* A visitor to walk the tree, rewriting all `this` references in the top-level scope to be
|
|
* `undefined`.
|
|
*/
|
|
const rewriteThisVisitor = {
|
|
ThisExpression(path) {
|
|
path.replaceWith(path.scope.buildUndefinedNode());
|
|
},
|
|
Function(path) {
|
|
if (!path.isArrowFunctionExpression()) path.skip();
|
|
},
|
|
ClassProperty(path) {
|
|
path.skip();
|
|
},
|
|
ClassPrivateProperty(path) {
|
|
path.skip();
|
|
},
|
|
};
|