You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
4.3 KiB

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