Skip to content

Commit

Permalink
Fix 'Container is falsy' bug due to Babel upgrade (#2175)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie committed Sep 18, 2024
2 parents 3f9f015 + c69b2fd commit d9d175b
Show file tree
Hide file tree
Showing 6 changed files with 910 additions and 832 deletions.
8 changes: 8 additions & 0 deletions .changeset/heavy-radios-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"eslint-plugin-graphile-export": patch
"graphile-export": patch
"postgraphile": patch
---

Fix 'Container is falsy' error message the latest Babel patch release would
cause.
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@
"prettier:check": "yarn prettier:all --list-different"
},
"devDependencies": {
"@babel/cli": "^7.22.10",
"@babel/core": "^7.22.11",
"@babel/eslint-parser": "^7.22.11",
"@babel/cli": "^7.25.6",
"@babel/core": "^7.25.2",
"@babel/eslint-parser": "^7.25.1",
"@babel/plugin-syntax-object-rest-spread": "^7.8.3",
"@babel/plugin-transform-modules-commonjs": "^7.22.11",
"@babel/plugin-transform-runtime": "^7.22.10",
"@babel/preset-env": "^7.22.14",
"@babel/preset-react": "^7.22.5",
"@babel/preset-typescript": "^7.22.11",
"@babel/plugin-transform-modules-commonjs": "^7.24.8",
"@babel/plugin-transform-runtime": "^7.25.4",
"@babel/preset-env": "^7.25.4",
"@babel/preset-react": "^7.24.7",
"@babel/preset-typescript": "^7.24.7",
"@knodes/typedoc-plugin-monorepo-readmes": "^0.23.1",
"@knodes/typedoc-plugin-pages": "^0.23.4",
"@localrepo/prettier2-for-jest": "npm:prettier@^2",
Expand All @@ -48,7 +48,7 @@
"@typescript-eslint/eslint-plugin": "^6.5.0",
"@typescript-eslint/parser": "^6.5.0",
"@typescript-eslint/typescript-estree": "^6.5.0",
"babel-jest": "^29.6.4",
"babel-jest": "^29.7.0",
"babel-plugin-transform-import-meta": "^2.2.1",
"concurrently": "^8.2.1",
"eslint": "^8.48.0",
Expand Down
2 changes: 1 addition & 1 deletion utils/eslint-plugin-graphile-export/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"dist"
],
"devDependencies": {
"@babel/types": "^7.22.11",
"@babel/types": "^7.25.6",
"@types/eslint": "^8.44.2",
"@types/estree": "^1.0.1",
"@types/jest": "^29.5.4",
Expand Down
14 changes: 7 additions & 7 deletions utils/graphile-export/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@
},
"homepage": "https://graphile.org/",
"dependencies": {
"@babel/generator": "^7.22.10",
"@babel/parser": "^7.22.14",
"@babel/template": "^7.22.5",
"@babel/traverse": "^7.23.2",
"@babel/types": "^7.22.11",
"@babel/generator": "^7.25.6",
"@babel/parser": "^7.25.6",
"@babel/template": "^7.25.0",
"@babel/traverse": "^7.25.6",
"@babel/types": "^7.25.6",
"@types/node": "^20.5.7",
"prettier": "^3.0.3",
"tslib": "^2.6.2"
Expand All @@ -69,8 +69,8 @@
"dist"
],
"devDependencies": {
"@types/babel__generator": "^7.6.4",
"@types/babel__template": "^7.4.1",
"@types/babel__generator": "^7.6.8",
"@types/babel__template": "^7.4.4",
"@types/jest": "^29.5.4",
"@types/prettier": "^3.0.0",
"jest": "^29.6.4",
Expand Down
72 changes: 45 additions & 27 deletions utils/graphile-export/src/optimize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,47 +170,63 @@ export const optimize = (inAst: t.File, runs = 1): t.File => {
},
},
Program: {
exit(path) {
// Replace all things that are only referenced once
for (const [bindingName, binding] of Object.entries(
path.scope.bindings,
)) {
if (
!t.isVariableDeclarator(binding.path.node) &&
!t.isFunctionDeclaration(binding.path.node)
) {
continue;
exit(exitPath) {
// Make sure our scope information is up to date!
exitPath.scope.crawl();

// Replace all things that are only referenced once.
// This nested traversal approach inspired by https://github.com/babel/babel/issues/15544#issuecomment-1540542863
exitPath.traverse({
VariableDeclarator: visitSubpath,
FunctionDeclaration: visitSubpath,
});
function visitSubpath(
path:
| NodePath<t.VariableDeclarator>
| NodePath<t.FunctionDeclaration>,
) {
if (!t.isIdentifier(path.node.id)) {
return;
}
const bindingName = path.node.id.name;
const scope = t.isFunctionDeclaration(path.node)
? path.scope.parent
: path.scope;
// Only optimize at top level
if (scope !== exitPath.scope) {
return;
}
if (!t.isIdentifier(binding.path.node.id)) {
continue;
const binding = scope.bindings[bindingName];
if (!binding) {
return;
}
const expr = t.isFunctionDeclaration(binding.path.node)
const expr = t.isFunctionDeclaration(path.node)
? // Convert function to expression
t.functionExpression(
binding.path.node.id,
binding.path.node.params,
binding.path.node.body,
binding.path.node.generator,
binding.path.node.async,
path.node.id,
path.node.params,
path.node.body,
path.node.generator,
path.node.async,
)
: binding.path.node.init;
: path.node.init;
if (!expr) {
continue;
return;
}

// Skip if it's an export
const statementPath = binding.path.getStatementParent();
const statementPath = path.getStatementParent();
if (
!statementPath ||
t.isExportNamedDeclaration(statementPath.node) ||
t.isExportDefaultDeclaration(statementPath.node)
) {
continue;
return;
}

// Only replace if it's only referenced once (we don't want duplicates)
if (binding.referencePaths.length !== 1) {
continue;
return;
}
const targetPath = binding.referencePaths[0];
const parent = targetPath.parent;
Expand All @@ -222,7 +238,7 @@ export const optimize = (inAst: t.File, runs = 1): t.File => {
parent.callee === targetPath.node &&
(!t.isArrowFunctionExpression(expr) || t.isBlock(expr.body))
) {
continue;
return;
}

// It's allowed if:
Expand All @@ -231,12 +247,14 @@ export const optimize = (inAst: t.File, runs = 1): t.File => {
const targetFunctionParent = targetPath.getFunctionParent();
const sourceFunctionParent = path.getFunctionParent();
if (targetFunctionParent !== sourceFunctionParent) {
continue;
return;
}

targetPath.replaceWith(expr);
binding.path.remove();
binding.path.scope.removeBinding(bindingName);
// This stopping is important to avoid 'Container is falsy' errors.
targetPath.stop();
scope.removeBinding(bindingName);
path.remove();
}
},
},
Expand Down
Loading

0 comments on commit d9d175b

Please sign in to comment.