diff --git a/packages/babel-helper-create-class-features-plugin/src/decorators.js b/packages/babel-helper-create-class-features-plugin/src/decorators.ts similarity index 89% rename from packages/babel-helper-create-class-features-plugin/src/decorators.js rename to packages/babel-helper-create-class-features-plugin/src/decorators.ts index b5e90ad6e7..ae1f58f17e 100644 --- a/packages/babel-helper-create-class-features-plugin/src/decorators.js +++ b/packages/babel-helper-create-class-features-plugin/src/decorators.ts @@ -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; } diff --git a/packages/babel-helper-create-class-features-plugin/src/features.js b/packages/babel-helper-create-class-features-plugin/src/features.ts similarity index 100% rename from packages/babel-helper-create-class-features-plugin/src/features.js rename to packages/babel-helper-create-class-features-plugin/src/features.ts diff --git a/packages/babel-helper-create-class-features-plugin/src/fields.js b/packages/babel-helper-create-class-features-plugin/src/fields.ts similarity index 100% rename from packages/babel-helper-create-class-features-plugin/src/fields.js rename to packages/babel-helper-create-class-features-plugin/src/fields.ts diff --git a/packages/babel-helper-create-class-features-plugin/src/index.js b/packages/babel-helper-create-class-features-plugin/src/index.ts similarity index 98% rename from packages/babel-helper-create-class-features-plugin/src/index.js rename to packages/babel-helper-create-class-features-plugin/src/index.ts index 89394ba8f1..e071aa2845 100644 --- a/packages/babel-helper-create-class-features-plugin/src/index.js +++ b/packages/babel-helper-create-class-features-plugin/src/index.ts @@ -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"); diff --git a/packages/babel-helper-create-class-features-plugin/src/misc.js b/packages/babel-helper-create-class-features-plugin/src/misc.ts similarity index 99% rename from packages/babel-helper-create-class-features-plugin/src/misc.js rename to packages/babel-helper-create-class-features-plugin/src/misc.ts index 8bb57a85df..6bdf0b645d 100644 --- a/packages/babel-helper-create-class-features-plugin/src/misc.js +++ b/packages/babel-helper-create-class-features-plugin/src/misc.ts @@ -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; diff --git a/packages/babel-helper-create-class-features-plugin/src/typescript.js b/packages/babel-helper-create-class-features-plugin/src/typescript.ts similarity index 84% rename from packages/babel-helper-create-class-features-plugin/src/typescript.js rename to packages/babel-helper-create-class-features-plugin/src/typescript.ts index c18f0ea173..364ccd15e9 100644 --- a/packages/babel-helper-create-class-features-plugin/src/typescript.js +++ b/packages/babel-helper-create-class-features-plugin/src/typescript.ts @@ -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) { // TODO (Babel 8): Also check path.node.definite if (path.node.declare) { diff --git a/packages/babel-helper-replace-supers/src/index.ts b/packages/babel-helper-replace-supers/src/index.ts index 5fd6d4bc10..617c6c7746 100644 --- a/packages/babel-helper-replace-supers/src/index.ts +++ b/packages/babel-helper-replace-supers/src/index.ts @@ -254,7 +254,6 @@ const looseHandlers = { type ReplaceSupersOptionsBase = { methodPath: NodePath; - superRef: any; constantSuper?: boolean; file: any; // objectRef might have been shadowed in child scopes, diff --git a/tsconfig.json b/tsconfig.json index a6a886b9df..7fc7051507 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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" ],