Migrate Babel from Flow to TypeScript (except Babel parser) (#11578)
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { declare } from "@babel/helper-plugin-utils";
|
||||
import { types as t } from "@babel/core";
|
||||
import type { Scope } from "@babel/traverse";
|
||||
|
||||
export default declare((api, options) => {
|
||||
api.assertVersion(7);
|
||||
@@ -23,7 +24,7 @@ export default declare((api, options) => {
|
||||
*/
|
||||
|
||||
function variableDeclarationHasPattern(node) {
|
||||
for (const declar of (node.declarations: Array)) {
|
||||
for (const declar of node.declarations) {
|
||||
if (t.isPattern(declar.id)) {
|
||||
return true;
|
||||
}
|
||||
@@ -36,7 +37,7 @@ export default declare((api, options) => {
|
||||
*/
|
||||
|
||||
function hasRest(pattern) {
|
||||
for (const elem of (pattern.elements: Array)) {
|
||||
for (const elem of pattern.elements) {
|
||||
if (t.isRestElement(elem)) {
|
||||
return true;
|
||||
}
|
||||
@@ -49,7 +50,7 @@ export default declare((api, options) => {
|
||||
*/
|
||||
|
||||
function hasObjectRest(pattern) {
|
||||
for (const elem of (pattern.properties: Array)) {
|
||||
for (const elem of pattern.properties) {
|
||||
if (t.isRestElement(elem)) {
|
||||
return true;
|
||||
}
|
||||
@@ -77,6 +78,15 @@ export default declare((api, options) => {
|
||||
};
|
||||
|
||||
class DestructuringTransformer {
|
||||
private blockHoist: any;
|
||||
private operator: any;
|
||||
arrays: any;
|
||||
private nodes: any;
|
||||
private scope: Scope;
|
||||
private kind: any;
|
||||
private iterableIsArray: any;
|
||||
private arrayLikeIsIterable: any;
|
||||
private addHelper: any;
|
||||
constructor(opts) {
|
||||
this.blockHoist = opts.blockHoist;
|
||||
this.operator = opts.operator;
|
||||
@@ -118,6 +128,7 @@ export default declare((api, options) => {
|
||||
const declar = t.variableDeclaration("var", [
|
||||
t.variableDeclarator(t.cloneNode(id), t.cloneNode(init)),
|
||||
]);
|
||||
// @ts-expect-error todo(flow->ts): avoid mutations
|
||||
declar._blockHoist = this.blockHoist;
|
||||
return declar;
|
||||
}
|
||||
@@ -135,7 +146,7 @@ export default declare((api, options) => {
|
||||
}
|
||||
}
|
||||
|
||||
toArray(node, count) {
|
||||
toArray(node, count?) {
|
||||
if (
|
||||
this.iterableIsArray ||
|
||||
(t.isIdentifier(node) && this.arrays[node.name])
|
||||
@@ -208,6 +219,7 @@ export default declare((api, options) => {
|
||||
keys.push(t.cloneNode(key));
|
||||
hasTemplateLiteral = true;
|
||||
} else if (t.isLiteral(key)) {
|
||||
// @ts-expect-error todo(flow->ts) NullLiteral
|
||||
keys.push(t.stringLiteral(String(key.value)));
|
||||
} else {
|
||||
keys.push(t.cloneNode(key));
|
||||
@@ -222,7 +234,7 @@ export default declare((api, options) => {
|
||||
t.cloneNode(objRef),
|
||||
]);
|
||||
} else {
|
||||
let keyExpression = t.arrayExpression(keys);
|
||||
let keyExpression: t.Expression = t.arrayExpression(keys);
|
||||
|
||||
if (!allLiteral) {
|
||||
keyExpression = t.callExpression(
|
||||
@@ -342,7 +354,7 @@ export default declare((api, options) => {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const elem of (pattern.elements: Array)) {
|
||||
for (const elem of pattern.elements) {
|
||||
// deopt on holes
|
||||
if (!elem) return false;
|
||||
|
||||
@@ -350,7 +362,7 @@ export default declare((api, options) => {
|
||||
if (t.isMemberExpression(elem)) return false;
|
||||
}
|
||||
|
||||
for (const elem of (arr.elements: Array)) {
|
||||
for (const elem of arr.elements) {
|
||||
// deopt on spread elements
|
||||
if (t.isSpreadElement(elem)) return false;
|
||||
|
||||
Reference in New Issue
Block a user