@tanstack/devtools-vite 0.8.1 β†’ 0.8.3

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.
Files changed (81) hide show
  1. package/dist/esm/plugin.d.ts +4 -74
  2. package/dist/esm/plugin.js +2 -12
  3. package/dist/esm/plugin.js.map +1 -1
  4. package/package.json +2 -6
  5. package/src/plugin.ts +25 -97
  6. package/dist/esm/ast-utils.d.ts +0 -11
  7. package/dist/esm/ast-utils.js +0 -44
  8. package/dist/esm/ast-utils.js.map +0 -1
  9. package/dist/esm/ast-utils.test.d.ts +0 -1
  10. package/dist/esm/devtools-packages.d.ts +0 -2
  11. package/dist/esm/devtools-packages.js +0 -15
  12. package/dist/esm/devtools-packages.js.map +0 -1
  13. package/dist/esm/devtools-packages.test.d.ts +0 -1
  14. package/dist/esm/editor.d.ts +0 -25
  15. package/dist/esm/editor.js +0 -20
  16. package/dist/esm/editor.js.map +0 -1
  17. package/dist/esm/editor.test.d.ts +0 -1
  18. package/dist/esm/enhance-logs.d.ts +0 -4
  19. package/dist/esm/enhance-logs.js +0 -51
  20. package/dist/esm/enhance-logs.js.map +0 -1
  21. package/dist/esm/enhance-logs.test.d.ts +0 -1
  22. package/dist/esm/inject-plugin.d.ts +0 -22
  23. package/dist/esm/inject-plugin.js +0 -179
  24. package/dist/esm/inject-plugin.js.map +0 -1
  25. package/dist/esm/inject-plugin.test.d.ts +0 -1
  26. package/dist/esm/inject-source.d.ts +0 -7
  27. package/dist/esm/inject-source.js +0 -114
  28. package/dist/esm/inject-source.js.map +0 -1
  29. package/dist/esm/inject-source.test.d.ts +0 -1
  30. package/dist/esm/matcher.d.ts +0 -1
  31. package/dist/esm/matcher.js +0 -13
  32. package/dist/esm/matcher.js.map +0 -1
  33. package/dist/esm/matcher.test.d.ts +0 -1
  34. package/dist/esm/offset-to-loc.d.ts +0 -14
  35. package/dist/esm/offset-to-loc.js +0 -29
  36. package/dist/esm/offset-to-loc.js.map +0 -1
  37. package/dist/esm/offset-to-loc.test.d.ts +0 -1
  38. package/dist/esm/package-manager.d.ts +0 -16
  39. package/dist/esm/package-manager.js +0 -98
  40. package/dist/esm/package-manager.js.map +0 -1
  41. package/dist/esm/remove-devtools.d.ts +0 -4
  42. package/dist/esm/remove-devtools.js +0 -96
  43. package/dist/esm/remove-devtools.js.map +0 -1
  44. package/dist/esm/remove-devtools.test.d.ts +0 -1
  45. package/dist/esm/runtime-bridge.d.ts +0 -31
  46. package/dist/esm/runtime-bridge.js +0 -75
  47. package/dist/esm/runtime-bridge.js.map +0 -1
  48. package/dist/esm/runtime-bridge.test.d.ts +0 -1
  49. package/dist/esm/utils.d.ts +0 -31
  50. package/dist/esm/utils.js +0 -155
  51. package/dist/esm/utils.js.map +0 -1
  52. package/dist/esm/utils.test.d.ts +0 -1
  53. package/dist/esm/virtual-console.d.ts +0 -19
  54. package/dist/esm/virtual-console.js +0 -336
  55. package/dist/esm/virtual-console.js.map +0 -1
  56. package/dist/esm/virtual-console.test.d.ts +0 -1
  57. package/src/ast-utils.test.ts +0 -109
  58. package/src/ast-utils.ts +0 -58
  59. package/src/devtools-packages.test.ts +0 -58
  60. package/src/devtools-packages.ts +0 -12
  61. package/src/editor.test.ts +0 -75
  62. package/src/editor.ts +0 -53
  63. package/src/enhance-logs.test.ts +0 -132
  64. package/src/enhance-logs.ts +0 -71
  65. package/src/inject-plugin.test.ts +0 -1139
  66. package/src/inject-plugin.ts +0 -300
  67. package/src/inject-source.test.ts +0 -939
  68. package/src/inject-source.ts +0 -235
  69. package/src/matcher.test.ts +0 -33
  70. package/src/matcher.ts +0 -19
  71. package/src/offset-to-loc.test.ts +0 -34
  72. package/src/offset-to-loc.ts +0 -39
  73. package/src/package-manager.ts +0 -181
  74. package/src/remove-devtools.test.ts +0 -607
  75. package/src/remove-devtools.ts +0 -190
  76. package/src/runtime-bridge.test.ts +0 -179
  77. package/src/runtime-bridge.ts +0 -113
  78. package/src/utils.test.ts +0 -184
  79. package/src/utils.ts +0 -247
  80. package/src/virtual-console.test.ts +0 -244
  81. package/src/virtual-console.ts +0 -341
