diff --git a/src/rules/requireValidFileAnnotation.js b/src/rules/requireValidFileAnnotation.js index 86421e8b..45c061a5 100644 --- a/src/rules/requireValidFileAnnotation.js +++ b/src/rules/requireValidFileAnnotation.js @@ -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)) { diff --git a/tests/rules/assertions/requireValidFileAnnotation.js b/tests/rules/assertions/requireValidFileAnnotation.js index e3c538a5..35f643dd 100644 --- a/tests/rules/assertions/requireValidFileAnnotation.js +++ b/tests/rules/assertions/requireValidFileAnnotation.js @@ -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: [ {