react-refresh 0.16.0-rc.0 → 0.16.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,853 +8,594 @@
8
8
  * LICENSE file in the root directory of this source tree.
9
9
  */
10
10
 
11
- 'use strict';
12
-
13
- if (process.env.NODE_ENV !== "production") {
14
- (function() {
15
- 'use strict';
16
-
17
- function ReactFreshBabelPlugin (babel) {
18
- var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
19
-
20
- if (typeof babel.env === 'function') {
21
- // Only available in Babel 7.
22
- var env = babel.env();
23
-
24
- if (env !== 'development' && !opts.skipEnvCheck) {
25
- throw new Error('React Refresh Babel transform should only be enabled in development environment. ' + 'Instead, the environment is: "' + env + '". If you want to override this check, pass {skipEnvCheck: true} as plugin options.');
11
+ "use strict";
12
+ "production" !== process.env.NODE_ENV &&
13
+ (module.exports = function (babel) {
14
+ function createRegistration(programPath, persistentID) {
15
+ var handle = programPath.scope.generateUidIdentifier("c");
16
+ registrationsByProgramPath.has(programPath) ||
17
+ registrationsByProgramPath.set(programPath, []);
18
+ registrationsByProgramPath
19
+ .get(programPath)
20
+ .push({ handle: handle, persistentID: persistentID });
21
+ return handle;
26
22
  }
27
- }
28
-
29
- var t = babel.types;
30
- var refreshReg = t.identifier(opts.refreshReg || '$RefreshReg$');
31
- var refreshSig = t.identifier(opts.refreshSig || '$RefreshSig$');
32
- var registrationsByProgramPath = new Map();
33
-
34
- function createRegistration(programPath, persistentID) {
35
- var handle = programPath.scope.generateUidIdentifier('c');
36
-
37
- if (!registrationsByProgramPath.has(programPath)) {
38
- registrationsByProgramPath.set(programPath, []);
23
+ function isComponentishName(name) {
24
+ return "string" === typeof name && "A" <= name[0] && "Z" >= name[0];
39
25
  }
40
-
41
- var registrations = registrationsByProgramPath.get(programPath);
42
- registrations.push({
43
- handle: handle,
44
- persistentID: persistentID
45
- });
46
- return handle;
47
- }
48
-
49
- function isComponentishName(name) {
50
- return typeof name === 'string' && name[0] >= 'A' && name[0] <= 'Z';
51
- }
52
-
53
- function findInnerComponents(inferredName, path, callback) {
54
- var node = path.node;
55
-
56
- switch (node.type) {
57
- case 'Identifier':
58
- {
59
- if (!isComponentishName(node.name)) {
60
- return false;
61
- } // export default hoc(Foo)
62
- // const X = hoc(Foo)
63
-
64
-
26
+ function findInnerComponents(inferredName, path, callback) {
27
+ var node = path.node;
28
+ switch (node.type) {
29
+ case "Identifier":
30
+ if (!isComponentishName(node.name)) break;
65
31
  callback(inferredName, node, null);
66
- return true;
67
- }
68
-
69
- case 'FunctionDeclaration':
70
- {
71
- // function Foo() {}
72
- // export function Foo() {}
73
- // export default function Foo() {}
74
- callback(inferredName, node.id, null);
75
- return true;
76
- }
77
-
78
- case 'ArrowFunctionExpression':
79
- {
80
- if (node.body.type === 'ArrowFunctionExpression') {
81
- return false;
82
- } // let Foo = () => {}
83
- // export default hoc1(hoc2(() => {}))
84
-
85
-
86
- callback(inferredName, node, path);
87
- return true;
88
- }
89
-
90
- case 'FunctionExpression':
91
- {
92
- // let Foo = function() {}
93
- // const Foo = hoc1(forwardRef(function renderFoo() {}))
94
- // export default memo(function() {})
32
+ return !0;
33
+ case "FunctionDeclaration":
34
+ return callback(inferredName, node.id, null), !0;
35
+ case "ArrowFunctionExpression":
36
+ if ("ArrowFunctionExpression" === node.body.type) break;
95
37
  callback(inferredName, node, path);
96
- return true;
97
- }
98
-
99
- case 'CallExpression':
100
- {
101
- var argsPath = path.get('arguments');
102
-
103
- if (argsPath === undefined || argsPath.length === 0) {
104
- return false;
105
- }
106
-
107
- var calleePath = path.get('callee');
108
-
38
+ return !0;
39
+ case "FunctionExpression":
40
+ return callback(inferredName, node, path), !0;
41
+ case "CallExpression":
42
+ var argsPath = path.get("arguments");
43
+ if (void 0 === argsPath || 0 === argsPath.length) break;
44
+ var calleePath = path.get("callee");
109
45
  switch (calleePath.node.type) {
110
- case 'MemberExpression':
111
- case 'Identifier':
112
- {
113
- var calleeSource = calleePath.getSource();
114
- var firstArgPath = argsPath[0];
115
- var innerName = inferredName + '$' + calleeSource;
116
- var foundInside = findInnerComponents(innerName, firstArgPath, callback);
117
-
118
- if (!foundInside) {
119
- return false;
120
- } // const Foo = hoc1(hoc2(() => {}))
121
- // export default memo(React.forwardRef(function() {}))
122
-
123
-
124
- callback(inferredName, node, path);
125
- return true;
126
- }
127
-
46
+ case "MemberExpression":
47
+ case "Identifier":
48
+ calleePath = calleePath.getSource();
49
+ if (
50
+ !findInnerComponents(
51
+ inferredName + "$" + calleePath,
52
+ argsPath[0],
53
+ callback
54
+ )
55
+ )
56
+ return !1;
57
+ callback(inferredName, node, path);
58
+ return !0;
128
59
  default:
129
- {
130
- return false;
131
- }
60
+ return !1;
132
61
  }
133
- }
134
-
135
- case 'VariableDeclarator':
136
- {
137
- var init = node.init;
138
-
139
- if (init === null) {
140
- return false;
141
- }
142
-
143
- var name = node.id.name;
144
-
145
- if (!isComponentishName(name)) {
146
- return false;
147
- }
148
-
149
- switch (init.type) {
150
- case 'ArrowFunctionExpression':
151
- case 'FunctionExpression':
152
- // Likely component definitions.
153
- break;
154
-
155
- case 'CallExpression':
156
- {
157
- // Maybe a HOC.
158
- // Try to determine if this is some form of import.
159
- var callee = init.callee;
160
- var calleeType = callee.type;
161
-
162
- if (calleeType === 'Import') {
163
- return false;
164
- } else if (calleeType === 'Identifier') {
165
- if (callee.name.indexOf('require') === 0) {
166
- return false;
167
- } else if (callee.name.indexOf('import') === 0) {
168
- return false;
169
- } // Neither require nor import. Might be a HOC.
170
- // Pass through.
171
-
172
- } else ;
173
-
62
+ case "VariableDeclarator":
63
+ if (
64
+ ((argsPath = node.init),
65
+ null !== argsPath &&
66
+ ((calleePath = node.id.name), isComponentishName(calleePath)))
67
+ ) {
68
+ switch (argsPath.type) {
69
+ case "ArrowFunctionExpression":
70
+ case "FunctionExpression":
174
71
  break;
175
- }
176
-
177
- case 'TaggedTemplateExpression':
178
- // Maybe something like styled.div`...`
179
- break;
180
-
181
- default:
182
- return false;
183
- }
184
-
185
- var initPath = path.get('init');
186
-
187
- var _foundInside = findInnerComponents(inferredName, initPath, callback);
188
-
189
- if (_foundInside) {
190
- return true;
191
- } // See if this identifier is used in JSX. Then it's a component.
192
-
193
-
194
- var binding = path.scope.getBinding(name);
195
-
196
- if (binding === undefined) {
197
- return;
198
- }
199
-
200
- var isLikelyUsedAsType = false;
201
- var referencePaths = binding.referencePaths;
202
-
203
- for (var i = 0; i < referencePaths.length; i++) {
204
- var ref = referencePaths[i];
205
-
206
- if (ref.node && ref.node.type !== 'JSXIdentifier' && ref.node.type !== 'Identifier') {
207
- continue;
72
+ case "CallExpression":
73
+ node = argsPath.callee;
74
+ var calleeType = node.type;
75
+ if (
76
+ "Import" === calleeType ||
77
+ ("Identifier" === calleeType &&
78
+ (0 === node.name.indexOf("require") ||
79
+ 0 === node.name.indexOf("import")))
80
+ )
81
+ return !1;
82
+ break;
83
+ case "TaggedTemplateExpression":
84
+ break;
85
+ default:
86
+ return !1;
208
87
  }
209
-
210
- var refParent = ref.parent;
211
-
212
- if (refParent.type === 'JSXOpeningElement') {
213
- isLikelyUsedAsType = true;
214
- } else if (refParent.type === 'CallExpression') {
215
- var _callee = refParent.callee;
216
- var fnName = void 0;
217
-
218
- switch (_callee.type) {
219
- case 'Identifier':
220
- fnName = _callee.name;
221
- break;
222
-
223
- case 'MemberExpression':
224
- fnName = _callee.property.name;
225
- break;
226
- }
227
-
228
- switch (fnName) {
229
- case 'createElement':
230
- case 'jsx':
231
- case 'jsxDEV':
232
- case 'jsxs':
233
- isLikelyUsedAsType = true;
234
- break;
88
+ node = path.get("init");
89
+ if (findInnerComponents(inferredName, node, callback)) return !0;
90
+ calleePath = path.scope.getBinding(calleePath);
91
+ if (void 0 === calleePath) return;
92
+ path = !1;
93
+ calleePath = calleePath.referencePaths;
94
+ for (calleeType = 0; calleeType < calleePath.length; calleeType++) {
95
+ var ref = calleePath[calleeType];
96
+ if (
97
+ !ref.node ||
98
+ "JSXIdentifier" === ref.node.type ||
99
+ "Identifier" === ref.node.type
100
+ ) {
101
+ ref = ref.parent;
102
+ if ("JSXOpeningElement" === ref.type) path = !0;
103
+ else if ("CallExpression" === ref.type) {
104
+ ref = ref.callee;
105
+ var fnName = void 0;
106
+ switch (ref.type) {
107
+ case "Identifier":
108
+ fnName = ref.name;
109
+ break;
110
+ case "MemberExpression":
111
+ fnName = ref.property.name;
112
+ }
113
+ switch (fnName) {
114
+ case "createElement":
115
+ case "jsx":
116
+ case "jsxDEV":
117
+ case "jsxs":
118
+ path = !0;
119
+ }
120
+ }
121
+ if (path) return callback(inferredName, argsPath, node), !0;
235
122
  }
236
123
  }
237
-
238
- if (isLikelyUsedAsType) {
239
- // const X = ... + later <X />
240
- callback(inferredName, init, initPath);
241
- return true;
242
- }
243
124
  }
244
- }
245
- }
246
-
247
- return false;
248
- }
249
-
250
- function isBuiltinHook(hookName) {
251
- switch (hookName) {
252
- case 'useState':
253
- case 'React.useState':
254
- case 'useReducer':
255
- case 'React.useReducer':
256
- case 'useEffect':
257
- case 'React.useEffect':
258
- case 'useLayoutEffect':
259
- case 'React.useLayoutEffect':
260
- case 'useMemo':
261
- case 'React.useMemo':
262
- case 'useCallback':
263
- case 'React.useCallback':
264
- case 'useRef':
265
- case 'React.useRef':
266
- case 'useContext':
267
- case 'React.useContext':
268
- case 'useImperativeHandle':
269
- case 'React.useImperativeHandle':
270
- case 'useDebugValue':
271
- case 'React.useDebugValue':
272
- case 'useId':
273
- case 'React.useId':
274
- case 'useDeferredValue':
275
- case 'React.useDeferredValue':
276
- case 'useTransition':
277
- case 'React.useTransition':
278
- case 'useInsertionEffect':
279
- case 'React.useInsertionEffect':
280
- case 'useSyncExternalStore':
281
- case 'React.useSyncExternalStore':
282
- case 'useFormStatus':
283
- case 'React.useFormStatus':
284
- case 'useFormState':
285
- case 'React.useFormState':
286
- case 'useActionState':
287
- case 'React.useActionState':
288
- case 'useOptimistic':
289
- case 'React.useOptimistic':
290
- return true;
291
-
292
- default:
293
- return false;
125
+ }
126
+ return !1;
294
127
  }
295
- }
296
-
297
- function getHookCallsSignature(functionNode) {
298
- var fnHookCalls = hookCalls.get(functionNode);
299
-
300
- if (fnHookCalls === undefined) {
301
- return null;
128
+ function getHookCallsSignature(functionNode) {
129
+ functionNode = hookCalls.get(functionNode);
130
+ return void 0 === functionNode
131
+ ? null
132
+ : {
133
+ key: functionNode
134
+ .map(function (call) {
135
+ return call.name + "{" + call.key + "}";
136
+ })
137
+ .join("\n"),
138
+ customHooks: functionNode
139
+ .filter(function (call) {
140
+ a: switch (call.name) {
141
+ case "useState":
142
+ case "React.useState":
143
+ case "useReducer":
144
+ case "React.useReducer":
145
+ case "useEffect":
146
+ case "React.useEffect":
147
+ case "useLayoutEffect":
148
+ case "React.useLayoutEffect":
149
+ case "useMemo":
150
+ case "React.useMemo":
151
+ case "useCallback":
152
+ case "React.useCallback":
153
+ case "useRef":
154
+ case "React.useRef":
155
+ case "useContext":
156
+ case "React.useContext":
157
+ case "useImperativeHandle":
158
+ case "React.useImperativeHandle":
159
+ case "useDebugValue":
160
+ case "React.useDebugValue":
161
+ case "useId":
162
+ case "React.useId":
163
+ case "useDeferredValue":
164
+ case "React.useDeferredValue":
165
+ case "useTransition":
166
+ case "React.useTransition":
167
+ case "useInsertionEffect":
168
+ case "React.useInsertionEffect":
169
+ case "useSyncExternalStore":
170
+ case "React.useSyncExternalStore":
171
+ case "useFormStatus":
172
+ case "React.useFormStatus":
173
+ case "useFormState":
174
+ case "React.useFormState":
175
+ case "useActionState":
176
+ case "React.useActionState":
177
+ case "useOptimistic":
178
+ case "React.useOptimistic":
179
+ call = !0;
180
+ break a;
181
+ default:
182
+ call = !1;
183
+ }
184
+ return !call;
185
+ })
186
+ .map(function (call) {
187
+ return t.cloneDeep(call.callee);
188
+ })
189
+ };
302
190
  }
303
-
304
- return {
305
- key: fnHookCalls.map(function (call) {
306
- return call.name + '{' + call.key + '}';
307
- }).join('\n'),
308
- customHooks: fnHookCalls.filter(function (call) {
309
- return !isBuiltinHook(call.name);
310
- }).map(function (call) {
311
- return t.cloneDeep(call.callee);
312
- })
313
- };
314
- }
315
-
316
- var hasForceResetCommentByFile = new WeakMap(); // We let user do /* @refresh reset */ to reset state in the whole file.
317
-
318
- function hasForceResetComment(path) {
319
- var file = path.hub.file;
320
- var hasForceReset = hasForceResetCommentByFile.get(file);
321
-
322
- if (hasForceReset !== undefined) {
191
+ function hasForceResetComment(path) {
192
+ path = path.hub.file;
193
+ var hasForceReset = hasForceResetCommentByFile.get(path);
194
+ if (void 0 !== hasForceReset) return hasForceReset;
195
+ hasForceReset = !1;
196
+ for (var comments = path.ast.comments, i = 0; i < comments.length; i++)
197
+ if (-1 !== comments[i].value.indexOf("@refresh reset")) {
198
+ hasForceReset = !0;
199
+ break;
200
+ }
201
+ hasForceResetCommentByFile.set(path, hasForceReset);
323
202
  return hasForceReset;
324
203
  }
325
-
326
- hasForceReset = false;
327
- var comments = file.ast.comments;
328
-
329
- for (var i = 0; i < comments.length; i++) {
330
- var cmt = comments[i];
331
-
332
- if (cmt.value.indexOf('@refresh reset') !== -1) {
333
- hasForceReset = true;
334
- break;
335
- }
204
+ function createArgumentsForSignature(node, signature, scope) {
205
+ var key = signature.key;
206
+ signature = signature.customHooks;
207
+ var forceReset = hasForceResetComment(scope.path),
208
+ customHooksInScope = [];
209
+ signature.forEach(function (callee) {
210
+ switch (callee.type) {
211
+ case "MemberExpression":
212
+ if ("Identifier" === callee.object.type)
213
+ var bindingName = callee.object.name;
214
+ break;
215
+ case "Identifier":
216
+ bindingName = callee.name;
217
+ }
218
+ scope.hasBinding(bindingName)
219
+ ? customHooksInScope.push(callee)
220
+ : (forceReset = !0);
221
+ });
222
+ signature = key;
223
+ "function" !== typeof require ||
224
+ opts.emitFullSignatures ||
225
+ (signature = require("crypto")
226
+ .createHash("sha1")
227
+ .update(key)
228
+ .digest("base64"));
229
+ node = [node, t.stringLiteral(signature)];
230
+ (forceReset || 0 < customHooksInScope.length) &&
231
+ node.push(t.booleanLiteral(forceReset));
232
+ 0 < customHooksInScope.length &&
233
+ node.push(
234
+ t.functionExpression(
235
+ null,
236
+ [],
237
+ t.blockStatement([
238
+ t.returnStatement(t.arrayExpression(customHooksInScope))
239
+ ])
240
+ )
241
+ );
242
+ return node;
336
243
  }
337
-
338
- hasForceResetCommentByFile.set(file, hasForceReset);
339
- return hasForceReset;
340
- }
341
-
342
- function createArgumentsForSignature(node, signature, scope) {
343
- var key = signature.key,
344
- customHooks = signature.customHooks;
345
- var forceReset = hasForceResetComment(scope.path);
346
- var customHooksInScope = [];
347
- customHooks.forEach(function (callee) {
348
- // Check if a corresponding binding exists where we emit the signature.
349
- var bindingName;
350
-
351
- switch (callee.type) {
352
- case 'MemberExpression':
353
- if (callee.object.type === 'Identifier') {
354
- bindingName = callee.object.name;
355
- }
356
-
357
- break;
358
-
359
- case 'Identifier':
360
- bindingName = callee.name;
361
- break;
244
+ function findHOCCallPathsAbove(path) {
245
+ for (var calls = []; ; ) {
246
+ if (!path) return calls;
247
+ var parentPath = path.parentPath;
248
+ if (!parentPath) return calls;
249
+ if (
250
+ "AssignmentExpression" === parentPath.node.type &&
251
+ path.node === parentPath.node.right
252
+ )
253
+ path = parentPath;
254
+ else if (
255
+ "CallExpression" === parentPath.node.type &&
256
+ path.node !== parentPath.node.callee
257
+ )
258
+ calls.push(parentPath), (path = parentPath);
259
+ else return calls;
362
260
  }
363
-
364
- if (scope.hasBinding(bindingName)) {
365
- customHooksInScope.push(callee);
366
- } else {
367
- // We don't have anything to put in the array because Hook is out of scope.
368
- // Since it could potentially have been edited, remount the component.
369
- forceReset = true;
370
- }
371
- });
372
- var finalKey = key;
373
-
374
- if (typeof require === 'function' && !opts.emitFullSignatures) {
375
- // Prefer to hash when we can (e.g. outside of ASTExplorer).
376
- // This makes it deterministically compact, even if there's
377
- // e.g. a useState initializer with some code inside.
378
- // We also need it for www that has transforms like cx()
379
- // that don't understand if something is part of a string.
380
- finalKey = require('crypto').createHash('sha1').update(key).digest('base64');
381
261
  }
382
-
383
- var args = [node, t.stringLiteral(finalKey)];
384
-
385
- if (forceReset || customHooksInScope.length > 0) {
386
- args.push(t.booleanLiteral(forceReset));
387
- }
388
-
389
- if (customHooksInScope.length > 0) {
390
- args.push( // TODO: We could use an arrow here to be more compact.
391
- // However, don't do it until AMA can run them natively.
392
- t.functionExpression(null, [], t.blockStatement([t.returnStatement(t.arrayExpression(customHooksInScope))])));
393
- }
394
-
395
- return args;
396
- }
397
-
398
- function findHOCCallPathsAbove(path) {
399
- var calls = [];
400
-
401
- while (true) {
402
- if (!path) {
403
- return calls;
404
- }
405
-
406
- var parentPath = path.parentPath;
407
-
408
- if (!parentPath) {
409
- return calls;
410
- }
411
-
412
- if ( // hoc(_c = function() { })
413
- parentPath.node.type === 'AssignmentExpression' && path.node === parentPath.node.right) {
414
- // Ignore registrations.
415
- path = parentPath;
416
- continue;
417
- }
418
-
419
- if ( // hoc1(hoc2(...))
420
- parentPath.node.type === 'CallExpression' && path.node !== parentPath.node.callee) {
421
- calls.push(parentPath);
422
- path = parentPath;
423
- continue;
424
- }
425
-
426
- return calls; // Stop at other types.
262
+ var opts =
263
+ 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : {};
264
+ if ("function" === typeof babel.env) {
265
+ var env = babel.env();
266
+ if ("development" !== env && !opts.skipEnvCheck)
267
+ throw Error(
268
+ 'React Refresh Babel transform should only be enabled in development environment. Instead, the environment is: "' +
269
+ env +
270
+ '". If you want to override this check, pass {skipEnvCheck: true} as plugin options.'
271
+ );
427
272
  }
428
- }
429
-
430
- var seenForRegistration = new WeakSet();
431
- var seenForSignature = new WeakSet();
432
- var seenForOutro = new WeakSet();
433
- var hookCalls = new WeakMap();
434
- var HookCallsVisitor = {
435
- CallExpression: function (path) {
436
- var node = path.node;
437
- var callee = node.callee; // Note: this visitor MUST NOT mutate the tree in any way.
438
- // It runs early in a separate traversal and should be very fast.
439
-
440
- var name = null;
441
-
442
- switch (callee.type) {
443
- case 'Identifier':
444
- name = callee.name;
445
- break;
446
-
447
- case 'MemberExpression':
448
- name = callee.property.name;
449
- break;
450
- }
451
-
452
- if (name === null || !/^use[A-Z]/.test(name)) {
453
- return;
454
- }
455
-
456
- var fnScope = path.scope.getFunctionParent();
457
-
458
- if (fnScope === null) {
459
- return;
460
- } // This is a Hook call. Record it.
461
-
462
-
463
- var fnNode = fnScope.block;
464
-
465
- if (!hookCalls.has(fnNode)) {
466
- hookCalls.set(fnNode, []);
467
- }
468
-
469
- var hookCallsForFn = hookCalls.get(fnNode);
470
- var key = '';
471
-
472
- if (path.parent.type === 'VariableDeclarator') {
473
- // TODO: if there is no LHS, consider some other heuristic.
474
- key = path.parentPath.get('id').getSource();
475
- } // Some built-in Hooks reset on edits to arguments.
476
-
477
-
478
- var args = path.get('arguments');
479
-
480
- if (name === 'useState' && args.length > 0) {
481
- // useState second argument is initial state.
482
- key += '(' + args[0].getSource() + ')';
483
- } else if (name === 'useReducer' && args.length > 1) {
484
- // useReducer second argument is initial state.
485
- key += '(' + args[1].getSource() + ')';
486
- }
487
-
488
- hookCallsForFn.push({
489
- callee: path.node.callee,
490
- name: name,
491
- key: key
492
- });
493
- }
494
- };
495
- return {
496
- visitor: {
497
- ExportDefaultDeclaration: function (path) {
498
- var node = path.node;
499
- var decl = node.declaration;
500
- var declPath = path.get('declaration');
501
-
502
- if (decl.type !== 'CallExpression') {
503
- // For now, we only support possible HOC calls here.
504
- // Named function declarations are handled in FunctionDeclaration.
505
- // Anonymous direct exports like export default function() {}
506
- // are currently ignored.
507
- return;
508
- } // Make sure we're not mutating the same tree twice.
509
- // This can happen if another Babel plugin replaces parents.
510
-
511
-
512
- if (seenForRegistration.has(node)) {
513
- return;
273
+ var t = babel.types,
274
+ refreshReg = t.identifier(opts.refreshReg || "$RefreshReg$"),
275
+ refreshSig = t.identifier(opts.refreshSig || "$RefreshSig$"),
276
+ registrationsByProgramPath = new Map(),
277
+ hasForceResetCommentByFile = new WeakMap(),
278
+ seenForRegistration = new WeakSet(),
279
+ seenForSignature = new WeakSet(),
280
+ seenForOutro = new WeakSet(),
281
+ hookCalls = new WeakMap(),
282
+ HookCallsVisitor = {
283
+ CallExpression: function (path) {
284
+ var callee = path.node.callee,
285
+ name = null;
286
+ switch (callee.type) {
287
+ case "Identifier":
288
+ name = callee.name;
289
+ break;
290
+ case "MemberExpression":
291
+ name = callee.property.name;
292
+ }
293
+ if (
294
+ null !== name &&
295
+ /^use[A-Z]/.test(name) &&
296
+ ((callee = path.scope.getFunctionParent()), null !== callee)
297
+ ) {
298
+ callee = callee.block;
299
+ hookCalls.has(callee) || hookCalls.set(callee, []);
300
+ callee = hookCalls.get(callee);
301
+ var key = "";
302
+ "VariableDeclarator" === path.parent.type &&
303
+ (key = path.parentPath.get("id").getSource());
304
+ var args = path.get("arguments");
305
+ "useState" === name && 0 < args.length
306
+ ? (key += "(" + args[0].getSource() + ")")
307
+ : "useReducer" === name &&
308
+ 1 < args.length &&
309
+ (key += "(" + args[1].getSource() + ")");
310
+ callee.push({ callee: path.node.callee, name: name, key: key });
311
+ }
514
312
  }
515
-
516
- seenForRegistration.add(node); // Don't mutate the tree above this point.
517
- // This code path handles nested cases like:
518
- // export default memo(() => {})
519
- // In those cases it is more plausible people will omit names
520
- // so they're worth handling despite possible false positives.
521
- // More importantly, it handles the named case:
522
- // export default memo(function Named() {})
523
-
524
- var inferredName = '%default%';
525
- var programPath = path.parentPath;
526
- findInnerComponents(inferredName, declPath, function (persistentID, targetExpr, targetPath) {
527
- if (targetPath === null) {
528
- // For case like:
529
- // export default hoc(Foo)
530
- // we don't want to wrap Foo inside the call.
531
- // Instead we assume it's registered at definition.
532
- return;
313
+ };
314
+ return {
315
+ visitor: {
316
+ ExportDefaultDeclaration: function (path) {
317
+ var node = path.node,
318
+ decl = node.declaration,
319
+ declPath = path.get("declaration");
320
+ if (
321
+ "CallExpression" === decl.type &&
322
+ !seenForRegistration.has(node)
323
+ ) {
324
+ seenForRegistration.add(node);
325
+ var programPath = path.parentPath;
326
+ findInnerComponents(
327
+ "%default%",
328
+ declPath,
329
+ function (persistentID, targetExpr, targetPath) {
330
+ null !== targetPath &&
331
+ ((persistentID = createRegistration(
332
+ programPath,
333
+ persistentID
334
+ )),
335
+ targetPath.replaceWith(
336
+ t.assignmentExpression("=", persistentID, targetExpr)
337
+ ));
338
+ }
339
+ );
533
340
  }
534
-
535
- var handle = createRegistration(programPath, persistentID);
536
- targetPath.replaceWith(t.assignmentExpression('=', handle, targetExpr));
537
- });
538
- },
539
- FunctionDeclaration: {
540
- enter: function (path) {
541
- var node = path.node;
542
- var programPath;
543
- var insertAfterPath;
544
- var modulePrefix = '';
545
-
341
+ },
342
+ FunctionDeclaration: {
343
+ enter: function (path) {
344
+ var node = path.node,
345
+ modulePrefix = "";
346
+ switch (path.parent.type) {
347
+ case "Program":
348
+ var insertAfterPath = path;
349
+ var programPath = path.parentPath;
350
+ break;
351
+ case "TSModuleBlock":
352
+ insertAfterPath = path;
353
+ programPath = insertAfterPath.parentPath.parentPath;
354
+ break;
355
+ case "ExportNamedDeclaration":
356
+ insertAfterPath = path.parentPath;
357
+ programPath = insertAfterPath.parentPath;
358
+ break;
359
+ case "ExportDefaultDeclaration":
360
+ insertAfterPath = path.parentPath;
361
+ programPath = insertAfterPath.parentPath;
362
+ break;
363
+ default:
364
+ return;
365
+ }
366
+ if (
367
+ "TSModuleBlock" === path.parent.type ||
368
+ "ExportNamedDeclaration" === path.parent.type
369
+ )
370
+ for (; "Program" !== programPath.type; ) {
371
+ if ("TSModuleDeclaration" === programPath.type) {
372
+ if (
373
+ "Program" !== programPath.parentPath.type &&
374
+ "ExportNamedDeclaration" !== programPath.parentPath.type
375
+ )
376
+ return;
377
+ modulePrefix = programPath.node.id.name + "$" + modulePrefix;
378
+ }
379
+ programPath = programPath.parentPath;
380
+ }
381
+ var id = node.id;
382
+ null !== id &&
383
+ ((id = id.name),
384
+ isComponentishName(id) &&
385
+ !seenForRegistration.has(node) &&
386
+ (seenForRegistration.add(node),
387
+ findInnerComponents(
388
+ modulePrefix + id,
389
+ path,
390
+ function (persistentID, targetExpr) {
391
+ persistentID = createRegistration(
392
+ programPath,
393
+ persistentID
394
+ );
395
+ insertAfterPath.insertAfter(
396
+ t.expressionStatement(
397
+ t.assignmentExpression("=", persistentID, targetExpr)
398
+ )
399
+ );
400
+ }
401
+ )));
402
+ },
403
+ exit: function (path) {
404
+ var node = path.node,
405
+ id = node.id;
406
+ if (null !== id) {
407
+ var signature = getHookCallsSignature(node);
408
+ if (null !== signature && !seenForSignature.has(node)) {
409
+ seenForSignature.add(node);
410
+ node = path.scope.generateUidIdentifier("_s");
411
+ path.scope.parent.push({
412
+ id: node,
413
+ init: t.callExpression(refreshSig, [])
414
+ });
415
+ path
416
+ .get("body")
417
+ .unshiftContainer(
418
+ "body",
419
+ t.expressionStatement(t.callExpression(node, []))
420
+ );
421
+ var insertAfterPath = null;
422
+ path.find(function (p) {
423
+ if (p.parentPath.isBlock()) return (insertAfterPath = p), !0;
424
+ });
425
+ null !== insertAfterPath &&
426
+ insertAfterPath.insertAfter(
427
+ t.expressionStatement(
428
+ t.callExpression(
429
+ node,
430
+ createArgumentsForSignature(
431
+ id,
432
+ signature,
433
+ insertAfterPath.scope
434
+ )
435
+ )
436
+ )
437
+ );
438
+ }
439
+ }
440
+ }
441
+ },
442
+ "ArrowFunctionExpression|FunctionExpression": {
443
+ exit: function (path) {
444
+ var node = path.node,
445
+ signature = getHookCallsSignature(node);
446
+ if (null !== signature && !seenForSignature.has(node)) {
447
+ seenForSignature.add(node);
448
+ var sigCallID = path.scope.generateUidIdentifier("_s");
449
+ path.scope.parent.push({
450
+ id: sigCallID,
451
+ init: t.callExpression(refreshSig, [])
452
+ });
453
+ "BlockStatement" !== path.node.body.type &&
454
+ (path.node.body = t.blockStatement([
455
+ t.returnStatement(path.node.body)
456
+ ]));
457
+ path
458
+ .get("body")
459
+ .unshiftContainer(
460
+ "body",
461
+ t.expressionStatement(t.callExpression(sigCallID, []))
462
+ );
463
+ if ("VariableDeclarator" === path.parent.type) {
464
+ var insertAfterPath = null;
465
+ path.find(function (p) {
466
+ if (p.parentPath.isBlock()) return (insertAfterPath = p), !0;
467
+ });
468
+ null !== insertAfterPath &&
469
+ insertAfterPath.insertAfter(
470
+ t.expressionStatement(
471
+ t.callExpression(
472
+ sigCallID,
473
+ createArgumentsForSignature(
474
+ path.parent.id,
475
+ signature,
476
+ insertAfterPath.scope
477
+ )
478
+ )
479
+ )
480
+ );
481
+ } else
482
+ [path]
483
+ .concat(findHOCCallPathsAbove(path))
484
+ .forEach(function (p) {
485
+ p.replaceWith(
486
+ t.callExpression(
487
+ sigCallID,
488
+ createArgumentsForSignature(p.node, signature, p.scope)
489
+ )
490
+ );
491
+ });
492
+ }
493
+ }
494
+ },
495
+ VariableDeclaration: function (path) {
496
+ var node = path.node,
497
+ modulePrefix = "";
546
498
  switch (path.parent.type) {
547
- case 'Program':
548
- insertAfterPath = path;
549
- programPath = path.parentPath;
499
+ case "Program":
500
+ var insertAfterPath = path;
501
+ var programPath = path.parentPath;
550
502
  break;
551
-
552
- case 'TSModuleBlock':
503
+ case "TSModuleBlock":
553
504
  insertAfterPath = path;
554
505
  programPath = insertAfterPath.parentPath.parentPath;
555
506
  break;
556
-
557
- case 'ExportNamedDeclaration':
507
+ case "ExportNamedDeclaration":
558
508
  insertAfterPath = path.parentPath;
559
509
  programPath = insertAfterPath.parentPath;
560
510
  break;
561
-
562
- case 'ExportDefaultDeclaration':
511
+ case "ExportDefaultDeclaration":
563
512
  insertAfterPath = path.parentPath;
564
513
  programPath = insertAfterPath.parentPath;
565
514
  break;
566
-
567
515
  default:
568
516
  return;
569
- } // These types can be nested in typescript namespace
570
- // We need to find the export chain
571
- // Or return if it stays local
572
-
573
-
574
- if (path.parent.type === 'TSModuleBlock' || path.parent.type === 'ExportNamedDeclaration') {
575
- while (programPath.type !== 'Program') {
576
- if (programPath.type === 'TSModuleDeclaration') {
577
- if (programPath.parentPath.type !== 'Program' && programPath.parentPath.type !== 'ExportNamedDeclaration') {
517
+ }
518
+ if (
519
+ "TSModuleBlock" === path.parent.type ||
520
+ "ExportNamedDeclaration" === path.parent.type
521
+ )
522
+ for (; "Program" !== programPath.type; ) {
523
+ if ("TSModuleDeclaration" === programPath.type) {
524
+ if (
525
+ "Program" !== programPath.parentPath.type &&
526
+ "ExportNamedDeclaration" !== programPath.parentPath.type
527
+ )
578
528
  return;
579
- }
580
-
581
- modulePrefix = programPath.node.id.name + '$' + modulePrefix;
529
+ modulePrefix = programPath.node.id.name + "$" + modulePrefix;
582
530
  }
583
-
584
531
  programPath = programPath.parentPath;
585
532
  }
586
- }
587
-
588
- var id = node.id;
589
-
590
- if (id === null) {
591
- // We don't currently handle anonymous default exports.
592
- return;
593
- }
594
-
595
- var inferredName = id.name;
596
-
597
- if (!isComponentishName(inferredName)) {
598
- return;
599
- } // Make sure we're not mutating the same tree twice.
600
- // This can happen if another Babel plugin replaces parents.
601
-
602
-
603
- if (seenForRegistration.has(node)) {
604
- return;
605
- }
606
-
607
- seenForRegistration.add(node); // Don't mutate the tree above this point.
608
-
609
- var innerName = modulePrefix + inferredName; // export function Named() {}
610
- // function Named() {}
611
-
612
- findInnerComponents(innerName, path, function (persistentID, targetExpr) {
613
- var handle = createRegistration(programPath, persistentID);
614
- insertAfterPath.insertAfter(t.expressionStatement(t.assignmentExpression('=', handle, targetExpr)));
615
- });
616
- },
617
- exit: function (path) {
618
- var node = path.node;
619
- var id = node.id;
620
-
621
- if (id === null) {
622
- return;
623
- }
624
-
625
- var signature = getHookCallsSignature(node);
626
-
627
- if (signature === null) {
628
- return;
629
- } // Make sure we're not mutating the same tree twice.
630
- // This can happen if another Babel plugin replaces parents.
631
-
632
-
633
- if (seenForSignature.has(node)) {
634
- return;
635
- }
636
-
637
- seenForSignature.add(node); // Don't mutate the tree above this point.
638
-
639
- var sigCallID = path.scope.generateUidIdentifier('_s');
640
- path.scope.parent.push({
641
- id: sigCallID,
642
- init: t.callExpression(refreshSig, [])
643
- }); // The signature call is split in two parts. One part is called inside the function.
644
- // This is used to signal when first render happens.
645
-
646
- path.get('body').unshiftContainer('body', t.expressionStatement(t.callExpression(sigCallID, []))); // The second call is around the function itself.
647
- // This is used to associate a type with a signature.
648
- // Unlike with $RefreshReg$, this needs to work for nested
649
- // declarations too. So we need to search for a path where
650
- // we can insert a statement rather than hard coding it.
651
-
652
- var insertAfterPath = null;
653
- path.find(function (p) {
654
- if (p.parentPath.isBlock()) {
655
- insertAfterPath = p;
656
- return true;
657
- }
658
- });
659
-
660
- if (insertAfterPath === null) {
661
- return;
662
- }
663
-
664
- insertAfterPath.insertAfter(t.expressionStatement(t.callExpression(sigCallID, createArgumentsForSignature(id, signature, insertAfterPath.scope))));
665
- }
666
- },
667
- 'ArrowFunctionExpression|FunctionExpression': {
668
- exit: function (path) {
669
- var node = path.node;
670
- var signature = getHookCallsSignature(node);
671
-
672
- if (signature === null) {
673
- return;
674
- } // Make sure we're not mutating the same tree twice.
675
- // This can happen if another Babel plugin replaces parents.
676
-
677
-
678
- if (seenForSignature.has(node)) {
679
- return;
680
- }
681
-
682
- seenForSignature.add(node); // Don't mutate the tree above this point.
683
-
684
- var sigCallID = path.scope.generateUidIdentifier('_s');
685
- path.scope.parent.push({
686
- id: sigCallID,
687
- init: t.callExpression(refreshSig, [])
688
- }); // The signature call is split in two parts. One part is called inside the function.
689
- // This is used to signal when first render happens.
690
-
691
- if (path.node.body.type !== 'BlockStatement') {
692
- path.node.body = t.blockStatement([t.returnStatement(path.node.body)]);
693
- }
694
-
695
- path.get('body').unshiftContainer('body', t.expressionStatement(t.callExpression(sigCallID, []))); // The second call is around the function itself.
696
- // This is used to associate a type with a signature.
697
-
698
- if (path.parent.type === 'VariableDeclarator') {
699
- var insertAfterPath = null;
700
- path.find(function (p) {
701
- if (p.parentPath.isBlock()) {
702
- insertAfterPath = p;
703
- return true;
533
+ if (
534
+ !seenForRegistration.has(node) &&
535
+ (seenForRegistration.add(node),
536
+ (path = path.get("declarations")),
537
+ 1 === path.length)
538
+ ) {
539
+ var declPath = path[0];
540
+ findInnerComponents(
541
+ modulePrefix + declPath.node.id.name,
542
+ declPath,
543
+ function (persistentID, targetExpr, targetPath) {
544
+ null !== targetPath &&
545
+ ((persistentID = createRegistration(
546
+ programPath,
547
+ persistentID
548
+ )),
549
+ "VariableDeclarator" === targetPath.parent.type
550
+ ? insertAfterPath.insertAfter(
551
+ t.expressionStatement(
552
+ t.assignmentExpression(
553
+ "=",
554
+ persistentID,
555
+ declPath.node.id
556
+ )
557
+ )
558
+ )
559
+ : targetPath.replaceWith(
560
+ t.assignmentExpression("=", persistentID, targetExpr)
561
+ ));
704
562
  }
705
- });
706
-
707
- if (insertAfterPath === null) {
708
- return;
709
- } // Special case when a function would get an inferred name:
710
- // let Foo = () => {}
711
- // let Foo = function() {}
712
- // We'll add signature it on next line so that
713
- // we don't mess up the inferred 'Foo' function name.
714
-
715
-
716
- insertAfterPath.insertAfter(t.expressionStatement(t.callExpression(sigCallID, createArgumentsForSignature(path.parent.id, signature, insertAfterPath.scope)))); // Result: let Foo = () => {}; __signature(Foo, ...);
717
- } else {
718
- // let Foo = hoc(() => {})
719
- var paths = [path].concat(findHOCCallPathsAbove(path));
720
- paths.forEach(function (p) {
721
- p.replaceWith(t.callExpression(sigCallID, createArgumentsForSignature(p.node, signature, p.scope)));
722
- }); // Result: let Foo = __signature(hoc(__signature(() => {}, ...)), ...)
563
+ );
723
564
  }
724
- }
725
- },
726
- VariableDeclaration: function (path) {
727
- var node = path.node;
728
- var programPath;
729
- var insertAfterPath;
730
- var modulePrefix = '';
731
-
732
- switch (path.parent.type) {
733
- case 'Program':
734
- insertAfterPath = path;
735
- programPath = path.parentPath;
736
- break;
737
-
738
- case 'TSModuleBlock':
739
- insertAfterPath = path;
740
- programPath = insertAfterPath.parentPath.parentPath;
741
- break;
742
-
743
- case 'ExportNamedDeclaration':
744
- insertAfterPath = path.parentPath;
745
- programPath = insertAfterPath.parentPath;
746
- break;
747
-
748
- case 'ExportDefaultDeclaration':
749
- insertAfterPath = path.parentPath;
750
- programPath = insertAfterPath.parentPath;
751
- break;
752
-
753
- default:
754
- return;
755
- } // These types can be nested in typescript namespace
756
- // We need to find the export chain
757
- // Or return if it stays local
758
-
759
-
760
- if (path.parent.type === 'TSModuleBlock' || path.parent.type === 'ExportNamedDeclaration') {
761
- while (programPath.type !== 'Program') {
762
- if (programPath.type === 'TSModuleDeclaration') {
763
- if (programPath.parentPath.type !== 'Program' && programPath.parentPath.type !== 'ExportNamedDeclaration') {
764
- return;
565
+ },
566
+ Program: {
567
+ enter: function (path) {
568
+ path.traverse(HookCallsVisitor);
569
+ },
570
+ exit: function (path) {
571
+ var registrations = registrationsByProgramPath.get(path);
572
+ if (void 0 !== registrations) {
573
+ var node = path.node;
574
+ if (!seenForOutro.has(node)) {
575
+ seenForOutro.add(node);
576
+ registrationsByProgramPath.delete(path);
577
+ var declarators = [];
578
+ path.pushContainer(
579
+ "body",
580
+ t.variableDeclaration("var", declarators)
581
+ );
582
+ registrations.forEach(function (_ref) {
583
+ var handle = _ref.handle;
584
+ path.pushContainer(
585
+ "body",
586
+ t.expressionStatement(
587
+ t.callExpression(refreshReg, [
588
+ handle,
589
+ t.stringLiteral(_ref.persistentID)
590
+ ])
591
+ )
592
+ );
593
+ declarators.push(t.variableDeclarator(handle));
594
+ });
765
595
  }
766
-
767
- modulePrefix = programPath.node.id.name + '$' + modulePrefix;
768
596
  }
769
-
770
- programPath = programPath.parentPath;
771
- }
772
- } // Make sure we're not mutating the same tree twice.
773
- // This can happen if another Babel plugin replaces parents.
774
-
775
-
776
- if (seenForRegistration.has(node)) {
777
- return;
778
- }
779
-
780
- seenForRegistration.add(node); // Don't mutate the tree above this point.
781
-
782
- var declPaths = path.get('declarations');
783
-
784
- if (declPaths.length !== 1) {
785
- return;
786
- }
787
-
788
- var declPath = declPaths[0];
789
- var inferredName = declPath.node.id.name;
790
- var innerName = modulePrefix + inferredName;
791
- findInnerComponents(innerName, declPath, function (persistentID, targetExpr, targetPath) {
792
- if (targetPath === null) {
793
- // For case like:
794
- // export const Something = hoc(Foo)
795
- // we don't want to wrap Foo inside the call.
796
- // Instead we assume it's registered at definition.
797
- return;
798
597
  }
799
-
800
- var handle = createRegistration(programPath, persistentID);
801
-
802
- if (targetPath.parent.type === 'VariableDeclarator') {
803
- // Special case when a variable would get an inferred name:
804
- // let Foo = () => {}
805
- // let Foo = function() {}
806
- // let Foo = styled.div``;
807
- // We'll register it on next line so that
808
- // we don't mess up the inferred 'Foo' function name.
809
- // (eg: with @babel/plugin-transform-react-display-name or
810
- // babel-plugin-styled-components)
811
- insertAfterPath.insertAfter(t.expressionStatement(t.assignmentExpression('=', handle, declPath.node.id))); // Result: let Foo = () => {}; _c1 = Foo;
812
- } else {
813
- // let Foo = hoc(() => {})
814
- targetPath.replaceWith(t.assignmentExpression('=', handle, targetExpr)); // Result: let Foo = hoc(_c1 = () => {})
815
- }
816
- });
817
- },
818
- Program: {
819
- enter: function (path) {
820
- // This is a separate early visitor because we need to collect Hook calls
821
- // and "const [foo, setFoo] = ..." signatures before the destructuring
822
- // transform mangles them. This extra traversal is not ideal for perf,
823
- // but it's the best we can do until we stop transpiling destructuring.
824
- path.traverse(HookCallsVisitor);
825
- },
826
- exit: function (path) {
827
- var registrations = registrationsByProgramPath.get(path);
828
-
829
- if (registrations === undefined) {
830
- return;
831
- } // Make sure we're not mutating the same tree twice.
832
- // This can happen if another Babel plugin replaces parents.
833
-
834
-
835
- var node = path.node;
836
-
837
- if (seenForOutro.has(node)) {
838
- return;
839
- }
840
-
841
- seenForOutro.add(node); // Don't mutate the tree above this point.
842
-
843
- registrationsByProgramPath.delete(path);
844
- var declarators = [];
845
- path.pushContainer('body', t.variableDeclaration('var', declarators));
846
- registrations.forEach(function (_ref) {
847
- var handle = _ref.handle,
848
- persistentID = _ref.persistentID;
849
- path.pushContainer('body', t.expressionStatement(t.callExpression(refreshReg, [handle, t.stringLiteral(persistentID)])));
850
- declarators.push(t.variableDeclarator(handle));
851
- });
852
598
  }
853
599
  }
854
- }
855
- };
856
- }
857
-
858
- module.exports = ReactFreshBabelPlugin;
859
- })();
860
- }
600
+ };
601
+ });