Skip to content

Commit

Permalink
Minor restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
lxsmnsyc committed Feb 2, 2024
1 parent 864be60 commit 2f39a15
Show file tree
Hide file tree
Showing 19 changed files with 175 additions and 188 deletions.
22 changes: 12 additions & 10 deletions packages/forgetti/src/core/expand-expressions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as t from '@babel/types';
import type * as babel from '@babel/core';
import * as t from '@babel/types';
import { isPathValid } from './utils/checks';
import { getHookCallType } from './utils/get-hook-call-type';
import type { ComponentNode, StateContext } from './types';
import { getHookCallType } from './get-hook-call-type';
import { isPathValid } from './checks';
import unwrapNode from './unwrap-node';
import unwrapNode from './utils/unwrap-node';

function isStatementValid(path: babel.NodePath): boolean {
if (path) {
Expand Down Expand Up @@ -170,8 +170,10 @@ export function expandExpressions(
!isPathValid(p.parentPath, t.isVariableDeclarator)
) {
const id = p.scope.generateUidIdentifier('hoisted');
statement.insertBefore(
t.variableDeclaration('let', [t.variableDeclarator(id, p.node)]),
statement.scope.registerDeclaration(
statement.insertBefore(
t.variableDeclaration('let', [t.variableDeclarator(id, p.node)]),
)[0],
);
p.replaceWith(id);
}
Expand All @@ -189,14 +191,14 @@ export function expandExpressions(
const hookType = getHookCallType(ctx, p);
if (hookType === 'custom' || hookType === 'effect') {
const id = p.scope.generateUidIdentifier('hoisted');
statement.insertBefore(
t.variableDeclaration('let', [t.variableDeclarator(id, p.node)]),
statement.scope.registerDeclaration(
statement.insertBefore(
t.variableDeclaration('let', [t.variableDeclarator(id, p.node)]),
)[0],
);
p.replaceWith(id);
}
}
},
});

path.scope.crawl();
}
2 changes: 1 addition & 1 deletion packages/forgetti/src/core/inline-expressions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type * as babel from '@babel/core';
import * as t from '@babel/types';
import { isPathValid } from './utils/checks';
import type { ComponentNode } from './types';
import { isPathValid } from './checks';

function isInValidExpression(path: babel.NodePath): boolean {
let current = path.parentPath;
Expand Down
13 changes: 10 additions & 3 deletions packages/forgetti/src/core/is-constant.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import type * as babel from '@babel/core';
import * as t from '@babel/types';
import { isNestedExpression, isPathValid, shouldSkipNode } from './checks';
import getForeignBindings, { isForeignBinding } from './get-foreign-bindings';
import { getHookCallType } from './get-hook-call-type';
import type OptimizerScope from './optimizer-scope';
import type { ComponentNode, StateContext } from './types';
import {
isNestedExpression,
isPathValid,
shouldSkipNode,
} from './utils/checks';
import {
getForeignBindings,
isForeignBinding,
} from './utils/get-foreign-bindings';
import { getHookCallType } from './utils/get-hook-call-type';

interface OptimizerInstance {
ctx: StateContext;
Expand Down
50 changes: 23 additions & 27 deletions packages/forgetti/src/core/optimize-jsx.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type * as babel from '@babel/core';
import * as t from '@babel/types';
import type { ComponentNode, StateContext } from './types';
import getImportIdentifier from './get-import-identifier';
import { isPathValid, shouldSkipJSX } from './utils/checks';
import { getDescriptiveName } from './utils/get-descriptive-name';
import { getImportIdentifier } from './utils/get-import-identifier';
import { getRootStatementPath } from './utils/get-root-statement-path';
import { RUNTIME_MEMO } from './imports';
import { shouldSkipJSX, isPathValid } from './checks';
import type { ImportDefinition } from './presets';
import { getDescriptiveName } from './get-descriptive-name';
import type { ComponentNode, StateContext } from './types';

interface JSXReplacement {
id: t.Identifier;
Expand Down Expand Up @@ -46,19 +47,7 @@ function extractJSXExpressionFromNormalAttribute(
extractJSXExpressions(value, state, false);
}
if (isPathValid(value, t.isJSXExpressionContainer)) {
const expr = value.get('expression');
if (
isPathValid(expr, t.isJSXElement) ||
isPathValid(expr, t.isJSXFragment)
) {
extractJSXExpressions(expr, state, false);
} else if (isPathValid(expr, t.isExpression)) {
const id = state.expressions.length;
state.expressions.push(expr.node);
expr.replaceWith(
t.memberExpression(state.source, t.numericLiteral(id), true),
);
}
extractJSXExpressionsFromJSXExpressionContainer(state, value);
}
}

Expand Down Expand Up @@ -240,15 +229,23 @@ function transformJSX(
} else {
body = path.node;
}
path.scope.getProgramParent().push({
kind: 'const',
id: memoComponent,
init: t.callExpression(getImportIdentifier(ctx, path, RUNTIME_MEMO), [
getImportIdentifier(ctx, path, memoDefinition),
t.stringLiteral(memoComponent.name),
t.arrowFunctionExpression([state.source], body),
]),
});

const root = getRootStatementPath(path);

root.scope.registerDeclaration(
root.insertBefore(
t.variableDeclaration('const', [
t.variableDeclarator(
memoComponent,
t.callExpression(getImportIdentifier(ctx, path, RUNTIME_MEMO), [
getImportIdentifier(ctx, path, memoDefinition),
t.stringLiteral(memoComponent.name),
t.arrowFunctionExpression([state.source], body),
]),
),
]),
)[0],
);

const attrs = [];

Expand Down Expand Up @@ -297,6 +294,5 @@ export default function optimizeJSX(
transformJSX(ctx, p, memoDefinition);
},
});
path.scope.crawl();
}
}
14 changes: 10 additions & 4 deletions packages/forgetti/src/core/optimizer-scope.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import type * as babel from '@babel/core';
import * as t from '@babel/types';
import type { OptimizedExpression, StateContext } from './types';
import getImportIdentifier from './get-import-identifier';
import { RUNTIME_BRANCH, RUNTIME_CACHE, RUNTIME_REF } from './imports';
import type { OptimizedExpression, StateContext } from './types';
import { getImportIdentifier } from './utils/get-import-identifier';

const ArrayPrototypePush = Array.prototype.push;

function mergeVariableDeclaration(statements: t.Statement[]): t.Statement[] {
let stack: t.VariableDeclarator[] = [];
const newStatements: t.Statement[] = [];
let value: t.Statement;
for (let i = 0, len = statements.length; i < len; i++) {
value = statements[i];
if (value.type === 'VariableDeclaration' && value.kind === 'let') {
stack.push(...value.declarations);
if (t.isVariableDeclaration(value) && value.kind === 'let') {
ArrayPrototypePush.apply(stack, value.declarations);
} else {
if (stack.length) {
newStatements.push(t.variableDeclaration('let', stack));
Expand All @@ -24,12 +26,16 @@ function mergeVariableDeclaration(statements: t.Statement[]): t.Statement[] {
}

export default class OptimizerScope {
// Reference to the root memo
memo: t.Identifier | undefined;

// Reference to the root ref
ref: t.Identifier | undefined;

// Size of memo
indecesMemo = 0;

// Size of ref
indecesRef = 0;

ctx: StateContext;
Expand Down
Loading

0 comments on commit 2f39a15

Please sign in to comment.