Skip to content

Commit

Permalink
Fix lost bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
lxsmnsyc committed Jan 30, 2024
1 parent f0c3751 commit c791b96
Show file tree
Hide file tree
Showing 24 changed files with 820 additions and 812 deletions.
2 changes: 2 additions & 0 deletions input.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Hello

function Example(props) {
return (
<h1>
Expand Down
9 changes: 5 additions & 4 deletions src/babel/core/create-registry.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { StateContext } from './types';
import type * as babel from '@babel/core';
import * as t from '@babel/types';
import { getImportIdentifier } from './get-import-identifier';
import { IMPORT_REFRESH, IMPORT_REGISTRY } from './constants';
import { getHotIdentifier } from './get-hot-identifier';
import { generateViteHMRRequirement } from './get-vite-hmr-requirement';
import { getImportIdentifier } from './get-import-identifier';
import { getRootStatementPath } from './get-root-statement-path';
import { generateViteHMRRequirement } from './get-vite-hmr-requirement';
import type { StateContext } from './types';

const REGISTRY = 'REGISTRY';

Expand All @@ -20,14 +20,15 @@ export function createRegistry(
const root = getRootStatementPath(path);
const identifier = path.scope.generateUidIdentifier(REGISTRY);

root.insertBefore(
const [tmp] = root.insertBefore(
t.variableDeclaration('const', [
t.variableDeclarator(
identifier,
t.callExpression(getImportIdentifier(state, path, IMPORT_REGISTRY), []),
),
]),
);
root.scope.registerDeclaration(tmp);
const pathToHot = getHotIdentifier(state);
const statements: t.Statement[] = [
t.expressionStatement(
Expand Down
4 changes: 3 additions & 1 deletion src/babel/core/transform-jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,12 @@ export function transformJSX(
templateComp.loc = path.node.loc;
}

rootPath.insertBefore(
const [tmp] = rootPath.insertBefore(
t.variableDeclaration('const', [t.variableDeclarator(id, templateComp)]),
);

rootPath.scope.registerDeclaration(tmp);

path.replaceWith(
skippableJSX(
t.jsxElement(
Expand Down
17 changes: 10 additions & 7 deletions src/babel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@ function isStatementTopLevel(path: babel.NodePath<t.Statement>): boolean {
return programParent === blockParent;
}

function isValidFunction(
node: t.Node,
): node is t.ArrowFunctionExpression | t.FunctionExpression {
return t.isArrowFunctionExpression(node) || t.isFunctionExpression(node);
}

function transformVariableDeclarator(
state: StateContext,
path: babel.NodePath<t.VariableDeclarator>,
Expand All @@ -231,9 +237,7 @@ function transformVariableDeclarator(
return;
}
if (isComponentishName(identifier.name)) {
const trueFuncExpr =
unwrapNode(init, t.isFunctionExpression) ||
unwrapNode(init, t.isArrowFunctionExpression);
const trueFuncExpr = unwrapNode(init, isValidFunction);
// Check for valid FunctionExpression or ArrowFunctionExpression
if (
trueFuncExpr &&
Expand Down Expand Up @@ -274,7 +278,7 @@ function transformFunctionDeclaration(
// have zero or one parameter
decl.params.length < 2
) {
path.replaceWith(
const [tmp] = path.replaceWith(
t.variableDeclaration('const', [
t.variableDeclarator(
decl.id,
Expand All @@ -288,6 +292,7 @@ function transformFunctionDeclaration(
),
]),
);
path.scope.registerDeclaration(tmp);
path.skip();
}
}
Expand All @@ -312,6 +317,7 @@ function bubbleFunctionDeclaration(
) {
const first = program.get('body')[0];
const [tmp] = first.insertBefore(decl);
program.scope.registerDeclaration(tmp);
tmp.skip();
if (path.parentPath.isExportNamedDeclaration()) {
path.parentPath.replaceWith(
Expand Down Expand Up @@ -360,7 +366,6 @@ export default function solidRefreshPlugin(): babel.PluginObj<State> {
bubbleFunctionDeclaration(programPath, path);
},
});
programPath.scope.crawl();
programPath.traverse({
JSXElement(path) {
transformJSX(path);
Expand All @@ -369,7 +374,6 @@ export default function solidRefreshPlugin(): babel.PluginObj<State> {
transformJSX(path);
},
});
programPath.scope.crawl();
programPath.traverse({
VariableDeclarator(path) {
transformVariableDeclarator(state, path);
Expand All @@ -378,7 +382,6 @@ export default function solidRefreshPlugin(): babel.PluginObj<State> {
transformFunctionDeclaration(state, path);
},
});
programPath.scope.crawl();
},
},
};
Expand Down
80 changes: 40 additions & 40 deletions tests/client-hydratable/__snapshots__/esm.test.ts.snap

Large diffs are not rendered by default.

80 changes: 40 additions & 40 deletions tests/client-hydratable/__snapshots__/rspack-esm.test.ts.snap

Large diffs are not rendered by default.

80 changes: 40 additions & 40 deletions tests/client-hydratable/__snapshots__/standard.test.ts.snap

Large diffs are not rendered by default.

80 changes: 40 additions & 40 deletions tests/client-hydratable/__snapshots__/vite.test.ts.snap

Large diffs are not rendered by default.

80 changes: 40 additions & 40 deletions tests/client-hydratable/__snapshots__/webpack5.test.ts.snap

Large diffs are not rendered by default.

80 changes: 40 additions & 40 deletions tests/client/__snapshots__/esm.test.ts.snap

Large diffs are not rendered by default.

80 changes: 40 additions & 40 deletions tests/client/__snapshots__/rspack-esm.test.ts.snap

Large diffs are not rendered by default.

80 changes: 40 additions & 40 deletions tests/client/__snapshots__/standard.test.ts.snap

Large diffs are not rendered by default.

80 changes: 40 additions & 40 deletions tests/client/__snapshots__/vite.test.ts.snap

Large diffs are not rendered by default.

80 changes: 40 additions & 40 deletions tests/client/__snapshots__/webpack5.test.ts.snap

Large diffs are not rendered by default.

80 changes: 40 additions & 40 deletions tests/server-hydratable/__snapshots__/esm.test.ts.snap

Large diffs are not rendered by default.

80 changes: 40 additions & 40 deletions tests/server-hydratable/__snapshots__/rspack-esm.test.ts.snap

Large diffs are not rendered by default.

80 changes: 40 additions & 40 deletions tests/server-hydratable/__snapshots__/standard.test.ts.snap

Large diffs are not rendered by default.

80 changes: 40 additions & 40 deletions tests/server-hydratable/__snapshots__/vite.test.ts.snap

Large diffs are not rendered by default.

80 changes: 40 additions & 40 deletions tests/server-hydratable/__snapshots__/webpack5.test.ts.snap

Large diffs are not rendered by default.

80 changes: 40 additions & 40 deletions tests/server/__snapshots__/esm.test.ts.snap

Large diffs are not rendered by default.

80 changes: 40 additions & 40 deletions tests/server/__snapshots__/rspack-esm.test.ts.snap

Large diffs are not rendered by default.

80 changes: 40 additions & 40 deletions tests/server/__snapshots__/standard.test.ts.snap

Large diffs are not rendered by default.

80 changes: 40 additions & 40 deletions tests/server/__snapshots__/vite.test.ts.snap

Large diffs are not rendered by default.

80 changes: 40 additions & 40 deletions tests/server/__snapshots__/webpack5.test.ts.snap

Large diffs are not rendered by default.

0 comments on commit c791b96

Please sign in to comment.