( def: Dict, ctx: BaseParseContext )
| 44 | type MutableStructureSchema = mutable<NodeInput<"structure">, 2> |
| 45 | |
| 46 | export const parseObjectLiteral = ( |
| 47 | def: Dict, |
| 48 | ctx: BaseParseContext |
| 49 | ): BaseRoot => { |
| 50 | let spread: Structure.Node | undefined |
| 51 | let meta: TypeMeta.Collapsible | undefined |
| 52 | const structure: MutableStructureSchema = {} |
| 53 | // the order output lists keys in, as they're declared |
| 54 | const order: Key[] = [] |
| 55 | // We only allow a spread operator to be used as the first key in an object |
| 56 | // because to match JS behavior any keys before the spread are overwritten |
| 57 | // by the values in the target object, so there'd be no useful purpose in having it |
| 58 | // anywhere except for the beginning. |
| 59 | for (const k of ownKeysOf(def)) { |
| 60 | const v = def[k as string] |
| 61 | const parsedKey = preparseKey(k) |
| 62 | |
| 63 | if (parsedKey.kind === "spread") { |
| 64 | if (!isEmptyObject(structure)) |
| 65 | return throwParseError(nonLeadingSpreadError) |
| 66 | const operand = ctx.$.parseOwn(v, ctx) |
| 67 | // treat object domain as empty for spreading (useful for generic constraints) |
| 68 | if (operand.equals(intrinsic.object)) continue |
| 69 | if ( |
| 70 | !operand.is("intersection") || |
| 71 | // still error on attempts to spread proto nodes like ...Date |
| 72 | !operand.basis?.equals(intrinsic.object) |
| 73 | ) { |
| 74 | return throwParseError( |
| 75 | writeInvalidSpreadTypeMessage(operand.expression) |
| 76 | ) |
| 77 | } |
| 78 | spread = operand.structure |
| 79 | continue |
| 80 | } |
| 81 | |
| 82 | if (parsedKey.kind === "undeclared") { |
| 83 | if (v !== "reject" && v !== "delete" && v !== "ignore") |
| 84 | throwParseError(writeInvalidUndeclaredBehaviorMessage(v)) |
| 85 | structure.undeclared = v |
| 86 | continue |
| 87 | } |
| 88 | |
| 89 | if (parsedKey.kind === "meta") { |
| 90 | meta = v as TypeMeta.Collapsible |
| 91 | continue |
| 92 | } |
| 93 | |
| 94 | const parsedValue = parseProperty(v, ctx) |
| 95 | const parsedEntryKey = parsedKey as PreparsedEntryKey |
| 96 | |
| 97 | if (parsedKey.kind !== "index") order.push(parsedEntryKey.normalized) |
| 98 | |
| 99 | if (parsedKey.kind === "required") { |
| 100 | if (!isArray(parsedValue)) { |
| 101 | appendNamedProp( |
| 102 | structure, |
| 103 | "required", |
no test coverage detected