Skip to content

feat: add fixer for requireValidFileAnnotation when always #332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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