/
get-all-comments-from-nodes.spec.ts
62 lines (54 loc) · 2.08 KB
/
get-all-comments-from-nodes.spec.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
import { ParserOptions } from '@babel/parser';
import { CommentBlock, CommentLine, ImportDeclaration } from '@babel/types';
import { getAllCommentsFromNodes } from '../get-all-comments-from-nodes';
import { getImportNodes } from '../get-import-nodes';
import { getSortedNodes } from '../get-sorted-nodes';
const getSortedImportNodes = (code: string, options?: ParserOptions) => {
const importNodes: ImportDeclaration[] = getImportNodes(code, options);
return getSortedNodes(importNodes, {
importOrder: [],
importOrderBuiltinModulesToTop: false,
importOrderCaseInsensitive: false,
importOrderGroupNamespaceSpecifiers: false,
importOrderMergeDuplicateImports: false,
importOrderSeparation: false,
importOrderSortSpecifiers: false,
});
};
const getComments = (commentNodes: (CommentBlock | CommentLine)[]) =>
commentNodes.map((node) => node.value);
test('it returns empty array when there is no comment', () => {
const result = getSortedImportNodes(`import z from 'z';
`);
const commentNodes = getAllCommentsFromNodes(result);
const comments = getComments(commentNodes);
expect(comments).toEqual([]);
});
test('it returns single comment of a node', () => {
const result = getSortedImportNodes(`// first comment
import z from 'z';
`);
const commentNodes = getAllCommentsFromNodes(result);
const comments = getComments(commentNodes);
expect(comments).toEqual([' first comment']);
});
test('it returns all comments for a node', () => {
const result = getSortedImportNodes(`// first comment
// second comment
import z from 'z';
`);
const commentNodes = getAllCommentsFromNodes(result);
const comments = getComments(commentNodes);
expect(comments).toEqual([' first comment', ' second comment']);
});
test('it returns comment block for a node', () => {
const result = getSortedImportNodes(`
/**
* some block
*/
import z from 'z';
`);
const commentNodes = getAllCommentsFromNodes(result);
const comments = getComments(commentNodes);
expect(comments).toEqual(['*\n * some block\n ']);
});