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

Support parsing Flow's Indexed Access Types #13053

Merged
Merged
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 @@ -738,3 +738,10 @@ export function Variance(this: Printer, node: t.Variance) {
export function VoidTypeAnnotation(this: Printer) {
this.word("void");
}

export function IndexedAccessType(this: Printer, node: t.IndexedAccessType) {
this.print(node.objectType, node);
this.token("[");
this.print(node.indexType, node);
this.token("]");
}
@@ -0,0 +1,9 @@
type A = Obj['a'];

type B = Array<string>[number];

type C = Obj['bar'][foo]['boz'];

type D = (Obj['bar'])['baz'];

type E = Obj['bar'][];
@@ -0,0 +1,5 @@
type A = Obj['a'];
type B = Array<string>[number];
type C = Obj['bar'][foo]['boz'];
type D = Obj['bar']['baz'];
type E = Obj['bar'][];
16 changes: 13 additions & 3 deletions packages/babel-parser/src/plugins/flow/index.js
Expand Up @@ -1624,10 +1624,20 @@ 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.next(); // eat `]`
type = this.finishNode(node, "ArrayTypeAnnotation");
} else {
node.objectType = type;
node.indexType = this.flowParseType();
this.expect(tt.bracketR);
type = this.finishNode<N.FlowIndexedAccessType>(
node,
"IndexedAccessType",
);
}
}
return type;
}
Expand Down
6 changes: 6 additions & 0 deletions packages/babel-parser/src/types.js
Expand Up @@ -1050,6 +1050,12 @@ export type FlowInterfaceType = NodeBase & {
body: FlowObjectTypeAnnotation,
};

export type FlowIndexedAccessType = Node & {
type: "IndexedAccessType",
objectType: FlowType,
indexType: FlowType,
};

// ESTree

