Corey Farrell f31c72f310 Fix handling of class property value this in module transforms. (#9857)
Prevent module transforms from rewriting `this` to `void 0` when found in the values of private class properties.  Add tests to prevent regressions.
2019-04-15 22:45:10 +02:00

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