From e06f09a925fea9993f727e5bed1cd6bbdc448e43 Mon Sep 17 00:00:00 2001 From: Gajus Kuizinas Date: Fri, 15 May 2020 17:07:46 -0700 Subject: [PATCH] fix: support shebang (fixes #416) --- src/rules/requireValidFileAnnotation.js | 13 ++++++++++ .../assertions/requireValidFileAnnotation.js | 24 +++++++++++++++++++ 2 files changed, 37 insertions(+) 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: [