Skip to content

Commit

Permalink
refactor: move @babel/helper-hoist-variables to ts (#12413)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
  • Loading branch information
JLHwung and nicolo-ribaudo committed Feb 20, 2021
1 parent 62c1ccb commit bfd3f80
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
2 changes: 2 additions & 0 deletions packages/babel-helper-hoist-variables/package.json
Expand Up @@ -13,7 +13,9 @@
"access": "public"
},
"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": {
"@babel/traverse": "workspace:^7.12.17",
"@babel/types": "workspace:^7.12.13"
}
}
@@ -1,20 +1,36 @@
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 = {
Scope(path, state) {
Scope(path: NodePath, state: State) {
if (state.kind === "let") path.skip();
},

Function(path) {
Function(path: NodePath) {
path.skip();
},

VariableDeclaration(path, state) {
VariableDeclaration(path: NodePath<t.VariableDeclaration>, state: State) {
if (state.kind && path.node.kind !== state.kind) return;

const nodes = [];

const declarations: Array<Object> = path.get("declarations");
const declarations: ReadonlyArray<
NodePath<Unpacked<t.VariableDeclaration["declarations"]>>
> = path.get("declarations");
let firstId;

for (const declar of declarations) {
Expand Down Expand Up @@ -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 });
}
1 change: 1 addition & 0 deletions yarn.lock
Expand Up @@ -537,6 +537,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@babel/helper-hoist-variables@workspace:packages/babel-helper-hoist-variables"
dependencies:
"@babel/traverse": "workspace:^7.12.17"
"@babel/types": "workspace:^7.12.13"
languageName: unknown
linkType: soft
Expand Down

0 comments on commit bfd3f80

Please sign in to comment.