/
newlineAfterFlowAnnotation.js
75 lines (64 loc) · 1.8 KB
/
newlineAfterFlowAnnotation.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
import _ from 'lodash';
const looksLikeFlowFileAnnotation = (comment) => {
return /@(?:no)?flo/i.test(comment);
};
const schema = [
{
enum: ['always', 'always-windows', 'never'],
type: 'string',
},
];
const create = (context) => {
const mode = context.options[0];
const never = mode === 'never';
const newline = mode === 'always-windows' ? '\r\n' : '\n';
return {
Program (node) {
const sourceCode = context.getSourceCode();
const potentialFlowFileAnnotation = _.find(
context.getAllComments(),
(comment) => {
return looksLikeFlowFileAnnotation(comment.value);
},
);
if (potentialFlowFileAnnotation) {
const line = potentialFlowFileAnnotation.loc.end.line;
const nextLineIsEmpty = sourceCode.lines[line] === '';
if (!never && !nextLineIsEmpty) {
context.report({
fix: (fixer) => {
return fixer.insertTextAfter(
potentialFlowFileAnnotation,
newline,
);
},
message: 'Expected newline after flow annotation',
node,
});
}
if (never && nextLineIsEmpty) {
context.report({
fix: (fixer) => {
const lineBreak = sourceCode.text[potentialFlowFileAnnotation.range[1]];
return fixer.replaceTextRange(
[
potentialFlowFileAnnotation.range[1],
potentialFlowFileAnnotation.range[1] + (
lineBreak === '\r' ? 2 : 1
),
],
'',
);
},
message: 'Expected no newline after flow annotation',
node,
});
}
}
},
};
};
export default {
create,
schema,
};