Skip to content

Commit

Permalink
feat: add fixer for requireValidFileAnnotation when always (#332)
Browse files Browse the repository at this point in the history
* Add fixer for requireValidFileAnnotation when always

* Add autofix note in readme
  • Loading branch information
KevinGrandon authored and gajus committed May 29, 2018
1 parent cb3f3be commit 51f4ba2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2437,6 +2437,8 @@ This rule validates Flow file annotations.
This rule can optionally report missing or missed placed annotations, common typos (e.g. `// @floww`), and enforce a consistant annotation style.

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.

<a name="eslint-plugin-flowtype-rules-require-valid-file-annotation-options"></a>
#### Options

Expand Down
14 changes: 13 additions & 1 deletion src/rules/requireValidFileAnnotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ const create = (context) => {
Program (node) {
const firstToken = node.tokens[0];

const addAnnotation = () => {
return (fixer) => {
const annotation = ['line', 'none'].includes(style) ? '// @flow\n' : '/* @flow */\n';

return fixer.replaceTextRange([node.start, node.start], annotation);
};
};

const potentialFlowFileAnnotation = _.find(context.getAllComments(), (comment) => {
return looksLikeFlowFileAnnotation(comment.value);
});
Expand All @@ -70,7 +78,11 @@ const create = (context) => {
context.report(potentialFlowFileAnnotation, 'Malformed Flow file annotation.');
}
} else if (always) {
context.report(node, 'Flow file annotation is missing.');
context.report({
fix: addAnnotation(),
message: 'Flow file annotation is missing.',
node
});
}
}
};
Expand Down
27 changes: 27 additions & 0 deletions tests/rules/assertions/requireValidFileAnnotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,33 @@ export default {
annotationStyle: 'block'
}
]
},
{
code: 'a;',
errors: [
{
message: 'Flow file annotation is missing.'
}
],
options: [
'always'
],
output: '// @flow\na;'
},
{
code: 'a;',
errors: [
{
message: 'Flow file annotation is missing.'
}
],
options: [
'always',
{
annotationStyle: 'block'
}
],
output: '/* @flow */\na;'
}
],
misconfigured: [
Expand Down

0 comments on commit 51f4ba2

Please sign in to comment.