Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flow Indexed Access Type support #13072

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/babel-generator/src/generators/flow.ts
Expand Up @@ -462,6 +462,13 @@ export function ThisTypeAnnotation(this: Printer) {
this.word("this");
}

export function IndexedAccessType(this: Printer, node: t.IndexedAccessType) {
this.print(node.objectType, node);
this.token("[");
this.print(node.indexType, node);
this.token("]");
}

export function TupleTypeAnnotation(
this: Printer,
node: t.TupleTypeAnnotation,
Expand Down
@@ -0,0 +1 @@
type T = Obj[K];
@@ -0,0 +1 @@
type T = Obj[K];
13 changes: 10 additions & 3 deletions packages/babel-parser/src/plugins/flow/index.js
Expand Up @@ -1624,10 +1624,17 @@ export default (superClass: Class<Parser>): Class<Parser> =>
let type = this.flowParsePrimaryType();
while (this.match(tt.bracketL) && !this.canInsertSemicolon()) {
const node = this.startNodeAt(startPos, startLoc);
node.elementType = type;
this.expect(tt.bracketL);
this.expect(tt.bracketR);
type = this.finishNode(node, "ArrayTypeAnnotation");
if (this.match(tt.bracketR)) {
node.elementType = type;
this.expect(tt.bracketR);
type = this.finishNode(node, "ArrayTypeAnnotation");
} else {
node.objectType = type;
node.indexType = this.flowParseType();
this.expect(tt.bracketR);
type = this.finishNode(node, "IndexedAccessType");
}
}
return type;
}
Expand Down
@@ -0,0 +1 @@
type A = Obj['a'];
@@ -0,0 +1,46 @@
{
"type": "File",
"start":0,"end":18,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":18}},
"program": {
"type": "Program",
"start":0,"end":18,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":18}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "TypeAlias",
"start":0,"end":18,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":18}},
"id": {
"type": "Identifier",
"start":5,"end":6,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":6},"identifierName":"A"},
"name": "A"
},
"typeParameters": null,
"right": {
"type": "IndexedAccessType",
"start":9,"end":17,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":17}},
"objectType": {
"type": "GenericTypeAnnotation",
"start":9,"end":12,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":12}},
"typeParameters": null,
"id": {
"type": "Identifier",
"start":9,"end":12,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":12},"identifierName":"Obj"},
"name": "Obj"
}
},
"indexType": {
"type": "StringLiteralTypeAnnotation",
"start":13,"end":16,"loc":{"start":{"line":1,"column":13},"end":{"line":1,"column":16}},
"extra": {
"rawValue": "a",
"raw": "'a'"
},
"value": "a"
}
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
type E = Obj['bar'][];
@@ -0,0 +1,50 @@
{
"type": "File",
"start":0,"end":22,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":22}},
"program": {
"type": "Program",
"start":0,"end":22,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":22}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "TypeAlias",
"start":0,"end":22,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":22}},
"id": {
"type": "Identifier",
"start":5,"end":6,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":6},"identifierName":"E"},
"name": "E"
},
"typeParameters": null,
"right": {
"type": "ArrayTypeAnnotation",
"start":9,"end":21,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":21}},
"elementType": {
"type": "IndexedAccessType",
"start":9,"end":19,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":19}},
"objectType": {
"type": "GenericTypeAnnotation",
"start":9,"end":12,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":12}},
"typeParameters": null,
"id": {
"type": "Identifier",
"start":9,"end":12,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":12},"identifierName":"Obj"},
"name": "Obj"
}
},
"indexType": {
"type": "StringLiteralTypeAnnotation",
"start":13,"end":18,"loc":{"start":{"line":1,"column":13},"end":{"line":1,"column":18}},
"extra": {
"rawValue": "bar",
"raw": "'bar'"
},
"value": "bar"
}
}
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
type C = Obj['bar'][foo]['boz'];
@@ -0,0 +1,73 @@
{
"type": "File",
"start":0,"end":32,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":32}},
"program": {
"type": "Program",
"start":0,"end":32,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":32}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "TypeAlias",
"start":0,"end":32,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":32}},
"id": {
"type": "Identifier",
"start":5,"end":6,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":6},"identifierName":"C"},
"name": "C"
},
"typeParameters": null,
"right": {
"type": "IndexedAccessType",
"start":9,"end":31,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":31}},
"objectType": {
"type": "IndexedAccessType",
"start":9,"end":24,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":24}},
"objectType": {
"type": "IndexedAccessType",
"start":9,"end":19,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":19}},
"objectType": {
"type": "GenericTypeAnnotation",
"start":9,"end":12,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":12}},
"typeParameters": null,
"id": {
"type": "Identifier",
"start":9,"end":12,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":12},"identifierName":"Obj"},
"name": "Obj"
}
},
"indexType": {
"type": "StringLiteralTypeAnnotation",
"start":13,"end":18,"loc":{"start":{"line":1,"column":13},"end":{"line":1,"column":18}},
"extra": {
"rawValue": "bar",
"raw": "'bar'"
},
"value": "bar"
}
},
"indexType": {
"type": "GenericTypeAnnotation",
"start":20,"end":23,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":23}},
"typeParameters": null,
"id": {
"type": "Identifier",
"start":20,"end":23,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":23},"identifierName":"foo"},
"name": "foo"
}
}
},
"indexType": {
"type": "StringLiteralTypeAnnotation",
"start":25,"end":30,"loc":{"start":{"line":1,"column":25},"end":{"line":1,"column":30}},
"extra": {
"rawValue": "boz",
"raw": "'boz'"
},
"value": "boz"
}
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
type D = (Obj['bar'])['baz'];
@@ -0,0 +1,59 @@
{
"type": "File",
"start":0,"end":29,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":29}},
"program": {
"type": "Program",
"start":0,"end":29,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":29}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "TypeAlias",
"start":0,"end":29,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":29}},
"id": {
"type": "Identifier",
"start":5,"end":6,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":6},"identifierName":"D"},
"name": "D"
},
"typeParameters": null,
"right": {
"type": "IndexedAccessType",
"start":9,"end":28,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":28}},
"objectType": {
"type": "IndexedAccessType",
"start":10,"end":20,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":20}},
"objectType": {
"type": "GenericTypeAnnotation",
"start":10,"end":13,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":13}},
"typeParameters": null,
"id": {
"type": "Identifier",
"start":10,"end":13,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":13},"identifierName":"Obj"},
"name": "Obj"
}
},
"indexType": {
"type": "StringLiteralTypeAnnotation",
"start":14,"end":19,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":19}},
"extra": {
"rawValue": "bar",
"raw": "'bar'"
},
"value": "bar"
}
},
"indexType": {
"type": "StringLiteralTypeAnnotation",
"start":22,"end":27,"loc":{"start":{"line":1,"column":22},"end":{"line":1,"column":27}},
"extra": {
"rawValue": "baz",
"raw": "'baz'"
},
"value": "baz"
}
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
type B = Array<string>[number];
@@ -0,0 +1,50 @@
{
"type": "File",
"start":0,"end":31,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":31}},
"program": {
"type": "Program",
"start":0,"end":31,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":31}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "TypeAlias",
"start":0,"end":31,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":31}},
"id": {
"type": "Identifier",
"start":5,"end":6,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":6},"identifierName":"B"},
"name": "B"
},
"typeParameters": null,
"right": {
"type": "IndexedAccessType",
"start":9,"end":30,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":30}},
"objectType": {
"type": "GenericTypeAnnotation",
"start":9,"end":22,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":22}},
"typeParameters": {
"type": "TypeParameterInstantiation",
"start":14,"end":22,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":22}},
"params": [
{
"type": "StringTypeAnnotation",
"start":15,"end":21,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":21}}
}
]
},
"id": {
"type": "Identifier",
"start":9,"end":14,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":14},"identifierName":"Array"},
"name": "Array"
}
},
"indexType": {
"type": "NumberTypeAnnotation",
"start":23,"end":29,"loc":{"start":{"line":1,"column":23},"end":{"line":1,"column":29}}
}
}
}
],
"directives": []
}
}
3 changes: 3 additions & 0 deletions packages/babel-traverse/src/path/generated/asserts.ts
Expand Up @@ -257,6 +257,9 @@ export interface NodePathAssetions {
assertImportSpecifier(
opts?: object,
): asserts this is NodePath<t.ImportSpecifier>;
assertIndexedAccessType(
opts?: object,
): asserts this is NodePath<t.IndexedAccessType>;
assertInferredPredicate(
opts?: object,
): asserts this is NodePath<t.InferredPredicate>;
Expand Down
1 change: 1 addition & 0 deletions packages/babel-traverse/src/path/generated/validators.ts
Expand Up @@ -149,6 +149,7 @@ export interface NodePathValidators {
opts?: object,
): this is NodePath<t.ImportNamespaceSpecifier>;
isImportSpecifier(opts?: object): this is NodePath<t.ImportSpecifier>;
isIndexedAccessType(opts?: object): this is NodePath<t.IndexedAccessType>;
isInferredPredicate(opts?: object): this is NodePath<t.InferredPredicate>;
isInterfaceDeclaration(
opts?: object,
Expand Down
6 changes: 6 additions & 0 deletions packages/babel-types/src/asserts/generated/index.ts
Expand Up @@ -758,6 +758,12 @@ export function assertThisTypeAnnotation(
): asserts node is t.ThisTypeAnnotation {
assert("ThisTypeAnnotation", node, opts);
}
export function assertIndexedAccessType(
node: object | null | undefined,
opts?: object | null,
): asserts node is t.IndexedAccessType {
assert("IndexedAccessType", node, opts);
}
export function assertTupleTypeAnnotation(
node: object | null | undefined,
opts?: object | null,
Expand Down