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

chore(gatsby): Migrate build-example-data to TS #23389

Merged
merged 2 commits into from
Apr 24, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
const { groupBy } = require(`lodash`)

// See gatsby/src/schema/infer/inference-metadata.js for the ValueDescriptor structs (-> typeInfo)

const getExampleObject = ({ fieldMap = {}, typeName, typeConflictReporter }) =>
/* eslint-disable @typescript-eslint/no-use-before-define */
import { groupBy } from "lodash"
import {
IValueDescriptor,
ValueType,
ITypeMetadata,
} from "./inference-metadata"
import {
TypeConflictReporter,
ITypeConflictExample,
} from "./type-conflict-reporter"

// See gatsby/src/schema/infer/inference-metadata.ts for the ValueDescriptor structs (-> typeInfo)

const getExampleObject = ({
fieldMap = {},
typeName,
typeConflictReporter,
}: ITypeMetadata): { [k: string]: unknown } =>
Object.keys(fieldMap).reduce((acc, key) => {
const value = buildExampleValue({
path: `${typeName}.${key}`,
Expand All @@ -20,7 +34,12 @@ const buildExampleValue = ({
typeConflictReporter,
isArrayItem = false,
path = ``,
}) => {
}: {
descriptor: IValueDescriptor
typeConflictReporter?: TypeConflictReporter
path?: string
isArrayItem?: boolean
}): unknown | null => {
const [type, conflicts = false] = resolveWinnerType(descriptor)

if (conflicts && typeConflictReporter) {
Expand Down Expand Up @@ -89,7 +108,9 @@ const buildExampleValue = ({
}
}

const resolveWinnerType = descriptor => {
const resolveWinnerType = (
descriptor: IValueDescriptor
): [ValueType | "null", boolean?] => {
const candidates = possibleTypes(descriptor)
if (candidates.length === 1) {
return [candidates[0]]
Expand All @@ -106,8 +127,11 @@ const resolveWinnerType = descriptor => {
return [`null`]
}

const prepareConflictExamples = (descriptor, isArrayItem) => {
const typeNameMapper = typeName => {
const prepareConflictExamples = (
descriptor: IValueDescriptor,
isArrayItem: boolean
): ITypeConflictExample[] => {
const typeNameMapper = (typeName: ValueType): string => {
if (typeName === `relatedNode`) {
return `string`
}
Expand All @@ -116,26 +140,35 @@ const prepareConflictExamples = (descriptor, isArrayItem) => {
}
return [`float`, `int`].includes(typeName) ? `number` : typeName
}
const reportedValueMapper = typeName => {
const reportedValueMapper = (typeName: ValueType): unknown => {
if (typeName === `relatedNode`) {
// See FIXME in ./inference-metadata.ts
// eslint-disable-next-line
// @ts-ignore
const { nodes } = descriptor.relatedNode
return Object.keys(nodes).find(key => nodes[key] > 0)
}
if (typeName === `relatedNodeList`) {
// See FIXME in ./inference-metadata.ts
// eslint-disable-next-line
// @ts-ignore
const { nodes } = descriptor.relatedNodeList
return Object.keys(nodes).filter(key => nodes[key] > 0)
}
if (typeName === `object`) {
return getExampleObject({ typeName, fieldMap: descriptor.object.dprops })
return getExampleObject({
typeName,
fieldMap: descriptor!.object!.dprops,
})
}
if (typeName === `array`) {
const itemValue = buildExampleValue({
descriptor: descriptor.array.item,
descriptor: descriptor!.array!.item,
isArrayItem: true,
})
return itemValue === null || itemValue === undefined ? [] : [itemValue]
}
return descriptor[typeName].example
return descriptor[typeName]?.example
}
const conflictingTypes = possibleTypes(descriptor)

Expand All @@ -144,7 +177,7 @@ const prepareConflictExamples = (descriptor, isArrayItem) => {
// See Caveats section in the header of this file
const groups = groupBy(
conflictingTypes,
type => descriptor[type].first || ``
type => descriptor[type]?.first || ``
)
return Object.keys(groups).map(nodeId => {
return {
Expand All @@ -162,16 +195,24 @@ const prepareConflictExamples = (descriptor, isArrayItem) => {
})
}

const isMixedNumber = ({ float, int }) =>
float && float.total > 0 && int && int.total > 0
const isMixedNumber = (descriptor: IValueDescriptor): boolean => {
const { float, int } = descriptor
return Boolean(float?.total) && Boolean(int?.total)
}

const isMixOfDateAndString = ({ date, string }) =>
date && date.total > 0 && string && string.total > 0
const isMixOfDateAndString = (descriptor: IValueDescriptor): boolean => {
const { date, string } = descriptor
return Boolean(date?.total) && Boolean(string?.total)
}

const hasOnlyEmptyStrings = ({ string }) =>
string && string.empty === string.total
const hasOnlyEmptyStrings = (descriptor: IValueDescriptor): boolean => {
const { string } = descriptor
return string !== undefined && string?.empty === string?.total
}

const possibleTypes = (descriptor = {}) =>
Object.keys(descriptor).filter(type => descriptor[type].total > 0)
const possibleTypes = (descriptor: IValueDescriptor = {}): ValueType[] =>
Object.keys(descriptor).filter(
type => descriptor[type].total > 0
) as ValueType[]

export { getExampleObject }
22 changes: 11 additions & 11 deletions packages/gatsby/src/schema/infer/inference-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,44 +63,44 @@ import { looksLikeADate } from "../types/date"
import { Node } from "../../../index"
import { TypeConflictReporter } from "./type-conflict-reporter"

interface ITypeInfo {
export interface ITypeInfo {
first?: string
total: number
example?: unknown
}

interface ITypeInfoString extends ITypeInfo {
export interface ITypeInfoString extends ITypeInfo {
empty: number
example: string
}

interface ITypeInfoDate extends ITypeInfo {
export interface ITypeInfoDate extends ITypeInfo {
example: string
}

interface ITypeInfoNumber extends ITypeInfo {
export interface ITypeInfoNumber extends ITypeInfo {
example: number
}

interface ITypeInfoBoolean extends ITypeInfo {
export interface ITypeInfoBoolean extends ITypeInfo {
example: boolean
}

interface ITypeInfoArray extends ITypeInfo {
export interface ITypeInfoArray extends ITypeInfo {
item: IValueDescriptor
}

interface ITypeInfoRelatedNodes extends ITypeInfo {
export interface ITypeInfoRelatedNodes extends ITypeInfo {
nodes: { [key: string]: number }
}

interface ITypeInfoObject extends ITypeInfo {
export interface ITypeInfoObject extends ITypeInfo {
dprops: {
[name: string]: IValueDescriptor
}
}

interface IValueDescriptor {
export interface IValueDescriptor {
int?: ITypeInfoNumber
float?: ITypeInfoNumber
date?: ITypeInfoDate
Expand All @@ -112,11 +112,11 @@ interface IValueDescriptor {
object?: ITypeInfoObject
}

type ValueType = keyof IValueDescriptor
export type ValueType = keyof IValueDescriptor

export interface ITypeMetadata {
typeName?: string
disabled: boolean
disabled?: boolean
ignored?: boolean
dirty?: boolean
total?: number
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby/src/schema/infer/type-conflict-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { Node } from "../../../index"

export interface ITypeConflictExample {
value: unknown
parent: Node
type: string
arrayTypes: string[]
parent?: Node
arrayTypes?: string[]
}

interface ITypeConflict {
Expand Down