/
index.test.ts
536 lines (497 loc) · 12.6 KB
/
index.test.ts
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import os from 'os';
import * as docblock from '..';
describe('docblock', () => {
it('extracts valid docblock with line comment', () => {
const code =
'/**' +
os.EOL +
' * @team foo' +
os.EOL +
'* // TODO: test' +
os.EOL +
'*/' +
os.EOL +
'const x = foo;';
expect(docblock.extract(code)).toBe(
'/**' +
os.EOL +
' * @team foo' +
os.EOL +
'* // TODO: test' +
os.EOL +
'*/',
);
});
it('extracts valid docblock', () => {
const code =
'/**' +
os.EOL +
' * @team foo' +
os.EOL +
'*/' +
os.EOL +
'const x = foo;';
expect(docblock.extract(code)).toBe(
'/**' + os.EOL + ' * @team foo' + os.EOL + '*/',
);
});
it('extracts valid docblock with more comments', () => {
const code =
'/**' +
os.EOL +
' * @team foo' +
os.EOL +
'*/' +
os.EOL +
'const x = foo;' +
os.EOL +
'/**foo*/';
expect(docblock.extract(code)).toBe(
'/**' + os.EOL + ' * @team foo' + os.EOL + '*/',
);
});
it('extracts from invalid docblock', () => {
const code =
'/*' +
os.EOL +
' * @team foo' +
os.EOL +
'*/' +
os.EOL +
'const x = foo;';
expect(docblock.extract(code)).toBe(
'/*' + os.EOL + ' * @team foo' + os.EOL + '*/',
);
});
it('returns extract and parsedocblock', () => {
const code =
'/** @provides module-name */' +
os.EOL +
'' +
'' +
os.EOL +
'' +
'.dummy {}' +
os.EOL +
'';
expect(docblock.parse(docblock.extract(code))).toEqual({
provides: 'module-name',
});
});
it('parses directives out of a docblock', () => {
const code =
'/**' +
os.EOL +
'' +
' * @team foo' +
os.EOL +
'' +
' * @css a b' +
os.EOL +
'' +
' * @preserve-whitespace' +
os.EOL +
'' +
' */';
expect(docblock.parse(code)).toEqual({
css: 'a b',
'preserve-whitespace': '',
team: 'foo',
});
});
it('parses multiple of the same directives out of a docblock', () => {
const code =
'/**' +
os.EOL +
'' +
' * @x foo' +
os.EOL +
'' +
' * @x bar' +
os.EOL +
'' +
' * @y' +
os.EOL +
'' +
' */';
expect(docblock.parse(code)).toEqual({
x: ['foo', 'bar'],
y: '',
});
});
it('parses >=3 of the same directives out of a docblock', () => {
const code =
'/**' +
os.EOL +
'' +
' * @x foo' +
os.EOL +
'' +
' * @x bar' +
os.EOL +
'' +
' * @x baz' +
os.EOL +
'' +
' */';
expect(docblock.parse(code)).toEqual({
x: ['foo', 'bar', 'baz'],
});
});
it('parses directives out of a docblock with comments', () => {
const code =
'/**' +
os.EOL +
'' +
' * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.' +
os.EOL +
'' +
' * @team foo' +
os.EOL +
'' +
' * @css a b' +
os.EOL +
'' +
' *' +
os.EOL +
'' +
' * And some license here' +
os.EOL +
'' +
' * @preserve-whitespace' +
os.EOL +
'' +
' */';
expect(docblock.parse(code)).toEqual({
css: 'a b',
'preserve-whitespace': '',
team: 'foo',
});
});
it('parses directives out of a docblock with line comments', () => {
const code =
'/**' +
os.EOL +
'' +
' * @team foo' +
os.EOL +
'' +
' * // TODO: test' +
os.EOL +
'' +
' */';
expect(docblock.parseWithComments(code)).toEqual({
comments: '// TODO: test',
pragmas: {team: 'foo'},
});
});
it('parses multiline directives', () => {
const code =
'/**' +
os.EOL +
'' +
' * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.' +
os.EOL +
'' +
' * @class A long declaration of a class' +
os.EOL +
'' +
' * goes here, so we can read it and enjoy' +
os.EOL +
'' +
' *' +
os.EOL +
'' +
' * And some license here' +
os.EOL +
'' +
' * @preserve-whitespace' +
os.EOL +
'' +
' */';
expect(docblock.parse(code)).toEqual({
class:
'A long declaration of a class goes here, ' +
'so we can read it and enjoy',
'preserve-whitespace': '',
});
});
it('parses multiline directives even if there are linecomments within the docblock', () => {
const code =
'/**' +
os.EOL +
'' +
' * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.' +
os.EOL +
'' +
' * @class A long declaration of a class' +
os.EOL +
'' +
' * goes here, so we can read it and enjoy' +
os.EOL +
'' +
' *' +
os.EOL +
'' +
' * And some license here' +
os.EOL +
'' +
' * @preserve-whitespace' +
os.EOL +
'' +
'// heres a comment' +
' */';
expect(docblock.parseWithComments(code)).toEqual({
comments:
'Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.' +
os.EOL +
os.EOL +
'And some license here' +
os.EOL +
'// heres a comment',
pragmas: {
class:
'A long declaration of a class goes here, ' +
'so we can read it and enjoy',
'preserve-whitespace': '',
},
});
});
it('supports slashes in @team directive', () => {
const code = '/**' + os.EOL + ' * @team apple/banana' + os.EOL + ' */';
expect(docblock.parse(code)).toEqual({
team: 'apple/banana',
});
});
it('extracts comments from docblock', () => {
const code =
'/**' +
os.EOL +
' * hello world' +
os.EOL +
' * @flow yes' +
os.EOL +
' */';
expect(docblock.parseWithComments(code)).toEqual({
comments: 'hello world',
pragmas: {flow: 'yes'},
});
});
it('extracts multiline comments from docblock', () => {
const code =
'/**' +
os.EOL +
' * hello' +
os.EOL +
' * world' +
os.EOL +
' * @flow yes' +
os.EOL +
' */';
expect(docblock.parseWithComments(code)).toEqual({
comments: 'hello' + os.EOL + 'world',
pragmas: {flow: 'yes'},
});
});
it('preserves leading whitespace in multiline comments from docblock', () => {
const code =
'/**' + os.EOL + ' * hello' + os.EOL + ' * world' + os.EOL + ' */';
expect(docblock.parseWithComments(code).comments).toEqual(
' hello' + os.EOL + ' world',
);
});
it('removes leading newlines in multiline comments from docblock', () => {
const code =
'/**' +
os.EOL +
' * @snailcode' +
os.EOL +
' *' +
os.EOL +
' * hello world' +
os.EOL +
' */';
expect(docblock.parseWithComments(code).comments).toEqual(' hello world');
});
it('extracts comments from beginning and end of docblock', () => {
const code =
'/**' +
os.EOL +
' * hello' +
os.EOL +
' * @flow yes' +
os.EOL +
' * ' +
os.EOL +
' * world' +
os.EOL +
' */';
expect(docblock.parseWithComments(code)).toEqual({
comments: 'hello' + os.EOL + os.EOL + 'world',
pragmas: {flow: 'yes'},
});
});
it("preserve urls within a pragma's values", () => {
const code =
'/**' + os.EOL + ' * @see: https://example.com' + os.EOL + ' */';
expect(docblock.parseWithComments(code)).toEqual({
comments: '',
pragmas: {'see:': 'https://example.com'},
});
});
it('strip linecomments from pragmas but preserve for comments', () => {
const code =
'/**' + os.EOL + ' * @format: everything' + os.EOL + '// keep me */';
expect(docblock.parseWithComments(code)).toEqual({
comments: '// keep me',
pragmas: {'format:': 'everything'},
});
});
it('extracts docblock comments as CRLF when docblock contains CRLF', () => {
const code = '/**\r\n * foo\r\n * bar\r\n*/';
expect(docblock.parseWithComments(code)).toEqual({
comments: 'foo\r\nbar',
pragmas: {},
});
});
it('extracts docblock comments as LF when docblock contains LF', () => {
const code = '/**\n * foo\n * bar\n*/';
expect(docblock.parseWithComments(code)).toEqual({
comments: 'foo\nbar',
pragmas: {},
});
});
it('strips the docblock out of a file that contains a top docblock', () => {
const code = '/**\n * foo\n * bar\n*/\nthe rest';
expect(docblock.strip(code)).toEqual('\nthe rest');
});
it('returns a file unchanged if there is no top docblock to strip', () => {
const code = 'someCodeAtTheTop();\n/** docblock */';
expect(docblock.strip(code)).toEqual(code);
});
it('prints docblocks with no pragmas as empty string', () => {
const pragmas = {};
expect(docblock.print({pragmas})).toEqual('');
});
it('prints docblocks with one pragma on one line', () => {
const pragmas = {flow: ''};
expect(docblock.print({pragmas})).toEqual('/** @flow */');
});
it('prints docblocks with multiple pragmas on multiple lines', () => {
const pragmas = {
flow: '',
format: '',
};
expect(docblock.print({pragmas})).toEqual(
'/**' + os.EOL + ' * @flow' + os.EOL + ' * @format' + os.EOL + ' */',
);
});
it('prints docblocks with multiple of the same pragma', () => {
const pragmas = {
x: ['a', 'b'],
y: 'c',
};
expect(docblock.print({pragmas})).toEqual(
'/**' +
os.EOL +
' * @x a' +
os.EOL +
' * @x b' +
os.EOL +
' * @y c' +
os.EOL +
' */',
);
});
it('prints docblocks with pragmas', () => {
const pragmas = {
flow: 'foo',
team: 'x/y/z',
};
expect(docblock.print({pragmas})).toEqual(
'/**' +
os.EOL +
' * @flow foo' +
os.EOL +
' * @team x/y/z' +
os.EOL +
' */',
);
});
it('prints docblocks with comments', () => {
const pragmas = {flow: 'foo'};
const comments = 'hello';
expect(docblock.print({comments, pragmas})).toEqual(
'/**' +
os.EOL +
' * hello' +
os.EOL +
' *' +
os.EOL +
' * @flow foo' +
os.EOL +
' */',
);
});
it('prints docblocks with comments and no keys', () => {
const pragmas = {};
const comments =
'Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.';
expect(docblock.print({comments, pragmas})).toEqual(
'/**' + os.EOL + ' * ' + comments + os.EOL + ' */',
);
});
it('prints docblocks with multiline comments', () => {
const pragmas = {};
const comments = 'hello' + os.EOL + 'world';
expect(docblock.print({comments, pragmas})).toEqual(
'/**' + os.EOL + ' * hello' + os.EOL + ' * world' + os.EOL + ' */',
);
});
it('prints docblocks that are parseable', () => {
const pragmas = {a: 'b', c: ''};
const comments = 'hello world!';
const formatted = docblock.print({comments, pragmas});
const parsed = docblock.parse(formatted);
expect(parsed).toEqual(pragmas);
});
it('can augment existing docblocks with comments', () => {
const before =
'/**' + os.EOL + ' * Legalese' + os.EOL + ' * @flow' + os.EOL + ' */';
const {comments, pragmas} = docblock.parseWithComments(before);
pragmas.format = '';
const after = docblock.print({comments, pragmas});
expect(after).toEqual(
'/**' +
os.EOL +
' * Legalese' +
os.EOL +
' *' +
os.EOL +
' * @flow' +
os.EOL +
' * @format' +
os.EOL +
' */',
);
});
it('prints docblocks using CRLF if comments contains CRLF', () => {
const pragmas = {};
const comments = 'hello\r\nworld';
const formatted = docblock.print({comments, pragmas});
expect(formatted).toEqual('/**\r\n * hello\r\n * world\r\n */');
});
it('prints docblocks using LF if comments contains LF', () => {
const pragmas = {};
const comments = 'hello\nworld';
const formatted = docblock.print({comments, pragmas});
expect(formatted).toEqual('/**\n * hello\n * world\n */');
});
});