-
-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathrequireValidFileAnnotation.js
164 lines (141 loc) · 4.78 KB
/
requireValidFileAnnotation.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
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
import _ from 'lodash';
import {
isFlowFileAnnotation,
fuzzyStringMatch,
} from '../utilities';
const defaults = {
annotationStyle: 'none',
strict: false,
};
const looksLikeFlowFileAnnotation = (comment) => {
return /@(?:no)?flo/ui.test(comment);
};
const isValidAnnotationStyle = (node, style) => {
if (style === 'none') {
return true;
}
return style === node.type.toLowerCase();
};
const checkAnnotationSpelling = (comment) => {
return /@[a-z]+\b/u.test(comment) && fuzzyStringMatch(comment.replace(/no/ui, ''), '@flow', 0.2);
};
const isFlowStrict = (comment) => {
return /^@flow\sstrict\b/u.test(comment);
};
const noFlowAnnotation = (comment) => {
return /^@noflow\b/u.test(comment);
};
const schema = [
{
enum: ['always', 'never'],
type: 'string',
},
{
additionalProperties: false,
properties: {
annotationStyle: {
enum: ['none', 'line', 'block'],
type: 'string',
},
strict: {
enum: [true, false],
type: 'boolean',
},
},
type: 'object',
},
];
const create = (context) => {
const always = context.options[0] === 'always';
const style = _.get(context, 'options[1].annotationStyle', defaults.annotationStyle);
const flowStrict = _.get(context, 'options[1].strict', defaults.strict);
return {
Program (node) {
const firstToken = node.tokens[0];
const potentialFlowFileAnnotation = _.find(context.getSourceCode().getAllComments(), (comment) => {
return looksLikeFlowFileAnnotation(comment.value);
});
if (potentialFlowFileAnnotation) {
if (firstToken && firstToken.range[0] < potentialFlowFileAnnotation.range[0]) {
context.report({message: 'Flow file annotation not at the top of the file.', node: potentialFlowFileAnnotation});
}
const annotationValue = potentialFlowFileAnnotation.value.trim();
if (isFlowFileAnnotation(annotationValue)) {
if (!isValidAnnotationStyle(potentialFlowFileAnnotation, style)) {
const annotation = style === 'line' ? '// ' + annotationValue : '/* ' + annotationValue + ' */';
context.report({
fix: (fixer) => {
return fixer.replaceTextRange(
[
potentialFlowFileAnnotation.range[0],
potentialFlowFileAnnotation.range[1],
],
annotation,
);
},
message: 'Flow file annotation style must be `' + annotation + '`',
node: potentialFlowFileAnnotation,
});
}
if (!noFlowAnnotation(annotationValue) && flowStrict && !isFlowStrict(annotationValue)) {
const str = style === 'line' ? '`// @flow strict`' : '`/* @flow strict */`';
context.report({
fix: (fixer) => {
const annotation = ['line', 'none'].includes(style) ? '// @flow strict' : '/* @flow strict */';
return fixer.replaceTextRange([
potentialFlowFileAnnotation.range[0],
potentialFlowFileAnnotation.range[1],
], annotation);
},
message: 'Strict Flow file annotation is required, must be ' + str,
node,
});
}
} else if (checkAnnotationSpelling(annotationValue)) {
context.report({message: 'Misspelled or malformed Flow file annotation.', node: potentialFlowFileAnnotation});
} else {
context.report({message: 'Malformed Flow file annotation.', node: potentialFlowFileAnnotation});
}
} else if (always && !_.get(context, 'settings.flowtype.onlyFilesWithFlowAnnotation')) {
context.report({
fix: (fixer) => {
let annotation;
if (flowStrict) {
annotation = ['line', 'none'].includes(style) ? '// @flow strict\n' : '/* @flow strict */\n';
} else {
annotation = ['line', 'none'].includes(style) ? '// @flow\n' : '/* @flow */\n';
}
const firstComment = node.comments[0];
if (firstComment && firstComment.type === 'Shebang') {
return fixer
.replaceTextRange(
[
firstComment.range[1],
firstComment.range[1],
],
'\n' + annotation.trim(),
);
}
return fixer
.replaceTextRange(
[
node.range[0],
node.range[0],
],
annotation,
);
},
message: 'Flow file annotation is missing.',
node,
});
}
},
};
};
export default {
create,
meta: {
fixable: 'code',
},
schema,
};