Skip to content

Commit

Permalink
Allows the interface to be used as an Identifier for flow plugin (#12254
Browse files Browse the repository at this point in the history
)
  • Loading branch information
sosukesuzuki committed Oct 26, 2020
1 parent a967910 commit f5bd9f2
Show file tree
Hide file tree
Showing 27 changed files with 531 additions and 11 deletions.
24 changes: 13 additions & 11 deletions packages/babel-parser/src/plugins/flow.js
Expand Up @@ -13,7 +13,7 @@ import type { Pos, Position } from "../util/location";
import type State from "../tokenizer/state";
import { types as tc } from "../tokenizer/context";
import * as charCodes from "charcodes";
import { isIteratorStart } from "../util/identifier";
import { isIteratorStart, isKeyword } from "../util/identifier";
import {
type BindingTypes,
BIND_NONE,
Expand Down Expand Up @@ -1735,21 +1735,23 @@ export default (superClass: Class<Parser>): Class<Parser> =>
this.match(tt.name) &&
this.state.value === "interface"
) {
const node = this.startNode();
this.next();
return this.flowParseInterface(node);
const lookahead = this.lookahead();
if (lookahead.type === tt.name || isKeyword(lookahead.value)) {
const node = this.startNode();
this.next();
return this.flowParseInterface(node);
}
} else if (this.shouldParseEnums() && this.isContextual("enum")) {
const node = this.startNode();
this.next();
return this.flowParseEnumDeclaration(node);
} else {
const stmt = super.parseStatement(context, topLevel);
// We will parse a flow pragma in any comment before the first statement.
if (this.flowPragma === undefined && !this.isValidDirective(stmt)) {
this.flowPragma = null;
}
return stmt;
}
const stmt = super.parseStatement(context, topLevel);
// We will parse a flow pragma in any comment before the first statement.
if (this.flowPragma === undefined && !this.isValidDirective(stmt)) {
this.flowPragma = null;
}
return stmt;
}

// declares, interfaces and type aliases
Expand Down
@@ -0,0 +1 @@
interface = "foo";
@@ -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": []
}
}
@@ -0,0 +1 @@
interface + 3;
@@ -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": []
}
}
@@ -0,0 +1 @@
interface();
@@ -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": []
}
}
@@ -0,0 +1 @@
class interface {}
@@ -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": []
}
}
@@ -0,0 +1 @@
interface ? true : false;
@@ -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": []
}
}
@@ -0,0 +1 @@
function interface() {}
@@ -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": []
}
}
@@ -0,0 +1 @@
import interface from "foo";
@@ -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": []
}
}
@@ -0,0 +1,2 @@
foo.interface;
interface.foo;
@@ -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": []
}
}
@@ -0,0 +1 @@
new interface();

0 comments on commit f5bd9f2

Please sign in to comment.