Allows the interface to be used as an Identifier for flow plugin (#12254)

This commit is contained in:
Sosuke Suzuki 2020-10-26 22:50:57 +09:00 committed by GitHub
parent a967910b8b
commit f5bd9f2013
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 531 additions and 11 deletions

View File

@ -13,7 +13,7 @@ import type { Pos, Position } from "../util/location";
import type State from "../tokenizer/state"; import type State from "../tokenizer/state";
import { types as tc } from "../tokenizer/context"; import { types as tc } from "../tokenizer/context";
import * as charCodes from "charcodes"; import * as charCodes from "charcodes";
import { isIteratorStart } from "../util/identifier"; import { isIteratorStart, isKeyword } from "../util/identifier";
import { import {
type BindingTypes, type BindingTypes,
BIND_NONE, BIND_NONE,
@ -1735,14 +1735,17 @@ export default (superClass: Class<Parser>): Class<Parser> =>
this.match(tt.name) && this.match(tt.name) &&
this.state.value === "interface" this.state.value === "interface"
) { ) {
const lookahead = this.lookahead();
if (lookahead.type === tt.name || isKeyword(lookahead.value)) {
const node = this.startNode(); const node = this.startNode();
this.next(); this.next();
return this.flowParseInterface(node); return this.flowParseInterface(node);
}
} else if (this.shouldParseEnums() && this.isContextual("enum")) { } else if (this.shouldParseEnums() && this.isContextual("enum")) {
const node = this.startNode(); const node = this.startNode();
this.next(); this.next();
return this.flowParseEnumDeclaration(node); return this.flowParseEnumDeclaration(node);
} else { }
const stmt = super.parseStatement(context, topLevel); const stmt = super.parseStatement(context, topLevel);
// We will parse a flow pragma in any comment before the first statement. // We will parse a flow pragma in any comment before the first statement.
if (this.flowPragma === undefined && !this.isValidDirective(stmt)) { if (this.flowPragma === undefined && !this.isValidDirective(stmt)) {
@ -1750,7 +1753,6 @@ export default (superClass: Class<Parser>): Class<Parser> =>
} }
return stmt; return stmt;
} }
}
// declares, interfaces and type aliases // declares, interfaces and type aliases
parseExpressionStatement( parseExpressionStatement(

View File

@ -0,0 +1 @@
interface = "foo";

View File

@ -0,0 +1,39 @@
{
"type": "File",
"start":0,"end":18,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":18}},
"errors": [
"SyntaxError: Unexpected reserved word 'interface' (1:0)"
],
"program": {
"type": "Program",
"start":0,"end":18,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":18}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":18,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":18}},
"expression": {
"type": "AssignmentExpression",
"start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}},
"operator": "=",
"left": {
"type": "Identifier",
"start":0,"end":9,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":9},"identifierName":"interface"},
"name": "interface"
},
"right": {
"type": "StringLiteral",
"start":12,"end":17,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":17}},
"extra": {
"rawValue": "foo",
"raw": "\"foo\""
},
"value": "foo"
}
}
}
],
"directives": []
}
}

View File

@ -0,0 +1 @@
interface + 3;

View File

@ -0,0 +1,39 @@
{
"type": "File",
"start":0,"end":14,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":14}},
"errors": [
"SyntaxError: Unexpected reserved word 'interface' (1:0)"
],
"program": {
"type": "Program",
"start":0,"end":14,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":14}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":14,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":14}},
"expression": {
"type": "BinaryExpression",
"start":0,"end":13,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":13}},
"left": {
"type": "Identifier",
"start":0,"end":9,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":9},"identifierName":"interface"},
"name": "interface"
},
"operator": "+",
"right": {
"type": "NumericLiteral",
"start":12,"end":13,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":13}},
"extra": {
"rawValue": 3,
"raw": "3"
},
"value": 3
}
}
}
],
"directives": []
}
}

View File

@ -0,0 +1 @@
interface();

View File

@ -0,0 +1,30 @@
{
"type": "File",
"start":0,"end":12,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":12}},
"errors": [
"SyntaxError: Unexpected reserved word 'interface' (1:0)"
],
"program": {
"type": "Program",
"start":0,"end":12,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":12}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":12,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":12}},
"expression": {
"type": "CallExpression",
"start":0,"end":11,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":11}},
"callee": {
"type": "Identifier",
"start":0,"end":9,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":9},"identifierName":"interface"},
"name": "interface"
},
"arguments": []
}
}
],
"directives": []
}
}

