From 354666aa17295cf61cf9c465ed48f37ddc8d831a Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Tue, 7 May 2019 22:22:25 +0800 Subject: [PATCH] fix: allow shebang directive (#9922) * fix: allow shebang directive * Update packages/babel-parser/src/plugins/flow.js * chore: prettier --- packages/babel-parser/src/plugins/flow.js | 6 +- .../test/fixtures/flow/pragma/5/input.js | 5 + .../test/fixtures/flow/pragma/5/output.json | 304 ++++++++++++++++++ 3 files changed, 314 insertions(+), 1 deletion(-) create mode 100644 packages/babel-parser/test/fixtures/flow/pragma/5/input.js create mode 100644 packages/babel-parser/test/fixtures/flow/pragma/5/output.json diff --git a/packages/babel-parser/src/plugins/flow.js b/packages/babel-parser/src/plugins/flow.js index 6c03ce7a38..9522d13725 100644 --- a/packages/babel-parser/src/plugins/flow.js +++ b/packages/babel-parser/src/plugins/flow.js @@ -96,7 +96,11 @@ export default (superClass: Class): Class => } finishToken(type: TokenType, val: any): void { - if (!(type === tt.string || type === tt.semi)) { + if ( + type !== tt.string && + type !== tt.semi && + type !== tt.interpreterDirective + ) { if (this.flowPragma === undefined) { this.flowPragma = null; } diff --git a/packages/babel-parser/test/fixtures/flow/pragma/5/input.js b/packages/babel-parser/test/fixtures/flow/pragma/5/input.js new file mode 100644 index 0000000000..e2ee3d87b6 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/pragma/5/input.js @@ -0,0 +1,5 @@ +#!/usr/bin/env node +'use strict'; +// arbitrary comment +// @flow +foo(y); \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/pragma/5/output.json b/packages/babel-parser/test/fixtures/flow/pragma/5/output.json new file mode 100644 index 0000000000..c59fb12c86 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/pragma/5/output.json @@ -0,0 +1,304 @@ +{ + "type": "File", + "start": 0, + "end": 74, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 10 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 74, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 10 + } + }, + "sourceType": "module", + "interpreter": { + "type": "InterpreterDirective", + "start": 0, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "value": "/usr/bin/env node" + }, + "body": [ + { + "type": "ExpressionStatement", + "start": 64, + "end": 74, + "loc": { + "start": { + "line": 5, + "column": 0 + }, + "end": { + "line": 5, + "column": 10 + } + }, + "expression": { + "type": "CallExpression", + "start": 64, + "end": 73, + "loc": { + "start": { + "line": 5, + "column": 0 + }, + "end": { + "line": 5, + "column": 9 + } + }, + "callee": { + "type": "Identifier", + "start": 64, + "end": 67, + "loc": { + "start": { + "line": 5, + "column": 0 + }, + "end": { + "line": 5, + "column": 3 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "typeArguments": { + "type": "TypeParameterInstantiation", + "start": 67, + "end": 70, + "loc": { + "start": { + "line": 5, + "column": 3 + }, + "end": { + "line": 5, + "column": 6 + } + }, + "params": [ + { + "type": "GenericTypeAnnotation", + "start": 68, + "end": 69, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 5, + "column": 5 + } + }, + "typeParameters": null, + "id": { + "type": "Identifier", + "start": 68, + "end": 69, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 5, + "column": 5 + }, + "identifierName": "x" + }, + "name": "x" + } + } + ] + }, + "arguments": [ + { + "type": "Identifier", + "start": 71, + "end": 72, + "loc": { + "start": { + "line": 5, + "column": 7 + }, + "end": { + "line": 5, + "column": 8 + }, + "identifierName": "y" + }, + "name": "y" + } + ] + }, + "leadingComments": [ + { + "type": "CommentLine", + "value": " arbitrary comment", + "start": 34, + "end": 54, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 20 + } + } + }, + { + "type": "CommentLine", + "value": " @flow", + "start": 55, + "end": 63, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 8 + } + } + } + ] + } + ], + "directives": [ + { + "type": "Directive", + "start": 20, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 13 + } + }, + "value": { + "type": "DirectiveLiteral", + "start": 20, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 12 + } + }, + "value": "use strict", + "extra": { + "raw": "'use strict'", + "rawValue": "use strict" + } + }, + "trailingComments": [ + { + "type": "CommentLine", + "value": " arbitrary comment", + "start": 34, + "end": 54, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 20 + } + } + }, + { + "type": "CommentLine", + "value": " @flow", + "start": 55, + "end": 63, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 8 + } + } + } + ] + } + ] + }, + "comments": [ + { + "type": "CommentLine", + "value": " arbitrary comment", + "start": 34, + "end": 54, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 20 + } + } + }, + { + "type": "CommentLine", + "value": " @flow", + "start": 55, + "end": 63, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 8 + } + } + } + ] +} \ No newline at end of file