Transform class static block (#12143)

Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
Co-authored-by: Brian Ng <bng412@gmail.com>
This commit is contained in:
Huáng Jùnliàng
2020-10-14 13:54:41 -04:00
committed by Nicolò Ribaudo
parent 3ccca88178
commit f697e7995d
85 changed files with 861 additions and 24 deletions

View File

@@ -21,6 +21,7 @@
"@babel/helper-split-export-declaration": "workspace:^7.11.0",
"@babel/helper-validator-identifier": "workspace:^7.10.4",
"@babel/template": "workspace:^7.10.4",
"@babel/traverse": "workspace:^7.11.5",
"@babel/types": "workspace:^7.11.0",
"lodash": "^4.17.19"
}

View File

@@ -1,25 +1,21 @@
import { skipAllButComputedKey } from "@babel/helper-replace-supers";
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".
programPath.traverse(rewriteThisVisitor);
traverse(programPath.node, { ...rewriteThisVisitor, noScope: true });
}
/**
* A visitor to walk the tree, rewriting all `this` references in the top-level scope to be
* `undefined`.
* `void 0` (undefined).
*/
const rewriteThisVisitor = {
ThisExpression(path) {
path.replaceWith(path.scope.buildUndefinedNode());
const rewriteThisVisitor = traverse.visitors.merge([
environmentVisitor,
{
ThisExpression(path) {
path.replaceWith(t.unaryExpression("void", t.numericLiteral(0), true));
},
},
Function(path) {
if (path.isMethod()) skipAllButComputedKey(path);
else if (!path.isArrowFunctionExpression()) path.skip();
},
ClassProperty(path) {
skipAllButComputedKey(path);
},
ClassPrivateProperty(path) {
path.skip();
},
};
]);