Skip to content

wip: deduplicate inline unions #233

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
53 changes: 49 additions & 4 deletions src/typeEvaluator/optimizations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import type {TypeNode} from './types'
import type {InlineTypeNode, ObjectTypeNode, TypeNode, UnionTypeNode} from './types'

function hashObjectTypeNode(object: ObjectTypeNode, ignoreRest: boolean = false) {
return `${object.type}:(${Object.entries(object.attributes)
.map(([key, value]) => `${key}:${hashField(value.value)}`)
.join(
',',
)}):ref-${object.dereferencesTo}:${!ignoreRest && object.rest ? hashField(object.rest) : 'no-rest'}`
}

export function hashField(field: TypeNode): string {
switch (field.type) {
Expand All @@ -21,9 +29,7 @@ export function hashField(field: TypeNode): string {
}

case 'object': {
return `${field.type}:(${Object.entries(field.attributes)
.map(([key, value]) => `${key}:${hashField(value.value)}`)
.join(',')}):ref-${field.dereferencesTo}:${field.rest ? hashField(field.rest) : 'no-rest'}`
return hashObjectTypeNode(field)
}

case 'union': {
Expand Down Expand Up @@ -66,10 +72,49 @@ export function optimizeUnions(field: TypeNode): TypeNode {
if (field.type === 'union') {
field.of = removeDuplicateTypeNodes(field.of)

// empty union, abort early
if (field.of.length === 0) {
return field
}

// single element union, optimize
if (field.of.length === 1) {
return optimizeUnions(field.of[0])
}

//
if (field.of[0].type === 'object' && field.of[0].rest?.type === 'inline') {
const objectAttributeHash = hashObjectTypeNode(field.of[0], true)
if (
field.of.every(
(node) =>
node.type === 'object' &&
node.rest?.type === 'inline' &&
hashObjectTypeNode(node, true) === objectAttributeHash,
)
) {
const inlines: InlineTypeNode[] = []
for (const obj of field.of) {
// eslint-disable-next-line
if (obj.type !== 'object' || obj.rest === undefined || obj.rest.type !== 'inline') {
continue
}
inlines.push(obj.rest)
}

const rest = optimizeUnions({
type: 'union',
of: inlines,
}) as UnionTypeNode<InlineTypeNode> | InlineTypeNode // todo: fix me

return {
type: 'object',
attributes: field.of[0].attributes,
rest,
} satisfies ObjectTypeNode
}
}

// flatten union
for (let idx = 0; field.of.length > idx; idx++) {
const subField = field.of[idx]
Expand Down
40 changes: 40 additions & 0 deletions src/typeEvaluator/typeHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,53 @@ export function nullUnion(node: TypeNode): UnionTypeNode {
return unionOf(node, {type: 'null'})
}

export function arrayOf(node: TypeNode): ArrayTypeNode {
return {
type: 'array',
of: node,
} satisfies ArrayTypeNode
}

export function unionOf(...nodes: TypeNode[]): UnionTypeNode {
return {
type: 'union',
of: nodes,
} satisfies UnionTypeNode
}

export function objectWith(...attributes: [string, ObjectAttribute][]): ObjectTypeNode {
return {
type: 'object',
attributes: Object.fromEntries(attributes),
} satisfies ObjectTypeNode
}

export function objectWithRest(
rest: ObjectTypeNode['rest'],
...attributes: [string, ObjectAttribute][]
): ObjectTypeNode {
return {
type: 'object',
attributes: Object.fromEntries(attributes),
rest,
} satisfies ObjectTypeNode
}

export function objectAttribute(
key: string,
value: TypeNode,
optional?: boolean,
): [string, ObjectAttribute] {
return [
key,
{
type: 'objectAttribute',
value,
optional,
} satisfies ObjectAttribute,
]
}

type ConcreteTypeNode =
| BooleanTypeNode
| NullTypeNode
Expand Down
2 changes: 1 addition & 1 deletion src/typeEvaluator/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export interface ObjectTypeNode<T extends TypeNode = TypeNode> {
/** a collection of attributes */
attributes: Record<string, ObjectAttribute<T>>
/** an optional rest value */
rest?: ObjectTypeNode | UnknownTypeNode | InlineTypeNode
rest?: ObjectTypeNode | UnknownTypeNode | InlineTypeNode | UnionTypeNode<InlineTypeNode>
/* an optional reference to the document this object dereferences to */
dereferencesTo?: string
}
Expand Down
Loading