From 97a230b8fffa33d93ab29329b3e45f7f2ae328dd Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Sat, 11 Jan 2020 15:59:40 +0300 Subject: [PATCH] feat: add fixer to invalid file annotation style message (#440) * Add fixer to invalid file annotation style message * Add flow strict test --- src/rules/requireValidFileAnnotation.js | 15 ++++++++-- .../assertions/requireValidFileAnnotation.js | 30 +++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) 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: [ {