Allow flow internal slot properties to be optional (#7959)

See 23d1b1c5f2
This commit is contained in:
Sam Goldman 2018-05-17 18:01:03 +01:00 committed by Mateusz Burzyński
parent ac13c302f7
commit af7ab71486
9 changed files with 171 additions and 2 deletions

View File

@ -424,6 +424,7 @@ export function ObjectTypeInternalSlot(node: Object) {
this.print(node.id, node);
this.token("]");
this.token("]");
if (node.optional) this.token("?");
if (!node.method) {
this.token(":");
this.space();

View File

@ -4,3 +4,4 @@ interface T { [[foo]]: X }
interface T { [[foo]](): X }
type T = { [[foo]]: X }
type T = { [[foo]](): X }
type T = { [[foo]]?: X }

View File

@ -15,4 +15,7 @@ type T = {
};
type T = {
[[foo]]() => X
};
};
type T = {
[[foo]]?: X
};

View File

@ -272,11 +272,12 @@ defineType("ObjectTypeAnnotation", {
});
defineType("ObjectTypeInternalSlot", {
visitor: ["id", "value", "static", "method"],
visitor: ["id", "value", "optional", "static", "method"],
aliases: ["Flow", "UserWhitespacable"],
fields: {
id: validateType("Identifier"),
value: validateType("FlowType"),
optional: validate(assertValueType("boolean")),
static: validate(assertValueType("boolean")),
method: validate(assertValueType("boolean")),
},

View File

@ -647,11 +647,15 @@ export default (superClass: Class<Parser>): Class<Parser> =>
this.expect(tt.bracketR);
if (this.isRelational("<") || this.match(tt.parenL)) {
node.method = true;
node.optional = false;
node.value = this.flowParseObjectTypeMethodish(
this.startNodeAt(node.start, node.loc.start),
);
} else {
node.method = false;
if (this.eat(tt.question)) {
node.optional = true;
}
node.value = this.flowParseTypeInitialiser();
}
return this.finishNode(node, "ObjectTypeInternalSlot");

View File

@ -114,6 +114,7 @@
"name": "foo"
},
"method": true,
"optional": false,
"value": {
"type": "FunctionTypeAnnotation",
"start": 14,

View File

@ -111,6 +111,7 @@
"name": "foo"
},
"method": true,
"optional": false,
"value": {
"type": "FunctionTypeAnnotation",
"start": 11,

View File

@ -0,0 +1 @@
type T = { [[foo]]?: X }

View File

@ -0,0 +1,156 @@
{
"type": "File",
"start": 0,
"end": 24,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 24
}
},
"program": {
"type": "Program",
"start": 0,
"end": 24,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 24
}
},
"sourceType": "module",
"body": [
{
"type": "TypeAlias",
"start": 0,
"end": 24,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 24
}
},
"id": {
"type": "Identifier",
"start": 5,
"end": 6,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 6
},
"identifierName": "T"
},
"name": "T"
},
"typeParameters": null,
"right": {
"type": "ObjectTypeAnnotation",
"start": 9,
"end": 24,
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 24
}
},
"callProperties": [],
"properties": [],
"indexers": [],
"internalSlots": [
{
"type": "ObjectTypeInternalSlot",
"start": 11,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 11
},
"end": {
"line": 1,
"column": 22
}
},
"static": false,
"id": {
"type": "Identifier",
"start": 13,
"end": 16,
"loc": {
"start": {
"line": 1,
"column": 13
},
"end": {
"line": 1,
"column": 16
},
"identifierName": "foo"
},
"name": "foo"
},
"method": false,
"optional": true,
"value": {
"type": "GenericTypeAnnotation",
"start": 21,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 21
},
"end": {
"line": 1,
"column": 22
}
},
"typeParameters": null,
"id": {
"type": "Identifier",
"start": 21,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 21
},
"end": {
"line": 1,
"column": 22
},
"identifierName": "X"
},
"name": "X"
}
}
}
],
"exact": false
}
}
],
"directives": []
}
}