all repos — NoPaste @ de062991ccdf763b2091f26873047685f9c0c977

Resurrected - The PussTheCat.org fork of NoPaste

scripts/CodeMirror/mode/javascript/test.js (view raw)

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