Skip to content

Commit 51f4ba2

Browse files
KevinGrandongajus
authored andcommitted
feat: add fixer for requireValidFileAnnotation when always (#332)
* Add fixer for requireValidFileAnnotation when always * Add autofix note in readme
1 parent cb3f3be commit 51f4ba2

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

Diff for: README.md

+2
Original file line numberDiff line numberDiff line change
@@ -2437,6 +2437,8 @@ This rule validates Flow file annotations.
24372437
24382438
This rule can optionally report missing or missed placed annotations, common typos (e.g. `// @floww`), and enforce a consistant annotation style.
24392439

2440+
This rule is autofixable with the `--fix` argument. This will autofix files by adding missing flow annotations to the top of each file. To avoid autofixing this rule per-file, you can add a `// @noflow` annotation to the top of individual files.
2441+
24402442
<a name="eslint-plugin-flowtype-rules-require-valid-file-annotation-options"></a>
24412443
#### Options
24422444

Diff for: src/rules/requireValidFileAnnotation.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ const create = (context) => {
4949
Program (node) {
5050
const firstToken = node.tokens[0];
5151

52+
const addAnnotation = () => {
53+
return (fixer) => {
54+
const annotation = ['line', 'none'].includes(style) ? '// @flow\n' : '/* @flow */\n';
55+
56+
return fixer.replaceTextRange([node.start, node.start], annotation);
57+
};
58+
};
59+
5260
const potentialFlowFileAnnotation = _.find(context.getAllComments(), (comment) => {
5361
return looksLikeFlowFileAnnotation(comment.value);
5462
});
@@ -70,7 +78,11 @@ const create = (context) => {
7078
context.report(potentialFlowFileAnnotation, 'Malformed Flow file annotation.');
7179
}
7280
} else if (always) {
73-
context.report(node, 'Flow file annotation is missing.');
81+
context.report({
82+
fix: addAnnotation(),
83+
message: 'Flow file annotation is missing.',
84+
node
85+
});
7486
}
7587
}
7688
};

Diff for: tests/rules/assertions/requireValidFileAnnotation.js

+27
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,33 @@ export default {
122122
annotationStyle: 'block'
123123
}
124124
]
125+
},
126+
{
127+
code: 'a;',
128+
errors: [
129+
{
130+
message: 'Flow file annotation is missing.'
131+
}
132+
],
133+
options: [
134+
'always'
135+
],
136+
output: '// @flow\na;'
137+
},
138+
{
139+
code: 'a;',
140+
errors: [
141+
{
142+
message: 'Flow file annotation is missing.'
143+
}
144+
],
145+
options: [
146+
'always',
147+
{
148+
annotationStyle: 'block'
149+
}
150+
],
151+
output: '/* @flow */\na;'
125152
}
126153
],
127154
misconfigured: [

0 commit comments

Comments
 (0)