forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReactFreshBabelPlugin-test.js.snap
More file actions
638 lines (476 loc) · 12.7 KB
/
ReactFreshBabelPlugin-test.js.snap
File metadata and controls
638 lines (476 loc) · 12.7 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
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`ReactFreshBabelPlugin can handle implicit arrow returns 1`] = `
var _s = $RefreshSig$(),
_s2 = $RefreshSig$(),
_s3 = $RefreshSig$(),
_s4 = $RefreshSig$(),
_s5 = $RefreshSig$(),
_s6 = $RefreshSig$();
export default _s(() => {
_s();
return useContext(X);
}, "useContext{}");
export const Foo = () => {
_s2();
return useContext(X);
};
_s2(Foo, "useContext{}");
_c = Foo;
module.exports = _s3(() => {
_s3();
return useContext(X);
}, "useContext{}");
const Bar = () => {
_s4();
return useContext(X);
};
_s4(Bar, "useContext{}");
_c2 = Bar;
const Baz = memo(_c3 = _s5(() => {
_s5();
return useContext(X);
}, "useContext{}"));
_c4 = Baz;
const Qux = () => {
_s6();
return 0, useContext(X);
};
_s6(Qux, "useContext{}");
_c5 = Qux;
var _c, _c2, _c3, _c4, _c5;
$RefreshReg$(_c, "Foo");
$RefreshReg$(_c2, "Bar");
$RefreshReg$(_c3, "Baz$memo");
$RefreshReg$(_c4, "Baz");
$RefreshReg$(_c5, "Qux");
`;
exports[`ReactFreshBabelPlugin does not consider require-like methods to be HOCs 1`] = `
const A = require('A');
const B = foo ? require('X') : require('Y');
const C = requireCond(gk, 'C');
const D = import('D');
export default function App() {
return <div>
<A />
<B />
<C />
<D />
</div>;
}
_c = App;
var _c;
$RefreshReg$(_c, "App");
`;
exports[`ReactFreshBabelPlugin generates signatures for function declarations calling hooks 1`] = `
var _s = $RefreshSig$();
export default function App() {
_s();
const [foo, setFoo] = useState(0);
React.useEffect(() => {});
return <h1>{foo}</h1>;
}
_s(App, "useState{[foo, setFoo](0)}\\nuseEffect{}");
_c = App;
var _c;
$RefreshReg$(_c, "App");
`;
exports[`ReactFreshBabelPlugin generates signatures for function expressions calling hooks 1`] = `
var _s = $RefreshSig$(),
_s2 = $RefreshSig$();
export const A = React.memo(_c2 = React.forwardRef(_c = _s((props, ref) => {
_s();
const [foo, setFoo] = useState(0);
React.useEffect(() => {});
return <h1 ref={ref}>{foo}</h1>;
}, "useState{[foo, setFoo](0)}\\nuseEffect{}")));
_c3 = A;
export const B = React.memo(_c5 = React.forwardRef(_c4 = _s2(function (props, ref) {
_s2();
const [foo, setFoo] = useState(0);
React.useEffect(() => {});
return <h1 ref={ref}>{foo}</h1>;
}, "useState{[foo, setFoo](0)}\\nuseEffect{}")));
_c6 = B;
function hoc() {
var _s3 = $RefreshSig$();
return _s3(function Inner() {
_s3();
const [foo, setFoo] = useState(0);
React.useEffect(() => {});
return <h1 ref={ref}>{foo}</h1>;
}, "useState{[foo, setFoo](0)}\\nuseEffect{}");
}
export let C = hoc();
var _c, _c2, _c3, _c4, _c5, _c6;
$RefreshReg$(_c, "A$React.memo$React.forwardRef");
$RefreshReg$(_c2, "A$React.memo");
$RefreshReg$(_c3, "A");
$RefreshReg$(_c4, "B$React.memo$React.forwardRef");
$RefreshReg$(_c5, "B$React.memo");
$RefreshReg$(_c6, "B");
`;
exports[`ReactFreshBabelPlugin generates valid signature for exotic ways to call Hooks 1`] = `
var _s2 = $RefreshSig$();
import FancyHook from 'fancy';
export default function App() {
_s2();
var _s = $RefreshSig$();
function useFancyState() {
_s();
const [foo, setFoo] = React.useState(0);
useFancyEffect();
return foo;
}
_s(useFancyState, "useState{[foo, setFoo](0)}\\nuseFancyEffect{}", true);
const bar = useFancyState();
const baz = FancyHook.useThing();
React.useState();
useThePlatform();
return <h1>{bar}{baz}</h1>;
}
_s2(App, "useFancyState{bar}\\nuseThing{baz}\\nuseState{}\\nuseThePlatform{}", true, function () {
return [FancyHook.useThing];
});
_c = App;
var _c;
$RefreshReg$(_c, "App");
`;
exports[`ReactFreshBabelPlugin ignores HOC definitions 1`] = `
let connect = () => {
function Comp() {
const handleClick = () => {};
return <h1 onClick={handleClick}>Hi</h1>;
}
return Comp;
};
function withRouter() {
return function Child() {
const handleClick = () => {};
return <h1 onClick={handleClick}>Hi</h1>;
};
}
;
`;
exports[`ReactFreshBabelPlugin ignores complex definitions 1`] = `
let A = foo ? () => {
return <h1>Hi</h1>;
} : null;
const B = function Foo() {
return <h1>Hi</h1>;
}();
let C = () => () => {
return <h1>Hi</h1>;
};
let D = bar && (() => {
return <h1>Hi</h1>;
});
`;
exports[`ReactFreshBabelPlugin ignores higher-order functions that are not HOCs 1`] = `
const throttledAlert = throttle(function () {
alert('Hi');
});
const TooComplex = function () {
return hello;
}(() => {});
if (cond) {
const Foo = thing(() => {});
}
`;
exports[`ReactFreshBabelPlugin ignores unnamed function declarations 1`] = `export default function () {}`;
exports[`ReactFreshBabelPlugin includes custom hooks into the signatures 1`] = `
var _s = $RefreshSig$(),
_s2 = $RefreshSig$(),
_s3 = $RefreshSig$();
function useFancyState() {
_s();
const [foo, setFoo] = React.useState(0);
useFancyEffect();
return foo;
}
_s(useFancyState, "useState{[foo, setFoo](0)}\\nuseFancyEffect{}", false, function () {
return [useFancyEffect];
});
const useFancyEffect = () => {
_s2();
React.useEffect(() => {});
};
_s2(useFancyEffect, "useEffect{}");
export default function App() {
_s3();
const bar = useFancyState();
return <h1>{bar}</h1>;
}
_s3(App, "useFancyState{bar}", false, function () {
return [useFancyState];
});
_c = App;
var _c;
$RefreshReg$(_c, "App");
`;
exports[`ReactFreshBabelPlugin includes custom hooks into the signatures when commonjs target is used 1`] = `
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = App;
var _hooks = require("./hooks");
var _s = $RefreshSig$();
function App() {
_s();
const bar = (0, _hooks.useFancyState)();
return <h1>{bar}</h1>;
}
_s(App, "useFancyState{bar}", false, function () {
return [_hooks.useFancyState];
});
_c = App;
var _c;
$RefreshReg$(_c, "App");
`;
exports[`ReactFreshBabelPlugin only registers pascal case functions 1`] = `
function hello() {
return 2 * 2;
}
`;
exports[`ReactFreshBabelPlugin registers capitalized identifiers in HOC calls 1`] = `
function Foo() {
return <h1>Hi</h1>;
}
_c = Foo;
export default _c2 = hoc(Foo);
export const A = hoc(Foo);
_c3 = A;
const B = hoc(Foo);
_c4 = B;
var _c, _c2, _c3, _c4;
$RefreshReg$(_c, "Foo");
$RefreshReg$(_c2, "%default%");
$RefreshReg$(_c3, "A");
$RefreshReg$(_c4, "B");
`;
exports[`ReactFreshBabelPlugin registers identifiers used in JSX at definition site 1`] = `
import A from './A';
import Store from './Store';
Store.subscribe();
const Header = styled.div\`color: red\`;
_c = Header;
const StyledFactory1 = styled('div')\`color: hotpink\`;
_c2 = StyledFactory1;
const StyledFactory2 = styled('div')({
color: 'hotpink'
});
_c3 = StyledFactory2;
const StyledFactory3 = styled(A)({
color: 'hotpink'
});
_c4 = StyledFactory3;
const FunnyFactory = funny.factory\`\`;
let Alias1 = A;
let Alias2 = A.Foo;
const Dict = {};
function Foo() {
return <div><A /><B /><StyledFactory1 /><StyledFactory2 /><StyledFactory3 /><Alias1 /><Alias2 /><Header /><Dict.X /></div>;
}
_c5 = Foo;
const B = hoc(A); // This is currently registered as a false positive:
_c6 = B;
const NotAComponent = wow(A); // We could avoid it but it also doesn't hurt.
_c7 = NotAComponent;
var _c, _c2, _c3, _c4, _c5, _c6, _c7;
$RefreshReg$(_c, "Header");
$RefreshReg$(_c2, "StyledFactory1");
$RefreshReg$(_c3, "StyledFactory2");
$RefreshReg$(_c4, "StyledFactory3");
$RefreshReg$(_c5, "Foo");
$RefreshReg$(_c6, "B");
$RefreshReg$(_c7, "NotAComponent");
`;
exports[`ReactFreshBabelPlugin registers identifiers used in React.createElement at definition site 1`] = `
import A from './A';
import Store from './Store';
Store.subscribe();
const Header = styled.div\`color: red\`;
_c = Header;
const StyledFactory1 = styled('div')\`color: hotpink\`;
_c2 = StyledFactory1;
const StyledFactory2 = styled('div')({
color: 'hotpink'
});
_c3 = StyledFactory2;
const StyledFactory3 = styled(A)({
color: 'hotpink'
});
_c4 = StyledFactory3;
const FunnyFactory = funny.factory\`\`;
let Alias1 = A;
let Alias2 = A.Foo;
const Dict = {};
function Foo() {
return [React.createElement(A), React.createElement(B), React.createElement(StyledFactory1), React.createElement(StyledFactory2), React.createElement(StyledFactory3), React.createElement(Alias1), React.createElement(Alias2), jsx(Header), React.createElement(Dict.X)];
}
_c5 = Foo;
React.createContext(Store);
const B = hoc(A); // This is currently registered as a false positive:
_c6 = B;
const NotAComponent = wow(A); // We could avoid it but it also doesn't hurt.
_c7 = NotAComponent;
var _c, _c2, _c3, _c4, _c5, _c6, _c7;
$RefreshReg$(_c, "Header");
$RefreshReg$(_c2, "StyledFactory1");
$RefreshReg$(_c3, "StyledFactory2");
$RefreshReg$(_c4, "StyledFactory3");
$RefreshReg$(_c5, "Foo");
$RefreshReg$(_c6, "B");
$RefreshReg$(_c7, "NotAComponent");
`;
exports[`ReactFreshBabelPlugin registers likely HOCs with inline functions 1`] = `
const A = forwardRef(_c = function () {
return <h1>Foo</h1>;
});
_c2 = A;
const B = memo(_c4 = React.forwardRef(_c3 = () => {
return <h1>Foo</h1>;
}));
_c5 = B;
export default _c8 = React.memo(_c7 = forwardRef(_c6 = (props, ref) => {
return <h1>Foo</h1>;
}));
var _c, _c2, _c3, _c4, _c5, _c6, _c7, _c8;
$RefreshReg$(_c, "A$forwardRef");
$RefreshReg$(_c2, "A");
$RefreshReg$(_c3, "B$memo$React.forwardRef");
$RefreshReg$(_c4, "B$memo");
$RefreshReg$(_c5, "B");
$RefreshReg$(_c6, "%default%$React.memo$forwardRef");
$RefreshReg$(_c7, "%default%$React.memo");
$RefreshReg$(_c8, "%default%");
`;
exports[`ReactFreshBabelPlugin registers likely HOCs with inline functions 2`] = `
export default _c3 = React.memo(_c2 = forwardRef(_c = function (props, ref) {
return <h1>Foo</h1>;
}));
var _c, _c2, _c3;
$RefreshReg$(_c, "%default%$React.memo$forwardRef");
$RefreshReg$(_c2, "%default%$React.memo");
$RefreshReg$(_c3, "%default%");
`;
exports[`ReactFreshBabelPlugin registers likely HOCs with inline functions 3`] = `
export default _c3 = React.memo(_c2 = forwardRef(_c = function Named(props, ref) {
return <h1>Foo</h1>;
}));
var _c, _c2, _c3;
$RefreshReg$(_c, "%default%$React.memo$forwardRef");
$RefreshReg$(_c2, "%default%$React.memo");
$RefreshReg$(_c3, "%default%");
`;
exports[`ReactFreshBabelPlugin registers top-level exported function declarations 1`] = `
export function Hello() {
function handleClick() {}
return <h1 onClick={handleClick}>Hi</h1>;
}
_c = Hello;
export default function Bar() {
return <Hello />;
}
_c2 = Bar;
function Baz() {
return <h1>OK</h1>;
}
_c3 = Baz;
const NotAComp = 'hi';
export { Baz, NotAComp };
export function sum() {}
export const Bad = 42;
var _c, _c2, _c3;
$RefreshReg$(_c, "Hello");
$RefreshReg$(_c2, "Bar");
$RefreshReg$(_c3, "Baz");
`;
exports[`ReactFreshBabelPlugin registers top-level exported named arrow functions 1`] = `
export const Hello = () => {
function handleClick() {}
return <h1 onClick={handleClick}>Hi</h1>;
};
_c = Hello;
export let Bar = props => <Hello />;
_c2 = Bar;
export default (() => {
// This one should be ignored.
// You should name your components.
return <Hello />;
});
var _c, _c2;
$RefreshReg$(_c, "Hello");
$RefreshReg$(_c2, "Bar");
`;
exports[`ReactFreshBabelPlugin registers top-level function declarations 1`] = `
function Hello() {
function handleClick() {}
return <h1 onClick={handleClick}>Hi</h1>;
}
_c = Hello;
function Bar() {
return <Hello />;
}
_c2 = Bar;
var _c, _c2;
$RefreshReg$(_c, "Hello");
$RefreshReg$(_c2, "Bar");
`;
exports[`ReactFreshBabelPlugin registers top-level variable declarations with arrow functions 1`] = `
let Hello = () => {
const handleClick = () => {};
return <h1 onClick={handleClick}>Hi</h1>;
};
_c = Hello;
const Bar = () => {
return <Hello />;
};
_c2 = Bar;
var Baz = () => <div />;
_c3 = Baz;
var sum = () => {};
var _c, _c2, _c3;
$RefreshReg$(_c, "Hello");
$RefreshReg$(_c2, "Bar");
$RefreshReg$(_c3, "Baz");
`;
exports[`ReactFreshBabelPlugin registers top-level variable declarations with function expressions 1`] = `
let Hello = function () {
function handleClick() {}
return <h1 onClick={handleClick}>Hi</h1>;
};
_c = Hello;
const Bar = function Baz() {
return <Hello />;
};
_c2 = Bar;
function sum() {}
let Baz = 10;
var Qux;
var _c, _c2;
$RefreshReg$(_c, "Hello");
$RefreshReg$(_c2, "Bar");
`;
exports[`ReactFreshBabelPlugin uses custom identifiers for $RefreshReg$ and $RefreshSig$ 1`] = `
var _s = import.meta.refreshSig();
export default function Bar() {
_s();
useContext(X);
return <Foo />;
}
_s(Bar, "useContext{}");
_c = Bar;
;
var _c;
import.meta.refreshReg(_c, "Bar");
`;
exports[`ReactFreshBabelPlugin uses original function declaration if it get reassigned 1`] = `
function Hello() {
return <h1>Hi</h1>;
}
_c = Hello;
Hello = connect(Hello);
var _c;
$RefreshReg$(_c, "Hello");
`;