View File

@ -0,0 +1 @@
class interface {}

View File

@ -0,0 +1,31 @@
{
"type": "File",
"start":0,"end":18,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":18}},
"errors": [
"SyntaxError: Unexpected reserved word 'interface' (1:6)"
],
"program": {
"type": "Program",
"start":0,"end":18,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":18}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ClassDeclaration",
"start":0,"end":18,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":18}},
"id": {
"type": "Identifier",
"start":6,"end":15,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":15},"identifierName":"interface"},
"name": "interface"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start":16,"end":18,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":18}},
"body": []
}
}
],
"directives": []
}
}

View File

@ -0,0 +1 @@
interface ? true : false;

View File

@ -0,0 +1,39 @@
{
"type": "File",
"start":0,"end":25,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":25}},
"errors": [
"SyntaxError: Unexpected reserved word 'interface' (1:0)"
],
"program": {
"type": "Program",
"start":0,"end":25,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":25}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":25,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":25}},
"expression": {
"type": "ConditionalExpression",
"start":0,"end":24,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":24}},
"test": {
"type": "Identifier",
"start":0,"end":9,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":9},"identifierName":"interface"},
"name": "interface"
},
"consequent": {
"type": "BooleanLiteral",
"start":12,"end":16,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":16}},
"value": true
},
"alternate": {
"type": "BooleanLiteral",
"start":19,"end":24,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":24}},
"value": false
}
}
}
],
"directives": []
}
}

View File

@ -0,0 +1 @@
function interface() {}

View File

@ -0,0 +1,34 @@
{
"type": "File",
"start":0,"end":23,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":23}},
"errors": [
"SyntaxError: Unexpected reserved word 'interface' (1:9)"
],
"program": {
"type": "Program",
"start":0,"end":23,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":23}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "FunctionDeclaration",
"start":0,"end":23,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":23}},
"id": {
"type": "Identifier",
"start":9,"end":18,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":18},"identifierName":"interface"},
"name": "interface"
},
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start":21,"end":23,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":23}},
"body": [],
"directives": []
}
}
],
"directives": []
}
}

View File

@ -0,0 +1 @@
import interface from "foo";

View File

@ -0,0 +1,41 @@
{
"type": "File",
"start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":28}},
"errors": [
"SyntaxError: Unexpected reserved word 'interface' (1:7)"
],
"program": {
"type": "Program",
"start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":28}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ImportDeclaration",
"start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":28}},
"specifiers": [
{
"type": "ImportDefaultSpecifier",
"start":7,"end":16,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":16}},
"local": {
"type": "Identifier",
"start":7,"end":16,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":16},"identifierName":"interface"},
"name": "interface"
}
}
],
"importKind": "value",
"source": {
"type": "StringLiteral",
"start":22,"end":27,"loc":{"start":{"line":1,"column":22},"end":{"line":1,"column":27}},
"extra": {
"rawValue": "foo",
"raw": "\"foo\""
},
"value": "foo"
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,2 @@
foo.interface;
interface.foo;

View File

@ -0,0 +1,54 @@
{
"type": "File",
"start":0,"end":29,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":14}},
"errors": [
"SyntaxError: Unexpected reserved word 'interface' (2:0)"
],
"program": {
"type": "Program",
"start":0,"end":29,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":14}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":14,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":14}},
"expression": {
"type": "MemberExpression",
"start":0,"end":13,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":13}},
"object": {
"type": "Identifier",
"start":0,"end":3,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":3},"identifierName":"foo"},
"name": "foo"
},
"computed": false,
"property": {
"type": "Identifier",
"start":4,"end":13,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":13},"identifierName":"interface"},
"name": "interface"
}
}
},
{
"type": "ExpressionStatement",
"start":15,"end":29,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":14}},
"expression": {
"type": "MemberExpression",
"start":15,"end":28,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":13}},
"object": {
"type": "Identifier",
"start":15,"end":24,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":9},"identifierName":"interface"},
"name": "interface"
},
"computed": false,
"property": {
"type": "Identifier",
"start":25,"end":28,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":13},"identifierName":"foo"},
"name": "foo"
}
}
}
],
"directives": []
}
}

