Skip to content

Commit

Permalink
fix: support shebang (fixes #416)
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed May 16, 2020
1 parent 4238464 commit e06f09a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/rules/requireValidFileAnnotation.js
Expand Up @@ -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(
[
Expand Down
24 changes: 24 additions & 0 deletions 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: [
Expand Down

0 comments on commit e06f09a

Please sign in to comment.