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

Add Object Type Spread support #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions declarations/babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,11 @@ declare module '@babel/types' {
callProperties: ObjectTypeCallProperty[];
}

declare class ObjectTypeSpreadProperty extends Node {
type: 'ObjectTypeSpreadProperty';
argument: GenericTypeAnnotation;
}

declare class ObjectTypeCallProperty extends Node {
type: 'ObjectTypeCallProperty';
value: FlowTypeAnnotation;
Expand Down Expand Up @@ -1219,6 +1224,7 @@ declare module '@babel/types' {
declare function isObjectTypeCallProperty(node: mixed, opts: Object | void): boolean %checks (node instanceof ObjectTypeCallProperty);
declare function isObjectTypeIndexer(node: mixed, opts: Object | void): boolean %checks (node instanceof ObjectTypeIndexer);
declare function isObjectTypeProperty(node: mixed, opts: Object | void): boolean %checks (node instanceof ObjectTypeProperty);
declare function isObjectTypeSpreadProperty(node: mixed, opts: Object | void): boolean %checks (node instanceof ObjectTypeSpreadProperty);
declare function isQualifiedTypeIdentifier(node: mixed, opts: Object | void): boolean %checks (node instanceof QualifiedTypeIdentifier);
declare function isUnionTypeAnnotation(node: mixed, opts: Object | void): boolean %checks (node instanceof UnionTypeAnnotation);
declare function isVoidTypeAnnotation(node: mixed, opts: Object | void): boolean %checks (node instanceof VoidTypeAnnotation);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"bin/"
],
"dependencies": {
"@babel/types": "^7.0.0-beta.32",
"@babel/types": "^7.4.4",
"babylon": "^7.0.0-beta.32",
"json-stringify-pretty-compact": "^1.0.4",
"resolve": "^1.5.0",
Expand Down
23 changes: 22 additions & 1 deletion src/collector/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import type {
UnionTypeAnnotation, NullableTypeAnnotation, ObjectTypeIndexer, ObjectTypeProperty,
StringLiteralTypeAnnotation, ObjectTypeAnnotation, AnyTypeAnnotation, MixedTypeAnnotation,
TupleTypeAnnotation, DeclareTypeAlias, DeclareInterface, DeclareClass,
ObjectTypeSpreadProperty,
} from '@babel/types';

import {
isIdentifier, isStringLiteral, isObjectTypeProperty,
isStringLiteralTypeAnnotation, isClassProperty,
isStringLiteralTypeAnnotation, isClassProperty, isObjectTypeSpreadProperty,
} from '@babel/types';

import Context from './context';
Expand Down Expand Up @@ -157,6 +158,13 @@ function makeComplex(ctx: Context, node: ObjectTypeAnnotation): Type {
.filter()
.toArray();

if (node.properties.some(node => isObjectTypeSpreadProperty(node))) {
return t.createIntersection([
...makeSpreadTypes(ctx, node.properties),
...maps,
])
}

const record = makeRecord(ctx, node.properties);

if (maps.length === 1 && record.fields.length === 0) {
Expand All @@ -181,6 +189,19 @@ function makeRecord<T: ObjectTypeProperty | ClassProperty>(ctx: Context, nodes:
return t.createRecord(fields);
}

function makeSpreadTypes<T: ObjectTypeProperty | ClassProperty | ObjectTypeSpreadProperty>(ctx: Context, nodes: T[]): Type[] {
return wu(nodes)
.map(node => {
if (!isObjectTypeSpreadProperty(node)) {
return makeRecord(ctx, [node]);
}

return makeReference(ctx, node.argument);
})
.filter()
.toArray();
}

function makeField(ctx: Context, node: ObjectTypeProperty | ClassProperty): ?Field {
if ((node: $FlowIssue<3129>).static) {
return null;
Expand Down
34 changes: 34 additions & 0 deletions tests/samples/spread/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"$schema": "http://json-schema.org/draft-06/schema#",
"definitions": {
"spread::Base": {
"type": "object",
"properties": {
"foo": {"type": "string"},
"bar": {"type": "boolean"}
},
"required": ["foo", "bar"]
},
"spread::Type": {
"allOf": [
{
"$ref": "#/definitions/spread::Base"
},
{
"type": "object",
"properties": {
"baz": {"type": "number"}
},
"required": ["baz"]
},
{
"type": "object",
"properties": {
"bim": {"type": "string"}
},
"required": ["bim"]
}
]
}
}
}
12 changes: 12 additions & 0 deletions tests/samples/spread/source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
type Base = {
foo: string,
bar: boolean,
};

type Type = {
...Base,
baz: number,
bim: string,
};

export {Type};
29 changes: 29 additions & 0 deletions tests/samples/spread/types.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
- kind: record
fields:
- name: foo
value:
kind: string
required: true
- name: bar
value:
kind: boolean
required: true
id: [spread, Base]
- kind: intersection
parts:
- kind: reference
to: [spread, Base]
- kind: record
fields:
- name: baz
value:
kind: number
repr: f64
required: true
- kind: record
fields:
- name: bim
value:
kind: string
required: true
id: [spread, Type]