Skip to content

Commit 30f22cb

Browse files
committed
Fix removing unused function declarations
1 parent a25337f commit 30f22cb

2 files changed

Lines changed: 31 additions & 11 deletions

File tree

packages/start/src/directives/bubble-function-declaration.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,6 @@ export function bubbleFunctionDeclaration(path: babel.NodePath<t.FunctionDeclara
88
const block = (path.findParent(current => current.isBlockStatement()) ||
99
path.scope.getProgramParent().path) as babel.NodePath<t.BlockStatement>;
1010

11-
const [tmp] = block.unshiftContainer(
12-
"body",
13-
t.variableDeclaration("const", [
14-
t.variableDeclarator(
15-
decl.id,
16-
t.functionExpression(decl.id, decl.params, decl.body, decl.generator, decl.async),
17-
),
18-
]),
19-
);
20-
path.scope.registerDeclaration(tmp);
21-
// tmp.skip();
2211
if (path.parentPath.isExportNamedDeclaration()) {
2312
path.parentPath.replaceWith(
2413
t.exportNamedDeclaration(undefined, [t.exportSpecifier(decl.id, decl.id)]),
@@ -28,5 +17,17 @@ export function bubbleFunctionDeclaration(path: babel.NodePath<t.FunctionDeclara
2817
} else {
2918
path.remove();
3019
}
20+
21+
const [tmp] = block.unshiftContainer(
22+
"body",
23+
t.variableDeclaration("const", [
24+
t.variableDeclarator(
25+
decl.id,
26+
t.functionExpression(decl.id, decl.params, decl.body, decl.generator, decl.async),
27+
),
28+
]),
29+
);
30+
block.scope.registerDeclaration(tmp);
31+
tmp.skip();
3132
}
3233
}

packages/start/src/directives/remove-unused-variables.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type * as babel from "@babel/core";
22
import * as t from "@babel/types";
3+
import { generateUniqueName } from "./generate-unique-name.ts";
34
import { isPathValid } from "./paths.ts";
45

56
export function removeUnusedVariables(program: babel.NodePath<t.Program>) {
@@ -31,6 +32,24 @@ export function removeUnusedVariables(program: babel.NodePath<t.Program>) {
3132
} else {
3233
binding.path.remove();
3334
}
35+
36+
/**
37+
* TODO (Alexis):
38+
* For some reason, we can't directly remove function declarations,
39+
* Babel yields an error that is entirely on its fault.
40+
*
41+
* For now, we replace this with an unused variable and that variable
42+
* will then get removed on the next cycle.
43+
*
44+
* (Although weirdly enough, this should've been a variable
45+
* in the first phase during the earlier function bubbling phase)
46+
*/
47+
} else if (binding.kind === 'hoisted') {
48+
binding.path.replaceWith(
49+
t.variableDeclaration("const", [
50+
t.variableDeclarator(generateUniqueName(binding.path, "var")),
51+
]),
52+
);
3453
} else {
3554
binding.path.remove();
3655
}

0 commit comments

Comments
 (0)