Skip to content

Commit

Permalink
Fix references to undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
lxsmnsyc committed Jan 27, 2024
1 parent 0429b66 commit 282ffea
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
28 changes: 10 additions & 18 deletions packages/forgetti/src/core/is-constant.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type * as babel from '@babel/core';
import * as t from '@babel/types';
import { isNestedExpression, shouldSkipNode, isPathValid } from './checks';
import type OptimizerScope from './optimizer-scope';
import { isNestedExpression, isPathValid, shouldSkipNode } from './checks';
import getForeignBindings, { isForeignBinding } from './get-foreign-bindings';
import type { ComponentNode, StateContext } from './types';
import { getHookCallType } from './get-hook-call-type';
import type OptimizerScope from './optimizer-scope';
import type { ComponentNode, StateContext } from './types';

interface OptimizerInstance {
ctx: StateContext;
Expand Down Expand Up @@ -36,22 +36,14 @@ function isIdentifierConstant(
path: babel.NodePath,
node: t.Identifier,
): boolean {
switch (node.name) {
case 'undefined':
case 'NaN':
case 'Infinity':
return true;
default: {
if (isForeignBinding(instance.path, path, node.name)) {
return true;
}
const binding = path.scope.getBindingIdentifier(node.name);
if (binding) {
return instance.scope.hasConstant(binding);
}
return true;
}
if (isForeignBinding(instance.path, path, node.name)) {
return true;
}
const binding = path.scope.getBindingIdentifier(node.name);
if (binding) {
return instance.scope.hasConstant(binding);
}
return true;
}

function isMemberExpressionConstant(
Expand Down
4 changes: 2 additions & 2 deletions packages/forgetti/src/core/simplify-expressions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as t from '@babel/types';
import type * as babel from '@babel/core';
import * as t from '@babel/types';
import type { ComponentNode } from './types';

type LiteralState = 'truthy' | 'falsy' | 'nullish' | 'indeterminate';
Expand Down Expand Up @@ -82,7 +82,7 @@ export function simplifyExpressions(path: babel.NodePath<ComponentNode>): void {
switch (p.node.operator) {
case 'void': {
if (state !== 'indeterminate') {
p.replaceWith(t.identifier('undefined'));
p.replaceWith(t.unaryExpression('void', t.numericLiteral(0)));
}
break;
}
Expand Down

0 comments on commit 282ffea

Please sign in to comment.