test.js 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/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. var ts_mode = CodeMirror.getMode({indentUnit: 2}, "text/typescript-jsx")
  57. function TS(name) { test.mode(name, ts_mode, Array.prototype.slice.call(arguments, 1)) }
  58. TS("tsx_react_integration",
  59. "[keyword interface] [def Props] {",
  60. " [property foo]: [type string];",
  61. "}",
  62. "[keyword class] [def MyComponent] [keyword extends] [type React].[type Component] [operator <] [type Props], [type any] [operator >] {",
  63. " [property render]() {",
  64. " [keyword return] [bracket&tag <][tag span][bracket&tag >]{[keyword this].[property props].[property foo]}[bracket&tag </][tag span][bracket&tag >]",
  65. " }",
  66. "}",
  67. "[bracket&tag <][tag MyComponent] [attribute foo]=[string \"bar\"] [bracket&tag />]; [comment //ok]",
  68. "[bracket&tag <][tag MyComponent] [attribute foo]={[number 0]} [bracket&tag />]; [comment //error]")
  69. })()