export type EstreeProperty = NodeBase & {
Expand Down
@@ -0,0 +1,9 @@
type A = Obj['a'];

type B = Array<string>[number];

type C = Obj['bar'][foo]['boz'];

type D = (Obj['bar'])['baz'];

type E = Obj['bar'][];
@@ -0,0 +1,226 @@
{
"type": "File",
"start":0,"end":140,"loc":{"start":{"line":1,"column":0},"end":{"line":9,"column":22}},
"program": {
"type": "Program",
"start":0,"end":140,"loc":{"start":{"line":1,"column":0},"end":{"line":9,"column":22}},
"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"
}
}
},
{
"type": "TypeAlias",
"start":20,"end":51,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":31}},
"id": {
"type": "Identifier",
"start":25,"end":26,"loc":{"start":{"line":3,"column":5},"end":{"line":3,"column":6},"identifierName":"B"},
"name": "B"
},
"typeParameters": null,
"right": {
"type": "IndexedAccessType",
"start":29,"end":50,"loc":{"start":{"line":3,"column":9},"end":{"line":3,"column":30}},
"objectType": {
"type": "GenericTypeAnnotation",
"start":29,"end":42,"loc":{"start":{"line":3,"column":9},"end":{"line":3,"column":22}},
"typeParameters": {
"type": "TypeParameterInstantiation",
"start":34,"end":42,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":22}},
"params": [
{
"type": "StringTypeAnnotation",
"start":35,"end":41,"loc":{"start":{"line":3,"column":15},"end":{"line":3,"column":21}}
}
]
},
"id": {
"type": "Identifier",
"start":29,"end":34,"loc":{"start":{"line":3,"column":9},"end":{"line":3,"column":14},"identifierName":"Array"},
"name": "Array"
}
},
"indexType": {
"type": "NumberTypeAnnotation",
"start":43,"end":49,"loc":{"start":{"line":3,"column":23},"end":{"line":3,"column":29}}
}
}
},
{
"type": "TypeAlias",
"start":53,"end":85,"loc":{"start":{"line":5,"column":0},"end":{"line":5,"column":32}},
"id": {
"type": "Identifier",
"start":58,"end":59,"loc":{"start":{"line":5,"column":5},"end":{"line":5,"column":6},"identifierName":"C"},
"name": "C"
},
"typeParameters": null,
"right": {
"type": "IndexedAccessType",
"start":62,"end":84,"loc":{"start":{"line":5,"column":9},"end":{"line":5,"column":31}},
"objectType": {
"type": "IndexedAccessType",
"start":62,"end":77,"loc":{"start":{"line":5,"column":9},"end":{"line":5,"column":24}},
"objectType": {
"type": "IndexedAccessType",
"start":62,"end":72,"loc":{"start":{"line":5,"column":9},"end":{"line":5,"column":19}},
"objectType": {
"type": "GenericTypeAnnotation",
"start":62,"end":65,"loc":{"start":{"line":5,"column":9},"end":{"line":5,"column":12}},
"typeParameters": null,
"id": {
"type": "Identifier",
"start":62,"end":65,"loc":{"start":{"line":5,"column":9},"end":{"line":5,"column":12},"identifierName":"Obj"},
"name": "Obj"
}
},
"indexType": {
"type": "StringLiteralTypeAnnotation",
"start":66,"end":71,"loc":{"start":{"line":5,"column":13},"end":{"line":5,"column":18}},
"extra": {
"rawValue": "bar",
"raw": "'bar'"
},
"value": "bar"
}
},
"indexType": {
"type": "GenericTypeAnnotation",
"start":73,"end":76,"loc":{"start":{"line":5,"column":20},"end":{"line":5,"column":23}},
"typeParameters": null,
"id": {
"type": "Identifier",
"start":73,"end":76,"loc":{"start":{"line":5,"column":20},"end":{"line":5,"column":23},"identifierName":"foo"},
"name": "foo"
}
}
},
"indexType": {
"type": "StringLiteralTypeAnnotation",
"start":78,"end":83,"loc":{"start":{"line":5,"column":25},"end":{"line":5,"column":30}},
"extra": {
"rawValue": "boz",
"raw": "'boz'"
},
"value": "boz"
}
}
},
{
"type": "TypeAlias",
"start":87,"end":116,"loc":{"start":{"line":7,"column":0},"end":{"line":7,"column":29}},
"id": {
"type": "Identifier",
"start":92,"end":93,"loc":{"start":{"line":7,"column":5},"end":{"line":7,"column":6},"identifierName":"D"},
"name": "D"
},
"typeParameters": null,
"right": {
"type": "IndexedAccessType",
"start":96,"end":115,"loc":{"start":{"line":7,"column":9},"end":{"line":7,"column":28}},
"objectType": {
"type": "IndexedAccessType",
"start":97,"end":107,"loc":{"start":{"line":7,"column":10},"end":{"line":7,"column":20}},
"objectType": {
"type": "GenericTypeAnnotation",
"start":97,"end":100,"loc":{"start":{"line":7,"column":10},"end":{"line":7,"column":13}},
"typeParameters": null,
"id": {
"type": "Identifier",
"start":97,"end":100,"loc":{"start":{"line":7,"column":10},"end":{"line":7,"column":13},"identifierName":"Obj"},
"name": "Obj"
}
},
"indexType": {
"type": "StringLiteralTypeAnnotation",
"start":101,"end":106,"loc":{"start":{"line":7,"column":14},"end":{"line":7,"column":19}},
"extra": {
"rawValue": "bar",
"raw": "'bar'"
},
"value": "bar"
}
},
"indexType": {
"type": "StringLiteralTypeAnnotation",
"start":109,"end":114,"loc":{"start":{"line":7,"column":22},"end":{"line":7,"column":27}},
"extra": {
"rawValue": "baz",
"raw": "'baz'"
},
"value": "baz"
}
}
},
{
"type": "TypeAlias",
"start":118,"end":140,"loc":{"start":{"line":9,"column":0},"end":{"line":9,"column":22}},
"id": {
"type": "Identifier",
"start":123,"end":124,"loc":{"start":{"line":9,"column":5},"end":{"line":9,"column":6},"identifierName":"E"},
"name": "E"
},
"typeParameters": null,
"right": {
"type": "ArrayTypeAnnotation",
"start":127,"end":139,"loc":{"start":{"line":9,"column":9},"end":{"line":9,"column":21}},
"elementType": {
"type": "IndexedAccessType",
"start":127,"end":137,"loc":{"start":{"line":9,"column":9},"end":{"line":9,"column":19}},
"objectType": {
"type": "GenericTypeAnnotation",
"start":127,"end":130,"loc":{"start":{"line":9,"column":9},"end":{"line":9,"column":12}},
"typeParameters": null,
"id": {
"type": "Identifier",
"start":127,"end":130,"loc":{"start":{"line":9,"column":9},"end":{"line":9,"column":12},"identifierName":"Obj"},
"name": "Obj"
}
},
"indexType": {
"type": "StringLiteralTypeAnnotation",
"start":131,"end":136,"loc":{"start":{"line":9,"column":13},"end":{"line":9,"column":18}},
"extra": {
"rawValue": "bar",
"raw": "'bar'"
},
"value": "bar"
}
}
}
}
],
"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 @@ -878,6 +878,12 @@ export function assertEnumDefaultedMember(
): asserts node is t.EnumDefaultedMember {
assert("EnumDefaultedMember", node, opts);
}
export function assertIndexedAccessType(
node: object | null | undefined,
opts?: object | null,
): asserts node is t.IndexedAccessType {
assert("IndexedAccessType", node, opts);
}
export function assertJSXAttribute(
node: object | null | undefined,
opts?: object | null,
Expand Down
7 changes: 7 additions & 0 deletions packages/babel-types/src/ast-types/generated/index.ts
Expand Up @@ -151,6 +151,7 @@ export type Node =
| ImportDefaultSpecifier
| ImportNamespaceSpecifier
| ImportSpecifier
| IndexedAccessType
| InferredPredicate
| InterfaceDeclaration
| InterfaceExtends
Expand Down Expand Up @@ -1387,6 +1388,12 @@ export interface EnumDefaultedMember extends BaseNode {
id: Identifier;
}

export interface IndexedAccessType extends BaseNode {
type: "IndexedAccessType";
objectType: FlowType;
indexType: FlowType;
}

export interface JSXAttribute extends BaseNode {
type: "JSXAttribute";
name: JSXIdentifier | JSXNamespacedName;
Expand Down
6 changes: 6 additions & 0 deletions packages/babel-types/src/builders/generated/index.ts
Expand Up @@ -854,6 +854,12 @@ export function enumStringMember(
export function enumDefaultedMember(id: t.Identifier): t.EnumDefaultedMember {
return builder("EnumDefaultedMember", ...arguments);
}
export function indexedAccessType(
objectType: t.FlowType,
indexType: t.FlowType,
): t.IndexedAccessType {
return builder("IndexedAccessType", ...arguments);
}
export function jsxAttribute(
name: t.JSXIdentifier | t.JSXNamespacedName,
value?:
Expand Down
1 change: 1 addition & 0 deletions packages/babel-types/src/builders/generated/uppercase.js
Expand Up @@ -153,6 +153,7 @@ export {
enumNumberMember as EnumNumberMember,
enumStringMember as EnumStringMember,
enumDefaultedMember as EnumDefaultedMember,
indexedAccessType as IndexedAccessType,
jsxAttribute as JSXAttribute,
jsxClosingElement as JSXClosingElement,
jsxElement as JSXElement,
Expand Down
8 changes: 8 additions & 0 deletions packages/babel-types/src/definitions/flow.ts
Expand Up @@ -559,3 +559,11 @@ defineType("EnumDefaultedMember", {
id: validateType("Identifier"),
},
});

defineType("IndexedAccessType", {
visitor: ["objectType", "indexType"],
fields: {
objectType: validateType("FlowType"),
indexType: validateType("FlowType"),
},
});