-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
78 lines (62 loc) · 1.71 KB
/
index.js
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
// @flow
'use strict';
/*::
import type { CommentBlock, CommentLine } from 'babel-flow-types';
type Comment = CommentBlock | CommentLine;
type Comments = Array<Comment>;
*/
const EMPTY_LINE = /^[ \t]*\**[ \t]*$/;
const SPACE_UNTIL_CONTENT = /^[ \t\*]*(?=\S)/;
function normalizeComment(comment /*: Comment */) /*: string */ {
if (comment.type === 'CommentLine') {
return comment.value.trim();
}
if (comment.type !== 'CommentBlock') {
throw new Error(`Unknown comment type: ${comment.type}`);
}
let lines = comment.value.split('\n');
let start = 0;
let end = lines.length - 1;
while (start <= lines.length - 1) {
if (EMPTY_LINE.test(lines[start])) {
start++;
} else {
break;
}
}
while (end >= 0) {
if (EMPTY_LINE.test(lines[end])) {
end--;
} else {
break;
}
}
let trimmed = lines.slice(start, end + 1);
let indents = [];
let startPos = comment.loc.start.column + 2;
trimmed.forEach((line, index) => {
let offset = index === 0 && start === 0 ? startPos : 0;
let match = line.match(SPACE_UNTIL_CONTENT);
if (match) {
indents.push(offset + match[0].length);
}
});
let minIndent = Math.min.apply(Math, indents);
let normalized = trimmed.map((line, index) => {
let offset = index === 0 && start === 0 ? startPos : 0;
return line.slice(Math.abs(minIndent - offset)).trimRight();
});
return normalized.join('\n');
}
function normalizeComments(comments /*: Comments */) /*: string */ {
let results = [];
comments.forEach(comment => {
let res = normalizeComment(comment);
if (res !== '') results.push(res);
});
return results.join('\n');
}
module.exports = {
normalizeComment,
normalizeComments,
};