View File

@ -0,0 +1 @@
new interface();

View File

@ -0,0 +1,31 @@
{
"type": "File",
"start":0,"end":16,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":16}},
"errors": [
"SyntaxError: Unexpected reserved word 'interface' (1:4)"
],
"program": {
"type": "Program",
"start":0,"end":16,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":16}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":16,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":16}},
"expression": {
"type": "NewExpression",
"start":0,"end":15,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":15}},
"callee": {
"type": "Identifier",
"start":4,"end":13,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":13},"identifierName":"interface"},
"name": "interface"
},
"typeArguments": null,
"arguments": []
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
({
interface: "foo"
});

View File

@ -0,0 +1,48 @@
{
"type": "File",
"start":0,"end":25,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":3}},
"program": {
"type": "Program",
"start":0,"end":25,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":3}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":25,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":3}},
"expression": {
"type": "ObjectExpression",
"start":1,"end":23,"loc":{"start":{"line":1,"column":1},"end":{"line":3,"column":1}},
"extra": {
"parenthesized": true,
"parenStart": 0
},
"properties": [
{
"type": "ObjectProperty",
"start":5,"end":21,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":18}},
"method": false,
"key": {
"type": "Identifier",
"start":5,"end":14,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":11},"identifierName":"interface"},
"name": "interface"
},
"computed": false,
"shorthand": false,
"value": {
"type": "StringLiteral",
"start":16,"end":21,"loc":{"start":{"line":2,"column":13},"end":{"line":2,"column":18}},
"extra": {
"rawValue": "foo",
"raw": "\"foo\""
},
"value": "foo"
}
}
]
}
}
],
"directives": []
}
}

View File

@ -0,0 +1 @@
(interface, "foo");

View File

@ -0,0 +1,44 @@
{
"type": "File",
"start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}},
"errors": [
"SyntaxError: Unexpected reserved word 'interface' (1:1)"
],
"program": {
"type": "Program",
"start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}},
"expression": {
"type": "SequenceExpression",
"start":1,"end":17,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":17}},
"extra": {
"parenthesized": true,
"parenStart": 0
},
"expressions": [
{
"type": "Identifier",
"start":1,"end":10,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":10},"identifierName":"interface"},
"name": "interface"
},
{
"type": "StringLiteral",
"start":12,"end":17,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":17}},
"extra": {
"rawValue": "foo",
"raw": "\"foo\""
},
"value": "foo"
}
]
}
}
],
"directives": []
}
}

View File

@ -0,0 +1 @@
void interface;

View File

@ -0,0 +1,31 @@
{
"type": "File",
"start":0,"end":15,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":15}},
"errors": [
"SyntaxError: Unexpected reserved word 'interface' (1:5)"
],
"program": {
"type": "Program",
"start":0,"end":15,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":15}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":15,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":15}},
"expression": {
"type": "UnaryExpression",
"start":0,"end":14,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":14}},
"operator": "void",
"prefix": true,
"argument": {
"type": "Identifier",
"start":5,"end":14,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":14},"identifierName":"interface"},
"name": "interface"
}
}
}
],
"directives": []
}
}

View File

@ -0,0 +1 @@
const interface = "foo";

View File

@ -0,0 +1,41 @@
{
"type": "File",
"start":0,"end":24,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":24}},
"errors": [
"SyntaxError: Unexpected reserved word 'interface' (1:6)"
],
"program": {
"type": "Program",
"start":0,"end":24,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":24}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",
"start":0,"end":24,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":24}},
"declarations": [
{
"type": "VariableDeclarator",
"start":6,"end":23,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":23}},
"id": {
"type": "Identifier",
"start":6,"end":15,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":15},"identifierName":"interface"},
"name": "interface"
},
"init": {
"type": "StringLiteral",
"start":18,"end":23,"loc":{"start":{"line":1,"column":18},"end":{"line":1,"column":23}},
"extra": {
"rawValue": "foo",
"raw": "\"foo\""
},
"value": "foo"
}
}
],
"kind": "const"
}
],
"directives": []
}
}