diff --git a/packages/babel-plugin-transform-typescript/package.json b/packages/babel-plugin-transform-typescript/package.json index c5fcbf6050..65fa06be74 100644 --- a/packages/babel-plugin-transform-typescript/package.json +++ b/packages/babel-plugin-transform-typescript/package.json @@ -26,7 +26,9 @@ }, "devDependencies": { "@babel/core": "workspace:*", - "@babel/helper-plugin-test-runner": "workspace:*" + "@babel/helper-plugin-test-runner": "workspace:*", + "@babel/traverse": "workspace:*", + "@babel/types": "workspace:*" }, "homepage": "https://babel.dev/docs/en/next/babel-plugin-transform-typescript" } diff --git a/packages/babel-plugin-transform-typescript/src/enum.js b/packages/babel-plugin-transform-typescript/src/enum.ts similarity index 95% rename from packages/babel-plugin-transform-typescript/src/enum.js rename to packages/babel-plugin-transform-typescript/src/enum.ts index 808f2fcf49..156ccda524 100644 --- a/packages/babel-plugin-transform-typescript/src/enum.js +++ b/packages/babel-plugin-transform-typescript/src/enum.ts @@ -1,5 +1,7 @@ import assert from "assert"; import { template } from "@babel/core"; +import type * as t from "@babel/types"; +import type { NodePath } from "@babel/traverse"; export default function transpileEnum(path, t) { const { node } = path; @@ -35,7 +37,7 @@ export default function transpileEnum(path, t) { throw new Error(`Unexpected enum parent '${path.parent.type}`); } - function seen(parentPath: Path) { + function seen(parentPath: NodePath) { if (parentPath.isExportDeclaration()) { return seen(parentPath.parentPath); } @@ -49,7 +51,7 @@ export default function transpileEnum(path, t) { } } -function makeVar(id, t, kind): VariableDeclaration { +function makeVar(id, t, kind) { return t.variableDeclaration(kind, [t.variableDeclarator(id)]); } @@ -99,7 +101,9 @@ function enumFill(path, t, id) { * Z = X | Y, * } */ -type PreviousEnumMembers = { [name: string]: number | string }; +type PreviousEnumMembers = { + [name: string]: number | string; +}; function translateEnumValues(path, t) { const seen: PreviousEnumMembers = Object.create(null); @@ -108,7 +112,7 @@ function translateEnumValues(path, t) { return path.node.members.map(member => { const name = t.isIdentifier(member.id) ? member.id.name : member.id.value; const initializer = member.initializer; - let value: Expression; + let value: t.Expression; if (initializer) { const constValue = evaluate(initializer, seen); if (constValue !== undefined) { diff --git a/packages/babel-plugin-transform-typescript/src/index.js b/packages/babel-plugin-transform-typescript/src/index.ts similarity index 98% rename from packages/babel-plugin-transform-typescript/src/index.js rename to packages/babel-plugin-transform-typescript/src/index.ts index 4073d96c59..aec840965b 100644 --- a/packages/babel-plugin-transform-typescript/src/index.js +++ b/packages/babel-plugin-transform-typescript/src/index.ts @@ -5,6 +5,7 @@ import { injectInitialization } from "@babel/helper-create-class-features-plugin import transpileEnum from "./enum"; import transpileNamespace from "./namespace"; +import type { NodePath } from "@babel/traverse"; function isInType(path) { switch (path.parent.type) { @@ -184,7 +185,7 @@ export default declare((api, opts) => { } if (file.ast.comments) { - for (const comment of (file.ast.comments: Array)) { + for (const comment of file.ast.comments) { const jsxMatches = JSX_PRAGMA_REGEX.exec(comment.value); if (jsxMatches) { if (jsxMatches[1]) { @@ -209,7 +210,7 @@ export default declare((api, opts) => { // remove type imports for (let stmt of path.get("body")) { - if (t.isImportDeclaration(stmt)) { + if (stmt.isImportDeclaration()) { if (stmt.node.importKind === "type") { stmt.remove(); continue; @@ -225,7 +226,7 @@ export default declare((api, opts) => { } let allElided = true; - const importsToRemove: Path[] = []; + const importsToRemove: NodePath[] = []; for (const specifier of stmt.node.specifiers) { const binding = stmt.scope.getBinding(specifier.local.name); @@ -373,13 +374,13 @@ export default declare((api, opts) => { if (child.node.kind === "constructor") { classMemberVisitors.constructor(child, path); } else { - classMemberVisitors.method(child, path); + classMemberVisitors.method(child); } } else if ( child.isClassProperty() || child.isClassPrivateProperty() ) { - classMemberVisitors.field(child, path); + classMemberVisitors.field(child); } }); }, diff --git a/packages/babel-plugin-transform-typescript/src/namespace.js b/packages/babel-plugin-transform-typescript/src/namespace.ts similarity index 96% rename from packages/babel-plugin-transform-typescript/src/namespace.js rename to packages/babel-plugin-transform-typescript/src/namespace.ts index 664326f462..84ea984864 100644 --- a/packages/babel-plugin-transform-typescript/src/namespace.js +++ b/packages/babel-plugin-transform-typescript/src/namespace.ts @@ -67,10 +67,14 @@ function handleVariableDeclaration( ); } const { declarations } = node; - if (declarations.every(declarator => t.isIdentifier(declarator.id))) { + if ( + declarations.every((declarator): declarator is t.VariableDeclarator & { + id: t.Identifier; + } => t.isIdentifier(declarator.id)) + ) { // `export const a = 1` transforms to `const a = N.a = 1`, the output // is smaller than `const a = 1; N.a = a`; - for (const declarator of node.declarations) { + for (const declarator of declarations) { declarator.init = t.assignmentExpression( "=", getMemberExpression(t, name, declarator.id.name), @@ -97,7 +101,7 @@ function handleVariableDeclaration( return [node, t.expressionStatement(t.sequenceExpression(assignments))]; } -function handleNested(path, t, node, parentExport) { +function handleNested(path, t, node, parentExport?) { const names = new Set(); const realName = node.id; const name = path.scope.generateUid(realName.name); diff --git a/tsconfig.json b/tsconfig.json index 98d6c1a107..2fc9a72dbc 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -27,6 +27,7 @@ "./packages/babel-plugin-proposal-async-do-expressions/src/**/*.ts", "./packages/babel-plugin-syntax-async-do-expressions/src/**/*.ts", "./packages/babel-plugin-transform-react-jsx/src/**/*.ts", + "./packages/babel-plugin-transform-typescript/src/**/*.ts", "./packages/babel-template/src/**/*.ts", "./packages/babel-traverse/src/**/*.ts", "./packages/babel-types/src/**/*.ts" @@ -108,6 +109,9 @@ "@babel/plugin-transform-react-jsx": [ "./packages/babel-plugin-transform-react-jsx/src" ], + "@babel/plugin-transform-typescript": [ + "./packages/babel-plugin-transform-typescript/src" + ], "@babel/template": [ "./packages/babel-template/src" ], diff --git a/yarn.lock b/yarn.lock index 506cefb067..cbb92c2784 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3012,6 +3012,8 @@ __metadata: "@babel/helper-plugin-test-runner": "workspace:*" "@babel/helper-plugin-utils": "workspace:^7.13.0" "@babel/plugin-syntax-typescript": "workspace:^7.12.13" + "@babel/traverse": "workspace:*" + "@babel/types": "workspace:*" peerDependencies: "@babel/core": ^7.0.0-0 languageName: unknown