diff --git a/packages/babel-plugin-transform-typescript/src/enum.ts b/packages/babel-plugin-transform-typescript/src/enum.ts index 79368a06656a..9a3234286f69 100644 --- a/packages/babel-plugin-transform-typescript/src/enum.ts +++ b/packages/babel-plugin-transform-typescript/src/enum.ts @@ -39,7 +39,7 @@ export default function transpileEnum( throw new Error(`Unexpected enum parent '${path.parent.type}`); } - function seen(parentPath: NodePath) { + function seen(parentPath: NodePath): boolean { if (parentPath.isExportDeclaration()) { return seen(parentPath.parentPath); } @@ -193,12 +193,12 @@ export function translateEnumValues( // Based on the TypeScript repository's `evalConstant` in `checker.ts`. function evaluate( - expr, + expr: t.Node, seen: PreviousEnumMembers, ): number | string | typeof undefined { return evalConstant(expr); - function evalConstant(expr): number | typeof undefined { + function evalConstant(expr: t.Node): number | typeof undefined { switch (expr.type) { case "StringLiteral": return expr.value; @@ -225,7 +225,7 @@ function evaluate( function evalUnaryExpression({ argument, operator, - }): number | typeof undefined { + }: t.UnaryExpression): number | typeof undefined { const value = evalConstant(argument); if (value === undefined) { return undefined; @@ -243,7 +243,7 @@ function evaluate( } } - function evalBinaryExpression(expr): number | typeof undefined { + function evalBinaryExpression(expr: t.BinaryExpression): number | undefined { const left = evalConstant(expr.left); if (left === undefined) { return undefined; diff --git a/packages/babel-plugin-transform-typescript/src/index.ts b/packages/babel-plugin-transform-typescript/src/index.ts index ef57f17e9e34..01ad3eac6711 100644 --- a/packages/babel-plugin-transform-typescript/src/index.ts +++ b/packages/babel-plugin-transform-typescript/src/index.ts @@ -2,7 +2,7 @@ import { declare } from "@babel/helper-plugin-utils"; import syntaxTypeScript from "@babel/plugin-syntax-typescript"; import { types as t, template } from "@babel/core"; import { injectInitialization } from "@babel/helper-create-class-features-plugin"; -import type { NodePath } from "@babel/traverse"; +import type { Binding, NodePath } from "@babel/traverse"; import type { Options as SyntaxOptions } from "@babel/plugin-syntax-typescript"; import transpileConstEnum from "./const-enum"; @@ -149,7 +149,7 @@ export default declare((api, opts: Options) => { if (node.declare) node.declare = null; if (node.override) node.override = null; }, - method({ node }) { + method({ node }: NodePath) { if (node.accessibility) node.accessibility = null; if (node.abstract) node.abstract = null; if (node.optional) node.optional = null; @@ -157,7 +157,7 @@ export default declare((api, opts: Options) => { // Rest handled by Function visitor }, - constructor(path, classPath) { + constructor(path: NodePath, classPath: NodePath) { if (path.node.accessibility) path.node.accessibility = null; // Collects parameter properties so that we can add an assignment // for each of them in the constructor body @@ -487,7 +487,11 @@ export default declare((api, opts: Options) => { path.get("body.body").forEach(child => { if (child.isClassMethod() || child.isClassPrivateMethod()) { if (child.node.kind === "constructor") { - classMemberVisitors.constructor(child, path); + classMemberVisitors.constructor( + // @ts-expect-error A constructor must not be a private method + child, + path, + ); } else { classMemberVisitors.method(child); } @@ -512,7 +516,7 @@ export default declare((api, opts: Options) => { }, TSModuleDeclaration(path) { - transpileNamespace(path, t, allowNamespaces); + transpileNamespace(path, allowNamespaces); }, TSInterfaceDeclaration(path) { @@ -614,7 +618,9 @@ export default declare((api, opts: Options) => { return node; } - function visitPattern({ node }) { + function visitPattern({ + node, + }: NodePath) { if (node.typeAnnotation) node.typeAnnotation = null; if (t.isIdentifier(node) && node.optional) node.optional = null; // 'access' and 'readonly' are only for parameter properties, so constructor visitor will handle them. @@ -625,6 +631,11 @@ export default declare((api, opts: Options) => { programPath, pragmaImportName, pragmaFragImportName, + }: { + binding: Binding; + programPath: NodePath; + pragmaImportName: string; + pragmaFragImportName: string; }) { for (const path of binding.referencePaths) { if (!isInType(path)) { diff --git a/packages/babel-plugin-transform-typescript/src/namespace.ts b/packages/babel-plugin-transform-typescript/src/namespace.ts index 186da7c3c77c..74532eca9412 100644 --- a/packages/babel-plugin-transform-typescript/src/namespace.ts +++ b/packages/babel-plugin-transform-typescript/src/namespace.ts @@ -1,29 +1,33 @@ import { template, types as t } from "@babel/core"; import type { NodePath } from "@babel/traverse"; -export default function transpileNamespace(path, t, allowNamespaces) { +export default function transpileNamespace( + path: NodePath, + allowNamespaces: boolean, +) { if (path.node.declare || path.node.id.type === "StringLiteral") { path.remove(); return; } if (!allowNamespaces) { - throw path.hub.file.buildCodeFrameError( - path.node.id, - "Namespace not marked type-only declare." + - " Non-declarative namespaces are only supported experimentally in Babel." + - " To enable and review caveats see:" + - " https://babeljs.io/docs/en/babel-plugin-transform-typescript", - ); + throw path + .get("id") + .buildCodeFrameError( + "Namespace not marked type-only declare." + + " Non-declarative namespaces are only supported experimentally in Babel." + + " To enable and review caveats see:" + + " https://babeljs.io/docs/en/babel-plugin-transform-typescript", + ); } const name = path.node.id.name; - const value = handleNested(path, t, t.cloneDeep(path.node)); + const value = handleNested(path, t.cloneNode(path.node, true)); const bound = path.scope.hasOwnBinding(name); if (path.parent.type === "ExportNamedDeclaration") { if (!bound) { path.parentPath.insertAfter(value); - path.replaceWith(getDeclaration(t, name)); + path.replaceWith(getDeclaration(name)); path.scope.registerDeclaration(path.parentPath); } else { path.parentPath.replaceWith(value); @@ -32,18 +36,18 @@ export default function transpileNamespace(path, t, allowNamespaces) { path.replaceWith(value); } else { path.scope.registerDeclaration( - path.replaceWithMultiple([getDeclaration(t, name), value])[0], + path.replaceWithMultiple([getDeclaration(name), value])[0], ); } } -function getDeclaration(t, name) { +function getDeclaration(name: string) { return t.variableDeclaration("let", [ t.variableDeclarator(t.identifier(name)), ]); } -function getMemberExpression(t, name, itemName) { +function getMemberExpression(name: string, itemName: string) { return t.memberExpression(t.identifier(name), t.identifier(itemName)); } @@ -79,7 +83,7 @@ function handleVariableDeclaration( for (const declarator of declarations) { declarator.init = t.assignmentExpression( "=", - getMemberExpression(t, name, declarator.id.name), + getMemberExpression(name, declarator.id.name), declarator.init, ); } @@ -95,7 +99,7 @@ function handleVariableDeclaration( assignments.push( t.assignmentExpression( "=", - getMemberExpression(t, name, idName), + getMemberExpression(name, idName), t.cloneNode(bindingIdentifiers[idName]), ), ); @@ -113,9 +117,8 @@ function buildNestedAmbiendModuleError(path: NodePath, node: t.Node) { function handleNested( path: NodePath, - t: typeof import("@babel/types"), node: t.TSModuleDeclaration, - parentExport?, + parentExport?: t.Expression, ) { const names = new Set(); const realName = node.id; @@ -142,7 +145,7 @@ function handleNested( throw buildNestedAmbiendModuleError(path, subNode); } - const transformed = handleNested(path, t, subNode); + const transformed = handleNested(path, subNode); const moduleName = subNode.id.name; if (names.has(moduleName)) { namespaceTopLevel[i] = transformed; @@ -151,7 +154,7 @@ function handleNested( namespaceTopLevel.splice( i++, 1, - getDeclaration(t, moduleName), + getDeclaration(moduleName), transformed, ); } @@ -191,7 +194,7 @@ function handleNested( t.expressionStatement( t.assignmentExpression( "=", - getMemberExpression(t, name, itemName), + getMemberExpression(name, itemName), t.identifier(itemName), ), ), @@ -215,7 +218,6 @@ function handleNested( const transformed = handleNested( path, - t, subNode.declaration, t.identifier(name), ); @@ -227,7 +229,7 @@ function handleNested( namespaceTopLevel.splice( i++, 1, - getDeclaration(t, moduleName), + getDeclaration(moduleName), transformed, ); } diff --git a/packages/babel-preset-typescript/src/index.ts b/packages/babel-preset-typescript/src/index.ts index e9fb7b515204..5fce63166f9e 100644 --- a/packages/babel-preset-typescript/src/index.ts +++ b/packages/babel-preset-typescript/src/index.ts @@ -18,7 +18,7 @@ export default declarePreset((api, opts: Options) => { } = normalizeOptions(opts); const pluginOptions = process.env.BABEL_8_BREAKING - ? (isTSX, disallowAmbiguousJSXLike) => ({ + ? (isTSX: boolean, disallowAmbiguousJSXLike: boolean) => ({ allowNamespaces, disallowAmbiguousJSXLike, isTSX, @@ -27,7 +27,7 @@ export default declarePreset((api, opts: Options) => { onlyRemoveTypeImports, optimizeConstEnums, }) - : (isTSX, disallowAmbiguousJSXLike) => ({ + : (isTSX: boolean, disallowAmbiguousJSXLike: boolean) => ({ allowDeclareFields: opts.allowDeclareFields, allowNamespaces, disallowAmbiguousJSXLike,