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

View File

@ -22,6 +22,8 @@ import {
export { FEATURES, enableFeature, injectInitialization }; 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 // 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 // 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 // package, but it breaks if we release x.y.z where x, y or z are
@ -38,6 +40,12 @@ export function createClassFeaturePlugin({
manipulateOptions, manipulateOptions,
// TODO(Babel 8): Remove the default falue // TODO(Babel 8): Remove the default falue
api = { assumption: () => {} }, api = { assumption: () => {} },
}: {
name;
feature;
loose?;
manipulateOptions;
api?;
}) { }) {
const setPublicClassFields = api.assumption("setPublicClassFields"); const setPublicClassFields = api.assumption("setPublicClassFields");
const privateFieldsAsProperties = api.assumption("privateFieldsAsProperties"); const privateFieldsAsProperties = api.assumption("privateFieldsAsProperties");

View File

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

View File

@ -1,8 +1,7 @@
// @flow
import type { NodePath } from "@babel/traverse"; 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 // TODO (Babel 8): Also check path.node.definite
if (path.node.declare) { if (path.node.declare) {

View File

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

View File

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