Migrate Babel from Flow to TypeScript (except Babel parser) (#11578)
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
This commit is contained in:
@@ -9,6 +9,7 @@ import {
|
||||
isRestElement,
|
||||
returnStatement,
|
||||
} from "@babel/types";
|
||||
import type * as t from "@babel/types";
|
||||
|
||||
const buildAnonymousExpressionWrapper = template.expression(`
|
||||
(function () {
|
||||
@@ -37,7 +38,10 @@ const buildDeclarationWrapper = template(`
|
||||
}
|
||||
`);
|
||||
|
||||
function classOrObjectMethod(path: NodePath, callId: Object) {
|
||||
function classOrObjectMethod(
|
||||
path: NodePath<t.ClassMethod | t.ClassPrivateMethod | t.ObjectMethod>,
|
||||
callId: any,
|
||||
) {
|
||||
const node = path.node;
|
||||
const body = node.body;
|
||||
|
||||
@@ -57,12 +61,12 @@ function classOrObjectMethod(path: NodePath, callId: Object) {
|
||||
node.generator = false;
|
||||
|
||||
// Unwrap the wrapper IIFE's environment so super and this and such still work.
|
||||
path
|
||||
.get("body.body.0.argument.callee.arguments.0")
|
||||
.unwrapFunctionEnvironment();
|
||||
(
|
||||
path.get("body.body.0.argument.callee.arguments.0") as NodePath
|
||||
).unwrapFunctionEnvironment();
|
||||
}
|
||||
|
||||
function plainFunction(path: NodePath, callId: Object, noNewArrows: boolean) {
|
||||
function plainFunction(path: NodePath<any>, callId: any, noNewArrows: boolean) {
|
||||
const node = path.node;
|
||||
const isDeclaration = path.isFunctionDeclaration();
|
||||
const functionId = node.id;
|
||||
@@ -109,6 +113,7 @@ function plainFunction(path: NodePath, callId: Object, noNewArrows: boolean) {
|
||||
path.replaceWith(container[0]);
|
||||
path.insertAfter(container[1]);
|
||||
} else {
|
||||
// @ts-expect-error todo(flow->ts) separate `wrapper` for `isDeclaration` and `else` branches
|
||||
const retFunction = container.callee.body.body[1].argument;
|
||||
if (!functionId) {
|
||||
nameFunction({
|
||||
@@ -120,6 +125,7 @@ function plainFunction(path: NodePath, callId: Object, noNewArrows: boolean) {
|
||||
|
||||
if (!retFunction || retFunction.id || node.params.length) {
|
||||
// we have an inferred function id or params so we need this wrapper
|
||||
// @ts-expect-error todo(flow->ts) separate `wrapper` for `isDeclaration` and `else` branches
|
||||
path.replaceWith(container);
|
||||
} else {
|
||||
// we can omit this wrapper as the conditions it protects for do not apply
|
||||
@@ -130,7 +136,7 @@ function plainFunction(path: NodePath, callId: Object, noNewArrows: boolean) {
|
||||
|
||||
export default function wrapFunction(
|
||||
path: NodePath,
|
||||
callId: Object,
|
||||
callId: any,
|
||||
// TODO(Babel 8): Consider defaulting to false for spec compliancy
|
||||
noNewArrows: boolean = true,
|
||||
) {
|
||||
Reference in New Issue
Block a user