scripts/CodeMirror/mode/jsx/test.js (view raw)
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4(function() {
5 var mode = CodeMirror.getMode({indentUnit: 2}, "jsx")
6 function MT(name) { test.mode(name, mode, Array.prototype.slice.call(arguments, 1)) }
7
8 MT("selfclose",
9 "[keyword var] [def x] [operator =] [bracket&tag <] [tag foo] [bracket&tag />] [operator +] [number 1];")
10
11 MT("openclose",
12 "([bracket&tag <][tag foo][bracket&tag >]hello [atom &][bracket&tag </][tag foo][bracket&tag >][operator ++])")
13
14 MT("openclosefragment",
15 "([bracket&tag <><][tag foo][bracket&tag >]hello [atom &][bracket&tag </][tag foo][bracket&tag ></>][operator ++])")
16
17 MT("attr",
18 "([bracket&tag <][tag foo] [attribute abc]=[string 'value'][bracket&tag >]hello [atom &][bracket&tag </][tag foo][bracket&tag >][operator ++])")
19
20 MT("braced_attr",
21 "([bracket&tag <][tag foo] [attribute abc]={[number 10]}[bracket&tag >]hello [atom &][bracket&tag </][tag foo][bracket&tag >][operator ++])")
22
23 MT("braced_text",
24 "([bracket&tag <][tag foo][bracket&tag >]hello {[number 10]} [atom &][bracket&tag </][tag foo][bracket&tag >][operator ++])")
25
26 MT("nested_tag",
27 "([bracket&tag <][tag foo][bracket&tag ><][tag bar][bracket&tag ></][tag bar][bracket&tag ></][tag foo][bracket&tag >][operator ++])")
28
29 MT("nested_jsx",
30 "[keyword return] (",
31 " [bracket&tag <][tag foo][bracket&tag >]",
32 " say {[number 1] [operator +] [bracket&tag <][tag bar] [attribute attr]={[number 10]}[bracket&tag />]}!",
33 " [bracket&tag </][tag foo][bracket&tag >][operator ++]",
34 ")")
35
36 MT("preserve_js_context",
37 "[variable x] [operator =] [string-2 `quasi${][bracket&tag <][tag foo][bracket&tag />][string-2 }quoted`]")
38
39 MT("string_interpolation",
40 "[variable x] [operator =] [string-2 `quasi<code>${] [number 10] [string-2 }</code>`]")
41
42 MT("line_comment",
43 "([bracket&tag <][tag foo] [comment // hello]",
44 " [bracket&tag ></][tag foo][bracket&tag >][operator ++])")
45
46 MT("line_comment_not_in_tag",
47 "([bracket&tag <][tag foo][bracket&tag >] // hello",
48 " [bracket&tag </][tag foo][bracket&tag >][operator ++])")
49
50 MT("block_comment",
51 "([bracket&tag <][tag foo] [comment /* hello]",
52 "[comment line 2]",
53 "[comment line 3 */] [bracket&tag ></][tag foo][bracket&tag >][operator ++])")
54
55 MT("block_comment_not_in_tag",
56 "([bracket&tag <][tag foo][bracket&tag >]/* hello",
57 " line 2",
58 " line 3 */ [bracket&tag </][tag foo][bracket&tag >][operator ++])")
59
60 MT("missing_attr",
61 "([bracket&tag <][tag foo] [attribute selected][bracket&tag />][operator ++])")
62
63 MT("indent_js",
64 "([bracket&tag <][tag foo][bracket&tag >]",
65 " [bracket&tag <][tag bar] [attribute baz]={[keyword function]() {",
66 " [keyword return] [number 10]",
67 " }}[bracket&tag />]",
68 " [bracket&tag </][tag foo][bracket&tag >])")
69
70 MT("spread",
71 "([bracket&tag <][tag foo] [attribute bar]={[meta ...][variable baz] [operator /][number 2]}[bracket&tag />])")
72
73 MT("tag_attribute",
74 "([bracket&tag <][tag foo] [attribute bar]=[bracket&tag <][tag foo][bracket&tag />/>][operator ++])")
75
76 var ts_mode = CodeMirror.getMode({indentUnit: 2}, "text/typescript-jsx")
77 function TS(name) { test.mode(name, ts_mode, Array.prototype.slice.call(arguments, 1)) }
78
79 TS("tsx_react_integration",
80 "[keyword interface] [def Props] {",
81 " [property foo]: [type string];",
82 "}",
83 "[keyword class] [def MyComponent] [keyword extends] [type React].[type Component] [operator <] [type Props], [type any] [operator >] {",
84 " [property render]() {",
85 " [keyword return] [bracket&tag <][tag span][bracket&tag >]{[keyword this].[property props].[property foo]}[bracket&tag </][tag span][bracket&tag >]",
86 " }",
87 "}",
88 "[bracket&tag <][tag MyComponent] [attribute foo]=[string \"bar\"] [bracket&tag />]; [comment //ok]",
89 "[bracket&tag <][tag MyComponent] [attribute foo]={[number 0]} [bracket&tag />]; [comment //error]")
90
91})()