test.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3. (function() {
  4. var mode = CodeMirror.getMode({indentUnit: 2}, "javascript");
  5. function MT(name) { test.mode(name, mode, Array.prototype.slice.call(arguments, 1)); }
  6. MT("locals",
  7. "[keyword function] [variable foo]([def a], [def b]) { [keyword var] [def c] [operator =] [number 10]; [keyword return] [variable-2 a] [operator +] [variable-2 c] [operator +] [variable d]; }");
  8. MT("comma-and-binop",
  9. "[keyword function](){ [keyword var] [def x] [operator =] [number 1] [operator +] [number 2], [def y]; }");
  10. MT("destructuring",
  11. "([keyword function]([def a], [[[def b], [def c] ]]) {",
  12. " [keyword let] {[def d], [property foo]: [def c][operator =][number 10], [def x]} [operator =] [variable foo]([variable-2 a]);",
  13. " [[[variable-2 c], [variable y] ]] [operator =] [variable-2 c];",
  14. "})();");
  15. MT("class_body",
  16. "[keyword class] [variable Foo] {",
  17. " [property constructor]() {}",
  18. " [property sayName]() {",
  19. " [keyword return] [string-2 `foo${][variable foo][string-2 }oo`];",
  20. " }",
  21. "}");
  22. MT("class",
  23. "[keyword class] [variable Point] [keyword extends] [variable SuperThing] {",
  24. " [property get] [property prop]() { [keyword return] [number 24]; }",
  25. " [property constructor]([def x], [def y]) {",
  26. " [keyword super]([string 'something']);",
  27. " [keyword this].[property x] [operator =] [variable-2 x];",
  28. " }",
  29. "}");
  30. MT("module",
  31. "[keyword module] [string 'foo'] {",
  32. " [keyword export] [keyword let] [def x] [operator =] [number 42];",
  33. " [keyword export] [keyword *] [keyword from] [string 'somewhere'];",
  34. "}");
  35. MT("import",
  36. "[keyword function] [variable foo]() {",
  37. " [keyword import] [def $] [keyword from] [string 'jquery'];",
  38. " [keyword module] [def crypto] [keyword from] [string 'crypto'];",
  39. " [keyword import] { [def encrypt], [def decrypt] } [keyword from] [string 'crypto'];",
  40. "}");
  41. MT("const",
  42. "[keyword function] [variable f]() {",
  43. " [keyword const] [[ [def a], [def b] ]] [operator =] [[ [number 1], [number 2] ]];",
  44. "}");
  45. MT("for/of",
  46. "[keyword for]([keyword let] [variable of] [keyword of] [variable something]) {}");
  47. MT("generator",
  48. "[keyword function*] [variable repeat]([def n]) {",
  49. " [keyword for]([keyword var] [def i] [operator =] [number 0]; [variable-2 i] [operator <] [variable-2 n]; [operator ++][variable-2 i])",
  50. " [keyword yield] [variable-2 i];",
  51. "}");
  52. MT("quotedStringAddition",
  53. "[keyword let] [variable f] [operator =] [variable a] [operator +] [string 'fatarrow'] [operator +] [variable c];");
  54. MT("quotedFatArrow",
  55. "[keyword let] [variable f] [operator =] [variable a] [operator +] [string '=>'] [operator +] [variable c];");
  56. MT("fatArrow",
  57. "[variable array].[property filter]([def a] [operator =>] [variable-2 a] [operator +] [number 1]);",
  58. "[variable a];", // No longer in scope
  59. "[keyword let] [variable f] [operator =] ([[ [def a], [def b] ]], [def c]) [operator =>] [variable-2 a] [operator +] [variable-2 c];",
  60. "[variable c];");
  61. MT("spread",
  62. "[keyword function] [variable f]([def a], [meta ...][def b]) {",
  63. " [variable something]([variable-2 a], [meta ...][variable-2 b]);",
  64. "}");
  65. MT("comprehension",
  66. "[keyword function] [variable f]() {",
  67. " [[([variable x] [operator +] [number 1]) [keyword for] ([keyword var] [def x] [keyword in] [variable y]) [keyword if] [variable pred]([variable-2 x]) ]];",
  68. " ([variable u] [keyword for] ([keyword var] [def u] [keyword of] [variable generateValues]()) [keyword if] ([variable-2 u].[property color] [operator ===] [string 'blue']));",
  69. "}");
  70. MT("quasi",
  71. "[variable re][string-2 `fofdlakj${][variable x] [operator +] ([variable re][string-2 `foo`]) [operator +] [number 1][string-2 }fdsa`] [operator +] [number 2]");
  72. MT("quasi_no_function",
  73. "[variable x] [operator =] [string-2 `fofdlakj${][variable x] [operator +] [string-2 `foo`] [operator +] [number 1][string-2 }fdsa`] [operator +] [number 2]");
  74. MT("indent_statement",
  75. "[keyword var] [variable x] [operator =] [number 10]",
  76. "[variable x] [operator +=] [variable y] [operator +]",
  77. " [atom Infinity]",
  78. "[keyword debugger];");
  79. MT("indent_if",
  80. "[keyword if] ([number 1])",
  81. " [keyword break];",
  82. "[keyword else] [keyword if] ([number 2])",
  83. " [keyword continue];",
  84. "[keyword else]",
  85. " [number 10];",
  86. "[keyword if] ([number 1]) {",
  87. " [keyword break];",
  88. "} [keyword else] [keyword if] ([number 2]) {",
  89. " [keyword continue];",
  90. "} [keyword else] {",
  91. " [number 10];",
  92. "}");
  93. MT("indent_for",
  94. "[keyword for] ([keyword var] [variable i] [operator =] [number 0];",
  95. " [variable i] [operator <] [number 100];",
  96. " [variable i][operator ++])",
  97. " [variable doSomething]([variable i]);",
  98. "[keyword debugger];");
  99. MT("indent_c_style",
  100. "[keyword function] [variable foo]()",
  101. "{",
  102. " [keyword debugger];",
  103. "}");
  104. MT("indent_else",
  105. "[keyword for] (;;)",
  106. " [keyword if] ([variable foo])",
  107. " [keyword if] ([variable bar])",
  108. " [number 1];",
  109. " [keyword else]",
  110. " [number 2];",
  111. " [keyword else]",
  112. " [number 3];");
  113. MT("indent_funarg",
  114. "[variable foo]([number 10000],",
  115. " [keyword function]([def a]) {",
  116. " [keyword debugger];",
  117. "};");
  118. MT("indent_below_if",
  119. "[keyword for] (;;)",
  120. " [keyword if] ([variable foo])",
  121. " [number 1];",
  122. "[number 2];");
  123. MT("multilinestring",
  124. "[keyword var] [variable x] [operator =] [string 'foo\\]",
  125. "[string bar'];");
  126. MT("scary_regexp",
  127. "[string-2 /foo[[/]]bar/];");
  128. MT("indent_strange_array",
  129. "[keyword var] [variable x] [operator =] [[",
  130. " [number 1],,",
  131. " [number 2],",
  132. "]];",
  133. "[number 10];");
  134. var jsonld_mode = CodeMirror.getMode(
  135. {indentUnit: 2},
  136. {name: "javascript", jsonld: true}
  137. );
  138. function LD(name) {
  139. test.mode(name, jsonld_mode, Array.prototype.slice.call(arguments, 1));
  140. }
  141. LD("json_ld_keywords",
  142. '{',
  143. ' [meta "@context"]: {',
  144. ' [meta "@base"]: [string "http://example.com"],',
  145. ' [meta "@vocab"]: [string "http://xmlns.com/foaf/0.1/"],',
  146. ' [property "likesFlavor"]: {',
  147. ' [meta "@container"]: [meta "@list"]',
  148. ' [meta "@reverse"]: [string "@beFavoriteOf"]',
  149. ' },',
  150. ' [property "nick"]: { [meta "@container"]: [meta "@set"] },',
  151. ' [property "nick"]: { [meta "@container"]: [meta "@index"] }',
  152. ' },',
  153. ' [meta "@graph"]: [[ {',
  154. ' [meta "@id"]: [string "http://dbpedia.org/resource/John_Lennon"],',
  155. ' [property "name"]: [string "John Lennon"],',
  156. ' [property "modified"]: {',
  157. ' [meta "@value"]: [string "2010-05-29T14:17:39+02:00"],',
  158. ' [meta "@type"]: [string "http://www.w3.org/2001/XMLSchema#dateTime"]',
  159. ' }',
  160. ' } ]]',
  161. '}');
  162. LD("json_ld_fake",
  163. '{',
  164. ' [property "@fake"]: [string "@fake"],',
  165. ' [property "@contextual"]: [string "@identifier"],',
  166. ' [property "user@domain.com"]: [string "@graphical"],',
  167. ' [property "@ID"]: [string "@@ID"]',
  168. '}');
  169. })();