Repository navigation
Expand file tree
/
Copy pathtest.js
More file actions
451 lines (449 loc) · 17.6 KB
/
Copy pathtest.js
File metadata and controls
451 lines (449 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
import prettier from "prettier";
// Custom Prettier config with our plugin
const config = {
plugins: ["./index.js"],
parser: "babel",
useTabs: true,
trailingComma: "none",
};
export default {
run (arg) {
return prettier.format(arg, { ...config, ...this.data });
},
map: code => !code?.endsWith || code.endsWith("\n") ? code : code + "\n", // Append a new line to the expected value (the actual result already has one)
tests: [
{
name: "JavaScript",
tests: [
{
name: "What this plugin changes",
tests: [
{
name: "Function declarations",
arg: "function test(a, b) {\n\treturn a + b;\n}",
expect: "function test (a, b) {\n\treturn a + b;\n}",
},
{
name: "Async function declarations",
arg: "async function test(a, b) {\n\treturn a + b;\n}",
expect: "async function test (a, b) {\n\treturn a + b;\n}",
},
{
name: "Object methods",
arg: "const foo = {\n\tmethod(a, b) {\n\t\treturn a + b;\n\t}\n};",
expect: "const foo = {\n\tmethod (a, b) {\n\t\treturn a + b;\n\t}\n};",
},
{
name: "Async object methods",
arg: "const foo = {\n\tasync method(a, b) {\n\t\treturn a + b;\n\t}\n};",
expect: "const foo = {\n\tasync method (a, b) {\n\t\treturn a + b;\n\t}\n};",
},
{
name: "Computed object methods",
arg: "const foo = {\n\t[methodName]() {\n\t\treturn 42;\n\t}\n};",
expect: "const foo = {\n\t[methodName] () {\n\t\treturn 42;\n\t}\n};",
},
{
name: "Object getters",
arg: "const foo = {\n\tget foo() {\n\t\treturn true;\n\t}\n};",
expect: "const foo = {\n\tget foo () {\n\t\treturn true;\n\t}\n};",
},
{
name: "Computed object getters",
arg: "const foo = {\n\tget [name]() {\n\t\treturn this._name;\n\t}\n};",
expect: "const foo = {\n\tget [name] () {\n\t\treturn this._name;\n\t}\n};",
},
{
name: "Object setters",
arg: "const foo = {\n\tset foo(value) {\n\t\tthis._foo = value;\n\t}\n};",
expect: "const foo = {\n\tset foo (value) {\n\t\tthis._foo = value;\n\t}\n};",
},
{
name: "Computed object setters",
arg: "const foo = {\n\tset [name](value) {\n\t\tthis._name = value;\n\t}\n};",
expect: "const foo = {\n\tset [name] (value) {\n\t\tthis._name = value;\n\t}\n};",
},
{
name: "Class constructors",
arg: "class Foo {\n\tconstructor(a, b) {\n\t\tthis.a = a;\n\t\tthis.b = b;\n\t}\n}",
expect: "class Foo {\n\tconstructor (a, b) {\n\t\tthis.a = a;\n\t\tthis.b = b;\n\t}\n}",
},
{
name: "Class methods",
arg: "class Foo {\n\tmethod(a, b) {\n\t\treturn a + b;\n\t}\n}",
expect: "class Foo {\n\tmethod (a, b) {\n\t\treturn a + b;\n\t}\n}",
},
{
name: "Private class methods",
arg: "class Foo {\n\t#method(a, b) {\n\t\treturn a + b;\n\t}\n}",
expect: "class Foo {\n\t#method (a, b) {\n\t\treturn a + b;\n\t}\n}",
},
{
name: "Async class methods",
arg: "class Foo {\n\tasync method(a, b) {\n\t\treturn a + b;\n\t}\n}",
expect: "class Foo {\n\tasync method (a, b) {\n\t\treturn a + b;\n\t}\n}",
},
{
name: "Private async class methods",
arg: "class Foo {\n\tasync #method(a, b) {\n\t\treturn a + b;\n\t}\n}",
expect: "class Foo {\n\tasync #method (a, b) {\n\t\treturn a + b;\n\t}\n}",
},
{
name: "Computed class methods",
arg: "class Foo {\n\t[methodName]() {\n\t\treturn 42;\n\t}\n}",
expect: "class Foo {\n\t[methodName] () {\n\t\treturn 42;\n\t}\n}",
},
{
name: "Static class methods",
arg: "class Foo {\n\tstatic method(a, b) {\n\t\treturn a + b;\n\t}\n}",
expect: "class Foo {\n\tstatic method (a, b) {\n\t\treturn a + b;\n\t}\n}",
},
{
name: "Private static class methods",
arg: "class Foo {\n\tstatic #method(a, b) {\n\t\treturn a + b;\n\t}\n}",
expect: "class Foo {\n\tstatic #method (a, b) {\n\t\treturn a + b;\n\t}\n}",
},
{
name: "Class getters",
arg: "class Foo {\n\tget foo() {\n\t\treturn true;\n\t}\n}",
expect: "class Foo {\n\tget foo () {\n\t\treturn true;\n\t}\n}",
},
{
name: "Private class getters",
arg: "class Foo {\n\tget #foo() {\n\t\treturn true;\n\t}\n}",
expect: "class Foo {\n\tget #foo () {\n\t\treturn true;\n\t}\n}",
},
{
name: "Class setters",
arg: "class Foo {\n\tset foo(value) {\n\t\tthis._foo = value;\n\t}\n}",
expect: "class Foo {\n\tset foo (value) {\n\t\tthis._foo = value;\n\t}\n}",
},
{
name: "Private class setters",
arg: "class Foo {\n\tset #foo(value) {\n\t\tthis._foo = value;\n\t}\n}",
expect: "class Foo {\n\tset #foo (value) {\n\t\tthis._foo = value;\n\t}\n}",
},
{
name: "Generator functions",
arg: "function* test() {\n\tyield 1;\n}",
expect: "function* test () {\n\tyield 1;\n}",
},
{
name: "Generator object methods",
arg: "const foo = {\n\t*method() {\n\t\tyield 1;\n\t}\n};",
expect: "const foo = {\n\t*method () {\n\t\tyield 1;\n\t}\n};",
},
{
name: "Named function expressions",
arg: "class Foo {\n\tmethod = function named() {};\n}",
expect: "class Foo {\n\tmethod = function named () {};\n}",
},
{
name: "Named exported functions",
arg: "export function test(a, b) {\n\treturn a + b;\n}",
expect: "export function test (a, b) {\n\treturn a + b;\n}",
},
{
name: "Default exported functions",
arg: "export default function test(a, b) {\n\treturn a + b;\n}",
expect: "export default function test (a, b) {\n\treturn a + b;\n}",
},
{
name: "Async exported functions",
arg: "export async function test(a, b) {\n\treturn a + b;\n}",
expect: "export async function test (a, b) {\n\treturn a + b;\n}",
},
{
name: "Generator exported functions",
arg: "export function* test() {\n\tyield 1;\n}",
expect: "export function* test () {\n\tyield 1;\n}",
},
],
},
{
name: "What remains unchanged",
tests: [
{
name: "Function calls",
arg: "test(1, 2);",
expect: "test(1, 2);",
},
{
name: "Arrow functions",
arg: "const add = (a, b) => a + b;",
expect: "const add = (a, b) => a + b;",
},
{
name: "Anonymous function expressions",
description: "Prettier already handles this case.",
arg: "const add = function(a, b) {\n\treturn a + b;\n};",
expect: "const add = function (a, b) {\n\treturn a + b;\n};",
},
{
name: "Anonymous function declarations",
description: "Prettier already handles this case.",
arg: "export default function(a, b) {\n\treturn a + b;\n}",
expect: "export default function (a, b) {\n\treturn a + b;\n}",
},
],
},
],
},
{
name: "TypeScript",
data: {
parser: "typescript",
},
tests: [
{
name: "What this plugin changes",
tests: [
{
name: "Function declarations",
arg: "function test(a: number, b: number): number {\n\treturn a + b;\n}",
expect: "function test (a: number, b: number): number {\n\treturn a + b;\n}",
},
{
name: "Async function declarations",
arg: "async function test(a: number, b: number): Promise<number> {\n\treturn a + b;\n}",
expect: "async function test (a: number, b: number): Promise<number> {\n\treturn a + b;\n}",
},
{
name: "Object methods",
arg: "const foo = {\n\tmethod(a: number, b: number): number {\n\t\treturn a + b;\n\t}\n};",
expect: "const foo = {\n\tmethod (a: number, b: number): number {\n\t\treturn a + b;\n\t}\n};",
},
{
name: "Async object methods",
arg: "const foo = {\n\tasync method(a: number, b: number): Promise<number> {\n\t\treturn a + b;\n\t}\n};",
expect: "const foo = {\n\tasync method (a: number, b: number): Promise<number> {\n\t\treturn a + b;\n\t}\n};",
},
{
name: "Computed object methods",
arg: "const foo = {\n\t[methodName](): void {\n\t\treturn;\n\t}\n};",
expect: "const foo = {\n\t[methodName] (): void {\n\t\treturn;\n\t}\n};",
},
{
name: "Object getters",
arg: "const foo = {\n\tget foo(): boolean {\n\t\treturn true;\n\t}\n};",
expect: "const foo = {\n\tget foo (): boolean {\n\t\treturn true;\n\t}\n};",
},
{
name: "Computed object getters",
arg: "const foo = {\n\tget [name](): string {\n\t\treturn '';\n\t}\n};",
expect: "const foo = {\n\tget [name] (): string {\n\t\treturn \"\";\n\t}\n};",
},
{
name: "Object setters",
arg: "const foo = {\n\tset foo(value: boolean) {\n\t\tthis._foo = value;\n\t}\n};",
expect: "const foo = {\n\tset foo (value: boolean) {\n\t\tthis._foo = value;\n\t}\n};",
},
{
name: "Computed object setters",
arg: "const foo = {\n\tset [name](value: string) {\n\t\tthis._name = value;\n\t}\n};",
expect: "const foo = {\n\tset [name] (value: string) {\n\t\tthis._name = value;\n\t}\n};",
},
{
name: "Class constructors",
arg: "class Foo {\n\tconstructor(a: number, b: number) {\n\t\tthis.a = a;\n\t\tthis.b = b;\n\t}\n}",
expect: "class Foo {\n\tconstructor (a: number, b: number) {\n\t\tthis.a = a;\n\t\tthis.b = b;\n\t}\n}",
},
{
name: "Class methods",
arg: "class Foo {\n\tmethod(a: number, b: number): number {\n\t\treturn a + b;\n\t}\n}",
expect: "class Foo {\n\tmethod (a: number, b: number): number {\n\t\treturn a + b;\n\t}\n}",
},
{
name: "Async class methods",
arg: "class Foo {\n\tasync method(a: number, b: number): Promise<number> {\n\t\treturn a + b;\n\t}\n}",
expect: "class Foo {\n\tasync method (a: number, b: number): Promise<number> {\n\t\treturn a + b;\n\t}\n}",
},
{
name: "Static class methods",
arg: "class Foo {\n\tstatic method(a: number, b: number): number {\n\t\treturn a + b;\n\t}\n}",
expect: "class Foo {\n\tstatic method (a: number, b: number): number {\n\t\treturn a + b;\n\t}\n}",
},
{
name: "Abstract class methods",
arg: "abstract class Foo {\n\tabstract method(a: number, b: number): number;\n}",
expect: "abstract class Foo {\n\tabstract method (a: number, b: number): number;\n}",
},
{
name: "Private class methods (access modifier)",
arg: "class Foo {\n\tprivate method(a: number, b: number): number {\n\t\treturn a + b;\n\t}\n}",
expect: "class Foo {\n\tprivate method (a: number, b: number): number {\n\t\treturn a + b;\n\t}\n}",
},
{
name: "Private class methods",
arg: "class Foo {\n\t#method(a: number, b: number): number {\n\t\treturn a + b;\n\t}\n}",
expect: "class Foo {\n\t#method (a: number, b: number): number {\n\t\treturn a + b;\n\t}\n}",
},
{
name: "Protected class methods",
arg: "class Foo {\n\tprotected method(a: number, b: number): number {\n\t\treturn a + b;\n\t}\n}",
expect: "class Foo {\n\tprotected method (a: number, b: number): number {\n\t\treturn a + b;\n\t}\n}",
},
{
name: "Public class methods",
arg: "class Foo {\n\tpublic method(a: number, b: number): number {\n\t\treturn a + b;\n\t}\n}",
expect: "class Foo {\n\tpublic method (a: number, b: number): number {\n\t\treturn a + b;\n\t}\n}",
},
{
name: "Optional class methods",
arg: "class Foo {\n\tmethod?(a: number, b: number): number {\n\t\treturn a + b;\n\t}\n}",
expect: "class Foo {\n\tmethod? (a: number, b: number): number {\n\t\treturn a + b;\n\t}\n}",
},
{
name: "Class getters",
arg: "class Foo {\n\tget foo(): boolean {\n\t\treturn true;\n\t}\n}",
expect: "class Foo {\n\tget foo (): boolean {\n\t\treturn true;\n\t}\n}",
},
{
name: "Private class getters",
arg: "class Foo {\n\tget #foo(): boolean {\n\t\treturn true;\n\t}\n}",
expect: "class Foo {\n\tget #foo (): boolean {\n\t\treturn true;\n\t}\n}",
},
{
name: "Class setters",
arg: "class Foo {\n\tset foo(value: boolean) {\n\t\tthis._foo = value;\n\t}\n}",
expect: "class Foo {\n\tset foo (value: boolean) {\n\t\tthis._foo = value;\n\t}\n}",
},
{
name: "Private class setters",
arg: "class Foo {\n\tset #foo(value: boolean) {\n\t\tthis._foo = value;\n\t}\n}",
expect: "class Foo {\n\tset #foo (value: boolean) {\n\t\tthis._foo = value;\n\t}\n}",
},
{
name: "Computed class methods",
arg: "class Foo {\n\t[methodName](): void {\n\t\treturn;\n\t}\n}",
expect: "class Foo {\n\t[methodName] (): void {\n\t\treturn;\n\t}\n}",
},
{
name: "Class methods with type parameters",
arg: "class Foo {\n\tmethod<T>(a: T): T {\n\t\treturn a;\n\t}\n}",
expect: "class Foo {\n\tmethod<T> (a: T): T {\n\t\treturn a;\n\t}\n}",
},
{
name: "Generator functions",
arg: "function* test(): Generator<number> {\n\tyield 1;\n}",
expect: "function* test (): Generator<number> {\n\tyield 1;\n}",
},
{
name: "Interface methods",
arg: "interface Foo {\n\tmethod(a: number, b: number): number;\n}",
expect: "interface Foo {\n\tmethod (a: number, b: number): number;\n}",
},
{
name: "Optional interface methods",
arg: "interface Foo {\n\tmethod?(a: number): number;\n}",
expect: "interface Foo {\n\tmethod? (a: number): number;\n}",
},
{
name: "Function with type parameters",
arg: "function foo<T>(arg: T): T {\n\treturn arg;\n}",
expect: "function foo<T> (arg: T): T {\n\treturn arg;\n}",
},
{
name: "Named exported functions",
arg: "export function test(a: number, b: number): number {\n\treturn a + b;\n}",
expect: "export function test (a: number, b: number): number {\n\treturn a + b;\n}",
},
{
name: "Default exported functions",
arg: "export default function test(a: number, b: number): number {\n\treturn a + b;\n}",
expect: "export default function test (a: number, b: number): number {\n\treturn a + b;\n}",
},
{
name: "Async exported functions",
arg: "export async function test(a: number, b: number): Promise<number> {\n\treturn a + b;\n}",
expect: "export async function test (a: number, b: number): Promise<number> {\n\treturn a + b;\n}",
},
{
name: "Generator exported functions",
arg: "export function* test(): Generator<number> {\n\tyield 1;\n}",
expect: "export function* test (): Generator<number> {\n\tyield 1;\n}",
},
{
name: "Exported function in namespace",
arg: "declare namespace Foo {\n\texport function test(a: number, b: number): number;\n}",
expect: "declare namespace Foo {\n\texport function test (a: number, b: number): number;\n}",
},
{
name: "Standalone declare function",
arg: "declare function foo(a: number): void;",
expect: "declare function foo (a: number): void;",
},
{
name: "Function type aliases",
arg: "type MethodType = {\n\tmethod(): void;\n};",
expect: "type MethodType = {\n\tmethod (): void;\n};",
},
{
name: "Decorated class methods",
arg: "class Foo {\n\t@decorator(() => {})\n\tmethod(): void {}\n}",
expect: "class Foo {\n\t@decorator(() => {})\n\tmethod (): void {}\n}",
},
],
},
{
name: "What remains unchanged",
tests: [
{
name: "Function calls",
arg: "test<number>(1, 2);",
expect: "test<number>(1, 2);",
},
{
name: "Arrow functions",
arg: "const add = (a: number, b: number): number => a + b;",
expect: "const add = (a: number, b: number): number => a + b;",
},
{
name: "Anonymous function expressions",
description: "Prettier already handles this case.",
arg: "const add = function(a: number, b: number): number {\n\treturn a + b;\n};",
expect: "const add = function (a: number, b: number): number {\n\treturn a + b;\n};",
},
{
name: "Anonymous function declarations",
description: "Prettier already handles this case.",
arg: "export default function(a: number, b: number): number {\n\treturn a + b;\n}",
expect: "export default function (a: number, b: number): number {\n\treturn a + b;\n}",
},
{
name: "Type aliases",
arg: "type NumberCallback = (x: number) => number;",
expect: "type NumberCallback = (x: number) => number;",
},
{
name: "Function types",
arg: "type Func = { (a: string): number };",
expect: "type Func = { (a: string): number };",
},
{
name: "Interface property with function type",
arg: "interface Foo {\n\tmethod: (a: number) => number;\n}",
expect: "interface Foo {\n\tmethod: (a: number) => number;\n}",
},
{
name: "Constructor signatures",
description: "Prettier already handles this case.",
arg: "type Ctor = {\n\tnew(): Foo;\n};",
expect: "type Ctor = {\n\tnew (): Foo;\n};",
},
{
name: "Index signatures",
arg: "interface Foo {\n\t[key: string]: number;\n}",
expect: "interface Foo {\n\t[key: string]: number;\n}",
},
{
name: "Object literal",
arg: "let a = { value: null, prev: null, next: null as never };",
expect: "let a = { value: null, prev: null, next: null as never };",
},
],
},
],
},
],
};