convert @babel/plugin-transform-typescript to typescript (#13222)

* babel-plugin-transform-typescript flowts rename

* babel-plugin-transform-typescript flowts convert

* babel-plugin-transform-typescript

* babel-plugin-transform-typescript type fixes

* babel-plugin-transform-typescript

* make generate-tsconfig

* yarn install

* babel-plugin-transform-typescript

* babel-plugin-transform-typescript avoid typecast
This commit is contained in:
Bogdan Savluk 2021-05-13 20:37:34 +02:00 committed by GitHub
parent 72371cb637
commit acfff5d7fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 30 additions and 13 deletions

View File

@ -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"
}

View File

@ -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<Node>) {
function seen(parentPath: NodePath<t.Node>) {
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) {

View File

@ -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<Object>)) {
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<Node>[] = [];
const importsToRemove: NodePath<t.Node>[] = [];
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);
}
});
},

View File

@ -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);

View File

@ -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"
],

View File

@ -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