Skip to content

Commit

Permalink
convert @babel/helper-create-class-features-plugin to TS (#13214)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
  • Loading branch information
zxbodya and nicolo-ribaudo committed May 26, 2021
1 parent 342fec1 commit 4ee6a27
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 25 deletions.
Expand Up @@ -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();

const properties = [
new ReplaceSupers({
methodPath: path,
objectRef: classRef,
superRef,
file: this,
refToPreserve: classRef,
}).replace();

const properties: t.ObjectExpression["properties"] = [
prop("kind", t.stringLiteral(isMethod ? node.kind : "field")),
prop("decorators", takeDecorators(node)),
prop("static", node.static && t.booleanLiteral(true)),
Expand Down Expand Up @@ -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}) {
Expand All @@ -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;
}

Expand Down
Expand Up @@ -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
Expand All @@ -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");
Expand Down
Expand Up @@ -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;
Expand Down
@@ -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) {
Expand Down
1 change: 0 additions & 1 deletion packages/babel-helper-replace-supers/src/index.ts
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions tsconfig.json
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
],
Expand Down

0 comments on commit 4ee6a27

Please sign in to comment.