Skip to content

Commit 6ef9ff1

Browse files
committed
Fix undefined literal
1 parent f0706e7 commit 6ef9ff1

File tree

8 files changed

+479
-484
lines changed

8 files changed

+479
-484
lines changed

packages/core/babel/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import * as t from '@babel/types';
2+
3+
export const UNDEFINED = t.unaryExpression('void', t.numericLiteral(0));

packages/core/babel/core/deferred-variable.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import * as t from '@babel/types';
33
import { accessorVariable } from './accessor-variable';
44
import { getImportIdentifier } from './get-import-identifier';
55
import type { State } from './types';
6+
import { UNDEFINED } from '../constants';
67

78
export function deferredVariable(
89
state: State,
910
path: babel.NodePath,
1011
deferredIdentifier: t.Identifier,
11-
stateIdentifier: t.Expression = t.identifier('undefined'),
12+
stateIdentifier: t.Expression = UNDEFINED,
1213
optionsIdentifier: t.Expression | undefined = undefined,
1314
): t.VariableDeclarator {
1415
const normalIdentifier = t.arrowFunctionExpression([], stateIdentifier);

packages/core/babel/core/destructure-variable.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { getImportIdentifier } from './get-import-identifier';
77
import { isStatic } from './is-static';
88
import type { State } from './types';
99
import { unwrapNode } from './unwrap-node';
10+
import { UNDEFINED } from '../constants';
1011

1112
export function destructureVariable(
1213
state: State,
@@ -63,11 +64,7 @@ export function destructureVariable(
6364
]),
6465
t.returnStatement(
6566
t.conditionalExpression(
66-
t.binaryExpression(
67-
'===',
68-
valueIdentifier,
69-
t.identifier('undefined'),
70-
),
67+
t.binaryExpression('===', valueIdentifier, UNDEFINED),
7168
isStaticValue
7269
? defaultIdentifier
7370
: t.callExpression(defaultIdentifier, []),
@@ -168,11 +165,7 @@ export function destructureVariable(
168165
]),
169166
t.returnStatement(
170167
t.conditionalExpression(
171-
t.binaryExpression(
172-
'===',
173-
valueIdentifier,
174-
t.identifier('undefined'),
175-
),
168+
t.binaryExpression('===', valueIdentifier, UNDEFINED),
176169
isStaticValue
177170
? defaultIdentifier
178171
: t.callExpression(defaultIdentifier, []),

packages/core/babel/core/memo-variable.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as t from '@babel/types';
33
import { accessorVariable } from './accessor-variable';
44
import { getImportIdentifier } from './get-import-identifier';
55
import type { State } from './types';
6+
import { UNDEFINED } from '../constants';
67

78
export function memoVariable(
89
state: State,
@@ -20,7 +21,7 @@ export function memoVariable(
2021
const exprs: t.Expression[] = [normalIdentifier];
2122

2223
if (state.opts.dev) {
23-
exprs.push(t.identifier('undefined'));
24+
exprs.push(UNDEFINED);
2425
if (optionsIdentifier) {
2526
exprs.push(
2627
t.callExpression(
@@ -47,7 +48,7 @@ export function memoVariable(
4748
);
4849
}
4950
} else if (optionsIdentifier) {
50-
exprs.push(t.identifier('undefined'));
51+
exprs.push(UNDEFINED);
5152
exprs.push(optionsIdentifier);
5253
}
5354

packages/core/babel/transform-comment.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { getImportIdentifier } from './core/get-import-identifier';
77
import { memoVariable } from './core/memo-variable';
88
import { signalVariable } from './core/signal-variable';
99
import type { State } from './core/types';
10+
import { UNDEFINED } from './constants';
1011

1112
const VARIABLE_LABEL = {
1213
'@signal': true,
@@ -85,7 +86,7 @@ const COMMENT_TRAVERSE: babel.Visitor<State> = {
8586
state,
8687
path,
8788
declarator.id,
88-
declarator.init ?? t.identifier('undefined'),
89+
declarator.init ?? UNDEFINED,
8990
),
9091
);
9192
}
@@ -97,7 +98,7 @@ const COMMENT_TRAVERSE: babel.Visitor<State> = {
9798
state,
9899
path,
99100
declarator.id,
100-
declarator.init ?? t.identifier('undefined'),
101+
declarator.init ?? UNDEFINED,
101102
),
102103
);
103104
}
@@ -109,7 +110,7 @@ const COMMENT_TRAVERSE: babel.Visitor<State> = {
109110
state,
110111
path,
111112
declarator.id,
112-
declarator.init ?? t.identifier('undefined'),
113+
declarator.init ?? UNDEFINED,
113114
),
114115
);
115116
}
@@ -141,7 +142,7 @@ const COMMENT_TRAVERSE: babel.Visitor<State> = {
141142
[
142143
t.arrowFunctionExpression(
143144
[],
144-
declarator.init ?? t.identifier('undefined'),
145+
declarator.init ?? UNDEFINED,
145146
),
146147
],
147148
),
@@ -173,7 +174,7 @@ const COMMENT_TRAVERSE: babel.Visitor<State> = {
173174
const args: t.Expression[] = [callback];
174175
if (named && nameOption) {
175176
args.push(
176-
t.identifier('undefined'),
177+
t.unaryExpression('void', t.numericLiteral(0)),
177178
t.objectExpression([
178179
t.objectProperty(
179180
t.identifier('name'),
@@ -204,7 +205,7 @@ const COMMENT_TRAVERSE: babel.Visitor<State> = {
204205
const args: t.Expression[] = [callback];
205206
if (named && nameOption) {
206207
args.push(
207-
t.identifier('undefined'),
208+
UNDEFINED,
208209
t.objectExpression([
209210
t.objectProperty(
210211
t.identifier('name'),

packages/core/babel/transform-ctf.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { memoVariable } from './core/memo-variable';
1313
import { signalVariable } from './core/signal-variable';
1414
import type { State } from './core/types';
1515
import { unwrapNode } from './core/unwrap-node';
16+
import { UNDEFINED } from './constants';
1617

1718
type AutoArrowCTF = [name: string, source: string, arguments: number];
1819

@@ -239,13 +240,7 @@ const CTF_TRAVERSE: babel.Visitor<State> = {
239240
}
240241
}
241242
path.replaceWith(
242-
signalVariable(
243-
state,
244-
path,
245-
id,
246-
argument || t.identifier('undefined'),
247-
options,
248-
),
243+
signalVariable(state, path, id, argument || UNDEFINED, options),
249244
);
250245
} else if (
251246
trueCallee.name === MEMO_CTF ||

packages/core/babel/transform-label.ts

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { getImportIdentifier } from './core/get-import-identifier';
99
import { memoVariable } from './core/memo-variable';
1010
import { signalVariable } from './core/signal-variable';
1111
import type { State } from './core/types';
12+
import { UNDEFINED } from './constants';
1213

1314
const REACTIVE_LABEL = '$';
1415

@@ -70,22 +71,12 @@ function transformDeclaratorFromVariableLabel(
7071
): t.VariableDeclarator[] {
7172
if (labelName === 'signal' && t.isIdentifier(declarator.id)) {
7273
return [
73-
signalVariable(
74-
state,
75-
path,
76-
declarator.id,
77-
declarator.init ?? t.identifier('undefined'),
78-
),
74+
signalVariable(state, path, declarator.id, declarator.init ?? UNDEFINED),
7975
];
8076
}
8177
if (labelName === 'memo' && t.isIdentifier(declarator.id)) {
8278
return [
83-
memoVariable(
84-
state,
85-
path,
86-
declarator.id,
87-
declarator.init ?? t.identifier('undefined'),
88-
),
79+
memoVariable(state, path, declarator.id, declarator.init ?? UNDEFINED),
8980
];
9081
}
9182
if (labelName === 'deferred' && t.isIdentifier(declarator.id)) {
@@ -94,7 +85,7 @@ function transformDeclaratorFromVariableLabel(
9485
state,
9586
path,
9687
declarator.id,
97-
declarator.init ?? t.identifier('undefined'),
88+
declarator.init ?? UNDEFINED,
9889
),
9990
];
10091
}
@@ -111,12 +102,7 @@ function transformDeclaratorFromVariableLabel(
111102
path,
112103
declarator.id,
113104
getImportIdentifier(state, path, 'children', 'solid-js'),
114-
[
115-
t.arrowFunctionExpression(
116-
[],
117-
declarator.init ?? t.identifier('undefined'),
118-
),
119-
],
105+
[t.arrowFunctionExpression([], declarator.init ?? UNDEFINED)],
120106
),
121107
];
122108
}
@@ -179,7 +165,7 @@ function transformCallbackLabel(
179165
const args: t.Expression[] = [callback];
180166
if (named && nameOption) {
181167
args.push(
182-
t.identifier('undefined'),
168+
UNDEFINED,
183169
t.objectExpression([
184170
t.objectProperty(t.identifier('name'), t.stringLiteral(nameOption)),
185171
]),

0 commit comments

Comments
 (0)