-
-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathindex.ts
More file actions
744 lines (664 loc) · 20.9 KB
/
Copy pathindex.ts
File metadata and controls
744 lines (664 loc) · 20.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
import {
types as BabelTypes,
template as BabelTemplate,
PluginObj,
PluginPass,
NodePath,
template,
} from "@babel/core";
import { isModule, addNamed } from "@babel/helper-module-imports";
import type { Scope, VisitNodeObject } from "@babel/traverse";
import debug from "debug";
interface PluginArgs {
types: typeof BabelTypes;
template: typeof BabelTemplate;
}
const optOutCommentIdentifier = /(^|\s)@no(Use|Track)Signals(\s|$)/;
const optInCommentIdentifier = /(^|\s)@(use|track)Signals(\s|$)/;
const dataNamespace = "@preact/signals-react-transform";
const defaultImportSource = "@preact/signals-react/runtime";
const importName = "useSignals";
const getHookIdentifier = "getHookIdentifier";
const maybeUsesSignal = "maybeUsesSignal";
const containsJSX = "containsJSX";
const alreadyTransformed = "alreadyTransformed";
const jsxIdentifiers = "jsxIdentifiers";
const jsxObjects = "jsxObjects";
const UNMANAGED = "0";
const MANAGED_COMPONENT = "1";
const MANAGED_HOOK = "2";
type HookUsage =
| typeof UNMANAGED
| typeof MANAGED_COMPONENT
| typeof MANAGED_HOOK;
const logger = {
transformed: debug("signals:react-transform:transformed"),
skipped: debug("signals:react-transform:skipped"),
};
const get = (pass: PluginPass, name: any) =>
pass.get(`${dataNamespace}/${name}`);
const set = (pass: PluginPass, name: string, v: any) =>
pass.set(`${dataNamespace}/${name}`, v);
interface DataContainer {
getData(name: string): any;
setData(name: string, value: any): void;
}
const setData = (node: DataContainer, name: string, value: any) =>
node.setData(`${dataNamespace}/${name}`, value);
const getData = (node: DataContainer, name: string) =>
node.getData(`${dataNamespace}/${name}`);
function getComponentFunctionDeclaration(
path: NodePath,
filename: string | undefined,
prev?: Scope
): Scope | null {
const functionScope = path.scope.getFunctionParent();
if (functionScope) {
const parent = functionScope.path.parent;
let functionName = getFunctionName(functionScope.path as any);
if (functionName === DefaultExportSymbol) {
functionName = filename || null;
}
if (isComponentFunction(functionScope.path as any, functionName)) {
return functionScope;
} else if (
parent.type === "CallExpression" &&
parent.callee.type === "Identifier" &&
parent.callee.name.startsWith("use") &&
parent.callee.name[3] === parent.callee.name[3].toUpperCase()
) {
return null;
}
return getComponentFunctionDeclaration(
functionScope.parent.path,
filename,
functionScope
);
} else {
return prev || null;
}
}
function setOnFunctionScope(
path: NodePath,
key: string,
value: any,
filename: string | undefined
) {
const functionScope = getComponentFunctionDeclaration(path, filename);
if (functionScope) {
setData(functionScope, key, value);
}
}
type FunctionLike =
| BabelTypes.ArrowFunctionExpression
| BabelTypes.FunctionExpression
| BabelTypes.FunctionDeclaration
| BabelTypes.ObjectMethod;
/**
* Simple "best effort" to get the base name of a file path. Not fool proof but
* works in browsers and servers. Good enough for our purposes.
*/
function basename(filename: string | undefined): string | undefined {
return filename?.split(/[\\/]/).pop();
}
const DefaultExportSymbol = Symbol("DefaultExportSymbol");
function getObjectPropertyKey(
node: BabelTypes.ObjectProperty | BabelTypes.ObjectMethod
): string | null {
if (node.key.type === "Identifier") {
return node.key.name;
} else if (node.key.type === "StringLiteral") {
return node.key.value;
}
return null;
}
/**
* If the function node has a name (i.e. is a function declaration with a
* name), return that. Else return null.
*/
function getFunctionNodeName(node: FunctionLike): string | null {
if (
(node.type === "FunctionDeclaration" ||
node.type === "FunctionExpression") &&
node.id
) {
return node.id.name;
} else if (node.type === "ObjectMethod") {
return getObjectPropertyKey(node);
}
return null;
}
/**
* Given a function path's parent path, determine the "name" associated with the
* function. If the function is an inline default export (e.g. `export default
* () => {}`), returns a symbol indicating it is a default export. If the
* function is an anonymous function wrapped in higher order functions (e.g.
* memo(() => {})) we'll climb through the higher order functions to find the
* name of the variable that the function is assigned to, if any. Other cases
* handled too (see implementation). Else returns null.
*/
function getFunctionNameFromParent(
parentPath: NodePath<BabelTypes.Node>
): string | null | typeof DefaultExportSymbol {
if (
parentPath.node.type === "VariableDeclarator" &&
parentPath.node.id.type === "Identifier"
) {
return parentPath.node.id.name;
} else if (parentPath.node.type === "AssignmentExpression") {
const left = parentPath.node.left;
if (left.type === "Identifier") {
return left.name;
} else if (left.type === "MemberExpression") {
let property = left.property;
while (property.type === "MemberExpression") {
property = property.property;
}
if (property.type === "Identifier") {
return property.name;
} else if (property.type === "StringLiteral") {
return property.value;
}
return null;
} else {
return null;
}
} else if (parentPath.node.type === "ObjectProperty") {
return getObjectPropertyKey(parentPath.node);
} else if (parentPath.node.type === "ExportDefaultDeclaration") {
return DefaultExportSymbol;
} else if (
parentPath.node.type === "CallExpression" &&
parentPath.parentPath != null
) {
// If our parent is a Call Expression, then this function expression is
// wrapped in some higher order functions. Recurse through the higher order
// functions to determine if this expression is assigned to a name we can
// use as the function name
return getFunctionNameFromParent(parentPath.parentPath);
} else {
return null;
}
}
/* Determine the name of a function */
function getFunctionName(
path: NodePath<FunctionLike>
): string | typeof DefaultExportSymbol | null {
let nodeName = getFunctionNodeName(path.node);
if (nodeName) {
return nodeName;
}
return getFunctionNameFromParent(path.parentPath);
}
function isComponentName(name: string | null): boolean {
return name?.match(/^[A-Z]/) != null;
}
function isCustomHookName(name: string | null): boolean {
return name?.match(/^use[A-Z]/) != null;
}
function hasLeadingComment(path: NodePath, comment: RegExp): boolean {
const comments = path.node.leadingComments;
return comments?.some(c => c.value.match(comment) !== null) ?? false;
}
function hasLeadingOptInComment(path: NodePath) {
return hasLeadingComment(path, optInCommentIdentifier);
}
function hasLeadingOptOutComment(path: NodePath) {
return hasLeadingComment(path, optOutCommentIdentifier);
}
function isOptedIntoSignalTracking(path: NodePath | null): boolean {
if (!path) return false;
switch (path.node.type) {
case "ArrowFunctionExpression":
case "FunctionExpression":
case "FunctionDeclaration":
case "ObjectMethod":
case "ObjectExpression":
case "VariableDeclarator":
case "VariableDeclaration":
case "AssignmentExpression":
case "CallExpression":
return (
hasLeadingOptInComment(path) ||
isOptedIntoSignalTracking(path.parentPath)
);
case "ExportDefaultDeclaration":
case "ExportNamedDeclaration":
case "ObjectProperty":
case "ExpressionStatement":
return hasLeadingOptInComment(path);
default:
return false;
}
}
function isOptedOutOfSignalTracking(path: NodePath | null): boolean {
if (!path) return false;
switch (path.node.type) {
case "ArrowFunctionExpression":
case "FunctionExpression":
case "FunctionDeclaration":
case "ObjectMethod":
case "ObjectExpression":
case "VariableDeclarator":
case "VariableDeclaration":
case "AssignmentExpression":
case "CallExpression":
return (
hasLeadingOptOutComment(path) ||
isOptedOutOfSignalTracking(path.parentPath)
);
case "ExportDefaultDeclaration":
case "ExportNamedDeclaration":
case "ObjectProperty":
case "ExpressionStatement":
return hasLeadingOptOutComment(path);
default:
return false;
}
}
function isComponentFunction(
path: NodePath<FunctionLike>,
functionName: string | null
): boolean {
return (
getData(path.scope, containsJSX) === true && // Function contains JSX
isComponentName(functionName) // Function name indicates it's a component
);
}
function shouldTransform(
path: NodePath<FunctionLike>,
functionName: string | null,
options: PluginOptions
): boolean {
// Opt-out takes first precedence
if (isOptedOutOfSignalTracking(path)) return false;
// Opt-in opts in to transformation regardless of mode
if (isOptedIntoSignalTracking(path)) return true;
if (options.mode === "all") {
return isComponentFunction(path, functionName);
}
if (options.mode == null || options.mode === "auto") {
return (
getData(path.scope, maybeUsesSignal) === true && // Function appears to use signals;
(isComponentFunction(path, functionName) ||
isCustomHookName(functionName))
);
}
return false;
}
function isValueMemberExpression(
path: NodePath<BabelTypes.MemberExpression>
): boolean {
return (
(path.node.property.type === "Identifier" &&
path.node.property.name === "value") ||
(path.node.property.type === "StringLiteral" &&
path.node.property.value === "value")
);
}
function isJSXAlternativeCall(
path: NodePath<BabelTypes.CallExpression>,
state: PluginPass
): boolean {
const jsxIdentifierSet = get(state, jsxIdentifiers) as Set<string>;
const jsxObjectMap = get(state, jsxObjects) as Map<string, string[]>;
const callee = path.get("callee");
// Check direct function calls like _jsx("div", props) or createElement("div", props)
if (callee.isIdentifier()) {
return jsxIdentifierSet?.has(callee.node.name) ?? false;
}
// Check member expression calls like React.createElement("div", props) or jsxRuntime.jsx("div", props)
if (callee.isMemberExpression()) {
const object = callee.get("object");
const property = callee.get("property");
if (object.isIdentifier() && property.isIdentifier()) {
const objectName = object.node.name;
const methodName = property.node.name;
const allowedMethods = jsxObjectMap?.get(objectName);
return allowedMethods?.includes(methodName) ?? false;
}
}
return false;
}
function hasValuePropertyInPattern(pattern: BabelTypes.ObjectPattern): boolean {
for (const property of pattern.properties) {
if (BabelTypes.isObjectProperty(property)) {
const key = property.key;
if (BabelTypes.isIdentifier(key, { name: "value" })) {
return true;
}
}
}
return false;
}
const tryCatchTemplate = template.statements`var STORE_IDENTIFIER = HOOK_IDENTIFIER(HOOK_USAGE);
try {
BODY
} finally {
STORE_IDENTIFIER.f();
}`;
function wrapInTryFinally(
t: typeof BabelTypes,
path: NodePath<FunctionLike>,
state: PluginPass,
hookUsage: HookUsage
): BabelTypes.BlockStatement {
const stopTrackingIdentifier = path.scope.generateUidIdentifier("effect");
return t.blockStatement(
tryCatchTemplate({
STORE_IDENTIFIER: stopTrackingIdentifier,
HOOK_IDENTIFIER: get(state, getHookIdentifier)(),
HOOK_USAGE: hookUsage,
BODY: t.isBlockStatement(path.node.body)
? path.node.body.body // TODO: Is it okay to elide the block statement here?
: t.returnStatement(path.node.body),
})
);
}
function prependUseSignals<T extends FunctionLike>(
t: typeof BabelTypes,
path: NodePath<T>,
state: PluginPass
): BabelTypes.BlockStatement {
const body = t.blockStatement([
t.expressionStatement(
t.callExpression(get(state, getHookIdentifier)(), [])
),
]);
if (t.isBlockStatement(path.node.body)) {
// TODO: Is it okay to elide the block statement here?
body.body.push(...path.node.body.body);
} else {
body.body.push(t.returnStatement(path.node.body));
}
return body;
}
function transformFunction(
t: typeof BabelTypes,
options: PluginOptions,
path: NodePath<FunctionLike>,
functionName: string | null,
state: PluginPass
) {
const isHook = isCustomHookName(functionName);
const isComponent = isComponentName(functionName);
const hookUsage = options.experimental?.noTryFinally
? UNMANAGED
: isHook
? MANAGED_HOOK
: isComponent
? MANAGED_COMPONENT
: UNMANAGED;
let newBody: BabelTypes.BlockStatement;
if (hookUsage !== UNMANAGED) {
newBody = wrapInTryFinally(t, path, state, hookUsage);
} else {
newBody = prependUseSignals(t, path, state);
}
setData(path, alreadyTransformed, true);
path.get("body").replaceWith(newBody);
}
function createImportLazily(
types: typeof BabelTypes,
pass: PluginPass,
path: NodePath<BabelTypes.Program>,
importName: string,
source: string
): () => BabelTypes.Identifier {
return () => {
if (isModule(path)) {
let reference: BabelTypes.Identifier = get(pass, `imports/${importName}`);
if (reference) return types.cloneNode(reference);
reference = addNamed(path, importName, source, {
importedInterop: "uncompiled",
importPosition: "after",
});
set(pass, `imports/${importName}`, reference);
/** Helper function to determine if an import declaration's specifier matches the given importName */
const matchesImportName = (
s: BabelTypes.ImportDeclaration["specifiers"][0]
) => {
if (s.type !== "ImportSpecifier") return false;
return (
(s.imported.type === "Identifier" &&
s.imported.name === importName) ||
(s.imported.type === "StringLiteral" &&
s.imported.value === importName)
);
};
for (let statement of path.get("body")) {
if (
statement.isImportDeclaration() &&
statement.node.source.value === source &&
statement.node.specifiers.some(matchesImportName)
) {
path.scope.registerDeclaration(statement);
break;
}
}
return reference;
} else {
// This code originates from
// https://github.com/XantreDev/preact-signals/blob/%40preact-signals/safe-react%400.6.1/packages/react/src/babel.ts#L390-L400
let reference = get(pass, `requires/${importName}`);
if (reference) {
reference = types.cloneNode(reference);
} else {
reference = addNamed(path, importName, source, {
importedInterop: "uncompiled",
});
set(pass, `requires/${importName}`, reference);
}
return reference;
}
};
}
function detectJSXAlternativeImports(
path: NodePath<BabelTypes.Program>,
state: PluginPass
) {
const jsxIdentifierSet = new Set<string>();
const jsxObjectMap = new Map<string, string[]>();
const jsxPackages = {
"react/jsx-runtime": ["jsx", "jsxs"],
"react/jsx-dev-runtime": ["jsxDEV"],
react: ["createElement"],
};
path.traverse({
ImportDeclaration(importPath) {
const packageName = importPath.node.source.value;
const jsxMethods = jsxPackages[packageName as keyof typeof jsxPackages];
if (!jsxMethods) {
return;
}
for (const specifier of importPath.node.specifiers) {
if (
specifier.type === "ImportSpecifier" &&
specifier.imported.type === "Identifier"
) {
// Check if this is a function we care about
if (jsxMethods.includes(specifier.imported.name)) {
jsxIdentifierSet.add(specifier.local.name);
}
} else if (specifier.type === "ImportDefaultSpecifier") {
// Handle default imports - add to objects map for member access
jsxObjectMap.set(specifier.local.name, jsxMethods);
}
}
},
VariableDeclarator(varPath) {
const init = varPath.get("init");
if (init.isCallExpression()) {
const callee = init.get("callee");
const args = init.get("arguments");
if (
callee.isIdentifier() &&
callee.node.type === "Identifier" &&
callee.node.name === "require" &&
args.length > 0 &&
args[0].isStringLiteral()
) {
const packageName = args[0].node.value;
const jsxMethods =
jsxPackages[packageName as keyof typeof jsxPackages];
if (jsxMethods) {
if (varPath.node.id.type === "Identifier") {
// Handle CJS require like: const React = require("react")
jsxObjectMap.set(varPath.node.id.name, jsxMethods);
} else if (varPath.node.id.type === "ObjectPattern") {
// Handle destructured CJS require like: const { createElement } = require("react")
for (const prop of varPath.node.id.properties) {
if (
prop.type === "ObjectProperty" &&
prop.key.type === "Identifier" &&
prop.value.type === "Identifier" &&
jsxMethods.includes(prop.key.name)
) {
jsxIdentifierSet.add(prop.value.name);
}
}
}
}
}
}
},
});
set(state, jsxIdentifiers, jsxIdentifierSet);
set(state, jsxObjects, jsxObjectMap);
}
export interface PluginOptions {
/**
* Specify the mode to use:
* - `auto`: Automatically wrap all components that use signals.
* - `manual`: Only wrap components that are annotated with `@useSignals` in a JSX comment.
* - `all`: Makes all components reactive to signals.
*/
mode?: "auto" | "manual" | "all";
/** Specify a custom package to import the `useSignals` hook from. */
importSource?: string;
/**
* Detect JSX elements created using alternative methods like jsx-runtime or createElement calls.
* When enabled, detects patterns from react/jsx-runtime and react packages.
* @default false
*/
detectTransformedJSX?: boolean;
experimental?: {
/**
* If set to true, the component body will not be wrapped in a try/finally
* block and instead the next component render or a microtick will stop
* tracking signals for this component. This is an experimental feature and
* may be removed in the future.
* @default false
*/
noTryFinally?: boolean;
};
}
function log(
transformed: boolean,
path: NodePath<FunctionLike>,
functionName: string | null,
currentFile: string | undefined
) {
if (!logger.transformed.enabled && !logger.skipped.enabled) return;
let cwd = "";
if (typeof process !== undefined && typeof process.cwd == "function") {
cwd = process.cwd().replace(/\\([^ ])/g, "/$1");
cwd = cwd.endsWith("/") ? cwd : cwd + "/";
}
const relativePath = currentFile?.replace(cwd, "") ?? "";
const lineNum = path.node.loc?.start.line;
functionName = functionName ?? "<anonymous>";
if (transformed) {
logger.transformed(`${functionName} (${relativePath}:${lineNum})`);
} else {
logger.skipped(`${functionName} (${relativePath}:${lineNum}) %o`, {
hasSignals: getData(path.scope, maybeUsesSignal) ?? false,
hasJSX: getData(path.scope, containsJSX) ?? false,
});
}
}
function isComponentLike(
path: NodePath<FunctionLike>,
functionName: string | null
): boolean {
return !getData(path, alreadyTransformed) && isComponentName(functionName);
}
export default function signalsTransform(
{ types: t }: PluginArgs,
options: PluginOptions
): PluginObj {
// TODO: Consider alternate implementation, where on enter of a function
// expression, we run our own manual scan the AST to determine if the
// function uses signals and is a component. This manual scan once upon
// seeing a function would probably be faster than running an entire
// babel pass with plugins on components twice.
const visitFunction: VisitNodeObject<PluginPass, FunctionLike> = {
exit(path, state) {
if (getData(path, alreadyTransformed) === true) return false;
let functionName = getFunctionName(path);
if (functionName === DefaultExportSymbol) {
functionName = basename(this.filename) ?? null;
}
if (shouldTransform(path, functionName, state.opts)) {
transformFunction(t, state.opts, path, functionName, state);
log(true, path, functionName, this.filename);
} else if (isComponentLike(path, functionName)) {
log(false, path, functionName, this.filename);
}
},
};
return {
name: "@preact/signals-transform",
visitor: {
Program: {
enter(path, state) {
// Following the pattern of babel-plugin-transform-react-jsx, we
// lazily create the import statement for the useSignalTracking hook.
// We create a function and store it in the PluginPass object, so that
// on the first usage of the hook, we can create the import statement.
set(
state,
getHookIdentifier,
createImportLazily(
t,
state,
path,
importName,
options.importSource ?? defaultImportSource
)
);
if (options.detectTransformedJSX) {
detectJSXAlternativeImports(path, state);
}
},
},
ArrowFunctionExpression: visitFunction,
FunctionExpression: visitFunction,
FunctionDeclaration: visitFunction,
ObjectMethod: visitFunction,
CallExpression(path, state) {
if (options.detectTransformedJSX) {
if (isJSXAlternativeCall(path, state)) {
setOnFunctionScope(path, containsJSX, true, this.filename);
}
}
},
MemberExpression(path) {
if (isValueMemberExpression(path)) {
setOnFunctionScope(path, maybeUsesSignal, true, this.filename);
}
},
ObjectPattern(path) {
if (hasValuePropertyInPattern(path.node)) {
setOnFunctionScope(path, maybeUsesSignal, true, this.filename);
}
},
JSXElement(path) {
setOnFunctionScope(path, containsJSX, true, this.filename);
},
JSXFragment(path) {
setOnFunctionScope(path, containsJSX, true, this.filename);
},
},
};
}