-
Notifications
You must be signed in to change notification settings - Fork 32
/
rjsx-tests.el
2797 lines (2540 loc) · 75.5 KB
/
rjsx-tests.el
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
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; rjsx-tests.el --- Tests for rjsx-mode. -*- lexical-binding: t -*-
;; Copyright (C) 2016 Felipe Ochoa
;;;; Commentary:
;;
;;;; Code:
(load-file "./js2-tests.el")
(require 'rjsx-mode)
(require 'cl-lib)
(require 'ert)
(require 'ert-x)
(defun js2-mode--and-parse () ;; No point in advising, so we just overwrite this internal function
(rjsx-mode)
(js2-reparse))
(js2-deftest-parse no-attr-no-children-self-closing
"<div/>;")
(js2-deftest-parse no-attr-no-children-self-closing
"<div></div>;")
(js2-deftest-parse empty-attr-no-children
"<div attr></div>;")
(js2-deftest-parse no-children-self-closing
"<div a=\"1\" b={123} {...this.props}/>;")
(js2-deftest-parse no-attr-xml-child
"<div><span/></div>;")
(js2-deftest-parse no-attr-text-child
"<div>Hello world</div>;")
(js2-deftest-parse no-attr-expr-child
"let coolVar = false;\n<div>{coolVar ? 'abc' : 'xyz'}</div>;")
(js2-deftest-parse ultra-nested
"let fall = window.prompt('Fall?');\n<div a={<span>{fall ? <b>Fell</b> : <img src=\"s\"/>}</span>}></div>;")
(js2-deftest-parse hidden-behind-and
"let cond = true;\n<div>{cond && <span/>}</div>;")
(js2-deftest-parse ns-tag
"<xml:a/>;")
(js2-deftest-parse ns-tag-with-dashes
"<xml-lmx-m:a-b-c/>;")
(js2-deftest-parse ns-tag-with-dashes-at-end
"<xml-lmx-:a-b-/>;")
(js2-deftest-parse ns-attr
"<xml:a lmx:attr=\"1\"/>;")
(js2-deftest-parse ns-attr-with-dashes-at-end
"<xml:a lmx:attr-=\"1\"/>;")
(js2-deftest-parse ns-attr-with-dashes-at-end-of-ns
"<xml:a lmx-:attr=\"1\"/>;")
(js2-deftest-parse fragment
"<><span id='1'/><span id='2'></span></>;")
(js2-deftest-parse member-tag
"<Module.Component/>;"
:bind (js2-highlight-external-variables))
(js2-deftest-parse member-tag-many
"<Module.Component.Sub1.Sub2/>;"
:bind (js2-highlight-external-variables))
(js2-deftest-parse empty-attr-self-closing
"<Component required/>;"
:bind (js2-highlight-external-variables))
(js2-deftest-parse empty-attr-at-end
"<Component required>Hi</Component>;"
:bind (js2-highlight-external-variables))
(js2-deftest-parse empty-attr-in-middle
"<Component required otherAttr=\"123\"/>;"
:bind (js2-highlight-external-variables))
(js2-deftest-parse complex
"<form onSubmit={this.handleSubmit} className={className}>
<input type=\"text\"
onChange={this.getChangeHandler(\"name\")}
placeholder=\"Project name\"
className={errors.name ? \"invalid\" : \"\"}
ref={c => this._topInput = c}/>
{errors.name && <span className=\"error\">{errors.name}</span>}
{ } Empty is OK as child, but warning is issued
<Undeclared value={123}/>
{/* Node with comment gets no warning */}
hello <div { } /> This should be a spread, so error
<div empty={} /> Empty attributes are not allowed
{React.Children.count(this.props.children) === 1
? <OnlyChild {...this.props}>{this.props.children}</OnlyChild>
: React.Children.map(this.props.children, (child, index) => (
<li key={index} undefinedProp={notDefined}>
{index === 0 && <span className=\"first\"/>}
{React.cloneElement(child, {toolTip: <Tooltip index={index} />})}
</li>
))
}
</form>"
:errors-count 2
:warnings-count 2
:syntax-error "{ }")
(js2-deftest-parse empty-child
"<div>{}</div>;"
:warnings-count 1)
(js2-deftest-parse empty-child-with-comment
"<div>{/* this is a comment */}</div>;"
:warnings-count 0
:reference "<div>{}</div>;")
(js2-deftest-parse jsx-no-side-effects
"function abc() {\n <div/>;\n}"
:warnings-count 1)
(js2-deftest-parse undeclared-component
"const C = function() { return <Component abc={123}/>;\n};"
:warnings-count 1)
(js2-deftest-parse undeclared-component-lowercase-ok
"const C = function() { return <component abc={123}/>;\n};"
:warnings-count 0)
(ert-deftest rjsx-syntax-table-in-text ()
"Ensure JSX text sequences use the default syntax table."
(ert-with-test-buffer (:name 'rjsx)
(insert "const d = <div>{/* this is a comment */}This is")
(save-excursion (insert "' text{ anExpression }</div>;"))
(setq parse-sexp-lookup-properties t)
(js2-mode--and-parse)
(should (eq (syntax-class (syntax-after (point))) (syntax-class (string-to-syntax "."))))))
(defun rjsx-serialize-ast ()
"Return a streamlined ast representation."
(let ((stack '(())))
(js2-visit-ast
js2-mode-ast
(lambda (node end-p)
(if end-p
(push (nreverse (pop stack)) (car stack))
(push (list (js2-node-len node)
(js2-node-pos node)
(js2-node-short-name node))
stack))))
(caar stack)))
(ert-deftest rjsx-jsx-tag-names-ast ()
"Regression test for #54. Ensure the ast is properly built."
(ert-with-test-buffer (:name 'rjsx)
(insert "import Comp from 'abc';\n\nconst c = () => (\n <Comp>Child</Comp>\n);")
(js2-mode--and-parse)
(should (equal (rjsx-serialize-ast)
'("js2-ast-root" 1 66
("js2-import-node" 0 23
("js2-import-clause-node" 0 11
("js2-export-binding-node" 7 4
("js2-name-node" 0 4)))
("js2-from-clause-node" 12 10))
("js2-expr-stmt-node" 25 41
("js2-var-decl-node" 0 40
("js2-var-init-node" 6 34
("js2-name-node" 0 1)
("js2-function-node" 4 30
("js2-paren-node" 6 24
("rjsx-node" 4 18
("rjsx-member" 1 4
("rjsx-identifier" 0 4
("js2-name-node" 0 4)))
("rjsx-text" 6 5)
("rjsx-closing-tag" 11 7
("rjsx-member" 2 4
("rjsx-identifier" 0 4
("js2-name-node" 0 4)))))))))))))))
(ert-deftest rjsx-jsx-self-closing-tag-names-ast ()
"Regression test for #54. Ensure the ast is properly built."
(ert-with-test-buffer (:name 'rjsx)
(insert "import Component from 'abc';\n\nconst c = () => (\n <Component/>\n);")
(js2-mode--and-parse)
(should (equal (rjsx-serialize-ast)
'("js2-ast-root" 1 65
("js2-import-node" 0 28
("js2-import-clause-node" 0 16
("js2-export-binding-node" 7 9
("js2-name-node" 0 9)))
("js2-from-clause-node" 17 10))
("js2-expr-stmt-node" 30 35
("js2-var-decl-node" 0 34
("js2-var-init-node" 6 28
("js2-name-node" 0 1)
("js2-function-node" 4 24
("js2-paren-node" 6 18
("rjsx-node" 4 12
("rjsx-member" 1 9
("rjsx-identifier" 0 9
("js2-name-node" 0 9))))))))))))))
(ert-deftest rjsx-jsx-attr-pos-ast ()
"Regression test for #54. Ensure the ast is properly built."
(ert-with-test-buffer (:name 'rjsx)
(insert "import Comp from 'abc';\n\nconst c = () => (\n <Comp a=\"b\" {...{}}>Child</Comp>\n);")
(js2-mode--and-parse)
(should (equal (rjsx-serialize-ast)
'("js2-ast-root" 1 80
("js2-import-node" 0 23
("js2-import-clause-node" 0 11
("js2-export-binding-node" 7 4
("js2-name-node" 0 4)))
("js2-from-clause-node" 12 10))
("js2-expr-stmt-node" 25 55
("js2-var-decl-node" 0 54
("js2-var-init-node" 6 48
("js2-name-node" 0 1)
("js2-function-node" 4 44
("js2-paren-node" 6 38
("rjsx-node" 4 32
("rjsx-member" 1 4
("rjsx-identifier" 0 4
("js2-name-node" 0 4)))
("rjsx-attr" 6 5
("rjsx-identifier" 0 1
("js2-name-node" 0 1))
("js2-string-node" 2 3))
("rjsx-spread" 12 7
("js2-object-node" 4 2))
("rjsx-text" 20 5)
("rjsx-closing-tag" 25 7
("rjsx-member" 2 4
("rjsx-identifier" 0 4
("js2-name-node" 0 4)))))))))))))))
;;; Now we test all of the malformed bits:
(defun jsx-test--forms (forms)
"Test the parsing of FORMS.
FORMS must be a list of (CODE-STRING SYNTAX-ERROR &optional ERRORS-COUNT)
forms, where the values are as in `js2-parse-string'."
(ert-with-test-buffer (:name 'origin)
(dolist (form forms)
(cl-destructuring-bind (code-string syntax-error &optional (errors-count 1)) form
(erase-buffer)
(let* ((ast (js2-test-string-to-ast code-string))
(errors (js2-ast-root-errors ast)))
(should (= errors-count (length errors)))
(cl-destructuring-bind (_ pos len) (car (last errors))
(should (string= syntax-error (substring code-string
(1- pos) (+ pos len -1))))))
(message (format "Completed test for: %s" code-string))))))
(cl-defmacro jsx-deftest (name &rest forms)
"Define a new test for compactly checking multiple strings' parsing.
The test is named rjsx-NAME. FORMS is a list of (CODE-STRING
SYNTAX-ERROR ERRORS-COUNT) forms, where the values are as in
`js2-parse-string'. `:expect-fail' can be inserted anywhere between
between the forms to split the test into two: forms before the marker
are expected to pass and forms after the marker are expected to fail.
Currently only forms with syntax errors are supported.
\(fn NAME PASS-FORMS... [:expect-fail FAIL-FORMS...])"
(declare (indent defun))
(let (fail-forms pass-forms found-marker)
(dolist (form forms)
(if (eq :expect-fail form)
(if found-marker
(error "Received multiple :expect-fail markers")
(setq found-marker t))
(if found-marker
(push form fail-forms)
(push form pass-forms))))
(setq fail-forms (nreverse fail-forms)
pass-forms (nreverse pass-forms))
(let* ((i 0)
(tests (nconc
(and pass-forms
(list `(ert-deftest ,(intern (format "rjsx-%s" name)) ()
(jsx-test--forms ',pass-forms))))
(mapcar
(lambda (form)
`(ert-deftest ,(intern (format "rjsx-%s-expected-fail-%d" name (cl-incf i))) ()
:expected-result :failed
(jsx-test--forms '(,form))))
fail-forms))))
(cl-case (length tests)
(0 (error "Did not specify any forms"))
(1 (car tests))
(t `(progn ,@tests))))))
(defvar rjsx--find-test-regexp
(concat "^\\s-*(rjsx-deftest"
find-function-space-re
"%s\\(\\s-\\|$\\)")
"The regexp the `find-function' mechanisms use for finding RJSX test definitions.")
(push '(rjsx-deftest . rjsx--find-test-regexp) find-function-regexp-alist)
(defun rjsx-find-test-advice (orig-fn test-name)
"Advice for `ert-find-test-other-window' (ORIG-FN) to find TEST-NAME."
(interactive (list (ert-read-test-name-at-point "Find test definition: ")))
(if (string-match "^rjsx-\\([a-z-]+?\\)\\(-expected-fail\\)?$" (symbol-name test-name))
(let* ((file (symbol-file test-name 'ert-deftest))
(buffer-point (find-definition-noselect (intern (match-string 1 (symbol-name test-name)))
'rjsx-deftest
file))
(switch-fn 'switch-to-buffer-other-window)
;; The rest of this let-form was copy-pasted from
;; `find-funciton-do-it' to be able to override the
;; buffer-finding bit
(orig-point (point))
(orig-buffers (buffer-list))
;(buffer-point (save-excursion (find-definition-noselect symbol type)))
(new-buf (car buffer-point))
(new-point (cdr buffer-point)))
(when buffer-point
(when (memq new-buf orig-buffers)
(push-mark orig-point))
(funcall switch-fn new-buf)
(when new-point (goto-char new-point))
(recenter find-function-recenter-line)
(run-hooks 'find-function-after-hook)))
(funcall orig-fn test-name)))
(advice-add 'ert-find-test-other-window :around #'rjsx-find-test-advice)
;; Tag problems
(jsx-deftest mismatched-tags
("<div></vid>" "</vid>")
("<div-></vid->" "</vid->")
("<div-></div>" "</div>")
("<div-name></divname>" "</divname>")
("<ns:div></div>" "</div>")
("<ns-:div></ns:div>" "</ns:div>")
("<ns-a:div></nsa:div>" "</nsa:div>")
("<ns-a:div-></ns-a:div>" "</ns-a:div>"))
(js2-deftest-parse invalid-tag-member-and-ns-self-closing
"<xml:Component.Child/>"
:syntax-error ".") ;; TODO: report the error over the entire tag
(js2-deftest-parse invalid-ns-tag-with-double-dashes
"<xml-lmx--m:a-b-c/>;"
:syntax-error "--") ;; TODO: report the error over the entire tag
(js2-deftest-parse invalid-tag-whitespace-before-dash
"<div -attr/>"
:syntax-error "-")
(js2-deftest-parse missing-closing-lt-self-closing
"<div/"
:syntax-error "<div/")
(js2-deftest-parse invalid-tag-name
"<123 />"
:errors-count 2
:syntax-error "123")
(js2-deftest-parse invalid-tag-name-only-ns
"<abc: />"
:syntax-error "abc:")
(js2-deftest-parse invalid-attr-name-only-ns
"<xyz abc:={1} />"
:syntax-error "abc:")
;; Make sure we don't hang with unclosed tags
(js2-deftest-parse falls-off-a-cliff-but-doesnt-hang
"const Component = ({prop}) => <span>;\n\nexport default Component;"
:syntax-error "<span>;\n\nexport default Component;")
(js2-deftest-parse falls-off-a-cliff-but-doesnt-hang-even-with-braces
"const Component = ({prop}) => <span>;\n\nexport { Component };"
:syntax-error "<span>;\n\nexport { Component };")
(js2-deftest-parse falls-off-a-cliff-but-doesnt-hang-even-with-other-jsx
"const Component = ({prop}) => <span>;\nconst C2 = () => <span></span>\n\n"
:errors-count 2 ; 1 from the stray > in the arrow function and 1 from the missing closer
:syntax-error ";\nconst C2 = () =>")
(js2-deftest-parse falls-off-a-cliff-in-recursive-parse
"const Component = ({prop}) => <div>{pred && <span>};\n\nexport { Component }"
:errors-count 2
:syntax-error "}")
(jsx-deftest empty-tag-encounter-survived
("</>" "<")
("<div></></div>" "<")
("<div>{a && </>}</div>" "<"))
;; Malformed attributes have a number of permutations:
;;
;; A/ Missing value, missing right curly, bad expression
;; B/ Before another attribute (spread vs expr vs string) or
;; at the end of the tag (self-closing or not)
;; C/ No dashes in its name, ends in a dash, dashes but not at the end
;; D/ With a namespaced name or not
;;
;; Combinatorial explosion! 3 * 5 * 3 * 2 = 90!
;; Here are all the missing values sign tests:
(jsx-deftest attr-missing-value-no-dashes
("<div attr= attr2={123}/>" "attr=")
("<div attr= attr2=\"123\"/>" "attr=")
("<div attr=/>" "attr=")
:expect-fail
("<div attr= {...attr2}/>" "attr=") ; spread parsed as attribute value
("<div attr=></div>" "attr=")) ; JS2 parses the => as an arrow
(jsx-deftest attr-missing-value-ends-in-dash
("<div attr-= attr2={123}/>" "attr-=")
("<div attr-= attr2=\"123\"/>" "attr-=")
("<div attr-=/>" "attr-=")
:expect-fail
("<div attr-= {...attr2}/>" "attr-=")
("<div attr-=></div>" "attr-="))
(jsx-deftest attr-missing-value-interior-dashes
("<div attr-name= attr2={123}/>" "attr-name=")
("<div attr-name= attr2=\"123\"/>" "attr-name=")
("<div attr-name=/>" "attr-name=")
:expect-fail
("<div attr-name= {...attr2}/>" "attr-name=")
("<div attr-name=></div>" "attr-name="))
(jsx-deftest attr-missing-value-no-dashes-namespaced
("<div ns:attr= attr2={123}/>" "ns:attr=")
("<div ns:attr= attr2=\"123\"/>" "ns:attr=")
("<div ns:attr=/>" "ns:attr=")
:expect-fail
("<div ns:attr= {...attr2}/>" "ns:attr=")
("<div ns:attr=></div>" "ns:attr="))
(jsx-deftest attr-missing-value-ends-in-dash-namespaced
("<div ns:attr-= attr2={123}/>" "ns:attr-=")
("<div ns:attr-= attr2=\"123\"/>" "ns:attr-=")
("<div ns:attr-=/>" "ns:attr-=")
:expect-fail
("<div ns:attr-= {...attr2}/>" "ns:attr-=")
("<div ns:attr-=></div>" "ns:attr-="))
(jsx-deftest attr-missing-value-interior-dashes-namespaced
("<div ns:attr-name= attr2={123}/>" "ns:attr-name=")
("<div ns:attr-name= attr2=\"123\"/>" "ns:attr-name=")
("<div ns:attr-name=/>" "ns:attr-name=")
:expect-fail
("<div ns:attr-name= {...attr2}/>" "ns:attr-name=")
("<div ns:attr-name=></div>" "ns:attr-name="))
;; Missing right curly in attribute
(jsx-deftest attr-missing-rc-no-dashes
:expect-fail
("<div attr={123 {...attr2}/>" "attr={123")
("<div attr={123 attr2={123}/>" "attr={123")
("<div attr={123 attr2=\"123\"/>" "attr={123")
("<div attr={123/>" "attr={123")
("<div attr={123></div>" "attr={123"))
(jsx-deftest attr-missing-rc-ends-in-dash
:expect-fail
("<div attr-={123 {...attr2}/>" "attr-={123")
("<div attr-={123 attr2={123}/>" "attr-={123")
("<div attr-={123 attr2=\"123\"/>" "attr-={123")
("<div attr-={123/>" "attr-={123")
("<div attr-={123></div>" "attr-={123"))
(jsx-deftest attr-missing-rc-interior-dashes
:expect-fail
("<div attr-name={123 {...attr2}/>" "attr-name={123")
("<div attr-name={123 attr2={123}/>" "attr-name={123")
("<div attr-name={123 attr2=\"123\"/>" "attr-name={123")
("<div attr-name={123/>" "attr-name={123")
("<div attr-name={123></div>" "attr-name={123"))
(jsx-deftest attr-missing-rc-no-dashes-namespaced
:expect-fail
("<div ns:attr={123 {...attr2}/>" "ns:attr={123")
("<div ns:attr={123 attr2={123}/>" "ns:attr={123")
("<div ns:attr={123 attr2=\"123\"/>" "ns:attr={123")
("<div ns:attr={123/>" "ns:attr={123")
("<div ns:attr={123></div>" "ns:attr={123"))
(jsx-deftest attr-missing-rc-ends-in-dash-namespaced
:expect-fail
("<div ns:attr-={123 {...attr2}/>" "ns:attr-={123")
("<div ns:attr-={123 attr2={123}/>" "ns:attr-={123")
("<div ns:attr-={123 attr2=\"123\"/>" "ns:attr-={123")
("<div ns:attr-={123/>" "ns:attr-={123")
("<div ns:attr-={123></div>" "ns:attr-={123"))
(jsx-deftest attr-missing-rc-interior-dashes-namespaced
:expect-fail
("<div ns:attr-name={123 {...attr2}/>" "ns:attr-name={123")
("<div ns:attr-name={123 attr2={123}/>" "ns:attr-name={123")
("<div ns:attr-name={123 attr2=\"123\"/>" "ns:attr-name={123")
("<div ns:attr-name={123/>" "ns:attr-name={123")
("<div ns:attr-name={123></div>" "ns:attr-name={123"))
;; Here are all the bad values sign tests:
(jsx-deftest attr-bad-value-no-dashes
("<div attr={&&} attr2={123}/>" "{&&}")
("<div attr={&&} {...attr2}/>" "{&&}")
("<div attr={&&} attr2=\"123\"/>" "{&&}")
("<div attr={&&}/>" "{&&}")
("<div attr={&&}></div>" "{&&}"))
(jsx-deftest attr-bad-value-ends-in-dash
("<div attr-={&&} {...attr2}/>" "{&&}")
("<div attr-={&&} attr2={123}/>" "{&&}")
("<div attr-={&&} attr2=\"123\"/>" "{&&}")
("<div attr-={&&}/>" "{&&}")
("<div attr-={&&}></div>" "{&&}"))
(jsx-deftest attr-bad-value-interior-dashes
("<div attr-name={&&} {...attr2}/>" "{&&}")
("<div attr-name={&&} attr2={123}/>" "{&&}")
("<div attr-name={&&} attr2=\"123\"/>" "{&&}")
("<div attr-name={&&}/>" "{&&}")
("<div attr-name={&&}></div>" "{&&}"))
(jsx-deftest attr-bad-value-no-dashes-namespaced
("<div ns:attr={&&} {...attr2}/>" "{&&}")
("<div ns:attr={&&} attr2={123}/>" "{&&}")
("<div ns:attr={&&} attr2=\"123\"/>" "{&&}")
("<div ns:attr={&&}/>" "{&&}")
("<div ns:attr={&&}></div>" "{&&}"))
(jsx-deftest attr-bad-value-ends-in-dash-namespaced
("<div ns:attr-={&&} {...attr2}/>" "{&&}")
("<div ns:attr-={&&} attr2={123}/>" "{&&}")
("<div ns:attr-={&&} attr2=\"123\"/>" "{&&}")
("<div ns:attr-={&&}/>" "{&&}")
("<div ns:attr-={&&}></div>" "{&&}"))
(jsx-deftest attr-bad-value-interior-dashes-namespaced
("<div ns:attr-name={&&} {...attr2}/>" "{&&}")
("<div ns:attr-name={&&} attr2={123}/>" "{&&}")
("<div ns:attr-name={&&} attr2=\"123\"/>" "{&&}")
("<div ns:attr-name={&&}/>" "{&&}")
("<div ns:attr-name={&&}></div>" "{&&}"))
;; Invalid jsx-strings
(js2-deftest-parse invalid-jsx-string-in-attr
"<div a=\"He said, \\\"Don't you worry child\\\"\"/>"
:syntax-error "\"He said, \\\"Don't you worry child\\\"\"")
(js2-deftest-parse invalid-jsx-string-in-attr-single-quotes
"<div a='He said, \"Don\\'t you worry child\"'/>"
:syntax-error "'He said, \"Don\\'t you worry child\"'")
;; Spread-specific errors also have some combinatorial complexity:
;; A/ Missing value or bad value or good value
;; B/ With or without dots
;; C/ With or without right curly (except if good value and dots are there)
;; D/ Before another attribute (spread vs expr vs string) or
;; at the end of the tag (self-closing or not)
;;
;; Total = (3 * 2 * 2 - 1) * 5 = 55
(jsx-deftest spread-no-value-no-dots-no-rc
:expect-fail
("<div { {...other}/>" "{")
("<div { attr={123}/>" "{")
("<div { attr=\"123\"/>" "{")
("<div { />" "{")
("<div { ></div>" "{"))
(jsx-deftest spread-no-value-no-dots-with-rc
("<div {} {...other}/>" "{}")
("<div {} attr={123}/>" "{}")
("<div {} attr=\"123\"/>" "{}")
("<div {} />" "{}")
("<div {} ></div>" "{}"))
(jsx-deftest spread-no-value-with-dots-no-rc
:expect-fail
("<div {... {...other}/>" "{...")
("<div {... attr={123}/>" "{...")
("<div {... attr=\"123\"/>" "{...")
("<div {... />" "{...")
("<div {... ></div>" "{..."))
(jsx-deftest spread-no-value-with-dots-with-rc
("<div {...} />" "{...}")
("<div {...} attr={123}/>" "{...}")
("<div {...} {...other}/>" "{...}")
("<div {...} attr=\"123\"/>" "{...}")
("<div {...} ></div>" "{...}"))
(jsx-deftest spread-bad-value-no-dots-no-rc
:expect-fail
("<div {&& {...other}/>" "{&&")
("<div {&& attr={123}/>" "{&&")
("<div {&& attr=\"123\"/>" "{&&")
("<div {&& />" "{&&")
("<div {&& ></div>" "{&&"))
(jsx-deftest spread-bad-value-no-dots-with-rc
("<div {&&} {...other}/>" "{&&}")
("<div {&&} attr={123}/>" "{&&}")
("<div {&&} attr=\"123\"/>" "{&&}")
("<div {&&} />" "{&&}")
("<div {&&} ></div>" "{&&}"))
(jsx-deftest spread-bad-value-with-dots-with-rc
("<div {...&&} {...other}/>" "{...&&}")
("<div {...&&} attr={123}/>" "{...&&}")
("<div {...&&} attr=\"123\"/>" "{...&&}")
("<div {...&&} />" "{...&&}")
("<div {...&&} ></div>" "{...&&}"))
(jsx-deftest spread-good-value-no-dots-no-rc
:expect-fail
("<div {{a: 123} {...other}/>" "{{a: 123}")
("<div {{a: 123} attr={123}/>" "{{a: 123}")
("<div {{a: 123} attr=\"123\"/>" "{{a: 123}")
("<div {{a: 123} />" "{{a: 123}")
("<div {{a: 123} ></div>" "{{a: 123}"))
(jsx-deftest spread-good-value-no-dots-with-rc
("<div {{a: 123}} {...other}/>" "{{a: 123}}")
("<div {{a: 123}} attr={123}/>" "{{a: 123}}")
("<div {{a: 123}} attr=\"123\"/>" "{{a: 123}}")
("<div {{a: 123}} />" "{{a: 123}}")
("<div {{a: 123}} ></div>" "{{a: 123}}"))
;; Other odds and ends
(ert-deftest rjsx-<->-token-class ()
(ert-with-test-buffer (:name 'origin)
(dolist (test '(("<" "div/" ">")
("<" "div" ">" "" "<" "/div" ">")
("<" "div" ">" "" "<" "/vid" ">")
("<" "" ">" "" "<" "/" ">")
("<" "div" ">" "\n hi\n {123 < 5}\n " "<" "span/" ">" "\n" "<" "/div" ">")))
(erase-buffer)
(mapc #'insert test)
(rjsx-mode)
(js2-reparse)
(goto-char (point-min))
(let ((is-tag t))
(dolist (frag test)
(cond
((string= frag ""))
(is-tag (should (memq (get-char-property (point) 'rjsx-class) '(< >))))
(t (should-not (memq (get-char-property (point) 'rjsx-class) '(< >)))))
(setq is-tag (not is-tag))
(forward-char (length frag)))))))
(ert-deftest rjsx-node-opening-tag ()
(ert-with-test-buffer (:name 'origin)
(dolist (test '(("<div/>" "div" "div")
("<div></div>" "div" "div")
("<div></vid>" "div" "div")
("<C-d-e:f-g-h-></C-d-e:f-g-h->" "C-d-e:f-g-h" "C-d-e:f-g-h")
("<C.D.E></C.D.E>" "C.D.E" "C.D.E")
("<C-a.D-a.E-a/>" "C-a.D-a.E-a" nil)
("<></>" "" "")))
(erase-buffer)
(js2-visit-ast
(js2-test-string-to-ast (car test))
(lambda (node end-p)
(when (not end-p)
(cond
((rjsx-node-p node)
(should (string= (cadr test) (rjsx-node-opening-tag-name node))))
((rjsx-closing-tag-p node)
(should (string= (caddr test) (rjsx-closing-tag-full-name node))))))
nil)))))
(ert-deftest rjsx-electric-lt ()
(let ((cases '("let c = "
"let c = (\n "
"let c = (\n <div>\n "
"let c = name => "
"let c = (\n <div>\n {value}\n "
"let c = <div a={"
"let c = [\n <div />,\n "
"let c = <div>{a && "
"let c = <div>{a || "
"let c = <div>{a ? "
"let c = <div>{a ? null :"
"return "
"return /*\n hello world \n*/\n // more comments\n ")))
(ert-with-test-buffer (:name 'origin)
(dolist (contents cases)
(insert contents)
(rjsx-mode) ; rjsx-electric-lt depends on the syntax table
(rjsx-electric-lt 1)
(should (string= (buffer-substring-no-properties (point-min) (point))
(concat contents "<")))
(should (string= (buffer-substring-no-properties (point) (point-max))
"/>"))
(erase-buffer)))))
(ert-deftest rjsx-electric-lt-in-jsx-text ()
"Regression test for #68"
;; This test ensures that newlines are properly skipped in JSX text when looking for the prior
;; token. In JSX text, newlines are not whitespace (cf #67) or comment enders, so aren't skipped
;; over by forward-comment
(ert-with-test-buffer (:name 'electric-lt-in-jsx-text)
(let ((pre "let c = (\n <div>\n ")
(post "\n </div>\n);"))
(insert pre)
(save-excursion (insert post))
(rjsx-mode)
(js2-reparse)
(rjsx-electric-lt 1)
(should (string= (buffer-substring-no-properties (point-min) (point))
(concat pre "<")))
(should (string= (buffer-substring-no-properties (point) (point-max))
(concat "/>" post)))
(erase-buffer))))
(ert-deftest rjsx-electric-lt-grounded ()
(let ((cases '("let c = 3 "
"if (n "
"(abc && def) ")))
(ert-with-test-buffer (:name 'origin)
(dolist (contents cases)
(insert contents)
(rjsx-electric-lt 1)
(should (string= (buffer-substring-no-properties (point-min) (point))
(concat contents "<")))
(should (string= (buffer-substring-no-properties (point) (point-max))
""))
(erase-buffer)))))
(ert-deftest rjsx-electric-lt-prefix-arg ()
(let ((cases '("let c = "
"let c = (\n "
"let c = (\n <div>\n "
"let c = name => "
"let c = (\n <div>\n {value}\n "
"let c = <div a={"
"let c = [\n <div />,\n "
"let c = <div>{a && "
"let c = <div>{a || "
"let c = <div>{a ? "
"let c = <div>{a ? null :"
"return "
"return /*\n hello world \n*/\n // more comments\n ")))
(ert-with-test-buffer (:name 'origin)
(dolist (contents cases)
(insert contents)
(rjsx-mode)
(rjsx-electric-lt 3)
(should (string= (buffer-substring-no-properties (point-min) (point))
(concat contents "<<<")))
(should (string= (buffer-substring-no-properties (point) (point-max))
""))
(erase-buffer)))))
(ert-deftest rjsx-electric-gt ()
(let ((cases '("let c = (\n <div>\n <Component a=\"123\"/>\n </div>)"
"let c = <Component/>"
"let c = (\n <Component {...props}/>\n)"
"let c = name => <Component b='123'/>"
"let c = (\n <div>\n {value}\n <Component/>\n </div>)"
"let c = <div a={<Component/>}/>"
"let c = <div>{a && <Component a={123}/>}</div>"
"let c = <div>{a || <Component/>}</div>"
"let c = <div>{a ? <Component/> : null}</div>"
"let c = <div>{a ? null : <Component/>}</div>"
"return <Component/>")))
(ert-with-test-buffer (:name 'origin)
(dolist (contents cases)
(insert contents)
(goto-char 0)
(search-forward "/>")
(backward-char 2)
(js2-mode--and-parse)
(let ((start-point (point)))
(rjsx-electric-gt 1)
(should (= (1+ start-point) (point)))
(should (string= (buffer-substring-no-properties (point-min) (point))
(concat (substring contents 0 (1- start-point)) ">")))
(should (string= (buffer-substring-no-properties (point) (point-max))
(concat "</Component>" (substring contents (1+ start-point)))))
(erase-buffer))
(message "succeeded with %s" (prin1 contents))))))
(ert-deftest rjsx-electric-gt-grounded ()
(let ((cases '(("let C = () => (\n <WithRegex a={" . "/>/}></WithRegex>\n);")
("let c = 3 " . "+ 4")
("if (n " . "=== undefined) return;")
("(abc && def) " . "\n")
("<Component><". "/Component"))))
(ert-with-test-buffer (:name 'origin)
(dolist (contents cases)
(insert (car contents))
(save-excursion (insert (cdr contents)))
(js2-mode--and-parse)
(rjsx-electric-gt 1)
(should (string= (buffer-substring-no-properties (point-min) (point))
(concat (car contents) ">")))
(should (string= (buffer-substring-no-properties (point) (point-max))
(cdr contents)))
(erase-buffer)))))
(ert-deftest rjsx-electric-gt-prefix-arg ()
(let ((cases '("let c = (\n <div>\n <Component a=\"123\"/>\n </div>)"
"let c = <Component/>"
"let c = (\n <Component {...props}/>\n)"
"let c = name => <Component b='123'/>"
"let c = (\n <div>\n {value}\n <Component/>\n </div>)"
"let c = <div a={<Component/>}/>"
"let c = <div>{a && <Component a={123}/>}</div>"
"let c = <div>{a || <Component/>}</div>"
"let c = <div>{a ? <Component/> : null}</div>"
"let c = <div>{a ? null : <Component/>}</div>"
"return <Component/>")))
(ert-with-test-buffer (:name 'origin)
(dolist (contents cases)
(insert contents)
(goto-char 0)
(search-forward "/>")
(backward-char 2)
(js2-mode--and-parse)
(let ((start-point (point)))
(rjsx-electric-gt 2)
(should (= (+ 2 start-point) (point)))
(should (string= (buffer-substring-no-properties (point-min) (point))
(concat (substring contents 0 (1- start-point)) ">>")))
(should (string= (buffer-substring-no-properties (point) (point-max))
(substring contents (1- start-point))))
(erase-buffer))
(message "succeeded with %s" (prin1 contents))))))
(ert-deftest rjsx-delete-creates-full-tag ()
(let ((cases '("let c = (\n <div>\n <Component a=\"123\"/>\n </div>)"
"let c = <Component/>"
"let c = (\n <Component {...props}/>\n)"
"let c = name => <Component b='123'/>"
"let c = (\n <div>\n {value}\n <Component/>\n </div>)"
"let c = <div a={<Component/>}/>"
"let c = <div>{a && <Component a={123}/>}</div>"
"let c = <div>{a || <Component/>}</div>"
"let c = <div>{a ? <Component/> : null}</div>"
"let c = <div>{a ? null : <Component/>}</div>"
"return <Component/>")))
(ert-with-test-buffer (:name 'origin)
(dolist (contents cases)
(insert contents)
(goto-char 0)
(search-forward "/>")
(backward-char 2)
(js2-mode--and-parse)
(let ((start-point (point)))
(rjsx-delete-creates-full-tag 1)
(should (= (1+ start-point) (point)))
(should (string= (buffer-substring-no-properties (point-min) (point))
(concat (substring contents 0 (1- start-point)) ">")))
(should (string= (buffer-substring-no-properties (point) (point-max))
(concat "</Component>" (substring contents (1+ start-point)))))
(erase-buffer))
(message "succeeded with %s" (prin1 contents))))))
(ert-deftest rjsx-delete-normal ()
(let ((cases '(("let C = () => (\n <WithRegex a={" . "/>/}></WithRegex>\n);")
("let c = 3 " . "+ 4")
("if (n " . "=== undefined) return;")
("(abc && def) " . "\n")
("<Component><". "/Component"))))
(ert-with-test-buffer (:name 'origin)
(dolist (contents cases)
(insert (car contents))
(save-excursion (insert (cdr contents)))
(js2-mode--and-parse)
(rjsx-delete-creates-full-tag 1)
(should (string= (buffer-substring-no-properties (point-min) (point))
(car contents)))
(should (string= (buffer-substring-no-properties (point) (point-max))
(substring (cdr contents) 1)))
(erase-buffer)))))
(ert-deftest rjsx-delete-normal-region ()
(let ((cases '(("let C = () => (\n <WithRegex a={" . "/>/}></WithRegex>\n);")
("let c = 3 " . "+ 4")
("if (n " . "=== undefined) return;")
("<Component><". "/Component"))))
(ert-with-test-buffer (:name 'origin)
(dolist (contents cases)
(insert (car contents))
(save-excursion (insert (cdr contents)))
(js2-mode--and-parse)
(let ((transient-mark-mode 1)
(delete-active-region t))
(set-mark (point))
(forward-char 3)
(call-interactively 'rjsx-delete-creates-full-tag))
(should (string= (buffer-substring-no-properties (point-min) (point))
(car contents)))
(should (string= (buffer-substring-no-properties (point) (point-max))
(substring (cdr contents) 3)))
(erase-buffer)))))
(ert-deftest rjsx--tag-at-point ()
(let ((cases '(("let c = (\n " " <div>\n <Component a=\"123\"/>\n </div>)" nil)
("let c = (\n " "<div>\n <Component a=\"123\"/>\n </div>)" "div")
("let c = (\n <" "div>\n <Component a=\"123\"/>\n </div>)" "div")
("let c = (\n <di" "v>\n <Component a=\"123\"/>\n </div>)" "div")
("let c = (\n <div" ">\n <Component a=\"123\"/>\n </div>)" "div")
("let c = (\n <div>\n " " <Component a=\"123\"/>\n </div>)" "div")
("let c = (\n <div>\n " "<Component a=\"123\"/>\n </div>)" "Component")
("let c = (\n <div>\n <" "Component a=\"123\"/>\n </div>)" "Component")
("let c = (\n <div>\n <Compo" "nent a=\"123\"/>\n </div>)" "Component")
("let c = (\n <div>\n <Component" " a=\"123\"/>\n </div>)" "Component")
("let c = (\n <div>\n <Component " "a=\"123\"/>\n </div>)" "Component")
("let c = (\n <div>\n <Component a" "=\"123\"/>\n </div>)" "Component")
("let c = (\n <div>\n <Component a=" "\"123\"/>\n </div>)" "Component")
("let c = (\n <div>\n <Component a=\"1" "23\"/>\n </div>)" "Component")
("let c = (\n <div>\n <Component a=\"123\"" "/>\n </div>)" "Component")
("let c = (\n <div>\n <Component a=\"123\"/" ">\n </div>)" "Component")
("let c = (\n <div>\n <Component a=\"123\"/>" "\n </div>)" "div")
("let c = (\n " " <Component {...props}/>\n)" nil)
("let c = (\n <Component " "{...props}/>\n)" "Component")
("let c = (\n <Component {" "...props}/>\n)" "Component")
("let c = (\n <Component {." "..props}/>\n)" "Component")
("let c = (\n <Component {..." "props}/>\n)" "Component")
("let c = (\n <Component {...pr" "ops}/>\n)" "Component")
("let c = (\n <Component {...props" "}/>\n)" "Component")
("let c = (\n <Component {...props}" "/>\n)" "Component")
("let c = (\n <Component {...props}/" ">\n)" "Component")
("let c = (\n <Component {...props}/>" "\n)" nil)
("let c = (\n <div>\n " "{value}\n <Component/>\n </div>)" "div")
("let c = (\n <div>\n {" "value}\n <Component/>\n </div>)" "div")
("let c = (\n <div>\n {v" "alue}\n <Component/>\n </div>)" "div")
("let c = (\n <div>\n {value" "}\n <Component/>\n </div>)" "div")
("let c = (\n <div>\n {value}" "\n <Component/>\n </div>)" "div")
("let c = <div a=" "{<Component/>}/>" "div")
("let c = <div a={" "<Component/>}/>" "Component")
("let c = <div a={<" "Component/>}/>" "Component")
("let c = <div a={<Comp" "onent/>}/>" "Component")
("let c = <div a={<Component" "/>}/>" "Component")
("let c = <div a={<Component/" ">}/>" "Component")
("let c = <div a={<Component/>" "}/>" "div")
("let c = <div a={<Component/>}" "/>" "div")
("let c = <div a={<Component/>}/" ">" "div")
("let c = <div a={<Component/>}/>" "" nil)
("let c = <div>{a && " "<Component a={123}/>}</div>" "Component")
("let c = <div>{a && <Component a={123}/" ">}</div>" "Component")
("let c = <div>{a &&" " <Component a={123}/>}</div>" "div")
("let c = <div>{a && <Component a={123}/>" "}</div>" "div"))))
(ert-with-test-buffer (:name 'origin)
(dolist (case cases)
(erase-buffer)
(cl-destructuring-bind (pre post exp) case
(insert pre)
(save-excursion (insert post))
(js2-mode--and-parse)
(let ((tag (rjsx--tag-at-point)))
(cond
(exp
(should (not (eq nil tag)))
(should (rjsx-node-p tag))
(should (string= (rjsx-node-opening-tag-name tag) exp)))
(t
(should (eq nil tag))))))))))
(ert-deftest rjsx-rename-tag-at-point ()
(let ((cases '(("let c = (\n " " <div>\n <Component a=\"123\"/>\n </div>)"
"let c = (\n <div>\n <Component a=\"123\"/>\n </div>)")
("let c = (\n <div>\n <Compo" "nent a=\"123\"/>\n </div>)"
"let c = (\n <div>\n <NewName a=\"123\"/>\n </div>)")
("let c = (\n <Component {" "...props}/>\n)"
"let c = (\n <NewName {...props}/>\n)")
("let c = <div a={<Comp" "onent/>}/>"
"let c = <div a={<NewName/>}/>")
("let c = <div>{a && <Component a={123}/" ">}</div>"
"let c = <div>{a && <NewName a={123}/>}</div>")
("let c = <div>{a && <" "></>}</div>"
"let c = <div>{a && <NewName></NewName>}</div>")
("let c = <div>{a && <>x" "yz\n <Component/></>}</div>"
"let c = <div>{a && <NewName>xyz\n <Component/></NewName>}</div>")
("let c = <div>{a && <" "><Component/></>}</div>"
"let c = <div>{a && <NewName><Component/></NewName>}</div>"))))
(ert-with-test-buffer (:name 'origin)
(dolist (case cases)
(erase-buffer)
(cl-destructuring-bind (pre post exp) case
(insert pre)