Skip to content

Commit

Permalink
feat: add fixer to invalid file annotation style message (#440)
Browse files Browse the repository at this point in the history
* Add fixer to invalid file annotation style message

* Add flow strict test
  • Loading branch information
TrySound authored and gajus committed Jan 11, 2020
1 parent dccaa76 commit 97a230b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/rules/requireValidFileAnnotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,18 @@ const create = (context) => {
const annotationValue = potentialFlowFileAnnotation.value.trim();
if (isFlowFileAnnotation(annotationValue)) {
if (!isValidAnnotationStyle(potentialFlowFileAnnotation, style)) {
const str = style === 'line' ? '`// ' + annotationValue + '`' : '`/* ' + annotationValue + ' */`';

context.report(potentialFlowFileAnnotation, 'Flow file annotation style must be ' + str);
const annotation = style === 'line' ? '// ' + annotationValue : '/* ' + annotationValue + ' */';

context.report({
fix: (fixer) => {
return fixer.replaceTextRange(
[potentialFlowFileAnnotation.start, potentialFlowFileAnnotation.end],
annotation
);
},
message: 'Flow file annotation style must be `' + annotation + '`',
node: potentialFlowFileAnnotation,
});
}
if (!noFlowAnnotation(annotationValue) && flowStrict) {
if (!isFlowStrict(annotationValue)) {
Expand Down
30 changes: 30 additions & 0 deletions tests/rules/assertions/requireValidFileAnnotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,36 @@ export default {
],
output: '// @flow strict\na;\nb;',
},
{
code: '/* @flow */\na;\nb;',
errors: [
{
message: 'Flow file annotation style must be `// @flow`',
},
],
options: [
'never',
{
annotationStyle: 'line',
},
],
output: '// @flow\na;\nb;',
},
{
code: '/* @flow strict */\na;\nb;',
errors: [
{
message: 'Flow file annotation style must be `// @flow strict`',
},
],
options: [
'never',
{
annotationStyle: 'line',
},
],
output: '// @flow strict\na;\nb;',
},
],
misconfigured: [
{
Expand Down

0 comments on commit 97a230b

Please sign in to comment.