diff --git a/src/rules/requireValidFileAnnotation.js b/src/rules/requireValidFileAnnotation.js index e6643ab2..51265988 100644 --- a/src/rules/requireValidFileAnnotation.js +++ b/src/rules/requireValidFileAnnotation.js @@ -124,6 +124,19 @@ const create = (context) => { annotation = ['line', 'none'].includes(style) ? '// @flow\n' : '/* @flow */\n'; } + const firstComment = node.comments[0]; + + if (firstComment && firstComment.type === 'Shebang') { + return fixer + .replaceTextRange( + [ + firstComment.range[1], + firstComment.range[1], + ], + '\n' + annotation.trim(), + ); + } + return fixer .replaceTextRange( [ diff --git a/tests/rules/assertions/requireValidFileAnnotation.js b/tests/rules/assertions/requireValidFileAnnotation.js index c2b627f6..5db3ebb9 100644 --- a/tests/rules/assertions/requireValidFileAnnotation.js +++ b/tests/rules/assertions/requireValidFileAnnotation.js @@ -1,5 +1,29 @@ export default { invalid: [ + { + code: '#!/usr/bin/env node', + errors: [ + { + message: 'Flow file annotation is missing.', + }, + ], + options: [ + 'always', + ], + output: '#!/usr/bin/env node\n// @flow', + }, + { + code: '#!/usr/bin/env node\na;', + errors: [ + { + message: 'Flow file annotation is missing.', + }, + ], + options: [ + 'always', + ], + output: '#!/usr/bin/env node\n// @flow\na;', + }, { code: ';// @flow', errors: [