convert @babel/helper-create-class-features-plugin to TS (#13214)

Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
This commit is contained in:
Bogdan Savluk 2021-05-26 15:49:35 +02:00 committed by GitHub
parent 342fec1a78
commit 4ee6a27780
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 29 additions and 24 deletions

View File

@ -59,21 +59,15 @@ function extractElementDescriptor(/* this: File, */ classRef, superRef, path) {
);
}
new ReplaceSupers(
{
methodPath: path,
methodNode: node,
objectRef: classRef,
isStatic: node.static,
superRef,
scope,
file: this,
refToPreserve: classRef,
},
true,
).replace();
new ReplaceSupers({
methodPath: path,
objectRef: classRef,
superRef,
file: this,
refToPreserve: classRef,
}).replace();
const properties = [
const properties: t.ObjectExpression["properties"] = [
prop("kind", t.stringLiteral(isMethod ? node.kind : "field")),
prop("decorators", takeDecorators(node)),
prop("static", node.static && t.booleanLiteral(true)),
@ -135,7 +129,7 @@ export function buildDecoratedClass(ref, path, elements, file) {
.map(extractElementDescriptor.bind(file, node.id, superId)),
);
let replacement = template.expression.ast`
const wrapperCall = template.expression.ast`
${addDecorateHelper(file)}(
${classDecorators || t.nullLiteral()},
function (${initializeId}, ${superClass ? t.cloneNode(superId) : null}) {
@ -144,17 +138,18 @@ export function buildDecoratedClass(ref, path, elements, file) {
},
${superClass}
)
`;
let classPathDesc = "arguments.1.body.body.0";
` as t.CallExpression & { arguments: [unknown, t.FunctionExpression] };
if (!isStrict) {
replacement.arguments[1].body.directives.push(
wrapperCall.arguments[1].body.directives.push(
t.directive(t.directiveLiteral("use strict")),
);
}
let replacement: t.Node = wrapperCall;
let classPathDesc = "arguments.1.body.body.0";
if (isDeclaration) {
replacement = template.ast`let ${ref} = ${replacement}`;
replacement = template.statement.ast`let ${ref} = ${wrapperCall}`;
classPathDesc = "declarations.0.init." + classPathDesc;
}

View File

@ -22,6 +22,8 @@ import {
export { FEATURES, enableFeature, injectInitialization };
declare const PACKAGE_JSON: { name: string; version: string };
// Note: Versions are represented as an integer. e.g. 7.1.5 is represented
// as 70000100005. This method is easier than using a semver-parsing
// package, but it breaks if we release x.y.z where x, y or z are
@ -38,6 +40,12 @@ export function createClassFeaturePlugin({
manipulateOptions,
// TODO(Babel 8): Remove the default falue
api = { assumption: () => {} },
}: {
name;
feature;
loose?;
manipulateOptions;
api?;
}) {
const setPublicClassFields = api.assumption("setPublicClassFields");
const privateFieldsAsProperties = api.assumption("privateFieldsAsProperties");

View File

@ -44,7 +44,7 @@ const classFieldDefinitionEvaluationTDZVisitor = {
ReferencedIdentifier: handleClassTDZ,
};
export function injectInitialization(path, constructor, nodes, renamer) {
export function injectInitialization(path, constructor, nodes, renamer?) {
if (!nodes.length) return;
const isDerived = !!path.node.superClass;

View File

@ -1,8 +1,7 @@
// @flow
import type { NodePath } from "@babel/traverse";
import type * as t from "@babel/types";
export function assertFieldTransformed(path: NodePath) {
export function assertFieldTransformed(path: NodePath<t.ClassProperty>) {
// TODO (Babel 8): Also check path.node.definite
if (path.node.declare) {

View File

@ -254,7 +254,6 @@ const looseHandlers = {
type ReplaceSupersOptionsBase = {
methodPath: NodePath<any>;
superRef: any;
constantSuper?: boolean;
file: any;
// objectRef might have been shadowed in child scopes,

View File

@ -7,6 +7,7 @@
"./packages/babel-generator/src/**/*.ts",
"./packages/babel-helper-annotate-as-pure/src/**/*.ts",
"./packages/babel-helper-compilation-targets/src/**/*.ts",
"./packages/babel-helper-create-class-features-plugin/src/**/*.ts",
"./packages/babel-helper-create-regexp-features-plugin/src/**/*.ts",
"./packages/babel-helper-explode-assignable-expression/src/**/*.ts",
"./packages/babel-helper-fixtures/src/**/*.ts",
@ -53,6 +54,9 @@
"@babel/helper-compilation-targets": [
"./packages/babel-helper-compilation-targets/src"
],
"@babel/helper-create-class-features-plugin": [
"./packages/babel-helper-create-class-features-plugin/src"
],
"@babel/helper-create-regexp-features-plugin": [
"./packages/babel-helper-create-regexp-features-plugin/src"
],