@@ -1,939 +0,0 @@
1
- import { describe, expect, it } from 'vitest'
2
- import { addSourceToJsx } from './inject-source'
3
-
4
- const removeEmptySpace = (str: string) => {
5
- return str.replace(/\s/g, '').trim()
6
- }
7
-
8
- describe('inject source', () => {
9
- it("shouldn't augment react fragments", () => {
10
- const output = addSourceToJsx(
11
- `
12
- export const Route = createFileRoute("/test")({
13
- component: function() { return <>Hello World</> },
14
- })
15
- `,
16
- 'test.jsx',
17
- )
18
- expect(output).toBe(undefined)
19
- })
20
-
21
- it("shouldn't augment react fragments if they start with Fragment ", () => {
22
- const output = addSourceToJsx(
23
- `
24
- export const Route = createFileRoute("/test")({
25
- component: function() { return <Fragment>Hello World</Fragment> },
26
- })
27
- `,
28
- 'test.jsx',
29
- )
30
- expect(output).toBe(undefined)
31
- })
32
- it("shouldn't augment react fragments if they start with React.Fragment ", () => {
33
- const output = addSourceToJsx(
34
- `
35
- export const Route = createFileRoute("/test")({
36
- component: function() { return <React.Fragment>Hello World</React.Fragment> },
37
- })
38
- `,
39
- 'test.jsx',
40
- )
41
- expect(output).toBe(undefined)
42
- })
43
- describe('FunctionExpression', () => {
44
- it('should work with deeply nested custom JSX syntax', () => {
45
- const output = removeEmptySpace(
46
- addSourceToJsx(
47
- `
48
- export const Route = createFileRoute("/test")({
49
- component: function() { return <div>Hello World</div> },
50
- })
51
- `,
52
- 'test.jsx',
53
- )!.code,
54
- )
55
- expect(output).toBe(
56
- removeEmptySpace(`
57
- export const Route = createFileRoute("/test")({
58
- component: function() { return <div data-tsd-source="test.jsx:3:38">Hello World</div> },
59
- })
60
- `),
61
- )
62
- })
63
-
64
- it('should work with props not destructured and spread', () => {
65
- const output = addSourceToJsx(
66
- `
67
- export const Route = createFileRoute("/test")({
68
- component: function(props) { return <div {...props}>Hello World</div> },
69
- })
70
- `,
71
- 'test.jsx',
72
- )
73
- expect(output).toBe(undefined)
74
- })
75
-
76
- it('should work with props destructured and spread', () => {
77
- const output = addSourceToJsx(
78
- `
79
- export const Route = createFileRoute("/test")({
80
- component: function({...props}) { return <div {...props}>Hello World</div> },
81
- })
82
- `,
83
- 'test.jsx',
84
- )
85
- expect(output).toBe(undefined)
86
- })
87
-
88
- it('should work with props destructured and spread with a different name', () => {
89
- const output = addSourceToJsx(
90
- `
91
- export const Route = createFileRoute("/test")({
92
- component: function({...rest}) { return <div {...rest}>Hello World</div> },
93
- })
94
- `,
95
- 'test.jsx',
96
- )
97
- expect(output).toBe(undefined)
98
- })
99
-
100
- it('should work with props spread and other normal elements', () => {
101
- const output = removeEmptySpace(
102
- addSourceToJsx(
103
- `
104
- export const Route = createFileRoute("/test")({
105
- component: function({...rest}) { return <div><div {...rest}>Hello World</div></div> }
106
- })
107
- `,
108
- 'test.jsx',
109
- )!.code,
110
- )
111
- expect(output).toBe(
112
- removeEmptySpace(`
113
- export const Route = createFileRoute("/test")({
114
- component: function({...rest}) { return <div data-tsd-source="test.jsx:3:47"><div {...rest}>Hello World</div></div> }
115
- })
116
- `),
117
- )
118
- })
119
- })
120
-
121
- describe('ArrowFunctionExpression', () => {
122
- it('should work with deeply nested custom JSX syntax', () => {
123
- const output = removeEmptySpace(
124
- addSourceToJsx(
125
- `
126
- export const Route = createFileRoute("/test")({
127
- component: () => <div>Hello World</div>,
128
- })
129
- `,
130
- 'test.jsx',
131
- )!.code,
132
- )
133
- expect(output).toBe(
134
- removeEmptySpace(`
135
- export const Route = createFileRoute("/test")({
136
- component: () => <div data-tsd-source="test.jsx:3:24">Hello World</div>,
137
- })
138
- `),
139
- )
140
- })
141
-
142
- it('should work with props not destructured and spread', () => {
143
- const output = addSourceToJsx(
144
- `
145
- export const Route = createFileRoute("/test")({
146
- component: (props) => <div {...props}>Hello World</div>,
147
- })
148
- `,
149
- 'test.jsx',
150
- )
151
- expect(output).toBe(undefined)
152
- })
153
-
154
- it('should work with props destructured and spread', () => {
155
- const output = addSourceToJsx(
156
- `
157
- export const Route = createFileRoute("/test")({
158
- component: ({...props}) => <div {...props}>Hello World</div>,
159
- })
160
- `,
161
- 'test.jsx',
162
- )
163
- expect(output).toBe(undefined)
164
- })
165
-
166
- it('should work with props destructured and spread with a different name', () => {
167
- const output = addSourceToJsx(
168
- `
169
- export const Route = createFileRoute("/test")({
170
- component: ({...rest}) => <div {...rest}>Hello World</div>,
171
- })
172
- `,
173
- 'test.jsx',
174
- )
175
- expect(output).toBe(undefined)
176
- })
177
-
178
- it('should work with props spread and other normal elements', () => {
179
- const output = removeEmptySpace(
180
- addSourceToJsx(
181
- `
182
- export const Route = createFileRoute("/test")({
183
- component: ({...rest}) => <div><div {...rest}>Hello World</div></div>,
184
- })
185
- `,
186
- 'test.jsx',
187
- )!.code,
188
- )
189
- expect(output).toBe(
190
- removeEmptySpace(`
191
- export const Route = createFileRoute("/test")({
192
- component: ({...rest}) => <div data-tsd-source="test.jsx:3:33"><div {...rest}>Hello World</div></div>,
193
- })
194
- `),
195
- )
196
- })
197
- })
198
- describe('function declarations', () => {
199
- it('should not duplicate the same property if there are nested functions', () => {
200
- const output = removeEmptySpace(
201
- addSourceToJsx(
202
- `
203
- function Parent({ ...props }) {
204
- function Child({ ...props }) {
205
- return <div />
206
- }
207
- return <Child {...props} />
208
- }
209
- `,
210
- 'test.jsx',
211
- )!.code,
212
- )
213
- expect(output).toBe(
214
- removeEmptySpace(`
215
- function Parent({ ...props }) {
216
- function Child({ ...props }) {
217
- return <div data-tsd-source="test.jsx:4:18" />
218
- }
219
- return <Child {...props} />
220
- }
221
- `),
222
- )
223
- })
224
- it('should apply data-tsd-source from parent props if an external import', () => {
225
- const output = removeEmptySpace(
226
- addSourceToJsx(
227
- `
228
-
229
- import Custom from "external";
230
-
231
- function test({...props }) {
232
- return <Custom children={props.children} />
233
- }
234
- `,
235
- 'test.tsx',
236
- )!.code,
237
- )
238
- expect(output).toBe(
239
- removeEmptySpace(`
240
-
241
- import Custom from "external";
242
-
243
- function test({...props }) {
244
- return <Custom children={props.children} data-tsd-source="test.tsx:6:10" />
245
- }
246
- `),
247
- )
248
- })
249
- it(' props not destructured', () => {
250
- const output = removeEmptySpace(
251
- addSourceToJsx(
252
- `
253
- function test(props){
254
- return <button children={props.children} />
255
- }
256
- `,
257
- 'test.jsx',
258
- )!.code,
259
- )
260
- expect(output).toBe(
261
- removeEmptySpace(`
262
- function test(props){
263
- return <button children={props.children} data-tsd-source="test.jsx:3:16" />
264
- }
265
- `),
266
- )
267
- })
268
-
269
- it("doesn't transform when props are spread across the element", () => {
270
- const output = addSourceToJsx(
271
- `
272
- function test(props) {
273
- return <button {...props} />
274
- }
275
- `,
276
- 'test.jsx',
277
- )
278
- expect(output).toBe(undefined)
279
- })
280
-
281
- it("doesn't transform when props are spread across the element but applies to other elements without any props", () => {
282
- const output = removeEmptySpace(
283
- addSourceToJsx(
284
- `
285
- function test(props) {
286
- return (<div>
287
- <button {...props} />
288
- </div>)
289
- }
290
- `,
291
- 'test.jsx',
292
- )!.code,
293
- )
294
- expect(output).toBe(
295
- removeEmptySpace(`
296
- function test(props) {
297
- return (<div data-tsd-source="test.jsx:3:17">
298
- <button {...props} />
299
- </div>)
300
- }
301
- `),
302
- )
303
- })
304
-
305
- it("doesn't transform when props are spread across the element but applies to other elements without any props even when props are destructured", () => {
306
- const output = removeEmptySpace(
307
- addSourceToJsx(
308
- `
309
- function test({...props}) {
310
- return (<div>
311
- <button {...props} />
312
- </div>)
313
- }
314
- `,
315
- 'test.jsx',
316
- )!.code,
317
- )
318
- expect(output).toBe(
319
- removeEmptySpace(`
320
- function test({...props}) {
321
- return (<div data-tsd-source="test.jsx:3:17">
322
- <button {...props} />
323
- </div>)
324
- }
325
- `),
326
- )
327
- })
328
-
329
- it("doesn't transform when props are spread across the element but applies to other elements without any props even when props are destructured and name is different", () => {
330
- const output = removeEmptySpace(
331
- addSourceToJsx(
332
- `
333
- function test({...rest}) {
334
- return (<div>
335
- <button {...rest} />
336
- </div>)
337
- }
338
- `,
339
- 'test.jsx',
340
- )!.code,
341
- )
342
- expect(output).toBe(
343
- removeEmptySpace(`
344
- function test({...rest}) {
345
- return (<div data-tsd-source="test.jsx:3:17">
346
- <button {...rest} />
347
- </div>)
348
- }
349
- `),
350
- )
351
- })
352
-
353
- it(' props destructured and collected with a different name', () => {
354
- const output = addSourceToJsx(
355
- `
356
- function test({ children, ...rest }) {
357
- return <button children={children} {...rest} />
358
- }
359
- `,
360
- 'test.jsx',
361
- )
362
- expect(output).toBe(undefined)
363
- })
364
-
365
- it(' props destructured and collected', () => {
366
- const output = removeEmptySpace(
367
- addSourceToJsx(
368
- `
369
- function test({ ...props }) {
370
- return <button children={props.children} />
371
- }
372
- `,
373
- 'test.jsx',
374
- )!.code,
375
- )
376
- expect(output).toBe(
377
- removeEmptySpace(`
378
- function test({ ...props }) {
379
- return <button children={props.children} data-tsd-source="test.jsx:3:16" />
380
- }
381
- `),
382
- )
383
- })
384
-
385
- it('props destructured and collected with a different name even on custom components', () => {
386
- const output = addSourceToJsx(
387
- `
388
- function test({ children, ...rest }) {
389
- return <CustomButton children={children} {...rest} />
390
- }
391
- `,
392
- 'test.jsx',
393
- )
394
- expect(output).toBe(undefined)
395
- })
396
-
397
- it('props destructured and collected even on custom components', () => {
398
- const output = removeEmptySpace(
399
- addSourceToJsx(
400
- `
401
- function test({ ...props }) {
402
- return <CustomButton children={props.children} />
403
- }
404
- `,
405
- 'test.jsx',
406
- )!.code,
407
- )
408
- expect(output).toBe(
409
- removeEmptySpace(`
410
- function test({ ...props }) {
411
- return <CustomButton children={props.children} data-tsd-source="test.jsx:3:16" />
412
- }
413
- `),
414
- )
415
- })
416
-
417
- it('props destructured and collected with a different name even on custom components even if exported', () => {
418
- const output = addSourceToJsx(
419
- `
420
- function test({ children, ...rest }) {
421
- return <CustomButton children={children} {...rest} />
422
- }
423
- `,
424
- 'test.jsx',
425
- )
426
- expect(output).toBe(undefined)
427
- })
428
-
429
- it('props destructured and collected even on custom components even if exported', () => {
430
- const output = removeEmptySpace(
431
- addSourceToJsx(
432
- `
433
- function test({ ...props }) {
434
- return <CustomButton children={props.children} />
435
- }
436
- `,
437
- 'test.jsx',
438
- )!.code,
439
- )
440
- expect(output).toBe(
441
- removeEmptySpace(`
442
- function test({ ...props }) {
443
- return <CustomButton children={props.children} data-tsd-source="test.jsx:3:16" />
444
- }
445
- `),
446
- )
447
- })
448
- })
449
- describe('variable declared functions', () => {
450
- it('works with function and props not destructured', () => {
451
- const output = removeEmptySpace(
452
- addSourceToJsx(
453
- `
454
- const ButtonWithProps = function test(props){
455
- return <button children={props.children} />
456
- }
457
- `,
458
- 'test.jsx',
459
- )!.code,
460
- )
461
- expect(output).toBe(
462
- removeEmptySpace(`
463
- const ButtonWithProps = function test(props){
464
- return <button children={props.children} data-tsd-source="test.jsx:3:16" />
465
- }
466
- `),
467
- )
468
- })
469
-
470
- it("doesn't transform when props are spread across the element", () => {
471
- const output = addSourceToJsx(
472
- `
473
- const ButtonWithProps = function test(props) {
474
- return <button {...props} />
475
- }
476
- `,
477
- 'test.jsx',
478
- )
479
- expect(output).toBe(undefined)
480
- })
481
-
482
- it("doesn't transform when props are spread across the element but applies to other elements without any props", () => {
483
- const output = removeEmptySpace(
484
- addSourceToJsx(
485
- `
486
- const ButtonWithProps = function test(props) {
487
- return (<div>
488
- <button {...props} />
489
- </div>)
490
- }
491
- `,
492
- 'test.jsx',
493
- )!.code,
494
- )
495
- expect(output).toBe(
496
- removeEmptySpace(`
497
- const ButtonWithProps = function test(props) {
498
- return (<div data-tsd-source="test.jsx:3:17">
499
- <button {...props} />
500
- </div>)
501
- }
502
- `),
503
- )
504
- })
505
-
506
- it("doesn't transform when props are spread across the element but applies to other elements without any props even when props are destructured", () => {
507
- const output = removeEmptySpace(
508
- addSourceToJsx(
509
- `
510
- const ButtonWithProps = function test({...props}) {
511
- return (<div>
512
- <button {...props} />
513
- </div>)
514
- }
515
- `,
516
- 'test.jsx',
517
- )!.code,
518
- )
519
- expect(output).toBe(
520
- removeEmptySpace(`
521
- const ButtonWithProps = function test({...props}) {
522
- return (<div data-tsd-source="test.jsx:3:17">
523
- <button {...props} />
524
- </div>)
525
- }
526
- `),
527
- )
528
- })
529
-
530
- it("doesn't transform when props are spread across the element but applies to other elements without any props even when props are destructured and name is different", () => {
531
- const output = removeEmptySpace(
532
- addSourceToJsx(
533
- `
534
- const ButtonWithProps = function test({...rest}) {
535
- return (<div>
536
- <button {...rest} />
537
- </div>)
538
- }
539
- `,
540
- 'test.jsx',
541
- )!.code,
542
- )
543
- expect(output).toBe(
544
- removeEmptySpace(`
545
- const ButtonWithProps = function test({...rest}) {
546
- return (<div data-tsd-source="test.jsx:3:17">
547
- <button {...rest} />
548
- </div>)
549
- }
550
- `),
551
- )
552
- })
553
-
554
- it(' props destructured and collected with a different name', () => {
555
- const output = addSourceToJsx(
556
- `
557
- const ButtonWithProps = function test({ children, ...rest }) {
558
- return <button children={children} {...rest} />
559
- }
560
- `,
561
- 'test.jsx',
562
- )
563
- expect(output).toBe(undefined)
564
- })
565
-
566
- it(' props destructured and collected', () => {
567
- const output = removeEmptySpace(
568
- addSourceToJsx(
569
- `
570
- const ButtonWithProps = function test({ ...props }) {
571
- return <button children={props.children} />
572
- }
573
- `,
574
- 'test.jsx',
575
- )!.code,
576
- )
577
- expect(output).toBe(
578
- removeEmptySpace(`
579
- const ButtonWithProps = function test({ ...props }) {
580
- return <button children={props.children} data-tsd-source="test.jsx:3:16" />
581
- }
582
- `),
583
- )
584
- })
585
-
586
- it('props destructured and collected with a different name even on custom components', () => {
587
- const output = addSourceToJsx(
588
- `
589
- const ButtonWithProps = function test({ children, ...rest }) {
590
- return <CustomButton children={children} {...rest} />
591
- }
592
- `,
593
- 'test.jsx',
594
- )
595
- expect(output).toBe(undefined)
596
- })
597
-
598
- it('props destructured and collected even on custom components', () => {
599
- const output = removeEmptySpace(
600
- addSourceToJsx(
601
- `
602
- const ButtonWithProps = function test({ ...props }) {
603
- return <CustomButton children={props.children} />
604
- }
605
- `,
606
- 'test.jsx',
607
- )!.code,
608
- )
609
- expect(output).toBe(
610
- removeEmptySpace(`
611
- const ButtonWithProps = function test({ ...props }) {
612
- return <CustomButton children={props.children} data-tsd-source="test.jsx:3:16" />
613
- }
614
- `),
615
- )
616
- })
617
-
618
- it('props destructured and collected with a different name even on custom components even if exported', () => {
619
- const output = addSourceToJsx(
620
- `
621
- export const ButtonWithProps = function test({ children, ...rest }) {
622
- return <CustomButton children={children} {...rest} />
623
- }
624
- `,
625
- 'test.jsx',
626
- )
627
- expect(output).toBe(undefined)
628
- })
629
-
630
- it('props destructured and collected even on custom components even if exported', () => {
631
- const output = removeEmptySpace(
632
- addSourceToJsx(
633
- `
634
- export const ButtonWithProps = function test({ ...props }) {
635
- return <CustomButton children={props.children} />
636
- }
637
- `,
638
- 'test.jsx',
639
- )!.code,
640
- )
641
- expect(output).toBe(
642
- removeEmptySpace(`
643
- export const ButtonWithProps = function test({ ...props }) {
644
- return <CustomButton children={props.children} data-tsd-source="test.jsx:3:16" />
645
- }
646
- `),
647
- )
648
- })
649
- })
650
- describe('arrow functions', () => {
651
- it('works with arrow function and props not destructured', () => {
652
- const output = removeEmptySpace(
653
- addSourceToJsx(
654
- `
655
- const ButtonWithProps = (props) => {
656
- return <button children={props.children} />
657
- }
658
- `,
659
- 'test.jsx',
660
- )!.code,
661
- )
662
- expect(output).toBe(
663
- removeEmptySpace(`
664
- const ButtonWithProps = (props) => {
665
- return <button children={props.children} data-tsd-source="test.jsx:3:16" />
666
- }
667
- `),
668
- )
669
- })
670
-
671
- it("doesn't transform when props are spread across the element", () => {
672
- const output = addSourceToJsx(
673
- `
674
- const ButtonWithProps = (props) => {
675
- return <button {...props} />
676
- }
677
- `,
678
- 'test.jsx',
679
- )
680
- expect(output).toBe(undefined)
681
- })
682
-
683
- it("doesn't transform when props are spread across the element but applies to other elements without any props", () => {
684
- const output = removeEmptySpace(
685
- addSourceToJsx(
686
- `
687
- const ButtonWithProps = (props) => {
688
- return (<div>
689
- <button {...props} />
690
- </div>)
691
- }
692
- `,
693
- 'test.jsx',
694
- )!.code,
695
- )
696
- expect(output).toBe(
697
- removeEmptySpace(`
698
- const ButtonWithProps = (props) => {
699
- return (<div data-tsd-source="test.jsx:3:17">
700
- <button {...props} />
701
- </div>)
702
- }
703
- `),
704
- )
705
- })
706
-
707
- it("doesn't transform when props are spread across the element but applies to other elements without any props even when props are destructured", () => {
708
- const output = removeEmptySpace(
709
- addSourceToJsx(
710
- `
711
- const ButtonWithProps = ({...props}) => {
712
- return (<div>
713
- <button {...props} />
714
- </div>)
715
- }
716
- `,
717
- 'test.jsx',
718
- )!.code,
719
- )
720
- expect(output).toBe(
721
- removeEmptySpace(`
722
- const ButtonWithProps = ({...props}) => {
723
- return (<div data-tsd-source="test.jsx:3:17">
724
- <button {...props} />
725
- </div>)
726
- }
727
- `),
728
- )
729
- })
730
-
731
- it("doesn't transform when props are spread across the element but applies to other elements without any props even when props are destructured and name is different", () => {
732
- const output = removeEmptySpace(
733
- addSourceToJsx(
734
- `
735
- const ButtonWithProps = ({...rest}) => {
736
- return (<div>
737
- <button {...rest} />
738
- </div>)
739
- }
740
- `,
741
- 'test.jsx',
742
- )!.code,
743
- )
744
- expect(output).toBe(
745
- removeEmptySpace(`
746
- const ButtonWithProps = ({...rest}) => {
747
- return (<div data-tsd-source="test.jsx:3:17">
748
- <button {...rest} />
749
- </div>)
750
- }
751
- `),
752
- )
753
- })
754
-
755
- it('works with arrow function and props destructured and collected with a different name', () => {
756
- const output = removeEmptySpace(
757
- addSourceToJsx(
758
- `
759
- const ButtonWithProps = ({ children, ...rest }) => {
760
- return <button children={children} />
761
- }
762
- `,
763
- 'test.jsx',
764
- )!.code,
765
- )
766
- expect(output).toBe(
767
- removeEmptySpace(`
768
- const ButtonWithProps = ({ children, ...rest }) => {
769
- return <button children={children} data-tsd-source="test.jsx:3:16" />
770
- }
771
- `),
772
- )
773
- })
774
-
775
- it('works with arrow function and props destructured and collected', () => {
776
- const output = removeEmptySpace(
777
- addSourceToJsx(
778
- `
779
- const ButtonWithProps = ({ ...props }) => {
780
- return <button children={props.children} />
781
- }
782
- `,
783
- 'test.jsx',
784
- )!.code,
785
- )
786
- expect(output).toBe(
787
- removeEmptySpace(`
788
- const ButtonWithProps = ({ ...props }) => {
789
- return <button children={props.children} data-tsd-source="test.jsx:3:16" />
790
- }
791
- `),
792
- )
793
- })
794
-
795
- it('works with arrow function and props destructured and collected with a different name even on custom components', () => {
796
- const output = addSourceToJsx(
797
- `
798
- const ButtonWithProps = ({ children, ...rest }) => {
799
- return <CustomButton children={children} {...rest} />
800
- }
801
- `,
802
- 'test.jsx',
803
- )
804
- expect(output).toBe(undefined)
805
- })
806
-
807
- it('works with arrow function and props destructured and collected even on custom components', () => {
808
- const output = removeEmptySpace(
809
- addSourceToJsx(
810
- `
811
- const ButtonWithProps = ({ ...props }) => {
812
- return <CustomButton children={props.children} />
813
- }
814
- `,
815
- 'test.jsx',
816
- )!.code,
817
- )
818
- expect(output).toBe(
819
- removeEmptySpace(`
820
- const ButtonWithProps = ({ ...props }) => {
821
- return <CustomButton children={props.children} data-tsd-source="test.jsx:3:16" />
822
- }
823
- `),
824
- )
825
- })
826
-
827
- it('works with arrow function and props destructured and collected with a different name even on custom components even if exported', () => {
828
- const output = addSourceToJsx(
829
- `
830
- export const ButtonWithProps = ({ children, ...rest }) => {
831
- return <CustomButton children={children} {...rest} />
832
- }
833
- `,
834
- 'test.jsx',
835
- )
836
- expect(output).toBe(undefined)
837
- })
838
-
839
- it('works with arrow function and props destructured and collected even on custom components even if exported', () => {
840
- const output = removeEmptySpace(
841
- addSourceToJsx(
842
- `
843
- export const ButtonWithProps = ({ ...props }) => {
844
- return <CustomButton children={props.children} />
845
- }
846
- `,
847
- 'test.jsx',
848
- )!.code,
849
- )
850
- expect(output).toBe(
851
- removeEmptySpace(`
852
- export const ButtonWithProps = ({ ...props }) => {
853
- return <CustomButton children={props.children} data-tsd-source="test.jsx:3:16" />
854
- }
855
- `),
856
- )
857
- })
858
- })
859
-
860
- describe('ignore patterns', () => {
861
- it('should skip injection for ignored component names (string)', () => {
862
- const output = addSourceToJsx(
863
- `
864
- function test() {
865
- return <Button />
866
- }
867
- `,
868
- 'test.jsx',
869
- {
870
- components: ['Button'],
871
- },
872
- )
873
- expect(output).toBe(undefined)
874
- })
875
-
876
- it('should skip injection for ignored file paths (glob)', () => {
877
- const output = addSourceToJsx(
878
- `
879
- function test() {
880
- return <div />
881
- }
882
- `,
883
- 'src/components/ignored-file.jsx',
884
- {
885
- files: ['**/ignored-file.jsx'],
886
- },
887
- )
888
- expect(output).toBe(undefined)
889
- })
890
- })
891
-
892
- describe('non-ASCII source positions', () => {
893
- // oxc-parser napi bindings return UTF-16 code-unit indices (matching JS
894
- // string indexing and magic-string's expectations). These tests lock that
895
- // contract in: positions and inserted attributes must remain correct when
896
- // the source contains multi-byte UTF-8 characters.
897
-
898
- it('locates JSX correctly after an emoji string literal', () => {
899
- const source = `
900
- const label = "πŸš€ launch"
901
- function Comp() {
902
- return <div />
903
- }
904
- `
905
- const output = addSourceToJsx(source, 'emoji.jsx')!.code
906
- // line numbers are 1-based; the <div /> is on line 4
907
- expect(output).toMatch(/<div\s+data-tsd-source="emoji\.jsx:4:10"/)
908
- })
909
-
910
- it('locates JSX correctly after CJK characters', () => {
911
- const source = `
912
- // δΈ­ζ–‡ζ³¨ι‡Š
913
- function Comp() {
914
- return <button />
915
- }
916
- `
917
- const output = addSourceToJsx(source, 'cjk.jsx')!.code
918
- expect(output).toMatch(/<button\s+data-tsd-source="cjk\.jsx:4:10"/)
919
- })
920
- })
921
-
922
- describe('nested function boundaries', () => {
923
- it('does not annotate inner-function JSX with the outer propsName', () => {
924
- // The inner arrow forwards its own innerProps via spread. The outer
925
- // function must NOT inject data-tsd-source onto that inner element,
926
- // since it has no relation to the outer `props`.
927
- const output = addSourceToJsx(
928
- `
929
- const Outer = (props) => {
930
- const Inner = (innerProps) => <span {...innerProps} />
931
- return <Inner {...props} />
932
- }
933
- `,
934
- 'nested.jsx',
935
- )
936
- expect(output).toBe(undefined)
937
- })
938
- })
939
- })