diff --git a/packages/babel-parser/src/plugins/flow.js b/packages/babel-parser/src/plugins/flow.js index 88acb476665c..6c03ce7a38bb 100644 --- a/packages/babel-parser/src/plugins/flow.js +++ b/packages/babel-parser/src/plugins/flow.js @@ -95,12 +95,21 @@ export default (superClass: Class): Class => return this.getPluginOption("flow", "all") || this.flowPragma === "flow"; } + finishToken(type: TokenType, val: any): void { + if (!(type === tt.string || type === tt.semi)) { + if (this.flowPragma === undefined) { + this.flowPragma = null; + } + } + return super.finishToken(type, val); + } + addComment(comment: N.Comment): void { if (this.flowPragma === undefined) { // Try to parse a flow pragma. const matches = FLOW_PRAGMA_REGEX.exec(comment.value); if (!matches) { - this.flowPragma = null; + // do nothing } else if (matches[1] === "flow") { this.flowPragma = "flow"; } else if (matches[1] === "noflow") { diff --git a/packages/babel-parser/test/fixtures/flow/pragma/1/input.js b/packages/babel-parser/test/fixtures/flow/pragma/1/input.js new file mode 100644 index 000000000000..f8d6e7e30a24 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/pragma/1/input.js @@ -0,0 +1,3 @@ +foo(y); +// @flow +foo(y); \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/pragma/1/output.json b/packages/babel-parser/test/fixtures/flow/pragma/1/output.json new file mode 100644 index 000000000000..f74bc34124e4 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/pragma/1/output.json @@ -0,0 +1,293 @@ +{ + "type": "File", + "start": 0, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 10 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 10 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "expression": { + "type": "BinaryExpression", + "start": 0, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "left": { + "type": "BinaryExpression", + "start": 0, + "end": 5, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 5 + } + }, + "left": { + "type": "Identifier", + "start": 0, + "end": 3, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 3 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "operator": "<", + "right": { + "type": "Identifier", + "start": 4, + "end": 5, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 5 + }, + "identifierName": "x" + }, + "name": "x" + } + }, + "operator": ">", + "right": { + "type": "Identifier", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + }, + "identifierName": "y" + }, + "name": "y", + "extra": { + "parenthesized": true, + "parenStart": 6 + } + } + }, + "trailingComments": [ + { + "type": "CommentLine", + "value": " @flow", + "start": 11, + "end": 19, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 8 + } + } + } + ] + }, + { + "type": "ExpressionStatement", + "start": 20, + "end": 30, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 10 + } + }, + "expression": { + "type": "BinaryExpression", + "start": 20, + "end": 29, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 9 + } + }, + "left": { + "type": "BinaryExpression", + "start": 20, + "end": 25, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 5 + } + }, + "left": { + "type": "Identifier", + "start": 20, + "end": 23, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 3 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "operator": "<", + "right": { + "type": "Identifier", + "start": 24, + "end": 25, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 5 + }, + "identifierName": "x" + }, + "name": "x" + } + }, + "operator": ">", + "right": { + "type": "Identifier", + "start": 27, + "end": 28, + "loc": { + "start": { + "line": 3, + "column": 7 + }, + "end": { + "line": 3, + "column": 8 + }, + "identifierName": "y" + }, + "name": "y", + "extra": { + "parenthesized": true, + "parenStart": 26 + } + } + }, + "leadingComments": [ + { + "type": "CommentLine", + "value": " @flow", + "start": 11, + "end": 19, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 8 + } + } + } + ] + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentLine", + "value": " @flow", + "start": 11, + "end": 19, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 8 + } + } + } + ] +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/pragma/2/input.js b/packages/babel-parser/test/fixtures/flow/pragma/2/input.js new file mode 100644 index 000000000000..df8b2dd733fb --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/pragma/2/input.js @@ -0,0 +1,3 @@ +'use strict'; +// @flow +foo(y); \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/pragma/2/output.json b/packages/babel-parser/test/fixtures/flow/pragma/2/output.json new file mode 100644 index 000000000000..d97623a0a101 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/pragma/2/output.json @@ -0,0 +1,241 @@ +{ + "type": "File", + "start": 0, + "end": 33, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 10 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 33, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 10 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start": 23, + "end": 33, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 10 + } + }, + "expression": { + "type": "CallExpression", + "start": 23, + "end": 32, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 9 + } + }, + "callee": { + "type": "Identifier", + "start": 23, + "end": 26, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 3 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "typeArguments": { + "type": "TypeParameterInstantiation", + "start": 26, + "end": 29, + "loc": { + "start": { + "line": 3, + "column": 3 + }, + "end": { + "line": 3, + "column": 6 + } + }, + "params": [ + { + "type": "GenericTypeAnnotation", + "start": 27, + "end": 28, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 5 + } + }, + "typeParameters": null, + "id": { + "type": "Identifier", + "start": 27, + "end": 28, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 5 + }, + "identifierName": "x" + }, + "name": "x" + } + } + ] + }, + "arguments": [ + { + "type": "Identifier", + "start": 30, + "end": 31, + "loc": { + "start": { + "line": 3, + "column": 7 + }, + "end": { + "line": 3, + "column": 8 + }, + "identifierName": "y" + }, + "name": "y" + } + ] + }, + "leadingComments": [ + { + "type": "CommentLine", + "value": " @flow", + "start": 14, + "end": 22, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 8 + } + } + } + ] + } + ], + "directives": [ + { + "type": "Directive", + "start": 0, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "value": { + "type": "DirectiveLiteral", + "start": 0, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "value": "use strict", + "extra": { + "raw": "'use strict'", + "rawValue": "use strict" + } + }, + "trailingComments": [ + { + "type": "CommentLine", + "value": " @flow", + "start": 14, + "end": 22, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 8 + } + } + } + ] + } + ] + }, + "comments": [ + { + "type": "CommentLine", + "value": " @flow", + "start": 14, + "end": 22, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 8 + } + } + } + ] +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/pragma/3/input.js b/packages/babel-parser/test/fixtures/flow/pragma/3/input.js new file mode 100644 index 000000000000..cf49c8b96de4 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/pragma/3/input.js @@ -0,0 +1,3 @@ +// arbitrary comment +// @flow +foo(y); \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/pragma/3/output.json b/packages/babel-parser/test/fixtures/flow/pragma/3/output.json new file mode 100644 index 000000000000..5baf19ddf253 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/pragma/3/output.json @@ -0,0 +1,219 @@ +{ + "type": "File", + "start": 0, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 10 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 10 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start": 30, + "end": 40, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 10 + } + }, + "expression": { + "type": "CallExpression", + "start": 30, + "end": 39, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 9 + } + }, + "callee": { + "type": "Identifier", + "start": 30, + "end": 33, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 3 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "typeArguments": { + "type": "TypeParameterInstantiation", + "start": 33, + "end": 36, + "loc": { + "start": { + "line": 3, + "column": 3 + }, + "end": { + "line": 3, + "column": 6 + } + }, + "params": [ + { + "type": "GenericTypeAnnotation", + "start": 34, + "end": 35, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 5 + } + }, + "typeParameters": null, + "id": { + "type": "Identifier", + "start": 34, + "end": 35, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 5 + }, + "identifierName": "x" + }, + "name": "x" + } + } + ] + }, + "arguments": [ + { + "type": "Identifier", + "start": 37, + "end": 38, + "loc": { + "start": { + "line": 3, + "column": 7 + }, + "end": { + "line": 3, + "column": 8 + }, + "identifierName": "y" + }, + "name": "y" + } + ] + }, + "leadingComments": [ + { + "type": "CommentLine", + "value": " arbitrary comment", + "start": 0, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 20 + } + } + }, + { + "type": "CommentLine", + "value": " @flow", + "start": 21, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 8 + } + } + } + ] + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentLine", + "value": " arbitrary comment", + "start": 0, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 20 + } + } + }, + { + "type": "CommentLine", + "value": " @flow", + "start": 21, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 8 + } + } + } + ] +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/pragma/4/input.js b/packages/babel-parser/test/fixtures/flow/pragma/4/input.js new file mode 100644 index 000000000000..458f7c2b9460 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/pragma/4/input.js @@ -0,0 +1,4 @@ +'use strict'; +// arbitrary comment +// @flow +foo(y); \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/pragma/4/output.json b/packages/babel-parser/test/fixtures/flow/pragma/4/output.json new file mode 100644 index 000000000000..46325d1919a8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/pragma/4/output.json @@ -0,0 +1,289 @@ +{ + "type": "File", + "start": 0, + "end": 54, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 10 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 54, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 10 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start": 44, + "end": 54, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 10 + } + }, + "expression": { + "type": "CallExpression", + "start": 44, + "end": 53, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 9 + } + }, + "callee": { + "type": "Identifier", + "start": 44, + "end": 47, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 3 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "typeArguments": { + "type": "TypeParameterInstantiation", + "start": 47, + "end": 50, + "loc": { + "start": { + "line": 4, + "column": 3 + }, + "end": { + "line": 4, + "column": 6 + } + }, + "params": [ + { + "type": "GenericTypeAnnotation", + "start": 48, + "end": 49, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 5 + } + }, + "typeParameters": null, + "id": { + "type": "Identifier", + "start": 48, + "end": 49, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 5 + }, + "identifierName": "x" + }, + "name": "x" + } + } + ] + }, + "arguments": [ + { + "type": "Identifier", + "start": 51, + "end": 52, + "loc": { + "start": { + "line": 4, + "column": 7 + }, + "end": { + "line": 4, + "column": 8 + }, + "identifierName": "y" + }, + "name": "y" + } + ] + }, + "leadingComments": [ + { + "type": "CommentLine", + "value": " arbitrary comment", + "start": 14, + "end": 34, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 20 + } + } + }, + { + "type": "CommentLine", + "value": " @flow", + "start": 35, + "end": 43, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 8 + } + } + } + ] + } + ], + "directives": [ + { + "type": "Directive", + "start": 0, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "value": { + "type": "DirectiveLiteral", + "start": 0, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "value": "use strict", + "extra": { + "raw": "'use strict'", + "rawValue": "use strict" + } + }, + "trailingComments": [ + { + "type": "CommentLine", + "value": " arbitrary comment", + "start": 14, + "end": 34, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 20 + } + } + }, + { + "type": "CommentLine", + "value": " @flow", + "start": 35, + "end": 43, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 8 + } + } + } + ] + } + ] + }, + "comments": [ + { + "type": "CommentLine", + "value": " arbitrary comment", + "start": 14, + "end": 34, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 20 + } + } + }, + { + "type": "CommentLine", + "value": " @flow", + "start": 35, + "end": 43, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 8 + } + } + } + ] +} \ No newline at end of file