allow directives and other comments before flow pragma (#9891)

This commit is contained in:
Tan Li Hau 2019-04-27 04:23:35 +08:00 committed by Nicolò Ribaudo
parent ca3c53ae0b
commit 277a2620c6
9 changed files with 1065 additions and 1 deletions

View File

@ -95,12 +95,21 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return this.getPluginOption("flow", "all") || this.flowPragma === "flow"; 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 { addComment(comment: N.Comment): void {
if (this.flowPragma === undefined) { if (this.flowPragma === undefined) {
// Try to parse a flow pragma. // Try to parse a flow pragma.
const matches = FLOW_PRAGMA_REGEX.exec(comment.value); const matches = FLOW_PRAGMA_REGEX.exec(comment.value);
if (!matches) { if (!matches) {
this.flowPragma = null; // do nothing
} else if (matches[1] === "flow") { } else if (matches[1] === "flow") {
this.flowPragma = "flow"; this.flowPragma = "flow";
} else if (matches[1] === "noflow") { } else if (matches[1] === "noflow") {

View File

@ -0,0 +1,3 @@
foo<x>(y);
// @flow
foo<x>(y);

View File

@ -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
}
}
}
]
}

View File

@ -0,0 +1,3 @@
'use strict';
// @flow
foo<x>(y);

View File

@ -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
}
}
}
]
}

View File

@ -0,0 +1,3 @@
// arbitrary comment
// @flow
foo<x>(y);

View File

@ -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
}
}
}
]
}

View File

@ -0,0 +1,4 @@
'use strict';
// arbitrary comment
// @flow
foo<x>(y);

View File

@ -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
}
}
}
]
}