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.

521 lines
20 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}, "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. " [keyword 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("anonymous_class_expression",
  34. "[keyword const] [def Adder] [operator =] [keyword class] [keyword extends] [variable Arithmetic] {",
  35. " [property add]([def a], [def b]) {}",
  36. "};");
  37. MT("named_class_expression",
  38. "[keyword const] [def Subber] [operator =] [keyword class] [def Subtract] {",
  39. " [property sub]([def a], [def b]) {}",
  40. "};");
  41. MT("class_async_method",
  42. "[keyword class] [def Foo] {",
  43. " [property sayName1]() {}",
  44. " [keyword async] [property sayName2]() {}",
  45. "}");
  46. MT("import",
  47. "[keyword function] [def foo]() {",
  48. " [keyword import] [def $] [keyword from] [string 'jquery'];",
  49. " [keyword import] { [def encrypt], [def decrypt] } [keyword from] [string 'crypto'];",
  50. "}");
  51. MT("import_trailing_comma",
  52. "[keyword import] {[def foo], [def bar],} [keyword from] [string 'baz']")
  53. MT("import_dynamic",
  54. "[keyword import]([string 'baz']).[property then]")
  55. MT("import_dynamic",
  56. "[keyword const] [def t] [operator =] [keyword import]([string 'baz']).[property then]")
  57. MT("const",
  58. "[keyword function] [def f]() {",
  59. " [keyword const] [[ [def a], [def b] ]] [operator =] [[ [number 1], [number 2] ]];",
  60. "}");
  61. MT("for/of",
  62. "[keyword for]([keyword let] [def of] [keyword of] [variable something]) {}");
  63. MT("for await",
  64. "[keyword for] [keyword await]([keyword let] [def of] [keyword of] [variable something]) {}");
  65. MT("generator",
  66. "[keyword function*] [def repeat]([def n]) {",
  67. " [keyword for]([keyword var] [def i] [operator =] [number 0]; [variable-2 i] [operator <] [variable-2 n]; [operator ++][variable-2 i])",
  68. " [keyword yield] [variable-2 i];",
  69. "}");
  70. MT("let_scoping",
  71. "[keyword function] [def scoped]([def n]) {",
  72. " { [keyword var] [def i]; } [variable-2 i];",
  73. " { [keyword let] [def j]; [variable-2 j]; } [variable j];",
  74. " [keyword if] ([atom true]) { [keyword const] [def k]; [variable-2 k]; } [variable k];",
  75. "}");
  76. MT("switch_scoping",
  77. "[keyword switch] ([variable x]) {",
  78. " [keyword default]:",
  79. " [keyword let] [def j];",
  80. " [keyword return] [variable-2 j]",
  81. "}",
  82. "[variable j];")
  83. MT("leaving_scope",
  84. "[keyword function] [def a]() {",
  85. " {",
  86. " [keyword const] [def x] [operator =] [number 1]",
  87. " [keyword if] ([atom true]) {",
  88. " [keyword let] [def y] [operator =] [number 2]",
  89. " [keyword var] [def z] [operator =] [number 3]",
  90. " [variable console].[property log]([variable-2 x], [variable-2 y], [variable-2 z])",
  91. " }",
  92. " [variable console].[property log]([variable-2 x], [variable y], [variable-2 z])",
  93. " }",
  94. " [variable console].[property log]([variable x], [variable y], [variable-2 z])",
  95. "}")
  96. MT("quotedStringAddition",
  97. "[keyword let] [def f] [operator =] [variable a] [operator +] [string 'fatarrow'] [operator +] [variable c];");
  98. MT("quotedFatArrow",
  99. "[keyword let] [def f] [operator =] [variable a] [operator +] [string '=>'] [operator +] [variable c];");
  100. MT("fatArrow",
  101. "[variable array].[property filter]([def a] [operator =>] [variable-2 a] [operator +] [number 1]);",
  102. "[variable a];", // No longer in scope
  103. "[keyword let] [def f] [operator =] ([[ [def a], [def b] ]], [def c]) [operator =>] [variable-2 a] [operator +] [variable-2 c];",
  104. "[variable c];");
  105. MT("fatArrow_stringDefault",
  106. "([def a], [def b] [operator =] [string 'x\\'y']) [operator =>] [variable-2 a] [operator +] [variable-2 b]")
  107. MT("spread",
  108. "[keyword function] [def f]([def a], [meta ...][def b]) {",
  109. " [variable something]([variable-2 a], [meta ...][variable-2 b]);",
  110. "}");
  111. MT("quasi",
  112. "[variable re][string-2 `fofdlakj${][variable x] [operator +] ([variable re][string-2 `foo`]) [operator +] [number 1][string-2 }fdsa`] [operator +] [number 2]");
  113. MT("quasi_no_function",
  114. "[variable x] [operator =] [string-2 `fofdlakj${][variable x] [operator +] [string-2 `foo`] [operator +] [number 1][string-2 }fdsa`] [operator +] [number 2]");
  115. MT("indent_statement",
  116. "[keyword var] [def x] [operator =] [number 10]",
  117. "[variable x] [operator +=] [variable y] [operator +]",
  118. " [atom Infinity]",
  119. "[keyword debugger];");
  120. MT("indent_if",
  121. "[keyword if] ([number 1])",
  122. " [keyword break];",
  123. "[keyword else] [keyword if] ([number 2])",
  124. " [keyword continue];",
  125. "[keyword else]",
  126. " [number 10];",
  127. "[keyword if] ([number 1]) {",
  128. " [keyword break];",
  129. "} [keyword else] [keyword if] ([number 2]) {",
  130. " [keyword continue];",
  131. "} [keyword else] {",
  132. " [number 10];",
  133. "}");
  134. MT("indent_for",
  135. "[keyword for] ([keyword var] [def i] [operator =] [number 0];",
  136. " [variable-2 i] [operator <] [number 100];",
  137. " [variable-2 i][operator ++])",
  138. " [variable doSomething]([variable-2 i]);",
  139. "[keyword debugger];");
  140. MT("indent_c_style",
  141. "[keyword function] [def foo]()",
  142. "{",
  143. " [keyword debugger];",
  144. "}");
  145. MT("indent_else",
  146. "[keyword for] (;;)",
  147. " [keyword if] ([variable foo])",
  148. " [keyword if] ([variable bar])",
  149. " [number 1];",
  150. " [keyword else]",
  151. " [number 2];",
  152. " [keyword else]",
  153. " [number 3];");
  154. MT("indent_funarg",
  155. "[variable foo]([number 10000],",
  156. " [keyword function]([def a]) {",
  157. " [keyword debugger];",
  158. "};");
  159. MT("indent_below_if",
  160. "[keyword for] (;;)",
  161. " [keyword if] ([variable foo])",
  162. " [number 1];",
  163. "[number 2];");
  164. MT("indent_semicolonless_if",
  165. "[keyword function] [def foo]() {",
  166. " [keyword if] ([variable x])",
  167. " [variable foo]()",
  168. "}")
  169. MT("indent_semicolonless_if_with_statement",
  170. "[keyword function] [def foo]() {",
  171. " [keyword if] ([variable x])",
  172. " [variable foo]()",
  173. " [variable bar]()",
  174. "}")
  175. MT("multilinestring",
  176. "[keyword var] [def x] [operator =] [string 'foo\\]",
  177. "[string bar'];");
  178. MT("scary_regexp",
  179. "[string-2 /foo[[/]]bar/];");
  180. MT("indent_strange_array",
  181. "[keyword var] [def x] [operator =] [[",
  182. " [number 1],,",
  183. " [number 2],",
  184. "]];",
  185. "[number 10];");
  186. MT("param_default",
  187. "[keyword function] [def foo]([def x] [operator =] [string-2 `foo${][number 10][string-2 }bar`]) {",
  188. " [keyword return] [variable-2 x];",
  189. "}");
  190. MT(
  191. "param_destructuring",
  192. "[keyword function] [def foo]([def x] [operator =] [string-2 `foo${][number 10][string-2 }bar`]) {",
  193. " [keyword return] [variable-2 x];",
  194. "}");
  195. MT("new_target",
  196. "[keyword function] [def F]([def target]) {",
  197. " [keyword if] ([variable-2 target] [operator &&] [keyword new].[keyword target].[property name]) {",
  198. " [keyword return] [keyword new]",
  199. " .[keyword target];",
  200. " }",
  201. "}");
  202. MT("async",
  203. "[keyword async] [keyword function] [def foo]([def args]) { [keyword return] [atom true]; }");
  204. MT("async_assignment",
  205. "[keyword const] [def foo] [operator =] [keyword async] [keyword function] ([def args]) { [keyword return] [atom true]; };");
  206. MT("async_object",
  207. "[keyword let] [def obj] [operator =] { [property async]: [atom false] };");
  208. // async be highlighted as keyword and foo as def, but it requires potentially expensive look-ahead. See #4173
  209. MT("async_object_function",
  210. "[keyword let] [def obj] [operator =] { [property async] [property foo]([def args]) { [keyword return] [atom true]; } };");
  211. MT("async_object_properties",
  212. "[keyword let] [def obj] [operator =] {",
  213. " [property prop1]: [keyword async] [keyword function] ([def args]) { [keyword return] [atom true]; },",
  214. " [property prop2]: [keyword async] [keyword function] ([def args]) { [keyword return] [atom true]; },",
  215. " [property prop3]: [keyword async] [keyword function] [def prop3]([def args]) { [keyword return] [atom true]; },",
  216. "};");
  217. MT("async_arrow",
  218. "[keyword const] [def foo] [operator =] [keyword async] ([def args]) [operator =>] { [keyword return] [atom true]; };");
  219. MT("async_jquery",
  220. "[variable $].[property ajax]({",
  221. " [property url]: [variable url],",
  222. " [property async]: [atom true],",
  223. " [property method]: [string 'GET']",
  224. "});");
  225. MT("async_variable",
  226. "[keyword const] [def async] [operator =] {[property a]: [number 1]};",
  227. "[keyword const] [def foo] [operator =] [string-2 `bar ${][variable async].[property a][string-2 }`];")
  228. MT("bigint", "[number 1n] [operator +] [number 0x1afn] [operator +] [number 0o064n] [operator +] [number 0b100n];")
  229. MT("async_comment",
  230. "[keyword async] [comment /**/] [keyword function] [def foo]([def args]) { [keyword return] [atom true]; }");
  231. MT("indent_switch",
  232. "[keyword switch] ([variable x]) {",
  233. " [keyword default]:",
  234. " [keyword return] [number 2]",
  235. "}")
  236. MT("regexp_corner_case",
  237. "[operator +]{} [operator /] [atom undefined];",
  238. "[[[meta ...][string-2 /\\//] ]];",
  239. "[keyword void] [string-2 /\\//];",
  240. "[keyword do] [string-2 /\\//]; [keyword while] ([number 0]);",
  241. "[keyword if] ([number 0]) {} [keyword else] [string-2 /\\//];",
  242. "[string-2 `${][variable async][operator ++][string-2 }//`];",
  243. "[string-2 `${]{} [operator /] [string-2 /\\//}`];")
  244. MT("return_eol",
  245. "[keyword return]",
  246. "{} [string-2 /5/]")
  247. MT("numeric separator",
  248. "[number 123_456];",
  249. "[number 0xdead_c0de];",
  250. "[number 0o123_456];",
  251. "[number 0b1101_1101];",
  252. "[number .123_456e0_1];",
  253. "[number 1E+123_456];",
  254. "[number 12_34_56n];")
  255. MT("underscore property",
  256. "[variable something].[property _property];",
  257. "[variable something].[property _123];",
  258. "[variable something].[property _for];",
  259. "[variable _for];",
  260. "[variable _123];")
  261. MT("private properties",
  262. "[keyword class] [def C] {",
  263. " [property #x] [operator =] [number 2];",
  264. " [property #read]() {",
  265. " [keyword return] [keyword this].[property #x]",
  266. " }",
  267. "}")
  268. var ts_mode = CodeMirror.getMode({indentUnit: 2}, "application/typescript")
  269. function TS(name) {
  270. test.mode(name, ts_mode, Array.prototype.slice.call(arguments, 1))
  271. }
  272. TS("typescript_extend_type",
  273. "[keyword class] [def Foo] [keyword extends] [type Some][operator <][type Type][operator >] {}")
  274. TS("typescript_arrow_type",
  275. "[keyword let] [def x]: ([variable arg]: [type Type]) [operator =>] [type ReturnType]")
  276. TS("typescript_class",
  277. "[keyword class] [def Foo] {",
  278. " [keyword public] [keyword static] [property main]() {}",
  279. " [keyword private] [property _foo]: [type string];",
  280. "}")
  281. TS("typescript_literal_types",
  282. "[keyword import] [keyword *] [keyword as] [def Sequelize] [keyword from] [string 'sequelize'];",
  283. "[keyword interface] [def MyAttributes] {",
  284. " [property truthy]: [string 'true'] [operator |] [number 1] [operator |] [atom true];",
  285. " [property falsy]: [string 'false'] [operator |] [number 0] [operator |] [atom false];",
  286. "}",
  287. "[keyword interface] [def MyInstance] [keyword extends] [type Sequelize].[type Instance] [operator <] [type MyAttributes] [operator >] {",
  288. " [property rawAttributes]: [type MyAttributes];",
  289. " [property truthy]: [string 'true'] [operator |] [number 1] [operator |] [atom true];",
  290. " [property falsy]: [string 'false'] [operator |] [number 0] [operator |] [atom false];",
  291. "}")
  292. TS("typescript_extend_operators",
  293. "[keyword export] [keyword interface] [def UserModel] [keyword extends]",
  294. " [type Sequelize].[type Model] [operator <] [type UserInstance], [type UserAttributes] [operator >] {",
  295. " [property findById]: (",
  296. " [variable userId]: [type number]",
  297. " ) [operator =>] [type Promise] [operator <] [type Array] [operator <] { [property id], [property name] } [operator >>];",
  298. " [property updateById]: (",
  299. " [variable userId]: [type number],",
  300. " [variable isActive]: [type boolean]",
  301. " ) [operator =>] [type Promise] [operator <] [type AccountHolderNotificationPreferenceInstance] [operator >];",
  302. " }")
  303. TS("typescript_interface_with_const",
  304. "[keyword const] [def hello]: {",
  305. " [property prop1][operator ?]: [type string];",
  306. " [property prop2][operator ?]: [type string];",
  307. "} [operator =] {};")
  308. TS("typescript_double_extend",
  309. "[keyword export] [keyword interface] [def UserAttributes] {",
  310. " [property id][operator ?]: [type number];",
  311. " [property createdAt][operator ?]: [type Date];",
  312. "}",
  313. "[keyword export] [keyword interface] [def UserInstance] [keyword extends] [type Sequelize].[type Instance][operator <][type UserAttributes][operator >], [type UserAttributes] {",
  314. " [property id]: [type number];",
  315. " [property createdAt]: [type Date];",
  316. "}");
  317. TS("typescript_index_signature",
  318. "[keyword interface] [def A] {",
  319. " [[ [variable prop]: [type string] ]]: [type any];",
  320. " [property prop1]: [type any];",
  321. "}");
  322. TS("typescript_generic_class",
  323. "[keyword class] [def Foo][operator <][type T][operator >] {",
  324. " [property bar]() {}",
  325. " [property foo](): [type Foo] {}",
  326. "}")
  327. TS("typescript_type_when_keyword",
  328. "[keyword export] [keyword type] [type AB] [operator =] [type A] [operator |] [type B];",
  329. "[keyword type] [type Flags] [operator =] {",
  330. " [property p1]: [type string];",
  331. " [property p2]: [type boolean];",
  332. "};")
  333. TS("typescript_type_when_not_keyword",
  334. "[keyword class] [def HasType] {",
  335. " [property type]: [type string];",
  336. " [property constructor]([def type]: [type string]) {",
  337. " [keyword this].[property type] [operator =] [variable-2 type];",
  338. " }",
  339. " [property setType]({ [def type] }: { [property type]: [type string]; }) {",
  340. " [keyword this].[property type] [operator =] [variable-2 type];",
  341. " }",
  342. "}")
  343. TS("typescript_function_generics",
  344. "[keyword function] [def a]() {}",
  345. "[keyword function] [def b][operator <][type IA] [keyword extends] [type object], [type IB] [keyword extends] [type object][operator >]() {}",
  346. "[keyword function] [def c]() {}")
  347. TS("typescript_complex_return_type",
  348. "[keyword function] [def A]() {",
  349. " [keyword return] [keyword this].[property property];",
  350. "}",
  351. "[keyword function] [def B](): [type Promise][operator <]{ [[ [variable key]: [type string] ]]: [type any] } [operator |] [atom null][operator >] {",
  352. " [keyword return] [keyword this].[property property];",
  353. "}")
  354. TS("typescript_complex_type_casting",
  355. "[keyword const] [def giftpay] [operator =] [variable config].[property get]([string 'giftpay']) [keyword as] { [[ [variable platformUuid]: [type string] ]]: { [property version]: [type number]; [property apiCode]: [type string]; } };")
  356. TS("typescript_keyof",
  357. "[keyword function] [def x][operator <][type T] [keyword extends] [keyword keyof] [type X][operator >]([def a]: [type T]) {",
  358. " [keyword return]")
  359. TS("typescript_new_typeargs",
  360. "[keyword let] [def x] [operator =] [keyword new] [variable Map][operator <][type string], [type Date][operator >]([string-2 `foo${][variable bar][string-2 }`])")
  361. TS("modifiers",
  362. "[keyword class] [def Foo] {",
  363. " [keyword public] [keyword abstract] [property bar]() {}",
  364. " [property constructor]([keyword readonly] [keyword private] [def x]) {}",
  365. "}")
  366. TS("arrow prop",
  367. "({[property a]: [def p] [operator =>] [variable-2 p]})")
  368. TS("generic in function call",
  369. "[keyword this].[property a][operator <][type Type][operator >]([variable foo]);",
  370. "[keyword this].[property a][operator <][variable Type][operator >][variable foo];")
  371. TS("type guard",
  372. "[keyword class] [def Appler] {",
  373. " [keyword static] [property assertApple]([def fruit]: [type Fruit]): [variable-2 fruit] [keyword is] [type Apple] {",
  374. " [keyword if] ([operator !]([variable-2 fruit] [keyword instanceof] [variable Apple]))",
  375. " [keyword throw] [keyword new] [variable Error]();",
  376. " }",
  377. "}")
  378. TS("type as variable",
  379. "[variable type] [operator =] [variable x] [keyword as] [type Bar];");
  380. TS("enum body",
  381. "[keyword export] [keyword const] [keyword enum] [def CodeInspectionResultType] {",
  382. " [def ERROR] [operator =] [string 'problem_type_error'],",
  383. " [def WARNING] [operator =] [string 'problem_type_warning'],",
  384. " [def META],",
  385. "}")
  386. TS("parenthesized type",
  387. "[keyword class] [def Foo] {",
  388. " [property x] [operator =] [keyword new] [variable A][operator <][type B], [type string][operator |](() [operator =>] [type void])[operator >]();",
  389. " [keyword private] [property bar]();",
  390. "}")
  391. TS("abstract class",
  392. "[keyword export] [keyword abstract] [keyword class] [def Foo] {}")
  393. TS("interface without semicolons",
  394. "[keyword interface] [def Foo] {",
  395. " [property greet]([def x]: [type int]): [type blah]",
  396. " [property bar]: [type void]",
  397. "}")
  398. var jsonld_mode = CodeMirror.getMode(
  399. {indentUnit: 2},
  400. {name: "javascript", jsonld: true}
  401. );
  402. function LD(name) {
  403. test.mode(name, jsonld_mode, Array.prototype.slice.call(arguments, 1));
  404. }
  405. LD("json_ld_keywords",
  406. '{',
  407. ' [meta "@context"]: {',
  408. ' [meta "@base"]: [string "http://example.com"],',
  409. ' [meta "@vocab"]: [string "http://xmlns.com/foaf/0.1/"],',
  410. ' [property "likesFlavor"]: {',
  411. ' [meta "@container"]: [meta "@list"]',
  412. ' [meta "@reverse"]: [string "@beFavoriteOf"]',
  413. ' },',
  414. ' [property "nick"]: { [meta "@container"]: [meta "@set"] },',
  415. ' [property "nick"]: { [meta "@container"]: [meta "@index"] }',
  416. ' },',
  417. ' [meta "@graph"]: [[ {',
  418. ' [meta "@id"]: [string "http://dbpedia.org/resource/John_Lennon"],',
  419. ' [property "name"]: [string "John Lennon"],',
  420. ' [property "modified"]: {',
  421. ' [meta "@value"]: [string "2010-05-29T14:17:39+02:00"],',
  422. ' [meta "@type"]: [string "http://www.w3.org/2001/XMLSchema#dateTime"]',
  423. ' }',
  424. ' } ]]',
  425. '}');
  426. LD("json_ld_fake",
  427. '{',
  428. ' [property "@fake"]: [string "@fake"],',
  429. ' [property "@contextual"]: [string "@identifier"],',
  430. ' [property "user@domain.com"]: [string "@graphical"],',
  431. ' [property "@ID"]: [string "@@ID"]',
  432. '}');
  433. })();