fix(class-properties): replace new.target in static properties with undefined (#13560)

* fix(class-properties): replace `new.target` in static properties with `undefined`

non-static prop is not affected

fix #12737

* Update packages/babel-helper-create-class-features-plugin/src/fields.ts

fix typo

Co-authored-by: Brian Ng <bng412@gmail.com>

* fix: add loose test case and fix replace condition

* test: add new.target tests for static block

* feat: move new-target replace into thisContextVisitor

defaults to replace new.target, do not replace within function

* feat: simplify thisContextVisitor

remove function visitor since environmentVisitor is skipping arrow function

* test: remove unused fixme comments

Co-authored-by: Brian Ng <bng412@gmail.com>
This commit is contained in:
王清雨
2021-07-17 03:58:55 +08:00
committed by GitHub
parent 6e57617138
commit aa18da08cd
12 changed files with 143 additions and 8 deletions

View File

@@ -1,4 +1,5 @@
import { template, traverse, types as t } from "@babel/core";
import type { NodePath } from "@babel/traverse";
import ReplaceSupers, {
environmentVisitor,
} from "@babel/helper-replace-supers";
@@ -665,6 +666,19 @@ const thisContextVisitor = traverse.visitors.merge([
state.needsClassRef = true;
path.replaceWith(t.cloneNode(state.classRef));
},
MetaProperty(path: NodePath<t.MetaProperty>) {
const meta = path.get("meta");
const property = path.get("property");
const { scope } = path;
// if there are `new.target` in static field
// we should replace it with `undefined`
if (
meta.isIdentifier({ name: "new" }) &&
property.isIdentifier({ name: "target" })
) {
path.replaceWith(scope.buildUndefinedNode());
}
},
},
environmentVisitor,
]);