Skip to content

Commit

Permalink
allow directives and other comments before flow pragma (#9891)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau authored and nicolo-ribaudo committed Apr 26, 2019
1 parent ca3c53a commit 277a262
Show file tree
Hide file tree
Showing 9 changed files with 1,065 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/babel-parser/src/plugins/flow.js
Expand Up @@ -95,12 +95,21 @@ export default (superClass: Class<Parser>): Class<Parser> =>
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") {
Expand Down
3 changes: 3 additions & 0 deletions packages/babel-parser/test/fixtures/flow/pragma/1/input.js
@@ -0,0 +1,3 @@
foo<x>(y);
// @flow
foo<x>(y);
293 changes: 293 additions & 0 deletions 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
}
}
}
]
}
3 changes: 3 additions & 0 deletions packages/babel-parser/test/fixtures/flow/pragma/2/input.js
@@ -0,0 +1,3 @@
'use strict';
// @flow
foo<x>(y);

0 comments on commit 277a262

Please sign in to comment.