/
requireValidFileAnnotation.js
139 lines (122 loc) · 4.12 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
import _ from 'lodash';
import {
isFlowFileAnnotation,
fuzzyStringMatch,
} from '../utilities';
const defaults = {
annotationStyle: 'none',
strict: false,
};
const looksLikeFlowFileAnnotation = (comment) => {
return /@(?:no)?flo/i.test(comment);
};
const isValidAnnotationStyle = (node, style) => {
if (style === 'none') {
return true;
}
return style === node.type.toLowerCase();
};
const checkAnnotationSpelling = (comment) => {
return /@[a-z]+\b/.test(comment) && fuzzyStringMatch(comment.replace(/no/i, ''), '@flow', 0.2);
};
const isFlowStrict = (comment) => {
return /^@flow\sstrict\b/.test(comment);
};
const noFlowAnnotation = (comment) => {
return /^@noflow\b/.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 addAnnotation = () => {
return (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';
}
return fixer.replaceTextRange([node.range[0], node.range[0]], annotation);
};
};
const addStrictAnnotation = () => {
return (fixer) => {
const annotation = ['line', 'none'].includes(style) ? '// @flow strict\n' : '/* @flow strict */\n';
return fixer.replaceTextRange([node.range[0], node.range[0]], annotation);
};
};
const potentialFlowFileAnnotation = _.find(context.getAllComments(), (comment) => {
return looksLikeFlowFileAnnotation(comment.value);
});
if (potentialFlowFileAnnotation) {
if (firstToken && firstToken.range[0] < potentialFlowFileAnnotation.range[0]) {
context.report(potentialFlowFileAnnotation, 'Flow file annotation not at the top of the file.');
}
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) {
if (!isFlowStrict(annotationValue)) {
const str = style === 'line' ? '`// @flow strict`' : '`/* @flow strict */`';
context.report({
fix: addStrictAnnotation(),
message: 'Strict Flow file annotation is required, should be ' + str,
node,
});
}
}
} else if (checkAnnotationSpelling(annotationValue)) {
context.report(potentialFlowFileAnnotation, 'Misspelled or malformed Flow file annotation.');
} else {
context.report(potentialFlowFileAnnotation, 'Malformed Flow file annotation.');
}
} else if (always) {
context.report({
fix: addAnnotation(),
message: 'Flow file annotation is missing.',
node,
});
}
},
};
};
export default {
create,
schema,
};