test.js 7.2 KB

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