exact object type annotations for Flow plugin (#104)

* exact object type annotations for Flow plugin

* Couple tweaks per suggestions

* s/==/===/

* add test for unexpected token in flowObjectType Semicolon
This commit is contained in:
Basil Hosmer 2016-09-13 07:07:23 -07:00 committed by Henry Zhu
parent 015035cd27
commit ddbda7dd04
8 changed files with 1820 additions and 9 deletions

View File

@ -335,7 +335,7 @@ pp.flowParseObjectTypeCallProperty = function (node, isStatic) {
return this.finishNode(node, "ObjectTypeCallProperty"); return this.finishNode(node, "ObjectTypeCallProperty");
}; };
pp.flowParseObjectType = function (allowStatic) { pp.flowParseObjectType = function (allowStatic, allowExact) {
let nodeStart = this.startNode(); let nodeStart = this.startNode();
let node; let node;
let propertyKey; let propertyKey;
@ -345,9 +345,21 @@ pp.flowParseObjectType = function (allowStatic) {
nodeStart.properties = []; nodeStart.properties = [];
nodeStart.indexers = []; nodeStart.indexers = [];
this.expect(tt.braceL); let endDelim;
let exact;
if (allowExact && this.match(tt.braceBarL)) {
this.expect(tt.braceBarL);
endDelim = tt.braceBarR;
exact = true;
} else {
this.expect(tt.braceL);
endDelim = tt.braceR;
exact = false;
}
while (!this.match(tt.braceR)) { nodeStart.exact = exact;
while (!this.match(endDelim)) {
let optional = false; let optional = false;
let startPos = this.state.start, startLoc = this.state.startLoc; let startPos = this.state.start, startLoc = this.state.startLoc;
node = this.startNode(); node = this.startNode();
@ -383,13 +395,14 @@ pp.flowParseObjectType = function (allowStatic) {
} }
} }
this.expect(tt.braceR); this.expect(endDelim);
return this.finishNode(nodeStart, "ObjectTypeAnnotation"); return this.finishNode(nodeStart, "ObjectTypeAnnotation");
}; };
pp.flowObjectTypeSemicolon = function () { pp.flowObjectTypeSemicolon = function () {
if (!this.eat(tt.semi) && !this.eat(tt.comma) && !this.match(tt.braceR)) { if (!this.eat(tt.semi) && !this.eat(tt.comma) &&
!this.match(tt.braceR) && !this.match(tt.braceBarR)) {
this.unexpected(); this.unexpected();
} }
}; };
@ -510,7 +523,10 @@ pp.flowParsePrimaryType = function () {
return this.flowIdentToTypeAnnotation(startPos, startLoc, node, this.parseIdentifier()); return this.flowIdentToTypeAnnotation(startPos, startLoc, node, this.parseIdentifier());
case tt.braceL: case tt.braceL:
return this.flowParseObjectType(); return this.flowParseObjectType(false, false);
case tt.braceBarL:
return this.flowParseObjectType(false, true);
case tt.bracketL: case tt.bracketL:
return this.flowParseTupleType(); return this.flowParseTupleType();

View File

@ -322,6 +322,7 @@ export default class Tokenizer {
let next = this.input.charCodeAt(this.state.pos + 1); let next = this.input.charCodeAt(this.state.pos + 1);
if (next === code) return this.finishOp(code === 124 ? tt.logicalOR : tt.logicalAND, 2); if (next === code) return this.finishOp(code === 124 ? tt.logicalOR : tt.logicalAND, 2);
if (next === 61) return this.finishOp(tt.assign, 2); if (next === 61) return this.finishOp(tt.assign, 2);
if (code === 124 && next === 125 && this.hasPlugin("flow")) return this.finishOp(tt.braceBarR, 2);
return this.finishOp(code === 124 ? tt.bitwiseOR : tt.bitwiseAND, 1); return this.finishOp(code === 124 ? tt.bitwiseOR : tt.bitwiseAND, 1);
} }
@ -404,8 +405,17 @@ export default class Tokenizer {
case 44: ++this.state.pos; return this.finishToken(tt.comma); case 44: ++this.state.pos; return this.finishToken(tt.comma);
case 91: ++this.state.pos; return this.finishToken(tt.bracketL); case 91: ++this.state.pos; return this.finishToken(tt.bracketL);
case 93: ++this.state.pos; return this.finishToken(tt.bracketR); case 93: ++this.state.pos; return this.finishToken(tt.bracketR);
case 123: ++this.state.pos; return this.finishToken(tt.braceL);
case 125: ++this.state.pos; return this.finishToken(tt.braceR); case 123:
if (this.hasPlugin("flow") && this.input.charCodeAt(this.state.pos + 1) === 124) {
return this.finishOp(tt.braceBarL, 2);
} else {
++this.state.pos;
return this.finishToken(tt.braceL);
}
case 125:
++this.state.pos; return this.finishToken(tt.braceR);
case 58: case 58:
if (this.hasPlugin("functionBind") && this.input.charCodeAt(this.state.pos + 1) === 58) { if (this.hasPlugin("functionBind") && this.input.charCodeAt(this.state.pos + 1) === 58) {

View File

@ -48,7 +48,9 @@ export const types = {
bracketL: new TokenType("[", {beforeExpr: true, startsExpr: true}), bracketL: new TokenType("[", {beforeExpr: true, startsExpr: true}),
bracketR: new TokenType("]"), bracketR: new TokenType("]"),
braceL: new TokenType("{", {beforeExpr: true, startsExpr: true}), braceL: new TokenType("{", {beforeExpr: true, startsExpr: true}),
braceBarL: new TokenType("{|", {beforeExpr: true, startsExpr: true}),
braceR: new TokenType("}"), braceR: new TokenType("}"),
braceBarR: new TokenType("|}"),
parenL: new TokenType("(", {beforeExpr: true, startsExpr: true}), parenL: new TokenType("(", {beforeExpr: true, startsExpr: true}),
parenR: new TokenType(")"), parenR: new TokenType(")"),
comma: new TokenType(",", beforeExpr), comma: new TokenType(",", beforeExpr),

View File

@ -0,0 +1,5 @@
var a : {| x: number, y: string |} = { x: 0, y: 'foo' };
var b : {| x: number, y: string, |} = { x: 0, y: 'foo' };
var c : {| |} = {};
var d : { a: {| x: number, y: string |}, b: boolean } = { a: { x: 0, y: 'foo' }, b: false };
var e : {| a: { x: number, y: string }, b: boolean |} = { a: { x: 0, y: 'foo' }, b: false };

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1 @@
var a : { x: number{ y: string } } = { x: 0, y: 'foo' };

View File

@ -0,0 +1,3 @@
{
"throws": "Unexpected token (1:19)"
}