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:
parent
72371cb637
commit
acfff5d7fe
@ -26,7 +26,9 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@babel/core": "workspace:*",
|
"@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"
|
"homepage": "https://babel.dev/docs/en/next/babel-plugin-transform-typescript"
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
import assert from "assert";
|
import assert from "assert";
|
||||||
import { template } from "@babel/core";
|
import { template } from "@babel/core";
|
||||||
|
import type * as t from "@babel/types";
|
||||||
|
import type { NodePath } from "@babel/traverse";
|
||||||
|
|
||||||
export default function transpileEnum(path, t) {
|
export default function transpileEnum(path, t) {
|
||||||
const { node } = path;
|
const { node } = path;
|
||||||
@ -35,7 +37,7 @@ export default function transpileEnum(path, t) {
|
|||||||
throw new Error(`Unexpected enum parent '${path.parent.type}`);
|
throw new Error(`Unexpected enum parent '${path.parent.type}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
function seen(parentPath: Path<Node>) {
|
function seen(parentPath: NodePath<t.Node>) {
|
||||||
if (parentPath.isExportDeclaration()) {
|
if (parentPath.isExportDeclaration()) {
|
||||||
return seen(parentPath.parentPath);
|
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)]);
|
return t.variableDeclaration(kind, [t.variableDeclarator(id)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,7 +101,9 @@ function enumFill(path, t, id) {
|
|||||||
* Z = X | Y,
|
* Z = X | Y,
|
||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
type PreviousEnumMembers = { [name: string]: number | string };
|
type PreviousEnumMembers = {
|
||||||
|
[name: string]: number | string;
|
||||||
|
};
|
||||||
|
|
||||||
function translateEnumValues(path, t) {
|
function translateEnumValues(path, t) {
|
||||||
const seen: PreviousEnumMembers = Object.create(null);
|
const seen: PreviousEnumMembers = Object.create(null);
|
||||||
@ -108,7 +112,7 @@ function translateEnumValues(path, t) {
|
|||||||
return path.node.members.map(member => {
|
return path.node.members.map(member => {
|
||||||
const name = t.isIdentifier(member.id) ? member.id.name : member.id.value;
|
const name = t.isIdentifier(member.id) ? member.id.name : member.id.value;
|
||||||
const initializer = member.initializer;
|
const initializer = member.initializer;
|
||||||
let value: Expression;
|
let value: t.Expression;
|
||||||
if (initializer) {
|
if (initializer) {
|
||||||
const constValue = evaluate(initializer, seen);
|
const constValue = evaluate(initializer, seen);
|
||||||
if (constValue !== undefined) {
|
if (constValue !== undefined) {
|
||||||
@ -5,6 +5,7 @@ import { injectInitialization } from "@babel/helper-create-class-features-plugin
|
|||||||
|
|
||||||
import transpileEnum from "./enum";
|
import transpileEnum from "./enum";
|
||||||
import transpileNamespace from "./namespace";
|
import transpileNamespace from "./namespace";
|
||||||
|
import type { NodePath } from "@babel/traverse";
|
||||||
|
|
||||||
function isInType(path) {
|
function isInType(path) {
|
||||||
switch (path.parent.type) {
|
switch (path.parent.type) {
|
||||||
@ -184,7 +185,7 @@ export default declare((api, opts) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (file.ast.comments) {
|
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);
|
const jsxMatches = JSX_PRAGMA_REGEX.exec(comment.value);
|
||||||
if (jsxMatches) {
|
if (jsxMatches) {
|
||||||
if (jsxMatches[1]) {
|
if (jsxMatches[1]) {
|
||||||
@ -209,7 +210,7 @@ export default declare((api, opts) => {
|
|||||||
|
|
||||||
// remove type imports
|
// remove type imports
|
||||||
for (let stmt of path.get("body")) {
|
for (let stmt of path.get("body")) {
|
||||||
if (t.isImportDeclaration(stmt)) {
|
if (stmt.isImportDeclaration()) {
|
||||||
if (stmt.node.importKind === "type") {
|
if (stmt.node.importKind === "type") {
|
||||||
stmt.remove();
|
stmt.remove();
|
||||||
continue;
|
continue;
|
||||||
@ -225,7 +226,7 @@ export default declare((api, opts) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let allElided = true;
|
let allElided = true;
|
||||||
const importsToRemove: Path<Node>[] = [];
|
const importsToRemove: NodePath<t.Node>[] = [];
|
||||||
|
|
||||||
for (const specifier of stmt.node.specifiers) {
|
for (const specifier of stmt.node.specifiers) {
|
||||||
const binding = stmt.scope.getBinding(specifier.local.name);
|
const binding = stmt.scope.getBinding(specifier.local.name);
|
||||||
@ -373,13 +374,13 @@ export default declare((api, opts) => {
|
|||||||
if (child.node.kind === "constructor") {
|
if (child.node.kind === "constructor") {
|
||||||
classMemberVisitors.constructor(child, path);
|
classMemberVisitors.constructor(child, path);
|
||||||
} else {
|
} else {
|
||||||
classMemberVisitors.method(child, path);
|
classMemberVisitors.method(child);
|
||||||
}
|
}
|
||||||
} else if (
|
} else if (
|
||||||
child.isClassProperty() ||
|
child.isClassProperty() ||
|
||||||
child.isClassPrivateProperty()
|
child.isClassPrivateProperty()
|
||||||
) {
|
) {
|
||||||
classMemberVisitors.field(child, path);
|
classMemberVisitors.field(child);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
@ -67,10 +67,14 @@ function handleVariableDeclaration(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
const { declarations } = node;
|
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
|
// `export const a = 1` transforms to `const a = N.a = 1`, the output
|
||||||
// is smaller than `const a = 1; N.a = a`;
|
// is smaller than `const a = 1; N.a = a`;
|
||||||
for (const declarator of node.declarations) {
|
for (const declarator of declarations) {
|
||||||
declarator.init = t.assignmentExpression(
|
declarator.init = t.assignmentExpression(
|
||||||
"=",
|
"=",
|
||||||
getMemberExpression(t, name, declarator.id.name),
|
getMemberExpression(t, name, declarator.id.name),
|
||||||
@ -97,7 +101,7 @@ function handleVariableDeclaration(
|
|||||||
return [node, t.expressionStatement(t.sequenceExpression(assignments))];
|
return [node, t.expressionStatement(t.sequenceExpression(assignments))];
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleNested(path, t, node, parentExport) {
|
function handleNested(path, t, node, parentExport?) {
|
||||||
const names = new Set();
|
const names = new Set();
|
||||||
const realName = node.id;
|
const realName = node.id;
|
||||||
const name = path.scope.generateUid(realName.name);
|
const name = path.scope.generateUid(realName.name);
|
||||||
@ -27,6 +27,7 @@
|
|||||||
"./packages/babel-plugin-proposal-async-do-expressions/src/**/*.ts",
|
"./packages/babel-plugin-proposal-async-do-expressions/src/**/*.ts",
|
||||||
"./packages/babel-plugin-syntax-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-react-jsx/src/**/*.ts",
|
||||||
|
"./packages/babel-plugin-transform-typescript/src/**/*.ts",
|
||||||
"./packages/babel-template/src/**/*.ts",
|
"./packages/babel-template/src/**/*.ts",
|
||||||
"./packages/babel-traverse/src/**/*.ts",
|
"./packages/babel-traverse/src/**/*.ts",
|
||||||
"./packages/babel-types/src/**/*.ts"
|
"./packages/babel-types/src/**/*.ts"
|
||||||
@ -108,6 +109,9 @@
|
|||||||
"@babel/plugin-transform-react-jsx": [
|
"@babel/plugin-transform-react-jsx": [
|
||||||
"./packages/babel-plugin-transform-react-jsx/src"
|
"./packages/babel-plugin-transform-react-jsx/src"
|
||||||
],
|
],
|
||||||
|
"@babel/plugin-transform-typescript": [
|
||||||
|
"./packages/babel-plugin-transform-typescript/src"
|
||||||
|
],
|
||||||
"@babel/template": [
|
"@babel/template": [
|
||||||
"./packages/babel-template/src"
|
"./packages/babel-template/src"
|
||||||
],
|
],
|
||||||
|
|||||||
@ -3012,6 +3012,8 @@ __metadata:
|
|||||||
"@babel/helper-plugin-test-runner": "workspace:*"
|
"@babel/helper-plugin-test-runner": "workspace:*"
|
||||||
"@babel/helper-plugin-utils": "workspace:^7.13.0"
|
"@babel/helper-plugin-utils": "workspace:^7.13.0"
|
||||||
"@babel/plugin-syntax-typescript": "workspace:^7.12.13"
|
"@babel/plugin-syntax-typescript": "workspace:^7.12.13"
|
||||||
|
"@babel/traverse": "workspace:*"
|
||||||
|
"@babel/types": "workspace:*"
|
||||||
peerDependencies:
|
peerDependencies:
|
||||||
"@babel/core": ^7.0.0-0
|
"@babel/core": ^7.0.0-0
|
||||||
languageName: unknown
|
languageName: unknown
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user