refactor: move @babel/helper-hoist-variables to ts (#12413)
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
This commit is contained in:
parent
62c1ccbf81
commit
bfd3f80bdb
@ -13,7 +13,9 @@
|
|||||||
"access": "public"
|
"access": "public"
|
||||||
},
|
},
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
|
"TODO": "The @babel/traverse dependency is only needed for the NodePath TS type. After converting @babel/core to TS we can import NodePath from there.",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@babel/traverse": "workspace:^7.12.17",
|
||||||
"@babel/types": "workspace:^7.12.13"
|
"@babel/types": "workspace:^7.12.13"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,20 +1,36 @@
|
|||||||
import * as t from "@babel/types";
|
import * as t from "@babel/types";
|
||||||
|
import type { NodePath } from "@babel/traverse";
|
||||||
|
|
||||||
|
export type EmitFunction = (
|
||||||
|
id: t.Identifier,
|
||||||
|
idName: string,
|
||||||
|
hasInit: boolean,
|
||||||
|
) => any;
|
||||||
|
|
||||||
|
type State = {
|
||||||
|
kind: "var" | "let";
|
||||||
|
emit: EmitFunction;
|
||||||
|
};
|
||||||
|
|
||||||
|
type Unpacked<T> = T extends (infer U)[] ? U : T;
|
||||||
|
|
||||||
const visitor = {
|
const visitor = {
|
||||||
Scope(path, state) {
|
Scope(path: NodePath, state: State) {
|
||||||
if (state.kind === "let") path.skip();
|
if (state.kind === "let") path.skip();
|
||||||
},
|
},
|
||||||
|
|
||||||
Function(path) {
|
Function(path: NodePath) {
|
||||||
path.skip();
|
path.skip();
|
||||||
},
|
},
|
||||||
|
|
||||||
VariableDeclaration(path, state) {
|
VariableDeclaration(path: NodePath<t.VariableDeclaration>, state: State) {
|
||||||
if (state.kind && path.node.kind !== state.kind) return;
|
if (state.kind && path.node.kind !== state.kind) return;
|
||||||
|
|
||||||
const nodes = [];
|
const nodes = [];
|
||||||
|
|
||||||
const declarations: Array<Object> = path.get("declarations");
|
const declarations: ReadonlyArray<
|
||||||
|
NodePath<Unpacked<t.VariableDeclaration["declarations"]>>
|
||||||
|
> = path.get("declarations");
|
||||||
let firstId;
|
let firstId;
|
||||||
|
|
||||||
for (const declar of declarations) {
|
for (const declar of declarations) {
|
||||||
@ -42,6 +58,10 @@ const visitor = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function (path, emit: Function, kind: "var" | "let" = "var") {
|
export default function hoistVariables(
|
||||||
|
path: NodePath,
|
||||||
|
emit: EmitFunction,
|
||||||
|
kind: "var" | "let" = "var",
|
||||||
|
) {
|
||||||
path.traverse(visitor, { kind, emit });
|
path.traverse(visitor, { kind, emit });
|
||||||
}
|
}
|
||||||
@ -537,6 +537,7 @@ __metadata:
|
|||||||
version: 0.0.0-use.local
|
version: 0.0.0-use.local
|
||||||
resolution: "@babel/helper-hoist-variables@workspace:packages/babel-helper-hoist-variables"
|
resolution: "@babel/helper-hoist-variables@workspace:packages/babel-helper-hoist-variables"
|
||||||
dependencies:
|
dependencies:
|
||||||
|
"@babel/traverse": "workspace:^7.12.17"
|
||||||
"@babel/types": "workspace:^7.12.13"
|
"@babel/types": "workspace:^7.12.13"
|
||||||
languageName: unknown
|
languageName: unknown
|
||||||
linkType: soft
|
linkType: soft
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user