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:
parent
015035cd27
commit
ddbda7dd04
@ -335,7 +335,7 @@ pp.flowParseObjectTypeCallProperty = function (node, isStatic) {
|
||||
return this.finishNode(node, "ObjectTypeCallProperty");
|
||||
};
|
||||
|
||||
pp.flowParseObjectType = function (allowStatic) {
|
||||
pp.flowParseObjectType = function (allowStatic, allowExact) {
|
||||
let nodeStart = this.startNode();
|
||||
let node;
|
||||
let propertyKey;
|
||||
@ -345,9 +345,21 @@ pp.flowParseObjectType = function (allowStatic) {
|
||||
nodeStart.properties = [];
|
||||
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 startPos = this.state.start, startLoc = this.state.startLoc;
|
||||
node = this.startNode();
|
||||
@ -383,13 +395,14 @@ pp.flowParseObjectType = function (allowStatic) {
|
||||
}
|
||||
}
|
||||
|
||||
this.expect(tt.braceR);
|
||||
this.expect(endDelim);
|
||||
|
||||
return this.finishNode(nodeStart, "ObjectTypeAnnotation");
|
||||
};
|
||||
|
||||
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();
|
||||
}
|
||||
};
|
||||
@ -510,7 +523,10 @@ pp.flowParsePrimaryType = function () {
|
||||
return this.flowIdentToTypeAnnotation(startPos, startLoc, node, this.parseIdentifier());
|
||||
|
||||
case tt.braceL:
|
||||
return this.flowParseObjectType();
|
||||
return this.flowParseObjectType(false, false);
|
||||
|
||||
case tt.braceBarL:
|
||||
return this.flowParseObjectType(false, true);
|
||||
|
||||
case tt.bracketL:
|
||||
return this.flowParseTupleType();
|
||||
|
||||
@ -322,6 +322,7 @@ export default class Tokenizer {
|
||||
let next = this.input.charCodeAt(this.state.pos + 1);
|
||||
if (next === code) return this.finishOp(code === 124 ? tt.logicalOR : tt.logicalAND, 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);
|
||||
}
|
||||
|
||||
@ -404,8 +405,17 @@ export default class Tokenizer {
|
||||
case 44: ++this.state.pos; return this.finishToken(tt.comma);
|
||||
case 91: ++this.state.pos; return this.finishToken(tt.bracketL);
|
||||
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:
|
||||
if (this.hasPlugin("functionBind") && this.input.charCodeAt(this.state.pos + 1) === 58) {
|
||||
|
||||
@ -48,7 +48,9 @@ export const types = {
|
||||
bracketL: new TokenType("[", {beforeExpr: true, startsExpr: true}),
|
||||
bracketR: new TokenType("]"),
|
||||
braceL: new TokenType("{", {beforeExpr: true, startsExpr: true}),
|
||||
braceBarL: new TokenType("{|", {beforeExpr: true, startsExpr: true}),
|
||||
braceR: new TokenType("}"),
|
||||
braceBarR: new TokenType("|}"),
|
||||
parenL: new TokenType("(", {beforeExpr: true, startsExpr: true}),
|
||||
parenR: new TokenType(")"),
|
||||
comma: new TokenType(",", beforeExpr),
|
||||
|
||||
5
test/fixtures/flow/type-annotations/108/actual.js
vendored
Normal file
5
test/fixtures/flow/type-annotations/108/actual.js
vendored
Normal 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 };
|
||||
1774
test/fixtures/flow/type-annotations/108/expected.json
vendored
Normal file
1774
test/fixtures/flow/type-annotations/108/expected.json
vendored
Normal file
File diff suppressed because it is too large
Load Diff
1
test/fixtures/flow/type-annotations/109/actual.js
vendored
Normal file
1
test/fixtures/flow/type-annotations/109/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
var a : { x: number{ y: string } } = { x: 0, y: 'foo' };
|
||||
3
test/fixtures/flow/type-annotations/109/options.json
vendored
Normal file
3
test/fixtures/flow/type-annotations/109/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:19)"
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user