openapi-typesafe 1.0.0-rc.1 → 1.0.0-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -5
- package/dist/adapters/redocly-loader/configured-headers.cjs +32 -0
- package/dist/adapters/redocly-loader/configured-headers.cjs.map +1 -0
- package/dist/adapters/redocly-loader/configured-headers.mjs +30 -0
- package/dist/adapters/redocly-loader/configured-headers.mjs.map +1 -0
- package/dist/adapters/redocly-loader/redocly-schema-loader.cjs +19 -6
- package/dist/adapters/redocly-loader/redocly-schema-loader.cjs.map +1 -1
- package/dist/adapters/redocly-loader/redocly-schema-loader.mjs +18 -5
- package/dist/adapters/redocly-loader/redocly-schema-loader.mjs.map +1 -1
- package/dist/adapters/redocly-loader/ref-access.cjs +37 -4
- package/dist/adapters/redocly-loader/ref-access.cjs.map +1 -1
- package/dist/adapters/redocly-loader/ref-access.mjs +37 -4
- package/dist/adapters/redocly-loader/ref-access.mjs.map +1 -1
- package/dist/adapters/redocly-loader/ref-resolution-check.cjs +191 -0
- package/dist/adapters/redocly-loader/ref-resolution-check.cjs.map +1 -0
- package/dist/adapters/redocly-loader/ref-resolution-check.mjs +189 -0
- package/dist/adapters/redocly-loader/ref-resolution-check.mjs.map +1 -0
- package/dist/adapters/redocly-loader/ref-resolution-steps.cjs +73 -0
- package/dist/adapters/redocly-loader/ref-resolution-steps.cjs.map +1 -0
- package/dist/adapters/redocly-loader/ref-resolution-steps.mjs +60 -0
- package/dist/adapters/redocly-loader/ref-resolution-steps.mjs.map +1 -0
- package/dist/adapters/redocly-loader/remote-fetch.cjs +41 -14
- package/dist/adapters/redocly-loader/remote-fetch.cjs.map +1 -1
- package/dist/adapters/redocly-loader/remote-fetch.mjs +40 -14
- package/dist/adapters/redocly-loader/remote-fetch.mjs.map +1 -1
- package/dist/adapters/redocly-loader/schema-load-errors.cjs +23 -5
- package/dist/adapters/redocly-loader/schema-load-errors.cjs.map +1 -1
- package/dist/adapters/redocly-loader/schema-load-errors.mjs +23 -5
- package/dist/adapters/redocly-loader/schema-load-errors.mjs.map +1 -1
- package/dist/adapters/redocly-loader/unsupported-keys.cjs +9 -1
- package/dist/adapters/redocly-loader/unsupported-keys.cjs.map +1 -1
- package/dist/adapters/redocly-loader/unsupported-keys.mjs +9 -1
- package/dist/adapters/redocly-loader/unsupported-keys.mjs.map +1 -1
- package/dist/adapters/terminal-logger/terminal-logger.cjs +6 -4
- package/dist/adapters/terminal-logger/terminal-logger.cjs.map +1 -1
- package/dist/adapters/terminal-logger/terminal-logger.mjs +6 -4
- package/dist/adapters/terminal-logger/terminal-logger.mjs.map +1 -1
- package/dist/adapters/type-ast-bridge/ir-to-typescript.cjs +2 -1
- package/dist/adapters/type-ast-bridge/ir-to-typescript.cjs.map +1 -1
- package/dist/adapters/type-ast-bridge/ir-to-typescript.mjs +2 -1
- package/dist/adapters/type-ast-bridge/ir-to-typescript.mjs.map +1 -1
- package/dist/adapters/type-ast-bridge/print-typescript.cjs.map +1 -1
- package/dist/adapters/type-ast-bridge/print-typescript.mjs.map +1 -1
- package/dist/api/default-ports.cjs +9 -0
- package/dist/api/default-ports.cjs.map +1 -1
- package/dist/api/default-ports.mjs +8 -1
- package/dist/api/default-ports.mjs.map +1 -1
- package/dist/api/discriminator-patch.cjs +5 -1
- package/dist/api/discriminator-patch.cjs.map +1 -1
- package/dist/api/discriminator-patch.mjs +5 -1
- package/dist/api/discriminator-patch.mjs.map +1 -1
- package/dist/api/document-refs.cjs +15 -12
- package/dist/api/document-refs.cjs.map +1 -1
- package/dist/api/document-refs.mjs +15 -12
- package/dist/api/document-refs.mjs.map +1 -1
- package/dist/api/document-run.cjs +4 -2
- package/dist/api/document-run.cjs.map +1 -1
- package/dist/api/document-run.mjs +4 -2
- package/dist/api/document-run.mjs.map +1 -1
- package/dist/api/footer-sink.cjs +25 -2
- package/dist/api/footer-sink.cjs.map +1 -1
- package/dist/api/footer-sink.mjs +25 -2
- package/dist/api/footer-sink.mjs.map +1 -1
- package/dist/api/legacy-callback-warning.cjs +4 -1
- package/dist/api/legacy-callback-warning.cjs.map +1 -1
- package/dist/api/legacy-callback-warning.mjs +4 -1
- package/dist/api/legacy-callback-warning.mjs.map +1 -1
- package/dist/api/legacy-ts-helpers.cjs.map +1 -1
- package/dist/api/legacy-ts-helpers.mjs.map +1 -1
- package/dist/api/legacy-utils.cjs.map +1 -1
- package/dist/api/legacy-utils.mjs.map +1 -1
- package/dist/api/openapi-ts.cjs +2 -1
- package/dist/api/openapi-ts.cjs.map +1 -1
- package/dist/api/openapi-ts.mjs +3 -2
- package/dist/api/openapi-ts.mjs.map +1 -1
- package/dist/api/transform-context.cjs +1 -1
- package/dist/api/transform-context.cjs.map +1 -1
- package/dist/api/transform-context.mjs +1 -1
- package/dist/api/transform-context.mjs.map +1 -1
- package/dist/api/transform-schema.cjs.map +1 -1
- package/dist/api/transform-schema.mjs.map +1 -1
- package/dist/application/print/type-printer.cjs +2 -1
- package/dist/application/print/type-printer.cjs.map +1 -1
- package/dist/application/print/type-printer.mjs +2 -1
- package/dist/application/print/type-printer.mjs.map +1 -1
- package/dist/application/transform/document/parameters.cjs +1 -1
- package/dist/application/transform/document/parameters.cjs.map +1 -1
- package/dist/application/transform/document/parameters.mjs +1 -1
- package/dist/application/transform/document/parameters.mjs.map +1 -1
- package/dist/application/transform/document/paths.cjs +1 -1
- package/dist/application/transform/document/paths.cjs.map +1 -1
- package/dist/application/transform/document/paths.mjs +1 -1
- package/dist/application/transform/document/paths.mjs.map +1 -1
- package/dist/application/transform/footer/evaluation.cjs +9 -0
- package/dist/application/transform/footer/evaluation.cjs.map +1 -1
- package/dist/application/transform/footer/evaluation.mjs +8 -1
- package/dist/application/transform/footer/evaluation.mjs.map +1 -1
- package/dist/application/transform/footer/type-evaluation.cjs +4 -112
- package/dist/application/transform/footer/type-evaluation.cjs.map +1 -1
- package/dist/application/transform/footer/type-evaluation.mjs +4 -112
- package/dist/application/transform/footer/type-evaluation.mjs.map +1 -1
- package/dist/application/transform/footer/type-indexing.cjs +107 -0
- package/dist/application/transform/footer/type-indexing.cjs.map +1 -0
- package/dist/application/transform/footer/type-indexing.mjs +105 -0
- package/dist/application/transform/footer/type-indexing.mjs.map +1 -0
- package/dist/cli/api-settings.cjs +78 -0
- package/dist/cli/api-settings.cjs.map +1 -0
- package/dist/cli/api-settings.mjs +73 -0
- package/dist/cli/api-settings.mjs.map +1 -0
- package/dist/cli/cli-arguments.cjs +1 -0
- package/dist/cli/cli-arguments.cjs.map +1 -1
- package/dist/cli/cli-arguments.mjs +1 -1
- package/dist/cli/cli-arguments.mjs.map +1 -1
- package/dist/cli/help-text.cjs +3 -3
- package/dist/cli/help-text.cjs.map +1 -1
- package/dist/cli/help-text.mjs +3 -3
- package/dist/cli/help-text.mjs.map +1 -1
- package/dist/cli/redocly-apis.cjs +8 -4
- package/dist/cli/redocly-apis.cjs.map +1 -1
- package/dist/cli/redocly-apis.mjs +7 -3
- package/dist/cli/redocly-apis.mjs.map +1 -1
- package/dist/cli/run-cli.cjs +5 -1
- package/dist/cli/run-cli.cjs.map +1 -1
- package/dist/cli/run-cli.mjs +5 -1
- package/dist/cli/run-cli.mjs.map +1 -1
- package/dist/domain/errors.cjs +4 -1
- package/dist/domain/errors.cjs.map +1 -1
- package/dist/domain/errors.mjs +4 -1
- package/dist/domain/errors.mjs.map +1 -1
- package/dist/domain/ir/constructors.cjs +2 -0
- package/dist/domain/ir/constructors.cjs.map +1 -1
- package/dist/domain/ir/constructors.mjs +2 -1
- package/dist/domain/ir/constructors.mjs.map +1 -1
- package/dist/domain/openapi/path-templates.cjs +1 -1
- package/dist/domain/openapi/path-templates.cjs.map +1 -1
- package/dist/domain/openapi/path-templates.mjs +1 -1
- package/dist/domain/openapi/path-templates.mjs.map +1 -1
- package/dist/domain/url-credentials.cjs +10 -0
- package/dist/domain/url-credentials.cjs.map +1 -0
- package/dist/domain/url-credentials.mjs +8 -0
- package/dist/domain/url-credentials.mjs.map +1 -0
- package/dist/domain/warnings.cjs +9 -1
- package/dist/domain/warnings.cjs.map +1 -1
- package/dist/domain/warnings.mjs +9 -1
- package/dist/domain/warnings.mjs.map +1 -1
- package/dist/index.cjs +2 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1160 -53
- package/dist/index.d.mts +1160 -53
- package/dist/index.d.ts +1160 -53
- package/dist/index.mjs +1 -0
- package/dist/index.mjs.map +1 -1
- package/dist/transform/path-item-object.cjs.map +1 -1
- package/dist/transform/path-item-object.mjs.map +1 -1
- package/package.json +6 -5
- package/src/adapters/redocly-loader/configured-headers.ts +65 -0
- package/src/adapters/redocly-loader/redocly-schema-loader.ts +22 -7
- package/src/adapters/redocly-loader/ref-access.ts +60 -5
- package/src/adapters/redocly-loader/ref-resolution-check.ts +257 -0
- package/src/adapters/redocly-loader/ref-resolution-steps.ts +113 -0
- package/src/adapters/redocly-loader/remote-fetch.ts +77 -21
- package/src/adapters/redocly-loader/schema-load-errors.ts +33 -11
- package/src/adapters/redocly-loader/unsupported-keys.ts +17 -2
- package/src/adapters/terminal-logger/terminal-logger.ts +9 -5
- package/src/adapters/type-ast-bridge/ir-to-typescript.ts +2 -1
- package/src/adapters/type-ast-bridge/print-typescript.ts +3 -1
- package/src/adapters/type-ast-bridge/typescript-types.ts +57 -5
- package/src/api/default-ports.ts +16 -0
- package/src/api/discriminator-patch.ts +7 -2
- package/src/api/document-refs.ts +19 -15
- package/src/api/document-run.ts +9 -6
- package/src/api/footer-sink.ts +34 -2
- package/src/api/legacy-callback-warning.ts +7 -3
- package/src/api/legacy-ts-helpers.ts +7 -5
- package/src/api/legacy-utils.ts +1 -1
- package/src/api/openapi-ts.ts +2 -0
- package/src/api/run-context.ts +34 -0
- package/src/api/transform-context.ts +10 -8
- package/src/api/transform-schema.ts +6 -5
- package/src/application/print/type-printer.ts +2 -1
- package/src/application/transform/document/parameters.ts +2 -1
- package/src/application/transform/document/paths.ts +2 -1
- package/src/application/transform/footer/evaluation.ts +10 -0
- package/src/application/transform/footer/type-evaluation.ts +6 -133
- package/src/application/transform/footer/type-indexing.ts +152 -0
- package/src/cli/api-settings.ts +109 -0
- package/src/cli/cli-arguments.ts +5 -2
- package/src/cli/help-text.ts +4 -4
- package/src/cli/redocly-apis.ts +13 -7
- package/src/cli/run-cli.ts +5 -1
- package/src/domain/errors.ts +8 -2
- package/src/domain/ir/constructors.ts +3 -0
- package/src/domain/ir/nodes.ts +9 -6
- package/src/domain/openapi/path-templates.ts +2 -1
- package/src/domain/url-credentials.ts +18 -0
- package/src/domain/warnings.ts +8 -0
- package/src/index.ts +1 -0
- package/src/transform/path-item-object.ts +6 -2
- package/src/types/generator-options.ts +12 -32
- package/src/types/openapi-common.ts +1 -0
- package/src/types/openapi-request.ts +3 -0
- package/src/types/openapi-schema.ts +2 -0
- package/src/types/openapi-security.ts +1 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type-printer.cjs","sources":["../../../src/application/print/type-printer.ts"],"sourcesContent":["import { assertNever } from \"../../domain/assert-never.js\";\nimport type {\n IrIndexedAccessType,\n IrMember,\n IrPropertyName,\n IrRawType,\n IrTupleElement,\n IrType,\n} from \"../../domain/ir/nodes.js\";\nimport { writeLeadingComment } from \"./comment-writer.js\";\nimport { printEscapedString, printNumberLiteral, printStringLiteral } from \"./literal-text.js\";\nimport type { TextWriter } from \"./text-writer.js\";\n\n/**\n * Parenthesization contexts. Each predicate says when a child type needs parentheses; together they\n * reproduce the TypeScript factory's parenthesizer rules for the node kinds of the IR, so output\n * matches the original even when unions are nested (`A | (B | C)`).\n */\nconst isUnionOrIntersection = (type: IrType): boolean =>\n type.kind === \"union\" || type.kind === \"intersection\";\n/** Union and intersection members (`parenthesizeConstituentTypeOf{Union,Intersection}Type`). */\nconst needsParensAsConstituent = isUnionOrIntersection;\n/** Array element and indexed-access object (`parenthesizeNonArrayTypeOfPostfixType`). */\nconst needsParensAsPostfixOperand = (type: IrType): boolean =>\n type.kind === \"readonly\" || isUnionOrIntersection(type);\n/** Operand of `readonly` (`parenthesizeOperandOfReadonlyTypeOperator`). */\nconst needsParensAsReadonlyOperand = needsParensAsPostfixOperand;\n\nfunction writeTypeIn(\n writer: TextWriter,\n type: IrType,\n needsParens: (type: IrType) => boolean,\n): void {\n if (!needsParens(type)) {\n writeType(writer, type);\n return;\n }\n writer.write(\"(\");\n writeType(writer, type);\n writer.write(\")\");\n}\n\n/** Write a single-line list: `separator` between items, nothing for an empty list. */\nfunction writeInlineList<T>(\n writer: TextWriter,\n items: readonly T[],\n separator: string,\n writeItem: (item: T) => void,\n) {\n items.forEach((item, index) => {\n if (index > 0) {\n writer.write(separator);\n }\n writeItem(item);\n });\n}\n\n/**\n * Write a multi-line list the way the TypeScript printer does: a line break after the opening\n * token, one item per indented line, `delimiter` after every item but the last, and a line break\n * before the closing token. An empty list prints the line break only when `isLineBreakWhenEmpty`\n * (interfaces, enums and tuples do; object type literals print `{}`).\n */\nexport function writeMultiLineList<T>(\n writer: TextWriter,\n items: readonly T[],\n format: { readonly delimiter: string; readonly isLineBreakWhenEmpty: boolean },\n writeItem: (item: T) => void,\n): void {\n if (items.length === 0) {\n if (format.isLineBreakWhenEmpty) {\n writer.writeLine();\n }\n return;\n }\n writer.writeLine();\n writer.increaseIndent();\n items.forEach((item, index) => {\n if (index > 0) {\n writer.write(format.delimiter);\n writer.writeLine();\n }\n writeItem(item);\n });\n writer.decreaseIndent();\n writer.writeLine();\n}\n\nfunction writePropertyName(writer: TextWriter, name: IrPropertyName): void {\n switch (name.kind) {\n case \"identifier\":\n case \"number\":\n writer.write(name.text);\n break;\n case \"string\":\n writer.write(printEscapedString(name.value));\n break;\n default:\n assertNever(name);\n }\n}\n\n/**\n * `[name: type]`. TypeScript prints index signature parameters as an indented list, so a multi-line\n * parameter type (an object or tuple) is indented one level deeper than the member.\n */\nfunction writeIndexSignatureParameter(writer: TextWriter, name: string, type: IrType): void {\n writer.write(`[${name}: `);\n writer.increaseIndent();\n writeType(writer, type);\n writer.decreaseIndent();\n writer.write(\"]\");\n}\n\n/** Write one member of an interface or object type, with its comment and trailing semicolon. */\nexport function writeMember(writer: TextWriter, member: IrMember): void {\n for (const comment of member.comments) {\n writeLeadingComment(writer, comment);\n }\n if (member.isReadonly) {\n writer.write(\"readonly \");\n }\n if (member.kind === \"property\") {\n writePropertyName(writer, member.name);\n writer.write(member.isOptional ? \"?: \" : \": \");\n } else {\n writeIndexSignatureParameter(writer, member.parameterName, member.parameterType);\n writer.write(\": \");\n }\n writeType(writer, member.type);\n writer.write(\";\");\n}\n\nfunction writeTupleElement(writer: TextWriter, element: IrTupleElement): void {\n if (element.isRest) {\n writer.write(\"...\");\n }\n if (element.isOptional) {\n // TypeScript parenthesizes an optional element as a postfix operand (`(A | B)?`).\n writeTypeIn(writer, element.type, needsParensAsPostfixOperand);\n writer.write(\"?\");\n return;\n }\n writeType(writer, element.type);\n}\n\nfunction writeObjectType(writer: TextWriter, members: readonly IrMember[]): void {\n writer.write(\"{\");\n writeMultiLineList(writer, members, { delimiter: \"\", isLineBreakWhenEmpty: false }, (member) =>\n writeMember(writer, member),\n );\n writer.write(\"}\");\n}\n\nfunction writeTupleType(writer: TextWriter, elements: readonly IrTupleElement[]): void {\n writer.write(\"[\");\n writeMultiLineList(writer, elements, { delimiter: \",\", isLineBreakWhenEmpty: true }, (element) =>\n writeTupleElement(writer, element),\n );\n writer.write(\"]\");\n}\n\nfunction writeReferenceType(\n writer: TextWriter,\n name: string,\n typeArguments: readonly IrType[],\n): void {\n writer.write(name);\n if (typeArguments.length > 0) {\n writer.write(\"<\");\n writeInlineList(writer, typeArguments, \", \", (argument) => writeType(writer, argument));\n writer.write(\">\");\n }\n}\n\nfunction writeIndexedAccessType(writer: TextWriter, type: IrIndexedAccessType): void {\n writeTypeIn(writer, type.object, needsParensAsPostfixOperand);\n writer.write(\"[\");\n writeType(writer, type.index);\n writer.write(\"]\");\n}\n\nfunction writeConstituents(writer: TextWriter, types: readonly IrType[], separator: string): void {\n writeInlineList(writer, types, separator, (member) =>\n writeTypeIn(writer, member, needsParensAsConstituent),\n );\n}\n\ntype IrLeafType = Extract<\n IrType,\n { kind: \"keyword\" | \"stringLiteral\" | \"numberLiteral\" | \"booleanLiteral\" }\n>;\n\nfunction printLeafType(type: IrLeafType): string {\n switch (type.kind) {\n case \"keyword\":\n return type.keyword;\n case \"stringLiteral\":\n return printStringLiteral(type.value, type.encoding);\n case \"numberLiteral\":\n return printNumberLiteral(type.value);\n case \"booleanLiteral\":\n return String(type.value);\n default:\n return assertNever(type);\n }\n}\n\n/**\n * Raw text is printed as if it started at column 0, so its continuation lines get the current\n * indentation, as the TypeScript printer indents a nested node.\n */\nfunction writeRawType(writer: TextWriter, text: string): void {\n text.split(\"\\n\").forEach((line, index) => {\n if (index > 0) {\n writer.rawWrite(writer.options.newLine);\n }\n writer.write(line);\n });\n}\n\nfunction writeArrayType(writer: TextWriter, element: IrType): void {\n writeTypeIn(writer, element, needsParensAsPostfixOperand);\n writer.write(\"[]\");\n}\n\nfunction writeReadonlyType(writer: TextWriter, operand: IrType): void {\n writer.write(\"readonly \");\n writeTypeIn(writer, operand, needsParensAsReadonlyOperand);\n}\n\ntype IrOperatorType = Extract<IrType, { kind: \"array\" | \"indexedAccess\" | \"readonly\" }>;\n\n/** Types built with a type operator: `T[]`, `T[K]` and `readonly T`. */\nfunction writeOperatorType(writer: TextWriter, type: IrOperatorType): void {\n if (type.kind === \"array\") {\n writeArrayType(writer, type.element);\n } else if (type.kind === \"indexedAccess\") {\n writeIndexedAccessType(writer, type);\n } else {\n writeReadonlyType(writer, type.type);\n }\n}\n\nconst CONSTITUENT_SEPARATORS = { union: \" | \", intersection: \" & \" } as const;\n\n/** A raw type may span lines and is re-indented; any other leaf prints on one line. */\nfunction writeLeafType(writer: TextWriter, type: IrLeafType | IrRawType): void {\n if (type.kind === \"raw\") {\n writeRawType(writer, type.text);\n } else {\n writer.write(printLeafType(type));\n }\n}\n\n/** Write a type at the writer's current position. */\nexport function writeType(writer: TextWriter, type: IrType): void {\n switch (type.kind) {\n case \"keyword\":\n case \"stringLiteral\":\n case \"numberLiteral\":\n case \"booleanLiteral\":\n case \"raw\":\n writeLeafType(writer, type);\n break;\n case \"reference\":\n writeReferenceType(writer, type.name, type.typeArguments);\n break;\n case \"array\":\n case \"indexedAccess\":\n case \"readonly\":\n writeOperatorType(writer, type);\n break;\n case \"union\":\n case \"intersection\":\n writeConstituents(writer, type.types, CONSTITUENT_SEPARATORS[type.kind]);\n break;\n case \"object\":\n writeObjectType(writer, type.members);\n break;\n case \"tuple\":\n writeTupleType(writer, type.elements);\n break;\n default:\n assertNever(type);\n }\n}\n"],"names":["printEscapedString","assertNever","writeLeadingComment","printStringLiteral","printNumberLiteral"],"mappings":";;;;;;AAkBA,MAAM,wBAAwB,CAAC,IAAA,KAC7B,KAAK,IAAA,KAAS,OAAA,IAAW,KAAK,IAAA,KAAS,cAAA;AAEzC,MAAM,wBAAA,GAA2B,qBAAA;AAEjC,MAAM,8BAA8B,CAAC,IAAA,KACnC,KAAK,IAAA,KAAS,UAAA,IAAc,sBAAsB,IAAI,CAAA;AAExD,MAAM,4BAAA,GAA+B,2BAAA;AAErC,SAAS,WAAA,CACP,MAAA,EACA,IAAA,EACA,WAAA,EACM;AACN,EAAA,IAAI,CAAC,WAAA,CAAY,IAAI,CAAA,EAAG;AACtB,IAAA,SAAA,CAAU,QAAQ,IAAI,CAAA;AACtB,IAAA;AAAA,EACF;AACA,EAAA,MAAA,CAAO,MAAM,GAAG,CAAA;AAChB,EAAA,SAAA,CAAU,QAAQ,IAAI,CAAA;AACtB,EAAA,MAAA,CAAO,MAAM,GAAG,CAAA;AAClB;AAGA,SAAS,eAAA,CACP,MAAA,EACA,KAAA,EACA,SAAA,EACA,SAAA,EACA;AACA,EAAA,KAAA,CAAM,OAAA,CAAQ,CAAC,IAAA,EAAM,KAAA,KAAU;AAC7B,IAAA,IAAI,QAAQ,CAAA,EAAG;AACb,MAAA,MAAA,CAAO,MAAM,SAAS,CAAA;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KAAK,OAAA;AAAA,IACL,KAAK,eAAA;AAAA,IACL,KAAK,UAAA;AACH,MAAA,iBAAA,CAAkB,QAAQ,IAAI,CAAA;AAC9B,MAAA;AAAA,IACF,KAAK,OAAA;AAAA,IACL,KAAK,cAAA;AACH,MAAA,iBAAA,CAAkB,QAAQ,IAAA,CAAK,KAAA,EAAO,sBAAA,CAAuB,IAAA,CAAK,IAAI,CAAC,CAAA;AACvE,MAAA;AAAA,IACF,KAAK,QAAA;AACH,MAAA,eAAA,CAAgB,MAAA,EAAQ,KAAK,OAAO,CAAA;AACpC,MAAA;AAAA,IACF,KAAK,OAAA;AACH,MAAA,cAAA,CAAe,MAAA,EAAQ,KAAK,QAAQ,CAAA;AACpC,MAAA;AAAA,IACF;AACE,MAAAA,uBAAA,CAAY,IAAI,CAAA;AAAA;AAEtB;;;;"}
|
|
1
|
+
{"version":3,"file":"type-printer.cjs","sources":["../../../src/application/print/type-printer.ts"],"sourcesContent":["import { assertNever } from \"../../domain/assert-never.js\";\nimport { DEFAULT_STRING_ENCODING } from \"../../domain/ir/constructors.js\";\nimport type {\n IrIndexedAccessType,\n IrMember,\n IrPropertyName,\n IrRawType,\n IrTupleElement,\n IrType,\n} from \"../../domain/ir/nodes.js\";\nimport { writeLeadingComment } from \"./comment-writer.js\";\nimport { printEscapedString, printNumberLiteral, printStringLiteral } from \"./literal-text.js\";\nimport type { TextWriter } from \"./text-writer.js\";\n\n/**\n * Parenthesization contexts. Each predicate says when a child type needs parentheses; together they\n * reproduce the TypeScript factory's parenthesizer rules for the node kinds of the IR, so output\n * matches the original even when unions are nested (`A | (B | C)`).\n */\nconst isUnionOrIntersection = (type: IrType): boolean =>\n type.kind === \"union\" || type.kind === \"intersection\";\n/** Union and intersection members (`parenthesizeConstituentTypeOf{Union,Intersection}Type`). */\nconst needsParensAsConstituent = isUnionOrIntersection;\n/** Array element and indexed-access object (`parenthesizeNonArrayTypeOfPostfixType`). */\nconst needsParensAsPostfixOperand = (type: IrType): boolean =>\n type.kind === \"readonly\" || isUnionOrIntersection(type);\n/** Operand of `readonly` (`parenthesizeOperandOfReadonlyTypeOperator`). */\nconst needsParensAsReadonlyOperand = needsParensAsPostfixOperand;\n\nfunction writeTypeIn(\n writer: TextWriter,\n type: IrType,\n needsParens: (type: IrType) => boolean,\n): void {\n if (!needsParens(type)) {\n writeType(writer, type);\n return;\n }\n writer.write(\"(\");\n writeType(writer, type);\n writer.write(\")\");\n}\n\n/** Write a single-line list: `separator` between items, nothing for an empty list. */\nfunction writeInlineList<T>(\n writer: TextWriter,\n items: readonly T[],\n separator: string,\n writeItem: (item: T) => void,\n) {\n items.forEach((item, index) => {\n if (index > 0) {\n writer.write(separator);\n }\n writeItem(item);\n });\n}\n\n/**\n * Write a multi-line list the way the TypeScript printer does: a line break after the opening\n * token, one item per indented line, `delimiter` after every item but the last, and a line break\n * before the closing token. An empty list prints the line break only when `isLineBreakWhenEmpty`\n * (interfaces, enums and tuples do; object type literals print `{}`).\n */\nexport function writeMultiLineList<T>(\n writer: TextWriter,\n items: readonly T[],\n format: { readonly delimiter: string; readonly isLineBreakWhenEmpty: boolean },\n writeItem: (item: T) => void,\n): void {\n if (items.length === 0) {\n if (format.isLineBreakWhenEmpty) {\n writer.writeLine();\n }\n return;\n }\n writer.writeLine();\n writer.increaseIndent();\n items.forEach((item, index) => {\n if (index > 0) {\n writer.write(format.delimiter);\n writer.writeLine();\n }\n writeItem(item);\n });\n writer.decreaseIndent();\n writer.writeLine();\n}\n\nfunction writePropertyName(writer: TextWriter, name: IrPropertyName): void {\n switch (name.kind) {\n case \"identifier\":\n case \"number\":\n writer.write(name.text);\n break;\n case \"string\":\n writer.write(printEscapedString(name.value));\n break;\n default:\n assertNever(name);\n }\n}\n\n/**\n * `[name: type]`. TypeScript prints index signature parameters as an indented list, so a multi-line\n * parameter type (an object or tuple) is indented one level deeper than the member.\n */\nfunction writeIndexSignatureParameter(writer: TextWriter, name: string, type: IrType): void {\n writer.write(`[${name}: `);\n writer.increaseIndent();\n writeType(writer, type);\n writer.decreaseIndent();\n writer.write(\"]\");\n}\n\n/** Write one member of an interface or object type, with its comment and trailing semicolon. */\nexport function writeMember(writer: TextWriter, member: IrMember): void {\n for (const comment of member.comments) {\n writeLeadingComment(writer, comment);\n }\n if (member.isReadonly) {\n writer.write(\"readonly \");\n }\n if (member.kind === \"property\") {\n writePropertyName(writer, member.name);\n writer.write(member.isOptional ? \"?: \" : \": \");\n } else {\n writeIndexSignatureParameter(writer, member.parameterName, member.parameterType);\n writer.write(\": \");\n }\n writeType(writer, member.type);\n writer.write(\";\");\n}\n\nfunction writeTupleElement(writer: TextWriter, element: IrTupleElement): void {\n if (element.isRest) {\n writer.write(\"...\");\n }\n if (element.isOptional) {\n // TypeScript parenthesizes an optional element as a postfix operand (`(A | B)?`).\n writeTypeIn(writer, element.type, needsParensAsPostfixOperand);\n writer.write(\"?\");\n return;\n }\n writeType(writer, element.type);\n}\n\nfunction writeObjectType(writer: TextWriter, members: readonly IrMember[]): void {\n writer.write(\"{\");\n writeMultiLineList(writer, members, { delimiter: \"\", isLineBreakWhenEmpty: false }, (member) =>\n writeMember(writer, member),\n );\n writer.write(\"}\");\n}\n\nfunction writeTupleType(writer: TextWriter, elements: readonly IrTupleElement[]): void {\n writer.write(\"[\");\n writeMultiLineList(writer, elements, { delimiter: \",\", isLineBreakWhenEmpty: true }, (element) =>\n writeTupleElement(writer, element),\n );\n writer.write(\"]\");\n}\n\nfunction writeReferenceType(\n writer: TextWriter,\n name: string,\n typeArguments: readonly IrType[],\n): void {\n writer.write(name);\n if (typeArguments.length > 0) {\n writer.write(\"<\");\n writeInlineList(writer, typeArguments, \", \", (argument) => writeType(writer, argument));\n writer.write(\">\");\n }\n}\n\nfunction writeIndexedAccessType(writer: TextWriter, type: IrIndexedAccessType): void {\n writeTypeIn(writer, type.object, needsParensAsPostfixOperand);\n writer.write(\"[\");\n writeType(writer, type.index);\n writer.write(\"]\");\n}\n\nfunction writeConstituents(writer: TextWriter, types: readonly IrType[], separator: string): void {\n writeInlineList(writer, types, separator, (member) =>\n writeTypeIn(writer, member, needsParensAsConstituent),\n );\n}\n\ntype IrLeafType = Extract<\n IrType,\n { kind: \"keyword\" | \"stringLiteral\" | \"numberLiteral\" | \"booleanLiteral\" }\n>;\n\nfunction printLeafType(type: IrLeafType): string {\n switch (type.kind) {\n case \"keyword\":\n return type.keyword;\n case \"stringLiteral\":\n return printStringLiteral(type.value, type.encoding ?? DEFAULT_STRING_ENCODING);\n case \"numberLiteral\":\n return printNumberLiteral(type.value);\n case \"booleanLiteral\":\n return String(type.value);\n default:\n return assertNever(type);\n }\n}\n\n/**\n * Raw text is printed as if it started at column 0, so its continuation lines get the current\n * indentation, as the TypeScript printer indents a nested node.\n */\nfunction writeRawType(writer: TextWriter, text: string): void {\n text.split(\"\\n\").forEach((line, index) => {\n if (index > 0) {\n writer.rawWrite(writer.options.newLine);\n }\n writer.write(line);\n });\n}\n\nfunction writeArrayType(writer: TextWriter, element: IrType): void {\n writeTypeIn(writer, element, needsParensAsPostfixOperand);\n writer.write(\"[]\");\n}\n\nfunction writeReadonlyType(writer: TextWriter, operand: IrType): void {\n writer.write(\"readonly \");\n writeTypeIn(writer, operand, needsParensAsReadonlyOperand);\n}\n\ntype IrOperatorType = Extract<IrType, { kind: \"array\" | \"indexedAccess\" | \"readonly\" }>;\n\n/** Types built with a type operator: `T[]`, `T[K]` and `readonly T`. */\nfunction writeOperatorType(writer: TextWriter, type: IrOperatorType): void {\n if (type.kind === \"array\") {\n writeArrayType(writer, type.element);\n } else if (type.kind === \"indexedAccess\") {\n writeIndexedAccessType(writer, type);\n } else {\n writeReadonlyType(writer, type.type);\n }\n}\n\nconst CONSTITUENT_SEPARATORS = { union: \" | \", intersection: \" & \" } as const;\n\n/** A raw type may span lines and is re-indented; any other leaf prints on one line. */\nfunction writeLeafType(writer: TextWriter, type: IrLeafType | IrRawType): void {\n if (type.kind === \"raw\") {\n writeRawType(writer, type.text);\n } else {\n writer.write(printLeafType(type));\n }\n}\n\n/** Write a type at the writer's current position. */\nexport function writeType(writer: TextWriter, type: IrType): void {\n switch (type.kind) {\n case \"keyword\":\n case \"stringLiteral\":\n case \"numberLiteral\":\n case \"booleanLiteral\":\n case \"raw\":\n writeLeafType(writer, type);\n break;\n case \"reference\":\n writeReferenceType(writer, type.name, type.typeArguments);\n break;\n case \"array\":\n case \"indexedAccess\":\n case \"readonly\":\n writeOperatorType(writer, type);\n break;\n case \"union\":\n case \"intersection\":\n writeConstituents(writer, type.types, CONSTITUENT_SEPARATORS[type.kind]);\n break;\n case \"object\":\n writeObjectType(writer, type.members);\n break;\n case \"tuple\":\n writeTupleType(writer, type.elements);\n break;\n default:\n assertNever(type);\n }\n}\n"],"names":["printEscapedString","assertNever","writeLeadingComment","printStringLiteral","DEFAULT_STRING_ENCODING","printNumberLiteral"],"mappings":";;;;;;;AAmBA,MAAM,wBAAwB,CAAC,IAAA,KAC7B,KAAK,IAAA,KAAS,OAAA,IAAW,KAAK,IAAA,KAAS,cAAA;AAEzC,MAAM,wBAAA,GAA2B,qBAAA;AAEjC,MAAM,8BAA8B,CAAC,IAAA,KACnC,KAAK,IAAA,KAAS,UAAA,IAAc,sBAAsB,IAAI,CAAA;AAExD,MAAM,4BAAA,GAA+B,2BAAA;AAErC,SAAS,WAAA,CACP,MAAA,EACA,IAAA,EACA,WAAA,EACM;AACN,EAAA,IAAI,CAAC,WAAA,CAAY,IAAI,CAAA,EAAG;AACtB,IAAA,SAAA,CAAU,QAAQ,IAAI,CAAA;AACtB,IAAA;AAAA,EACF;AACA,EAAA,MAAA,CAAO,MAAM,GAAG,CAAA;AAChB,EAAA,SAAA,CAAU,QAAQ,IAAI,CAAA;AACtB,EAAA,MAAA,CAAO,MAAM,GAAG,CAAA;AAClB;AAGA,SAAS,eAAA,CACP,MAAA,EACA,KAAA,EACA,SAAA,EACA,SAAA,EACA;AACA,EAAA,KAAA,CAAM,OAAA,CAAQ,CAAC,IAAA,EAAM,KAAA,KAAU;AAC7B,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AA,EAAQ,IAAA,CAAK,IAAA,EAAM,IAAA,CAAK,aAAa,CAAA;AACxD,MAAA;AAAA,IACF,KAAK,OAAA;AAAA,IACL,KAAK,eAAA;AAAA,IACL,KAAK,UAAA;AACH,MAAA,iBAAA,CAAkB,QAAQ,IAAI,CAAA;AAC9B,MAAA;AAAA,IACF,KAAK,OAAA;AAAA,IACL,KAAK,cAAA;AACH,MAAA,iBAAA,CAAkB,QAAQ,IAAA,CAAK,KAAA,EAAO,sBAAA,CAAuB,IAAA,CAAK,IAAI,CAAC,CAAA;AACvE,MAAA;AAAA,IACF,KAAK,QAAA;AACH,MAAA,eAAA,CAAgB,MAAA,EAAQ,KAAK,OAAO,CAAA;AACpC,MAAA;AAAA,IACF,KAAK,OAAA;AACH,MAAA,cAAA,CAAe,MAAA,EAAQ,KAAK,QAAQ,CAAA;AACpC,MAAA;AAAA,IACF;AACE,MAAAA,uBAAA,CAAY,IAAI,CAAA;AAAA;AAEtB;;;;"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { assertNever } from '../../domain/assert-never.mjs';
|
|
2
|
+
import { DEFAULT_STRING_ENCODING } from '../../domain/ir/constructors.mjs';
|
|
2
3
|
import { writeLeadingComment } from './comment-writer.mjs';
|
|
3
4
|
import { printEscapedString, printNumberLiteral, printStringLiteral } from './literal-text.mjs';
|
|
4
5
|
|
|
@@ -137,7 +138,7 @@ function printLeafType(type) {
|
|
|
137
138
|
case "keyword":
|
|
138
139
|
return type.keyword;
|
|
139
140
|
case "stringLiteral":
|
|
140
|
-
return printStringLiteral(type.value, type.encoding);
|
|
141
|
+
return printStringLiteral(type.value, type.encoding ?? DEFAULT_STRING_ENCODING);
|
|
141
142
|
case "numberLiteral":
|
|
142
143
|
return printNumberLiteral(type.value);
|
|
143
144
|
case "booleanLiteral":
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type-printer.mjs","sources":["../../../src/application/print/type-printer.ts"],"sourcesContent":["import { assertNever } from \"../../domain/assert-never.js\";\nimport type {\n IrIndexedAccessType,\n IrMember,\n IrPropertyName,\n IrRawType,\n IrTupleElement,\n IrType,\n} from \"../../domain/ir/nodes.js\";\nimport { writeLeadingComment } from \"./comment-writer.js\";\nimport { printEscapedString, printNumberLiteral, printStringLiteral } from \"./literal-text.js\";\nimport type { TextWriter } from \"./text-writer.js\";\n\n/**\n * Parenthesization contexts. Each predicate says when a child type needs parentheses; together they\n * reproduce the TypeScript factory's parenthesizer rules for the node kinds of the IR, so output\n * matches the original even when unions are nested (`A | (B | C)`).\n */\nconst isUnionOrIntersection = (type: IrType): boolean =>\n type.kind === \"union\" || type.kind === \"intersection\";\n/** Union and intersection members (`parenthesizeConstituentTypeOf{Union,Intersection}Type`). */\nconst needsParensAsConstituent = isUnionOrIntersection;\n/** Array element and indexed-access object (`parenthesizeNonArrayTypeOfPostfixType`). */\nconst needsParensAsPostfixOperand = (type: IrType): boolean =>\n type.kind === \"readonly\" || isUnionOrIntersection(type);\n/** Operand of `readonly` (`parenthesizeOperandOfReadonlyTypeOperator`). */\nconst needsParensAsReadonlyOperand = needsParensAsPostfixOperand;\n\nfunction writeTypeIn(\n writer: TextWriter,\n type: IrType,\n needsParens: (type: IrType) => boolean,\n): void {\n if (!needsParens(type)) {\n writeType(writer, type);\n return;\n }\n writer.write(\"(\");\n writeType(writer, type);\n writer.write(\")\");\n}\n\n/** Write a single-line list: `separator` between items, nothing for an empty list. */\nfunction writeInlineList<T>(\n writer: TextWriter,\n items: readonly T[],\n separator: string,\n writeItem: (item: T) => void,\n) {\n items.forEach((item, index) => {\n if (index > 0) {\n writer.write(separator);\n }\n writeItem(item);\n });\n}\n\n/**\n * Write a multi-line list the way the TypeScript printer does: a line break after the opening\n * token, one item per indented line, `delimiter` after every item but the last, and a line break\n * before the closing token. An empty list prints the line break only when `isLineBreakWhenEmpty`\n * (interfaces, enums and tuples do; object type literals print `{}`).\n */\nexport function writeMultiLineList<T>(\n writer: TextWriter,\n items: readonly T[],\n format: { readonly delimiter: string; readonly isLineBreakWhenEmpty: boolean },\n writeItem: (item: T) => void,\n): void {\n if (items.length === 0) {\n if (format.isLineBreakWhenEmpty) {\n writer.writeLine();\n }\n return;\n }\n writer.writeLine();\n writer.increaseIndent();\n items.forEach((item, index) => {\n if (index > 0) {\n writer.write(format.delimiter);\n writer.writeLine();\n }\n writeItem(item);\n });\n writer.decreaseIndent();\n writer.writeLine();\n}\n\nfunction writePropertyName(writer: TextWriter, name: IrPropertyName): void {\n switch (name.kind) {\n case \"identifier\":\n case \"number\":\n writer.write(name.text);\n break;\n case \"string\":\n writer.write(printEscapedString(name.value));\n break;\n default:\n assertNever(name);\n }\n}\n\n/**\n * `[name: type]`. TypeScript prints index signature parameters as an indented list, so a multi-line\n * parameter type (an object or tuple) is indented one level deeper than the member.\n */\nfunction writeIndexSignatureParameter(writer: TextWriter, name: string, type: IrType): void {\n writer.write(`[${name}: `);\n writer.increaseIndent();\n writeType(writer, type);\n writer.decreaseIndent();\n writer.write(\"]\");\n}\n\n/** Write one member of an interface or object type, with its comment and trailing semicolon. */\nexport function writeMember(writer: TextWriter, member: IrMember): void {\n for (const comment of member.comments) {\n writeLeadingComment(writer, comment);\n }\n if (member.isReadonly) {\n writer.write(\"readonly \");\n }\n if (member.kind === \"property\") {\n writePropertyName(writer, member.name);\n writer.write(member.isOptional ? \"?: \" : \": \");\n } else {\n writeIndexSignatureParameter(writer, member.parameterName, member.parameterType);\n writer.write(\": \");\n }\n writeType(writer, member.type);\n writer.write(\";\");\n}\n\nfunction writeTupleElement(writer: TextWriter, element: IrTupleElement): void {\n if (element.isRest) {\n writer.write(\"...\");\n }\n if (element.isOptional) {\n // TypeScript parenthesizes an optional element as a postfix operand (`(A | B)?`).\n writeTypeIn(writer, element.type, needsParensAsPostfixOperand);\n writer.write(\"?\");\n return;\n }\n writeType(writer, element.type);\n}\n\nfunction writeObjectType(writer: TextWriter, members: readonly IrMember[]): void {\n writer.write(\"{\");\n writeMultiLineList(writer, members, { delimiter: \"\", isLineBreakWhenEmpty: false }, (member) =>\n writeMember(writer, member),\n );\n writer.write(\"}\");\n}\n\nfunction writeTupleType(writer: TextWriter, elements: readonly IrTupleElement[]): void {\n writer.write(\"[\");\n writeMultiLineList(writer, elements, { delimiter: \",\", isLineBreakWhenEmpty: true }, (element) =>\n writeTupleElement(writer, element),\n );\n writer.write(\"]\");\n}\n\nfunction writeReferenceType(\n writer: TextWriter,\n name: string,\n typeArguments: readonly IrType[],\n): void {\n writer.write(name);\n if (typeArguments.length > 0) {\n writer.write(\"<\");\n writeInlineList(writer, typeArguments, \", \", (argument) => writeType(writer, argument));\n writer.write(\">\");\n }\n}\n\nfunction writeIndexedAccessType(writer: TextWriter, type: IrIndexedAccessType): void {\n writeTypeIn(writer, type.object, needsParensAsPostfixOperand);\n writer.write(\"[\");\n writeType(writer, type.index);\n writer.write(\"]\");\n}\n\nfunction writeConstituents(writer: TextWriter, types: readonly IrType[], separator: string): void {\n writeInlineList(writer, types, separator, (member) =>\n writeTypeIn(writer, member, needsParensAsConstituent),\n );\n}\n\ntype IrLeafType = Extract<\n IrType,\n { kind: \"keyword\" | \"stringLiteral\" | \"numberLiteral\" | \"booleanLiteral\" }\n>;\n\nfunction printLeafType(type: IrLeafType): string {\n switch (type.kind) {\n case \"keyword\":\n return type.keyword;\n case \"stringLiteral\":\n return printStringLiteral(type.value, type.encoding);\n case \"numberLiteral\":\n return printNumberLiteral(type.value);\n case \"booleanLiteral\":\n return String(type.value);\n default:\n return assertNever(type);\n }\n}\n\n/**\n * Raw text is printed as if it started at column 0, so its continuation lines get the current\n * indentation, as the TypeScript printer indents a nested node.\n */\nfunction writeRawType(writer: TextWriter, text: string): void {\n text.split(\"\\n\").forEach((line, index) => {\n if (index > 0) {\n writer.rawWrite(writer.options.newLine);\n }\n writer.write(line);\n });\n}\n\nfunction writeArrayType(writer: TextWriter, element: IrType): void {\n writeTypeIn(writer, element, needsParensAsPostfixOperand);\n writer.write(\"[]\");\n}\n\nfunction writeReadonlyType(writer: TextWriter, operand: IrType): void {\n writer.write(\"readonly \");\n writeTypeIn(writer, operand, needsParensAsReadonlyOperand);\n}\n\ntype IrOperatorType = Extract<IrType, { kind: \"array\" | \"indexedAccess\" | \"readonly\" }>;\n\n/** Types built with a type operator: `T[]`, `T[K]` and `readonly T`. */\nfunction writeOperatorType(writer: TextWriter, type: IrOperatorType): void {\n if (type.kind === \"array\") {\n writeArrayType(writer, type.element);\n } else if (type.kind === \"indexedAccess\") {\n writeIndexedAccessType(writer, type);\n } else {\n writeReadonlyType(writer, type.type);\n }\n}\n\nconst CONSTITUENT_SEPARATORS = { union: \" | \", intersection: \" & \" } as const;\n\n/** A raw type may span lines and is re-indented; any other leaf prints on one line. */\nfunction writeLeafType(writer: TextWriter, type: IrLeafType | IrRawType): void {\n if (type.kind === \"raw\") {\n writeRawType(writer, type.text);\n } else {\n writer.write(printLeafType(type));\n }\n}\n\n/** Write a type at the writer's current position. */\nexport function writeType(writer: TextWriter, type: IrType): void {\n switch (type.kind) {\n case \"keyword\":\n case \"stringLiteral\":\n case \"numberLiteral\":\n case \"booleanLiteral\":\n case \"raw\":\n writeLeafType(writer, type);\n break;\n case \"reference\":\n writeReferenceType(writer, type.name, type.typeArguments);\n break;\n case \"array\":\n case \"indexedAccess\":\n case \"readonly\":\n writeOperatorType(writer, type);\n break;\n case \"union\":\n case \"intersection\":\n writeConstituents(writer, type.types, CONSTITUENT_SEPARATORS[type.kind]);\n break;\n case \"object\":\n writeObjectType(writer, type.members);\n break;\n case \"tuple\":\n writeTupleType(writer, type.elements);\n break;\n default:\n assertNever(type);\n }\n}\n"],"names":[],"mappings":";;;;AAkBA,MAAM,wBAAwB,CAAC,IAAA,KAC7B,KAAK,IAAA,KAAS,OAAA,IAAW,KAAK,IAAA,KAAS,cAAA;AAEzC,MAAM,wBAAA,GAA2B,qBAAA;AAEjC,MAAM,8BAA8B,CAAC,IAAA,KACnC,KAAK,IAAA,KAAS,UAAA,IAAc,sBAAsB,IAAI,CAAA;A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AACvB,IAAA,YAAA,CAAa,MAAA,EAAQ,KAAK,IAAI,CAAA;AAAA,EAChC,CAAA,MAAO;AACL,IAAA,MAAA,CAAO,KAAA,CAAM,aAAA,CAAc,IAAI,CAAC,CAAA;AAAA,EAClC;AACF;AAGO,SAAS,SAAA,CAAU,QAAoB,IAAA,EAAoB;AAChE,EAAA,QAAQ,KAAK,IAAA;AAAM,IACjB,KAAK,SAAA;AAAA,IACL,KAAK,eAAA;AAAA,IACL,KAAK,eAAA;AAAA,IACL,KAAK,gBAAA;AAAA,IACL,KAAK,KAAA;AACH,MAAA,aAAA,CAAc,QAAQ,IAAI,CAAA;AAC1B,MAAA;AAAA,IACF,KAAK,WAAA;AACH,MAAA,kBAAA,CAAmB,MAAA,EAAQ,IAAA,CAAK,IAAA,EAAM,IAAA,CAAK,aAAa,CAAA;AACxD,MAAA;AAAA,IACF,KAAK,OAAA;AAAA,IACL,KAAK,eAAA;AAAA,IACL,KAAK,UAAA;AACH,MAAA,iBAAA,CAAkB,QAAQ,IAAI,CAAA;AAC9B,MAAA;AAAA,IACF,KAAK,OAAA;AAAA,IACL,KAAK,cAAA;AACH,MAAA,iBAAA,CAAkB,QAAQ,IAAA,CAAK,KAAA,EAAO,sBAAA,CAAuB,IAAA,CAAK,IAAI,CAAC,CAAA;AACvE,MAAA;AAAA,IACF,KAAK,QAAA;AACH,MAAA,eAAA,CAAgB,MAAA,EAAQ,KAAK,OAAO,CAAA;AACpC,MAAA;AAAA,IACF,KAAK,OAAA;AACH,MAAA,cAAA,CAAe,MAAA,EAAQ,KAAK,QAAQ,CAAA;AACpC,MAAA;AAAA,IACF;AACE,MAAA,WAAA,CAAY,IAAI,CAAA;AAAA;AAEtB;;"}
|
|
1
|
+
{"version":3,"file":"type-printer.mjs","sources":["../../../src/application/print/type-printer.ts"],"sourcesContent":["import { assertNever } from \"../../domain/assert-never.js\";\nimport { DEFAULT_STRING_ENCODING } from \"../../domain/ir/constructors.js\";\nimport type {\n IrIndexedAccessType,\n IrMember,\n IrPropertyName,\n IrRawType,\n IrTupleElement,\n IrType,\n} from \"../../domain/ir/nodes.js\";\nimport { writeLeadingComment } from \"./comment-writer.js\";\nimport { printEscapedString, printNumberLiteral, printStringLiteral } from \"./literal-text.js\";\nimport type { TextWriter } from \"./text-writer.js\";\n\n/**\n * Parenthesization contexts. Each predicate says when a child type needs parentheses; together they\n * reproduce the TypeScript factory's parenthesizer rules for the node kinds of the IR, so output\n * matches the original even when unions are nested (`A | (B | C)`).\n */\nconst isUnionOrIntersection = (type: IrType): boolean =>\n type.kind === \"union\" || type.kind === \"intersection\";\n/** Union and intersection members (`parenthesizeConstituentTypeOf{Union,Intersection}Type`). */\nconst needsParensAsConstituent = isUnionOrIntersection;\n/** Array element and indexed-access object (`parenthesizeNonArrayTypeOfPostfixType`). */\nconst needsParensAsPostfixOperand = (type: IrType): boolean =>\n type.kind === \"readonly\" || isUnionOrIntersection(type);\n/** Operand of `readonly` (`parenthesizeOperandOfReadonlyTypeOperator`). */\nconst needsParensAsReadonlyOperand = needsParensAsPostfixOperand;\n\nfunction writeTypeIn(\n writer: TextWriter,\n type: IrType,\n needsParens: (type: IrType) => boolean,\n): void {\n if (!needsParens(type)) {\n writeType(writer, type);\n return;\n }\n writer.write(\"(\");\n writeType(writer, type);\n writer.write(\")\");\n}\n\n/** Write a single-line list: `separator` between items, nothing for an empty list. */\nfunction writeInlineList<T>(\n writer: TextWriter,\n items: readonly T[],\n separator: string,\n writeItem: (item: T) => void,\n) {\n items.forEach((item, index) => {\n if (index > 0) {\n writer.write(separator);\n }\n writeItem(item);\n });\n}\n\n/**\n * Write a multi-line list the way the TypeScript printer does: a line break after the opening\n * token, one item per indented line, `delimiter` after every item but the last, and a line break\n * before the closing token. An empty list prints the line break only when `isLineBreakWhenEmpty`\n * (interfaces, enums and tuples do; object type literals print `{}`).\n */\nexport function writeMultiLineList<T>(\n writer: TextWriter,\n items: readonly T[],\n format: { readonly delimiter: string; readonly isLineBreakWhenEmpty: boolean },\n writeItem: (item: T) => void,\n): void {\n if (items.length === 0) {\n if (format.isLineBreakWhenEmpty) {\n writer.writeLine();\n }\n return;\n }\n writer.writeLine();\n writer.increaseIndent();\n items.forEach((item, index) => {\n if (index > 0) {\n writer.write(format.delimiter);\n writer.writeLine();\n }\n writeItem(item);\n });\n writer.decreaseIndent();\n writer.writeLine();\n}\n\nfunction writePropertyName(writer: TextWriter, name: IrPropertyName): void {\n switch (name.kind) {\n case \"identifier\":\n case \"number\":\n writer.write(name.text);\n break;\n case \"string\":\n writer.write(printEscapedString(name.value));\n break;\n default:\n assertNever(name);\n }\n}\n\n/**\n * `[name: type]`. TypeScript prints index signature parameters as an indented list, so a multi-line\n * parameter type (an object or tuple) is indented one level deeper than the member.\n */\nfunction writeIndexSignatureParameter(writer: TextWriter, name: string, type: IrType): void {\n writer.write(`[${name}: `);\n writer.increaseIndent();\n writeType(writer, type);\n writer.decreaseIndent();\n writer.write(\"]\");\n}\n\n/** Write one member of an interface or object type, with its comment and trailing semicolon. */\nexport function writeMember(writer: TextWriter, member: IrMember): void {\n for (const comment of member.comments) {\n writeLeadingComment(writer, comment);\n }\n if (member.isReadonly) {\n writer.write(\"readonly \");\n }\n if (member.kind === \"property\") {\n writePropertyName(writer, member.name);\n writer.write(member.isOptional ? \"?: \" : \": \");\n } else {\n writeIndexSignatureParameter(writer, member.parameterName, member.parameterType);\n writer.write(\": \");\n }\n writeType(writer, member.type);\n writer.write(\";\");\n}\n\nfunction writeTupleElement(writer: TextWriter, element: IrTupleElement): void {\n if (element.isRest) {\n writer.write(\"...\");\n }\n if (element.isOptional) {\n // TypeScript parenthesizes an optional element as a postfix operand (`(A | B)?`).\n writeTypeIn(writer, element.type, needsParensAsPostfixOperand);\n writer.write(\"?\");\n return;\n }\n writeType(writer, element.type);\n}\n\nfunction writeObjectType(writer: TextWriter, members: readonly IrMember[]): void {\n writer.write(\"{\");\n writeMultiLineList(writer, members, { delimiter: \"\", isLineBreakWhenEmpty: false }, (member) =>\n writeMember(writer, member),\n );\n writer.write(\"}\");\n}\n\nfunction writeTupleType(writer: TextWriter, elements: readonly IrTupleElement[]): void {\n writer.write(\"[\");\n writeMultiLineList(writer, elements, { delimiter: \",\", isLineBreakWhenEmpty: true }, (element) =>\n writeTupleElement(writer, element),\n );\n writer.write(\"]\");\n}\n\nfunction writeReferenceType(\n writer: TextWriter,\n name: string,\n typeArguments: readonly IrType[],\n): void {\n writer.write(name);\n if (typeArguments.length > 0) {\n writer.write(\"<\");\n writeInlineList(writer, typeArguments, \", \", (argument) => writeType(writer, argument));\n writer.write(\">\");\n }\n}\n\nfunction writeIndexedAccessType(writer: TextWriter, type: IrIndexedAccessType): void {\n writeTypeIn(writer, type.object, needsParensAsPostfixOperand);\n writer.write(\"[\");\n writeType(writer, type.index);\n writer.write(\"]\");\n}\n\nfunction writeConstituents(writer: TextWriter, types: readonly IrType[], separator: string): void {\n writeInlineList(writer, types, separator, (member) =>\n writeTypeIn(writer, member, needsParensAsConstituent),\n );\n}\n\ntype IrLeafType = Extract<\n IrType,\n { kind: \"keyword\" | \"stringLiteral\" | \"numberLiteral\" | \"booleanLiteral\" }\n>;\n\nfunction printLeafType(type: IrLeafType): string {\n switch (type.kind) {\n case \"keyword\":\n return type.keyword;\n case \"stringLiteral\":\n return printStringLiteral(type.value, type.encoding ?? DEFAULT_STRING_ENCODING);\n case \"numberLiteral\":\n return printNumberLiteral(type.value);\n case \"booleanLiteral\":\n return String(type.value);\n default:\n return assertNever(type);\n }\n}\n\n/**\n * Raw text is printed as if it started at column 0, so its continuation lines get the current\n * indentation, as the TypeScript printer indents a nested node.\n */\nfunction writeRawType(writer: TextWriter, text: string): void {\n text.split(\"\\n\").forEach((line, index) => {\n if (index > 0) {\n writer.rawWrite(writer.options.newLine);\n }\n writer.write(line);\n });\n}\n\nfunction writeArrayType(writer: TextWriter, element: IrType): void {\n writeTypeIn(writer, element, needsParensAsPostfixOperand);\n writer.write(\"[]\");\n}\n\nfunction writeReadonlyType(writer: TextWriter, operand: IrType): void {\n writer.write(\"readonly \");\n writeTypeIn(writer, operand, needsParensAsReadonlyOperand);\n}\n\ntype IrOperatorType = Extract<IrType, { kind: \"array\" | \"indexedAccess\" | \"readonly\" }>;\n\n/** Types built with a type operator: `T[]`, `T[K]` and `readonly T`. */\nfunction writeOperatorType(writer: TextWriter, type: IrOperatorType): void {\n if (type.kind === \"array\") {\n writeArrayType(writer, type.element);\n } else if (type.kind === \"indexedAccess\") {\n writeIndexedAccessType(writer, type);\n } else {\n writeReadonlyType(writer, type.type);\n }\n}\n\nconst CONSTITUENT_SEPARATORS = { union: \" | \", intersection: \" & \" } as const;\n\n/** A raw type may span lines and is re-indented; any other leaf prints on one line. */\nfunction writeLeafType(writer: TextWriter, type: IrLeafType | IrRawType): void {\n if (type.kind === \"raw\") {\n writeRawType(writer, type.text);\n } else {\n writer.write(printLeafType(type));\n }\n}\n\n/** Write a type at the writer's current position. */\nexport function writeType(writer: TextWriter, type: IrType): void {\n switch (type.kind) {\n case \"keyword\":\n case \"stringLiteral\":\n case \"numberLiteral\":\n case \"booleanLiteral\":\n case \"raw\":\n writeLeafType(writer, type);\n break;\n case \"reference\":\n writeReferenceType(writer, type.name, type.typeArguments);\n break;\n case \"array\":\n case \"indexedAccess\":\n case \"readonly\":\n writeOperatorType(writer, type);\n break;\n case \"union\":\n case \"intersection\":\n writeConstituents(writer, type.types, CONSTITUENT_SEPARATORS[type.kind]);\n break;\n case \"object\":\n writeObjectType(writer, type.members);\n break;\n case \"tuple\":\n writeTupleType(writer, type.elements);\n break;\n default:\n assertNever(type);\n }\n}\n"],"names":[],"mappings":";;;;;AAmBA,MAAM,wBAAwB,CAAC,IAAA,KAC7B,KAAK,IAAA,KAAS,OAAA,IAAW,KAAK,IAAA,KAAS,cAAA;AAEzC,MAAM,wBAAA,GAA2B,qBAAA;AAEjC,MAAM,8BAA8B,CAAC,IAAA,KACnC,KAAK,IAAA,KAAS,UAAA,IAAc,sBAAsB,IAAI,CAAA;AAExD,MAAM,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B,IAAA,YAAA,CAAa,MAAA,EAAQ,KAAK,IAAI,CAAA;AAAA,EAChC,CAAA,MAAO;AACL,IAAA,MAAA,CAAO,KAAA,CAAM,aAAA,CAAc,IAAI,CAAC,CAAA;AAAA,EAClC;AACF;AAGO,SAAS,SAAA,CAAU,QAAoB,IAAA,EAAoB;AAChE,EAAA,QAAQ,KAAK,IAAA;AAAM,IACjB,KAAK,SAAA;AAAA,IACL,KAAK,eAAA;AAAA,IACL,KAAK,eAAA;AAAA,IACL,KAAK,gBAAA;AAAA,IACL,KAAK,KAAA;AACH,MAAA,aAAA,CAAc,QAAQ,IAAI,CAAA;AAC1B,MAAA;AAAA,IACF,KAAK,WAAA;AACH,MAAA,kBAAA,CAAmB,MAAA,EAAQ,IAAA,CAAK,IAAA,EAAM,IAAA,CAAK,aAAa,CAAA;AACxD,MAAA;AAAA,IACF,KAAK,OAAA;AAAA,IACL,KAAK,eAAA;AAAA,IACL,KAAK,UAAA;AACH,MAAA,iBAAA,CAAkB,QAAQ,IAAI,CAAA;AAC9B,MAAA;AAAA,IACF,KAAK,OAAA;AAAA,IACL,KAAK,cAAA;AACH,MAAA,iBAAA,CAAkB,QAAQ,IAAA,CAAK,KAAA,EAAO,sBAAA,CAAuB,IAAA,CAAK,IAAI,CAAC,CAAA;AACvE,MAAA;AAAA,IACF,KAAK,QAAA;AACH,MAAA,eAAA,CAAgB,MAAA,EAAQ,KAAK,OAAO,CAAA;AACpC,MAAA;AAAA,IACF,KAAK,OAAA;AACH,MAAA,cAAA,CAAe,MAAA,EAAQ,KAAK,QAAQ,CAAA;AACpC,MAAA;AAAA,IACF;AACE,MAAA,WAAA,CAAY,IAAI,CAAA;AAAA;AAEtB;;"}
|
|
@@ -10,7 +10,7 @@ const schemaComment = require('../schema-comment.cjs');
|
|
|
10
10
|
const explodedQuery = require('./exploded-query.cjs');
|
|
11
11
|
const parameter = require('./parameter.cjs');
|
|
12
12
|
|
|
13
|
-
const PATH_TEMPLATE_RE = /\{([^}]+)\}/g;
|
|
13
|
+
const PATH_TEMPLATE_RE = /\{([^{}]+)\}/g;
|
|
14
14
|
function resolveParameter(parameter, context) {
|
|
15
15
|
return "$ref" in parameter ? context.refs.resolve(parameter.$ref) : parameter;
|
|
16
16
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parameters.cjs","sources":["../../../../src/application/transform/document/parameters.ts"],"sourcesContent":["import {\n irIntersection,\n irKeyword,\n irObject,\n irProperty,\n irReference,\n} from \"../../../domain/ir/constructors.js\";\nimport type { IrPropertyName, IrPropertySignature, IrType } from \"../../../domain/ir/nodes.js\";\nimport { toPropertyName } from \"../../../domain/ir/property-name.js\";\nimport { createRef } from \"../../../domain/openapi/json-pointer.js\";\nimport {\n PARAMETER_LOCATIONS,\n type ParameterLocation,\n type ParameterObject,\n type ParameterOrReference,\n QUERYSTRING_LOCATION,\n} from \"../../../domain/openapi/parameter-object.js\";\nimport type { SchemaObject } from \"../../../domain/openapi/schema-object.js\";\nimport { OutputFix } from \"../../../domain/output-fixes.js\";\nimport { refType } from \"../ref-type.js\";\nimport type { SchemaLocation } from \"../schema/schema-context.js\";\nimport { withSchemaComment } from \"../schema-comment.js\";\nimport type { TransformContext } from \"../transform-context.js\";\nimport { isExplodedQueryObject } from \"./exploded-query.js\";\nimport { parameterType } from \"./parameter.js\";\n\nconst PATH_TEMPLATE_RE = /\\{([^}]+)\\}/g;\n\n/** A parameter as written in the document, and the object its `$ref` points to. */\ninterface ParameterEntry {\n readonly original: ParameterOrReference;\n readonly resolved: ParameterObject | undefined;\n}\n\n/** An entry whose `$ref` resolved, so its location and name are known. */\ntype ResolvedEntry = ParameterEntry & { readonly resolved: ParameterObject };\n\nfunction resolveParameter(\n parameter: ParameterOrReference,\n context: TransformContext,\n): ParameterObject | undefined {\n return \"$ref\" in parameter\n ? (context.refs.resolve(parameter.$ref) as ParameterObject | undefined)\n : parameter;\n}\n\n/** Required string parameters for every `{name}` in a path template. */\nfunction pathTemplateParameters(path: string): ParameterObject[] {\n return (path.match(PATH_TEMPLATE_RE) ?? []).map((match) => ({\n name: match.slice(1, -1),\n in: \"path\",\n required: true,\n schema: { type: \"string\" },\n }));\n}\n\n/** `--generate-path-params`: add the path template parameters the list does not define. */\nfunction withPathTemplateParameters(\n parameters: readonly ParameterOrReference[],\n location: SchemaLocation,\n context: TransformContext,\n): ParameterOrReference[] {\n const result = [...parameters];\n if (!context.options.generatePathParams || !location.path) {\n return result;\n }\n for (const parameter of pathTemplateParameters(location.path)) {\n const isDefined = result.some((candidate) => {\n const resolved = resolveParameter(candidate, context);\n return resolved?.in === \"path\" && resolved.name === parameter.name;\n });\n if (!isDefined) {\n result.push(parameter);\n }\n }\n return result;\n}\n\nfunction isDeprecatedParameter(parameter: ParameterObject | undefined): boolean {\n const schema = parameter?.schema as SchemaObject | undefined;\n return Boolean(parameter?.deprecated || schema?.deprecated);\n}\n\n/** Resolved parameters, sorted for `--alphabetize`, without deprecated ones if excluded. */\nfunction parameterEntries(\n parameters: readonly ParameterOrReference[],\n context: TransformContext,\n): ParameterEntry[] {\n const entries = parameters.map((original) => ({\n original,\n resolved: resolveParameter(original, context),\n }));\n if (context.options.alphabetize) {\n // Unlike map keys, the original sorts parameters with the default locale's `localeCompare`.\n entries.sort((left, right) =>\n (left.resolved?.name ?? \"\").localeCompare(right.resolved?.name ?? \"\"),\n );\n }\n return context.options.excludeDeprecated\n ? entries.filter(({ resolved }) => !isDeprecatedParameter(resolved))\n : entries;\n}\n\n/** The original quotes a name that is not text (a missing name prints as `\"undefined\"`). */\nfunction parameterName(name: unknown): IrPropertyName {\n return typeof name === \"string\" || typeof name === \"number\"\n ? toPropertyName(name)\n : { kind: \"string\", value: String(name) };\n}\n\nfunction referencedParameterType(ref: string, resolved: ParameterObject): IrType {\n // A `$ref` that resolves to another `$ref` is not a parameter object yet: index it as a path.\n const isParameter = !(resolved as { $ref?: unknown }).$ref;\n const parameter = isParameter ? { in: resolved.in, name: resolved.name } : null;\n return refType(ref, { parameter });\n}\n\nfunction entryType(\n { original, resolved }: ResolvedEntry,\n location: SchemaLocation,\n context: TransformContext,\n): IrType {\n const path = createRef([location.path, \"parameters\", resolved.in, resolved.name]);\n return \"$ref\" in original\n ? referencedParameterType(original.$ref, resolved)\n : parameterType(resolved, { ...location, path }, context);\n}\n\nfunction parameterMember(\n entry: ResolvedEntry,\n location: SchemaLocation,\n context: TransformContext,\n): IrPropertySignature {\n const { resolved } = entry;\n const property = irProperty(parameterName(resolved.name), entryType(entry, location, context), {\n isOptional: resolved.in !== \"path\" && !resolved.required,\n isReadonly: context.options.immutable,\n });\n return withSchemaComment(property, resolved);\n}\n\n/**\n * The `query` member typed by an `in: querystring` parameter (OpenAPI 3.2, ADR-020): the parameter\n * is the whole query string. Null when there is none, or when `in: query` parameters sit next to\n * it, which 3.2 does not allow; those keep their types, and a warning names the parameter.\n */\nfunction querystringMember(\n entries: readonly ParameterEntry[],\n location: SchemaLocation,\n context: TransformContext,\n): IrPropertySignature | null {\n const querystring = entries.find(\n (entry): entry is ResolvedEntry => entry.resolved?.in === QUERYSTRING_LOCATION,\n );\n if (!querystring || entries.some((entry) => entry.resolved?.in === \"query\")) {\n return null;\n }\n const property = irProperty(toPropertyName(\"query\"), entryType(querystring, location, context), {\n isOptional: !querystring.resolved.required,\n isReadonly: context.options.immutable,\n });\n return withSchemaComment(property, querystring.resolved);\n}\n\n/** With `exploded-query-objects`, whether the parameter's object keys are keys of `query`. */\nfunction isExplodedEntry(entry: ResolvedEntry, context: TransformContext): boolean {\n return (\n context.options.fixes.has(OutputFix.EXPLODED_QUERY_OBJECTS) &&\n isExplodedQueryObject(entry.resolved, context.refs)\n );\n}\n\n/**\n * An exploded object parameter's part of `query`: its object type. A parameter that may be left\n * out sends none of its keys then, so they are optional: `Partial<…>`.\n */\nfunction explodedPart(\n entry: ResolvedEntry,\n location: SchemaLocation,\n context: TransformContext,\n): IrType {\n const type = entryType(entry, location, context);\n return entry.resolved.required ? type : irReference(\"Partial\", [type]);\n}\n\n/** A location's parameters as members, and the object types of its exploded parameters. */\ninterface LocationParts {\n readonly members: readonly IrPropertySignature[];\n readonly exploded: readonly IrType[];\n readonly isOptional: boolean;\n}\n\n/** Each parameter in document order, so the declarations they add keep their order. */\nfunction locationParts(\n entries: readonly ResolvedEntry[],\n location: SchemaLocation,\n context: TransformContext,\n): LocationParts {\n const members: IrPropertySignature[] = [];\n const exploded: IrType[] = [];\n let isOptional = true;\n for (const entry of entries) {\n if (isExplodedEntry(entry, context)) {\n exploded.push(explodedPart(entry, location, context));\n isOptional &&= !entry.resolved.required;\n continue;\n }\n const member = parameterMember(entry, location, context);\n members.push(member);\n isOptional &&= member.isOptional;\n }\n return { members, exploded, isOptional };\n}\n\n/** The members' object, intersected with the exploded parameters' types; `never` for none. */\nfunction locationType({ members, exploded }: LocationParts): IrType {\n const types = [...(members.length > 0 ? [irObject(members)] : []), ...exploded];\n const [first] = types;\n if (first === undefined) {\n return irKeyword(\"never\");\n }\n return types.length === 1 ? first : irIntersection(types);\n}\n\n/** One location's parameters; optional when all of them are, and `never` when there are none. */\nfunction locationMember(\n parameterIn: ParameterLocation,\n entries: readonly ParameterEntry[],\n location: SchemaLocation,\n context: TransformContext,\n): IrPropertySignature {\n const located = entries.filter(\n (entry): entry is ResolvedEntry => entry.resolved?.in === parameterIn,\n );\n const parts = locationParts(located, location, context);\n return irProperty(toPropertyName(parameterIn), locationType(parts), {\n isOptional: parts.isOptional,\n isReadonly: context.options.immutable,\n });\n}\n\n/**\n * The `parameters` property of an operation or path item: one member per location, each an object\n * of that location's parameters. `location.path` is the path template for\n * `--generate-path-params`.\n */\nexport function parametersMember(\n parameters: readonly ParameterOrReference[],\n location: SchemaLocation,\n context: TransformContext,\n): IrPropertySignature {\n const entries = parameterEntries(\n withPathTemplateParameters(parameters, location, context),\n context,\n );\n const querystring = querystringMember(entries, location, context);\n const members = PARAMETER_LOCATIONS.map((parameterIn) =>\n parameterIn === \"query\" && querystring\n ? querystring\n : locationMember(parameterIn, entries, location, context),\n );\n return irProperty(toPropertyName(\"parameters\"), irObject(members), {\n isReadonly: context.options.immutable,\n });\n}\n"],"names":["toPropertyName","refType","createRef","parameterType","irProperty","withSchemaComment","QUERYSTRING_LOCATION","OutputFix","isExplodedQueryObject","irReference","irObject","irKeyword","irIntersection","PARAMETER_LOCATIONS"],"mappings":";;;;;;;;;;;;AA0BA,MAAM,gBAAA,GAAmB,cAAA;AAWzB,SAAS,gBAAA,CACP,WACA,OAAA,EAC6B;AAC7B,EAAA,OAAO,UAAU,SAAA,GACZ,OAAA,CAAQ,KAAK,OAAA,CAAQ,SAAA,CAAU,IAAI,CAAA,GACpC,SAAA;AACN;AAGA,SAAS,uBAAuB,IAAA,EAAiC;AAC/D,EAAA,OAAA,CAAQ,IAAA,CAAK,MAAM,gBAAgB,CAAA,IAAK,EAAC,EAAG,GAAA,CAAI,CAAC,KAAA,MAAW;AAAA,IAC1D,IAAA,EAAM,KAAA,CAAM,KAAA,CAAM,CAAA,EAAG,EAAE,CAAA;AAAA,IACvB,EAAA,EAAI,MAAA;AAAA,IACJ,QAAA,EAAU,IAAA;AAAA,IACV,MAAA,EAAQ,EAAE,IAAA,EAAM,QAAA;AAAS,GAC3B,CAAE,CAAA;AACJ;AAGA,SAAS,0BAAA,CACP,UAAA,EACA,QAAA,EACA,OAAA,EACwB;AACxB,EAAA,MAAM,MAAA,GAAS,CAAC,GAAG,UAAU,CAAA;AAC7B,EAAA,IAAI,CAAC,OAAA,CAAQ,OAAA,CAAQ,kBAAA,IAAsB,CAAC,SAAS,IAAA,EAAM;AACzD,IAAA,OAAO,MAAA;AAAA,EACT;AACA,EAAA,KAAA,MAAW,SAAA,IAAa,sBAAA,CAAuB,QAAA,CAAS,IAAI,CAAA,EAAG;AAC7D,IAAA,MAAM,SAAA,GAAY,MAAA,CAAO,IAAA,CAAK,CAAC,SAAA,KAAc;AAC3C,MAAA,MAAM,QAAA,GAAW,gBAAA,CAAiB,SAAA,EAAW,OAAO,CAAA;AACpD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AAe,WAAW,CAAA,EAAG,YAAA,CAAa,KAAK,CAAA,EAAG;AAAA,IAClE,YAAY,KAAA,CAAM,UAAA;AAAA,IAClB,UAAA,EAAY,QAAQ,OAAA,CAAQ;AAAA,GAC7B,CAAA;AACH;AAOO,SAAS,gBAAA,CACd,UAAA,EACA,QAAA,EACA,OAAA,EACqB;AACrB,EAAA,MAAM,OAAA,GAAU,gBAAA;AAAA,IACd,0BAAA,CAA2B,UAAA,EAAY,QAAA,EAAU,OAAO,CAAA;AAAA,IACxD;AAAA,GACF;AACA,EAAA,MAAM,WAAA,GAAc,iBAAA,CAAkB,OAAA,EAAS,QAAA,EAAU,OAAO,CAAA;AAChE,EAAA,MAAM,UAAUa,mCAAA,CAAoB,GAAA;AAAA,IAAI,CAAC,WAAA,KACvC,WAAA,KAAgB,OAAA,IAAW,WAAA,GACvB,cACA,cAAA,CAAe,WAAA,EAAa,OAAA,EAAS,QAAA,EAAU,OAAO;AAAA,GAC5D;AACA,EAAA,OAAOT,wBAAWJ,2BAAA,CAAe,YAAY,CAAA,EAAGU,qBAAA,CAAS,OAAO,CAAA,EAAG;AAAA,IACjE,UAAA,EAAY,QAAQ,OAAA,CAAQ;AAAA,GAC7B,CAAA;AACH;;"}
|
|
1
|
+
{"version":3,"file":"parameters.cjs","sources":["../../../../src/application/transform/document/parameters.ts"],"sourcesContent":["import {\n irIntersection,\n irKeyword,\n irObject,\n irProperty,\n irReference,\n} from \"../../../domain/ir/constructors.js\";\nimport type { IrPropertyName, IrPropertySignature, IrType } from \"../../../domain/ir/nodes.js\";\nimport { toPropertyName } from \"../../../domain/ir/property-name.js\";\nimport { createRef } from \"../../../domain/openapi/json-pointer.js\";\nimport {\n PARAMETER_LOCATIONS,\n type ParameterLocation,\n type ParameterObject,\n type ParameterOrReference,\n QUERYSTRING_LOCATION,\n} from \"../../../domain/openapi/parameter-object.js\";\nimport type { SchemaObject } from \"../../../domain/openapi/schema-object.js\";\nimport { OutputFix } from \"../../../domain/output-fixes.js\";\nimport { refType } from \"../ref-type.js\";\nimport type { SchemaLocation } from \"../schema/schema-context.js\";\nimport { withSchemaComment } from \"../schema-comment.js\";\nimport type { TransformContext } from \"../transform-context.js\";\nimport { isExplodedQueryObject } from \"./exploded-query.js\";\nimport { parameterType } from \"./parameter.js\";\n\n/** `[^{}]` keeps the scan linear: `[^}]` retried from every `{` of a long run of them. */\nconst PATH_TEMPLATE_RE = /\\{([^{}]+)\\}/g;\n\n/** A parameter as written in the document, and the object its `$ref` points to. */\ninterface ParameterEntry {\n readonly original: ParameterOrReference;\n readonly resolved: ParameterObject | undefined;\n}\n\n/** An entry whose `$ref` resolved, so its location and name are known. */\ntype ResolvedEntry = ParameterEntry & { readonly resolved: ParameterObject };\n\nfunction resolveParameter(\n parameter: ParameterOrReference,\n context: TransformContext,\n): ParameterObject | undefined {\n return \"$ref\" in parameter\n ? (context.refs.resolve(parameter.$ref) as ParameterObject | undefined)\n : parameter;\n}\n\n/** Required string parameters for every `{name}` in a path template. */\nfunction pathTemplateParameters(path: string): ParameterObject[] {\n return (path.match(PATH_TEMPLATE_RE) ?? []).map((match) => ({\n name: match.slice(1, -1),\n in: \"path\",\n required: true,\n schema: { type: \"string\" },\n }));\n}\n\n/** `--generate-path-params`: add the path template parameters the list does not define. */\nfunction withPathTemplateParameters(\n parameters: readonly ParameterOrReference[],\n location: SchemaLocation,\n context: TransformContext,\n): ParameterOrReference[] {\n const result = [...parameters];\n if (!context.options.generatePathParams || !location.path) {\n return result;\n }\n for (const parameter of pathTemplateParameters(location.path)) {\n const isDefined = result.some((candidate) => {\n const resolved = resolveParameter(candidate, context);\n return resolved?.in === \"path\" && resolved.name === parameter.name;\n });\n if (!isDefined) {\n result.push(parameter);\n }\n }\n return result;\n}\n\nfunction isDeprecatedParameter(parameter: ParameterObject | undefined): boolean {\n const schema = parameter?.schema as SchemaObject | undefined;\n return Boolean(parameter?.deprecated || schema?.deprecated);\n}\n\n/** Resolved parameters, sorted for `--alphabetize`, without deprecated ones if excluded. */\nfunction parameterEntries(\n parameters: readonly ParameterOrReference[],\n context: TransformContext,\n): ParameterEntry[] {\n const entries = parameters.map((original) => ({\n original,\n resolved: resolveParameter(original, context),\n }));\n if (context.options.alphabetize) {\n // Unlike map keys, the original sorts parameters with the default locale's `localeCompare`.\n entries.sort((left, right) =>\n (left.resolved?.name ?? \"\").localeCompare(right.resolved?.name ?? \"\"),\n );\n }\n return context.options.excludeDeprecated\n ? entries.filter(({ resolved }) => !isDeprecatedParameter(resolved))\n : entries;\n}\n\n/** The original quotes a name that is not text (a missing name prints as `\"undefined\"`). */\nfunction parameterName(name: unknown): IrPropertyName {\n return typeof name === \"string\" || typeof name === \"number\"\n ? toPropertyName(name)\n : { kind: \"string\", value: String(name) };\n}\n\nfunction referencedParameterType(ref: string, resolved: ParameterObject): IrType {\n // A `$ref` that resolves to another `$ref` is not a parameter object yet: index it as a path.\n const isParameter = !(resolved as { $ref?: unknown }).$ref;\n const parameter = isParameter ? { in: resolved.in, name: resolved.name } : null;\n return refType(ref, { parameter });\n}\n\nfunction entryType(\n { original, resolved }: ResolvedEntry,\n location: SchemaLocation,\n context: TransformContext,\n): IrType {\n const path = createRef([location.path, \"parameters\", resolved.in, resolved.name]);\n return \"$ref\" in original\n ? referencedParameterType(original.$ref, resolved)\n : parameterType(resolved, { ...location, path }, context);\n}\n\nfunction parameterMember(\n entry: ResolvedEntry,\n location: SchemaLocation,\n context: TransformContext,\n): IrPropertySignature {\n const { resolved } = entry;\n const property = irProperty(parameterName(resolved.name), entryType(entry, location, context), {\n isOptional: resolved.in !== \"path\" && !resolved.required,\n isReadonly: context.options.immutable,\n });\n return withSchemaComment(property, resolved);\n}\n\n/**\n * The `query` member typed by an `in: querystring` parameter (OpenAPI 3.2, ADR-020): the parameter\n * is the whole query string. Null when there is none, or when `in: query` parameters sit next to\n * it, which 3.2 does not allow; those keep their types, and a warning names the parameter.\n */\nfunction querystringMember(\n entries: readonly ParameterEntry[],\n location: SchemaLocation,\n context: TransformContext,\n): IrPropertySignature | null {\n const querystring = entries.find(\n (entry): entry is ResolvedEntry => entry.resolved?.in === QUERYSTRING_LOCATION,\n );\n if (!querystring || entries.some((entry) => entry.resolved?.in === \"query\")) {\n return null;\n }\n const property = irProperty(toPropertyName(\"query\"), entryType(querystring, location, context), {\n isOptional: !querystring.resolved.required,\n isReadonly: context.options.immutable,\n });\n return withSchemaComment(property, querystring.resolved);\n}\n\n/** With `exploded-query-objects`, whether the parameter's object keys are keys of `query`. */\nfunction isExplodedEntry(entry: ResolvedEntry, context: TransformContext): boolean {\n return (\n context.options.fixes.has(OutputFix.EXPLODED_QUERY_OBJECTS) &&\n isExplodedQueryObject(entry.resolved, context.refs)\n );\n}\n\n/**\n * An exploded object parameter's part of `query`: its object type. A parameter that may be left\n * out sends none of its keys then, so they are optional: `Partial<…>`.\n */\nfunction explodedPart(\n entry: ResolvedEntry,\n location: SchemaLocation,\n context: TransformContext,\n): IrType {\n const type = entryType(entry, location, context);\n return entry.resolved.required ? type : irReference(\"Partial\", [type]);\n}\n\n/** A location's parameters as members, and the object types of its exploded parameters. */\ninterface LocationParts {\n readonly members: readonly IrPropertySignature[];\n readonly exploded: readonly IrType[];\n readonly isOptional: boolean;\n}\n\n/** Each parameter in document order, so the declarations they add keep their order. */\nfunction locationParts(\n entries: readonly ResolvedEntry[],\n location: SchemaLocation,\n context: TransformContext,\n): LocationParts {\n const members: IrPropertySignature[] = [];\n const exploded: IrType[] = [];\n let isOptional = true;\n for (const entry of entries) {\n if (isExplodedEntry(entry, context)) {\n exploded.push(explodedPart(entry, location, context));\n isOptional &&= !entry.resolved.required;\n continue;\n }\n const member = parameterMember(entry, location, context);\n members.push(member);\n isOptional &&= member.isOptional;\n }\n return { members, exploded, isOptional };\n}\n\n/** The members' object, intersected with the exploded parameters' types; `never` for none. */\nfunction locationType({ members, exploded }: LocationParts): IrType {\n const types = [...(members.length > 0 ? [irObject(members)] : []), ...exploded];\n const [first] = types;\n if (first === undefined) {\n return irKeyword(\"never\");\n }\n return types.length === 1 ? first : irIntersection(types);\n}\n\n/** One location's parameters; optional when all of them are, and `never` when there are none. */\nfunction locationMember(\n parameterIn: ParameterLocation,\n entries: readonly ParameterEntry[],\n location: SchemaLocation,\n context: TransformContext,\n): IrPropertySignature {\n const located = entries.filter(\n (entry): entry is ResolvedEntry => entry.resolved?.in === parameterIn,\n );\n const parts = locationParts(located, location, context);\n return irProperty(toPropertyName(parameterIn), locationType(parts), {\n isOptional: parts.isOptional,\n isReadonly: context.options.immutable,\n });\n}\n\n/**\n * The `parameters` property of an operation or path item: one member per location, each an object\n * of that location's parameters. `location.path` is the path template for\n * `--generate-path-params`.\n */\nexport function parametersMember(\n parameters: readonly ParameterOrReference[],\n location: SchemaLocation,\n context: TransformContext,\n): IrPropertySignature {\n const entries = parameterEntries(\n withPathTemplateParameters(parameters, location, context),\n context,\n );\n const querystring = querystringMember(entries, location, context);\n const members = PARAMETER_LOCATIONS.map((parameterIn) =>\n parameterIn === \"query\" && querystring\n ? querystring\n : locationMember(parameterIn, entries, location, context),\n );\n return irProperty(toPropertyName(\"parameters\"), irObject(members), {\n isReadonly: context.options.immutable,\n });\n}\n"],"names":["toPropertyName","refType","createRef","parameterType","irProperty","withSchemaComment","QUERYSTRING_LOCATION","OutputFix","isExplodedQueryObject","irReference","irObject","irKeyword","irIntersection","PARAMETER_LOCATIONS"],"mappings":";;;;;;;;;;;;AA2BA,MAAM,gBAAA,GAAmB,eAAA;AAWzB,SAAS,gBAAA,CACP,WACA,OAAA,EAC6B;AAC7B,EAAA,OAAO,UAAU,SAAA,GACZ,OAAA,CAAQ,KAAK,OAAA,CAAQ,SAAA,CAAU,IAAI,CAAA,GACpC,SAAA;AACN;AAGA,SAAS,uBAAuB,IAAA,EAAiC;AAC/D,EAAA,OAAA,CAAQ,IAAA,CAAK,MAAM,gBAAgB,CAAA,IAAK,EAAC,EAAG,GAAA,CAAI,CAAC,KAAA,MAAW;AAAA,IAC1D,IAAA,EAAM,KAAA,CAAM,KAA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AC5D;AACA,EAAA,OAAOT,wBAAWJ,2BAAA,CAAe,YAAY,CAAA,EAAGU,qBAAA,CAAS,OAAO,CAAA,EAAG;AAAA,IACjE,UAAA,EAAY,QAAQ,OAAA,CAAQ;AAAA,GAC7B,CAAA;AACH;;"}
|
|
@@ -8,7 +8,7 @@ import { withSchemaComment } from '../schema-comment.mjs';
|
|
|
8
8
|
import { isExplodedQueryObject } from './exploded-query.mjs';
|
|
9
9
|
import { parameterType } from './parameter.mjs';
|
|
10
10
|
|
|
11
|
-
const PATH_TEMPLATE_RE = /\{([^}]+)\}/g;
|
|
11
|
+
const PATH_TEMPLATE_RE = /\{([^{}]+)\}/g;
|
|
12
12
|
function resolveParameter(parameter, context) {
|
|
13
13
|
return "$ref" in parameter ? context.refs.resolve(parameter.$ref) : parameter;
|
|
14
14
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parameters.mjs","sources":["../../../../src/application/transform/document/parameters.ts"],"sourcesContent":["import {\n irIntersection,\n irKeyword,\n irObject,\n irProperty,\n irReference,\n} from \"../../../domain/ir/constructors.js\";\nimport type { IrPropertyName, IrPropertySignature, IrType } from \"../../../domain/ir/nodes.js\";\nimport { toPropertyName } from \"../../../domain/ir/property-name.js\";\nimport { createRef } from \"../../../domain/openapi/json-pointer.js\";\nimport {\n PARAMETER_LOCATIONS,\n type ParameterLocation,\n type ParameterObject,\n type ParameterOrReference,\n QUERYSTRING_LOCATION,\n} from \"../../../domain/openapi/parameter-object.js\";\nimport type { SchemaObject } from \"../../../domain/openapi/schema-object.js\";\nimport { OutputFix } from \"../../../domain/output-fixes.js\";\nimport { refType } from \"../ref-type.js\";\nimport type { SchemaLocation } from \"../schema/schema-context.js\";\nimport { withSchemaComment } from \"../schema-comment.js\";\nimport type { TransformContext } from \"../transform-context.js\";\nimport { isExplodedQueryObject } from \"./exploded-query.js\";\nimport { parameterType } from \"./parameter.js\";\n\nconst PATH_TEMPLATE_RE = /\\{([^}]+)\\}/g;\n\n/** A parameter as written in the document, and the object its `$ref` points to. */\ninterface ParameterEntry {\n readonly original: ParameterOrReference;\n readonly resolved: ParameterObject | undefined;\n}\n\n/** An entry whose `$ref` resolved, so its location and name are known. */\ntype ResolvedEntry = ParameterEntry & { readonly resolved: ParameterObject };\n\nfunction resolveParameter(\n parameter: ParameterOrReference,\n context: TransformContext,\n): ParameterObject | undefined {\n return \"$ref\" in parameter\n ? (context.refs.resolve(parameter.$ref) as ParameterObject | undefined)\n : parameter;\n}\n\n/** Required string parameters for every `{name}` in a path template. */\nfunction pathTemplateParameters(path: string): ParameterObject[] {\n return (path.match(PATH_TEMPLATE_RE) ?? []).map((match) => ({\n name: match.slice(1, -1),\n in: \"path\",\n required: true,\n schema: { type: \"string\" },\n }));\n}\n\n/** `--generate-path-params`: add the path template parameters the list does not define. */\nfunction withPathTemplateParameters(\n parameters: readonly ParameterOrReference[],\n location: SchemaLocation,\n context: TransformContext,\n): ParameterOrReference[] {\n const result = [...parameters];\n if (!context.options.generatePathParams || !location.path) {\n return result;\n }\n for (const parameter of pathTemplateParameters(location.path)) {\n const isDefined = result.some((candidate) => {\n const resolved = resolveParameter(candidate, context);\n return resolved?.in === \"path\" && resolved.name === parameter.name;\n });\n if (!isDefined) {\n result.push(parameter);\n }\n }\n return result;\n}\n\nfunction isDeprecatedParameter(parameter: ParameterObject | undefined): boolean {\n const schema = parameter?.schema as SchemaObject | undefined;\n return Boolean(parameter?.deprecated || schema?.deprecated);\n}\n\n/** Resolved parameters, sorted for `--alphabetize`, without deprecated ones if excluded. */\nfunction parameterEntries(\n parameters: readonly ParameterOrReference[],\n context: TransformContext,\n): ParameterEntry[] {\n const entries = parameters.map((original) => ({\n original,\n resolved: resolveParameter(original, context),\n }));\n if (context.options.alphabetize) {\n // Unlike map keys, the original sorts parameters with the default locale's `localeCompare`.\n entries.sort((left, right) =>\n (left.resolved?.name ?? \"\").localeCompare(right.resolved?.name ?? \"\"),\n );\n }\n return context.options.excludeDeprecated\n ? entries.filter(({ resolved }) => !isDeprecatedParameter(resolved))\n : entries;\n}\n\n/** The original quotes a name that is not text (a missing name prints as `\"undefined\"`). */\nfunction parameterName(name: unknown): IrPropertyName {\n return typeof name === \"string\" || typeof name === \"number\"\n ? toPropertyName(name)\n : { kind: \"string\", value: String(name) };\n}\n\nfunction referencedParameterType(ref: string, resolved: ParameterObject): IrType {\n // A `$ref` that resolves to another `$ref` is not a parameter object yet: index it as a path.\n const isParameter = !(resolved as { $ref?: unknown }).$ref;\n const parameter = isParameter ? { in: resolved.in, name: resolved.name } : null;\n return refType(ref, { parameter });\n}\n\nfunction entryType(\n { original, resolved }: ResolvedEntry,\n location: SchemaLocation,\n context: TransformContext,\n): IrType {\n const path = createRef([location.path, \"parameters\", resolved.in, resolved.name]);\n return \"$ref\" in original\n ? referencedParameterType(original.$ref, resolved)\n : parameterType(resolved, { ...location, path }, context);\n}\n\nfunction parameterMember(\n entry: ResolvedEntry,\n location: SchemaLocation,\n context: TransformContext,\n): IrPropertySignature {\n const { resolved } = entry;\n const property = irProperty(parameterName(resolved.name), entryType(entry, location, context), {\n isOptional: resolved.in !== \"path\" && !resolved.required,\n isReadonly: context.options.immutable,\n });\n return withSchemaComment(property, resolved);\n}\n\n/**\n * The `query` member typed by an `in: querystring` parameter (OpenAPI 3.2, ADR-020): the parameter\n * is the whole query string. Null when there is none, or when `in: query` parameters sit next to\n * it, which 3.2 does not allow; those keep their types, and a warning names the parameter.\n */\nfunction querystringMember(\n entries: readonly ParameterEntry[],\n location: SchemaLocation,\n context: TransformContext,\n): IrPropertySignature | null {\n const querystring = entries.find(\n (entry): entry is ResolvedEntry => entry.resolved?.in === QUERYSTRING_LOCATION,\n );\n if (!querystring || entries.some((entry) => entry.resolved?.in === \"query\")) {\n return null;\n }\n const property = irProperty(toPropertyName(\"query\"), entryType(querystring, location, context), {\n isOptional: !querystring.resolved.required,\n isReadonly: context.options.immutable,\n });\n return withSchemaComment(property, querystring.resolved);\n}\n\n/** With `exploded-query-objects`, whether the parameter's object keys are keys of `query`. */\nfunction isExplodedEntry(entry: ResolvedEntry, context: TransformContext): boolean {\n return (\n context.options.fixes.has(OutputFix.EXPLODED_QUERY_OBJECTS) &&\n isExplodedQueryObject(entry.resolved, context.refs)\n );\n}\n\n/**\n * An exploded object parameter's part of `query`: its object type. A parameter that may be left\n * out sends none of its keys then, so they are optional: `Partial<…>`.\n */\nfunction explodedPart(\n entry: ResolvedEntry,\n location: SchemaLocation,\n context: TransformContext,\n): IrType {\n const type = entryType(entry, location, context);\n return entry.resolved.required ? type : irReference(\"Partial\", [type]);\n}\n\n/** A location's parameters as members, and the object types of its exploded parameters. */\ninterface LocationParts {\n readonly members: readonly IrPropertySignature[];\n readonly exploded: readonly IrType[];\n readonly isOptional: boolean;\n}\n\n/** Each parameter in document order, so the declarations they add keep their order. */\nfunction locationParts(\n entries: readonly ResolvedEntry[],\n location: SchemaLocation,\n context: TransformContext,\n): LocationParts {\n const members: IrPropertySignature[] = [];\n const exploded: IrType[] = [];\n let isOptional = true;\n for (const entry of entries) {\n if (isExplodedEntry(entry, context)) {\n exploded.push(explodedPart(entry, location, context));\n isOptional &&= !entry.resolved.required;\n continue;\n }\n const member = parameterMember(entry, location, context);\n members.push(member);\n isOptional &&= member.isOptional;\n }\n return { members, exploded, isOptional };\n}\n\n/** The members' object, intersected with the exploded parameters' types; `never` for none. */\nfunction locationType({ members, exploded }: LocationParts): IrType {\n const types = [...(members.length > 0 ? [irObject(members)] : []), ...exploded];\n const [first] = types;\n if (first === undefined) {\n return irKeyword(\"never\");\n }\n return types.length === 1 ? first : irIntersection(types);\n}\n\n/** One location's parameters; optional when all of them are, and `never` when there are none. */\nfunction locationMember(\n parameterIn: ParameterLocation,\n entries: readonly ParameterEntry[],\n location: SchemaLocation,\n context: TransformContext,\n): IrPropertySignature {\n const located = entries.filter(\n (entry): entry is ResolvedEntry => entry.resolved?.in === parameterIn,\n );\n const parts = locationParts(located, location, context);\n return irProperty(toPropertyName(parameterIn), locationType(parts), {\n isOptional: parts.isOptional,\n isReadonly: context.options.immutable,\n });\n}\n\n/**\n * The `parameters` property of an operation or path item: one member per location, each an object\n * of that location's parameters. `location.path` is the path template for\n * `--generate-path-params`.\n */\nexport function parametersMember(\n parameters: readonly ParameterOrReference[],\n location: SchemaLocation,\n context: TransformContext,\n): IrPropertySignature {\n const entries = parameterEntries(\n withPathTemplateParameters(parameters, location, context),\n context,\n );\n const querystring = querystringMember(entries, location, context);\n const members = PARAMETER_LOCATIONS.map((parameterIn) =>\n parameterIn === \"query\" && querystring\n ? querystring\n : locationMember(parameterIn, entries, location, context),\n );\n return irProperty(toPropertyName(\"parameters\"), irObject(members), {\n isReadonly: context.options.immutable,\n });\n}\n"],"names":[],"mappings":";;;;;;;;;;AA0BA,MAAM,gBAAA,GAAmB,cAAA;AAWzB,SAAS,gBAAA,CACP,WACA,OAAA,EAC6B;AAC7B,EAAA,OAAO,UAAU,SAAA,GACZ,OAAA,CAAQ,KAAK,OAAA,CAAQ,SAAA,CAAU,IAAI,CAAA,GACpC,SAAA;AACN;AAGA,SAAS,uBAAuB,IAAA,EAAiC;AAC/D,EAAA,OAAA,CAAQ,IAAA,CAAK,MAAM,gBAAgB,CAAA,IAAK,EAAC,EAAG,GAAA,CAAI,CAAC,KAAA,MAAW;AAAA,IAC1D,IAAA,EAAM,KAAA,CAAM,KAAA,CAAM,CAAA,EAAG,EAAE,CAAA;AAAA,IACvB,EAAA,EAAI,MAAA;AAAA,IACJ,QAAA,EAAU,IAAA;AAAA,IACV,MAAA,EAAQ,EAAE,IAAA,EAAM,QAAA;AAAS,GAC3B,CAAE,CAAA;AACJ;AAGA,SAAS,0BAAA,CACP,UAAA,EACA,QAAA,EACA,OAAA,EACwB;AACxB,EAAA,MAAM,MAAA,GAAS,CAAC,GAAG,UAAU,CAAA;AAC7B,EAAA,IAAI,CAAC,OAAA,CAAQ,OAAA,CAAQ,kBAAA,IAAsB,CAAC,SAAS,IAAA,EAAM;AACzD,IAAA,OAAO,MAAA;AAA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,MAAA;AAAA,IACtB,CAAC,KAAA,KAAkC,KAAA,CAAM,QAAA,EAAU,EAAA,KAAO;AAAA,GAC5D;AACA,EAAA,MAAM,KAAA,GAAQ,aAAA,CAAc,OAAA,EAAS,QAAA,EAAU,OAAO,CAAA;AACtD,EAAA,OAAO,WAAW,cAAA,CAAe,WAAW,CAAA,EAAG,YAAA,CAAa,KAAK,CAAA,EAAG;AAAA,IAClE,YAAY,KAAA,CAAM,UAAA;AAAA,IAClB,UAAA,EAAY,QAAQ,OAAA,CAAQ;AAAA,GAC7B,CAAA;AACH;AAOO,SAAS,gBAAA,CACd,UAAA,EACA,QAAA,EACA,OAAA,EACqB;AACrB,EAAA,MAAM,OAAA,GAAU,gBAAA;AAAA,IACd,0BAAA,CAA2B,UAAA,EAAY,QAAA,EAAU,OAAO,CAAA;AAAA,IACxD;AAAA,GACF;AACA,EAAA,MAAM,WAAA,GAAc,iBAAA,CAAkB,OAAA,EAAS,QAAA,EAAU,OAAO,CAAA;AAChE,EAAA,MAAM,UAAU,mBAAA,CAAoB,GAAA;AAAA,IAAI,CAAC,WAAA,KACvC,WAAA,KAAgB,OAAA,IAAW,WAAA,GACvB,cACA,cAAA,CAAe,WAAA,EAAa,OAAA,EAAS,QAAA,EAAU,OAAO;AAAA,GAC5D;AACA,EAAA,OAAO,WAAW,cAAA,CAAe,YAAY,CAAA,EAAG,QAAA,CAAS,OAAO,CAAA,EAAG;AAAA,IACjE,UAAA,EAAY,QAAQ,OAAA,CAAQ;AAAA,GAC7B,CAAA;AACH;;"}
|
|
1
|
+
{"version":3,"file":"parameters.mjs","sources":["../../../../src/application/transform/document/parameters.ts"],"sourcesContent":["import {\n irIntersection,\n irKeyword,\n irObject,\n irProperty,\n irReference,\n} from \"../../../domain/ir/constructors.js\";\nimport type { IrPropertyName, IrPropertySignature, IrType } from \"../../../domain/ir/nodes.js\";\nimport { toPropertyName } from \"../../../domain/ir/property-name.js\";\nimport { createRef } from \"../../../domain/openapi/json-pointer.js\";\nimport {\n PARAMETER_LOCATIONS,\n type ParameterLocation,\n type ParameterObject,\n type ParameterOrReference,\n QUERYSTRING_LOCATION,\n} from \"../../../domain/openapi/parameter-object.js\";\nimport type { SchemaObject } from \"../../../domain/openapi/schema-object.js\";\nimport { OutputFix } from \"../../../domain/output-fixes.js\";\nimport { refType } from \"../ref-type.js\";\nimport type { SchemaLocation } from \"../schema/schema-context.js\";\nimport { withSchemaComment } from \"../schema-comment.js\";\nimport type { TransformContext } from \"../transform-context.js\";\nimport { isExplodedQueryObject } from \"./exploded-query.js\";\nimport { parameterType } from \"./parameter.js\";\n\n/** `[^{}]` keeps the scan linear: `[^}]` retried from every `{` of a long run of them. */\nconst PATH_TEMPLATE_RE = /\\{([^{}]+)\\}/g;\n\n/** A parameter as written in the document, and the object its `$ref` points to. */\ninterface ParameterEntry {\n readonly original: ParameterOrReference;\n readonly resolved: ParameterObject | undefined;\n}\n\n/** An entry whose `$ref` resolved, so its location and name are known. */\ntype ResolvedEntry = ParameterEntry & { readonly resolved: ParameterObject };\n\nfunction resolveParameter(\n parameter: ParameterOrReference,\n context: TransformContext,\n): ParameterObject | undefined {\n return \"$ref\" in parameter\n ? (context.refs.resolve(parameter.$ref) as ParameterObject | undefined)\n : parameter;\n}\n\n/** Required string parameters for every `{name}` in a path template. */\nfunction pathTemplateParameters(path: string): ParameterObject[] {\n return (path.match(PATH_TEMPLATE_RE) ?? []).map((match) => ({\n name: match.slice(1, -1),\n in: \"path\",\n required: true,\n schema: { type: \"string\" },\n }));\n}\n\n/** `--generate-path-params`: add the path template parameters the list does not define. */\nfunction withPathTemplateParameters(\n parameters: readonly ParameterOrReference[],\n location: SchemaLocation,\n context: TransformContext,\n): ParameterOrReference[] {\n const result = [...parameters];\n if (!context.options.generatePathParams || !location.path) {\n return result;\n }\n for (const parameter of pathTemplateParameters(location.path)) {\n const isDefined = result.some((candidate) => {\n const resolved = resolveParameter(candidate, context);\n return resolved?.in === \"path\" && resolved.name === parameter.name;\n });\n if (!isDefined) {\n result.push(parameter);\n }\n }\n return result;\n}\n\nfunction isDeprecatedParameter(parameter: ParameterObject | undefined): boolean {\n const schema = parameter?.schema as SchemaObject | undefined;\n return Boolean(parameter?.deprecated || schema?.deprecated);\n}\n\n/** Resolved parameters, sorted for `--alphabetize`, without deprecated ones if excluded. */\nfunction parameterEntries(\n parameters: readonly ParameterOrReference[],\n context: TransformContext,\n): ParameterEntry[] {\n const entries = parameters.map((original) => ({\n original,\n resolved: resolveParameter(original, context),\n }));\n if (context.options.alphabetize) {\n // Unlike map keys, the original sorts parameters with the default locale's `localeCompare`.\n entries.sort((left, right) =>\n (left.resolved?.name ?? \"\").localeCompare(right.resolved?.name ?? \"\"),\n );\n }\n return context.options.excludeDeprecated\n ? entries.filter(({ resolved }) => !isDeprecatedParameter(resolved))\n : entries;\n}\n\n/** The original quotes a name that is not text (a missing name prints as `\"undefined\"`). */\nfunction parameterName(name: unknown): IrPropertyName {\n return typeof name === \"string\" || typeof name === \"number\"\n ? toPropertyName(name)\n : { kind: \"string\", value: String(name) };\n}\n\nfunction referencedParameterType(ref: string, resolved: ParameterObject): IrType {\n // A `$ref` that resolves to another `$ref` is not a parameter object yet: index it as a path.\n const isParameter = !(resolved as { $ref?: unknown }).$ref;\n const parameter = isParameter ? { in: resolved.in, name: resolved.name } : null;\n return refType(ref, { parameter });\n}\n\nfunction entryType(\n { original, resolved }: ResolvedEntry,\n location: SchemaLocation,\n context: TransformContext,\n): IrType {\n const path = createRef([location.path, \"parameters\", resolved.in, resolved.name]);\n return \"$ref\" in original\n ? referencedParameterType(original.$ref, resolved)\n : parameterType(resolved, { ...location, path }, context);\n}\n\nfunction parameterMember(\n entry: ResolvedEntry,\n location: SchemaLocation,\n context: TransformContext,\n): IrPropertySignature {\n const { resolved } = entry;\n const property = irProperty(parameterName(resolved.name), entryType(entry, location, context), {\n isOptional: resolved.in !== \"path\" && !resolved.required,\n isReadonly: context.options.immutable,\n });\n return withSchemaComment(property, resolved);\n}\n\n/**\n * The `query` member typed by an `in: querystring` parameter (OpenAPI 3.2, ADR-020): the parameter\n * is the whole query string. Null when there is none, or when `in: query` parameters sit next to\n * it, which 3.2 does not allow; those keep their types, and a warning names the parameter.\n */\nfunction querystringMember(\n entries: readonly ParameterEntry[],\n location: SchemaLocation,\n context: TransformContext,\n): IrPropertySignature | null {\n const querystring = entries.find(\n (entry): entry is ResolvedEntry => entry.resolved?.in === QUERYSTRING_LOCATION,\n );\n if (!querystring || entries.some((entry) => entry.resolved?.in === \"query\")) {\n return null;\n }\n const property = irProperty(toPropertyName(\"query\"), entryType(querystring, location, context), {\n isOptional: !querystring.resolved.required,\n isReadonly: context.options.immutable,\n });\n return withSchemaComment(property, querystring.resolved);\n}\n\n/** With `exploded-query-objects`, whether the parameter's object keys are keys of `query`. */\nfunction isExplodedEntry(entry: ResolvedEntry, context: TransformContext): boolean {\n return (\n context.options.fixes.has(OutputFix.EXPLODED_QUERY_OBJECTS) &&\n isExplodedQueryObject(entry.resolved, context.refs)\n );\n}\n\n/**\n * An exploded object parameter's part of `query`: its object type. A parameter that may be left\n * out sends none of its keys then, so they are optional: `Partial<…>`.\n */\nfunction explodedPart(\n entry: ResolvedEntry,\n location: SchemaLocation,\n context: TransformContext,\n): IrType {\n const type = entryType(entry, location, context);\n return entry.resolved.required ? type : irReference(\"Partial\", [type]);\n}\n\n/** A location's parameters as members, and the object types of its exploded parameters. */\ninterface LocationParts {\n readonly members: readonly IrPropertySignature[];\n readonly exploded: readonly IrType[];\n readonly isOptional: boolean;\n}\n\n/** Each parameter in document order, so the declarations they add keep their order. */\nfunction locationParts(\n entries: readonly ResolvedEntry[],\n location: SchemaLocation,\n context: TransformContext,\n): LocationParts {\n const members: IrPropertySignature[] = [];\n const exploded: IrType[] = [];\n let isOptional = true;\n for (const entry of entries) {\n if (isExplodedEntry(entry, context)) {\n exploded.push(explodedPart(entry, location, context));\n isOptional &&= !entry.resolved.required;\n continue;\n }\n const member = parameterMember(entry, location, context);\n members.push(member);\n isOptional &&= member.isOptional;\n }\n return { members, exploded, isOptional };\n}\n\n/** The members' object, intersected with the exploded parameters' types; `never` for none. */\nfunction locationType({ members, exploded }: LocationParts): IrType {\n const types = [...(members.length > 0 ? [irObject(members)] : []), ...exploded];\n const [first] = types;\n if (first === undefined) {\n return irKeyword(\"never\");\n }\n return types.length === 1 ? first : irIntersection(types);\n}\n\n/** One location's parameters; optional when all of them are, and `never` when there are none. */\nfunction locationMember(\n parameterIn: ParameterLocation,\n entries: readonly ParameterEntry[],\n location: SchemaLocation,\n context: TransformContext,\n): IrPropertySignature {\n const located = entries.filter(\n (entry): entry is ResolvedEntry => entry.resolved?.in === parameterIn,\n );\n const parts = locationParts(located, location, context);\n return irProperty(toPropertyName(parameterIn), locationType(parts), {\n isOptional: parts.isOptional,\n isReadonly: context.options.immutable,\n });\n}\n\n/**\n * The `parameters` property of an operation or path item: one member per location, each an object\n * of that location's parameters. `location.path` is the path template for\n * `--generate-path-params`.\n */\nexport function parametersMember(\n parameters: readonly ParameterOrReference[],\n location: SchemaLocation,\n context: TransformContext,\n): IrPropertySignature {\n const entries = parameterEntries(\n withPathTemplateParameters(parameters, location, context),\n context,\n );\n const querystring = querystringMember(entries, location, context);\n const members = PARAMETER_LOCATIONS.map((parameterIn) =>\n parameterIn === \"query\" && querystring\n ? querystring\n : locationMember(parameterIn, entries, location, context),\n );\n return irProperty(toPropertyName(\"parameters\"), irObject(members), {\n isReadonly: context.options.immutable,\n });\n}\n"],"names":[],"mappings":";;;;;;;;;;AA2BA,MAAM,gBAAA,GAAmB,eAAA;AAWzB,SAAS,gBAAA,CACP,WACA,OAAA,EAC6B;AAC7B,EAAA,OAAO,UAAU,SAAA,GACZ,OAAA,CAAQ,KAAK,OAAA,CAAQ,SAAA,CAAU,IAAI,CAAA,GACpC,SAAA;AACN;AAGA,SAAS,uBAAuB,IAAA,EAAiC;AAC/D,EAAA,OAAA,CAAQ,IAAA,CAAK,MAAM,gBAAgB,CAAA,IAAK,EAAC,EAAG,GAAA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S,QAAA,EAAU,OAAO;AAAA,GAC5D;AACA,EAAA,OAAO,WAAW,cAAA,CAAe,YAAY,CAAA,EAAG,QAAA,CAAS,OAAO,CAAA,EAAG;AAAA,IACjE,UAAA,EAAY,QAAQ,OAAA,CAAQ;AAAA,GAC7B,CAAA;AACH;;"}
|
|
@@ -15,7 +15,7 @@ const pathKeyOverlaps = require('./path-key-overlaps.cjs');
|
|
|
15
15
|
const pathTemplateReplace = require('./path-template-replace.cjs');
|
|
16
16
|
const typeAssignability = require('./type-assignability.cjs');
|
|
17
17
|
|
|
18
|
-
const PATH_PARAMETER_RE = /\{[^}]+\}/g;
|
|
18
|
+
const PATH_PARAMETER_RE = /\{[^{}]+\}/g;
|
|
19
19
|
const PLACEHOLDERS = /* @__PURE__ */ new Map([
|
|
20
20
|
["number", "number"],
|
|
21
21
|
["integer", "number"],
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"paths.cjs","sources":["../../../../src/application/transform/document/paths.ts"],"sourcesContent":["import { irIntersection, irObject, irProperty, irRaw } from \"../../../domain/ir/constructors.js\";\nimport type { IrMember, IrType } from \"../../../domain/ir/nodes.js\";\nimport { toPropertyName } from \"../../../domain/ir/property-name.js\";\nimport { escapeTemplateText } from \"../../../domain/ir/raw-text-escapes.js\";\nimport { createRef } from \"../../../domain/openapi/json-pointer.js\";\nimport type { OperationObject } from \"../../../domain/openapi/operation-object.js\";\nimport type {\n ParameterObject,\n ParameterOrReference,\n} from \"../../../domain/openapi/parameter-object.js\";\nimport {\n type PathItemObject,\n type PathsObject,\n pathItemOperations,\n} from \"../../../domain/openapi/path-item-object.js\";\nimport type { ReferenceSchema, SchemaObject } from \"../../../domain/openapi/schema-object.js\";\nimport { OutputFix } from \"../../../domain/output-fixes.js\";\nimport { documentEntries } from \"../document-entries.js\";\nimport { refType } from \"../ref-type.js\";\nimport type { TransformContext } from \"../transform-context.js\";\nimport { type DeclaredTypeLookup, NO_DECLARED_TYPES } from \"./declared-types.js\";\nimport { documentMember } from \"./document-member.js\";\nimport { pathItemType } from \"./path-item.js\";\nimport {\n type CoverRejection,\n coveringTemplateKeys,\n type PathKey,\n type PathKeyPart,\n type PathPlaceholder,\n} from \"./path-key-overlaps.js\";\nimport {\n type PathParameterMatch,\n placeholderText,\n replacedTemplateParts,\n} from \"./path-template-replace.js\";\nimport { assignabilityProof } from \"./type-assignability.js\";\n\nconst PATH_PARAMETER_RE = /\\{[^}]+\\}/g;\n\n/** Placeholders by parameter schema type; anything else is a string placeholder. */\nconst PLACEHOLDERS: ReadonlyMap<unknown, PathPlaceholder> = new Map([\n [\"number\", \"number\"],\n [\"integer\", \"number\"],\n [\"boolean\", \"boolean\"],\n]);\nconst STRING_PLACEHOLDER: PathPlaceholder = \"string\";\n\nfunction resolvedParameter(\n parameter: ParameterOrReference,\n context: TransformContext,\n): ParameterObject | undefined {\n return \"$ref\" in parameter && parameter.$ref\n ? (context.refs.resolve(parameter.$ref) as ParameterObject | undefined)\n : (parameter as ParameterObject);\n}\n\nfunction addPathParameters(\n parameters: readonly ParameterOrReference[],\n byName: Map<string, ParameterObject>,\n context: TransformContext,\n): void {\n for (const parameter of parameters) {\n const resolved = resolvedParameter(parameter, context);\n if (resolved && resolved.in === \"path\") {\n byName.set(resolved.name, resolved);\n }\n }\n}\n\n/** Path parameters of the path item and of its operations; later definitions win. */\nfunction pathParameters(\n pathItem: PathItemObject,\n context: TransformContext,\n): Map<string, ParameterObject> {\n const byName = new Map<string, ParameterObject>();\n addPathParameters(pathItem.parameters ?? [], byName, context);\n for (const { operation: entry } of pathItemOperations(pathItem)) {\n const operation = (entry as ReferenceSchema).$ref\n ? (context.refs.resolve((entry as ReferenceSchema).$ref) as OperationObject | undefined)\n : (entry as OperationObject);\n addPathParameters(operation?.parameters ?? [], byName, context);\n }\n return byName;\n}\n\n/** Each `{name}` of the URL and its own placeholder, from the URL split at the matches. */\nfunction splitTemplateParts(url: string, matches: readonly PathParameterMatch[]): PathKeyPart[] {\n return url.split(PATH_PARAMETER_RE).flatMap((text, index): PathKeyPart[] => {\n const type = matches[index]?.type;\n const textPart: PathKeyPart = { kind: \"text\", text };\n return type === undefined ? [textPart] : [textPart, { kind: \"placeholder\", type }];\n });\n}\n\n/**\n * `--path-params-as-types`: the URL as template parts, each `{name}` replaced by a placeholder for\n * its parameter's type, the way the original replaces them unless the `path-param-type-names` fix\n * is on (see `replacedTemplateParts`). Null when the URL has no complete `{…}` placeholder.\n */\nfunction pathTemplateParts(\n url: string,\n pathItem: PathItemObject,\n context: TransformContext,\n): PathKeyPart[] | null {\n const found = url.match(PATH_PARAMETER_RE);\n if (!found) {\n return null;\n }\n const parameters = pathParameters(pathItem, context);\n const matches = found.map((match): PathParameterMatch => {\n const schema = parameters.get(match.slice(1, -1))?.schema as SchemaObject | undefined;\n return { match, type: PLACEHOLDERS.get(schema?.type) ?? STRING_PLACEHOLDER };\n });\n return context.options.fixes.has(OutputFix.PATH_PARAM_TYPE_NAMES)\n ? splitTemplateParts(url, matches)\n : replacedTemplateParts(url, matches);\n}\n\n/**\n * The template literal type of the parts. The original escaped only backticks: a backslash or\n * `${` in the URL could end the literal or open a placeholder, and add code to the generated file\n * (intentional fix). So the text parts are escaped.\n */\nfunction templateText(parts: readonly PathKeyPart[]): string {\n const text = parts\n .map((part) =>\n part.kind === \"text\" ? escapeTemplateText(part.text) : placeholderText(part.type),\n )\n .join(\"\");\n return `\\`${text}\\``;\n}\n\n/** A path's member, its key and its path item type for the overlap check. */\nexport interface PathMember {\n readonly url: string;\n readonly member: IrMember;\n readonly key: PathKey;\n readonly type: IrType;\n}\n\nfunction pathItemMember(\n url: string,\n pathItem: PathItemObject,\n context: TransformContext,\n): PathMember {\n const isReadonly = context.options.immutable;\n const type = pathItemType(pathItem, { path: createRef([\"paths\", url]) }, context);\n const parts =\n context.options.pathParamsAsTypes && url.includes(\"{\")\n ? pathTemplateParts(url, pathItem, context)\n : null;\n if (parts === null) {\n const member = irProperty(toPropertyName(url), type, { isReadonly });\n return { url, member, key: literalKey(url), type };\n }\n const member: IrMember = {\n kind: \"indexSignature\",\n parameterName: \"path\",\n parameterType: irRaw(templateText(parts)),\n type,\n isReadonly,\n comments: [],\n };\n return { url, member, key: { kind: \"template\", parts }, type };\n}\n\nfunction literalKey(url: string): PathKey {\n return { kind: \"literal\", url };\n}\n\nconst PATHS_TYPE_NAME = \"paths\";\n\n/** The URL of `paths[\"/apps/{id}\"]`, or null for any other type. */\nfunction pathsIndexOf(type: IrType): string | null {\n if (type.kind !== \"indexedAccess\" || type.index.kind !== \"stringLiteral\") {\n return null;\n }\n const { object } = type;\n const isPaths =\n object.kind === \"reference\" &&\n object.name === PATHS_TYPE_NAME &&\n object.typeArguments.length === 0;\n return isPaths ? type.index.value : null;\n}\n\n/**\n * The lookup, and `paths[\"/apps/{id}\"]` as that path's item. The declarations cannot read it: a\n * templated path is no property of `paths`, and TypeScript finds it through the signature.\n */\nfunction withPathItems(\n paths: readonly PathMember[],\n lookup: DeclaredTypeLookup,\n): DeclaredTypeLookup {\n const byUrl = new Map(paths.map((path) => [path.url, path.type]));\n return (type) => {\n const url = pathsIndexOf(type);\n return (url === null ? undefined : byUrl.get(url)) ?? lookup(type);\n };\n}\n\n/**\n * The rejection of a covered path item: only one proven not assignable to the signature's type\n * counts (ADR-028). A pair the proof cannot decide keeps the original output, which may well\n * compile (`Record<string, never>` fits `{ [key: string]: unknown }`).\n */\nfunction rejectionOf(paths: readonly PathMember[], lookup: DeclaredTypeLookup): CoverRejection {\n const relate = assignabilityProof(withPathItems(paths, lookup));\n return (coveringIndex, coveredIndex) => {\n const coveringPath = paths[coveringIndex];\n const coveredPath = paths[coveredIndex];\n return (\n coveringPath !== undefined &&\n coveredPath !== undefined &&\n relate(coveredPath.type, coveringPath.type) === \"rejected\"\n );\n };\n}\n\n/**\n * The members as one object type. When an index signature covers another key, TypeScript checks\n * the covered key's type against it. Where that check surely fails (TS2411, TS2413), each such\n * signature gets an object of its own in an intersection: an intersection does not check its parts\n * against each other, and every key has the type it had in the one object. Output that compiles\n * keeps the one object. The lookup reads the types that path items reference; without it, a\n * reference decides nothing.\n */\nexport function pathsObjectType(\n paths: readonly PathMember[],\n lookup: DeclaredTypeLookup = NO_DECLARED_TYPES,\n): IrType {\n const keys = paths.map(({ key }) => key);\n const covering = coveringTemplateKeys(keys, rejectionOf(paths, lookup));\n if (covering.size === 0) {\n return irObject(paths.map(({ member }) => member));\n }\n const rest = paths.filter((_, index) => !covering.has(index)).map(({ member }) => member);\n const own = paths\n .filter((_, index) => covering.has(index))\n .map(({ member }) => irObject([member]));\n return irIntersection([irObject(rest), ...own]);\n}\n\n/** The members of the `paths` interface (OpenAPI 3.1 §4.8.8): one per path, by URL. */\nexport function pathMembers(paths: PathsObject, context: TransformContext): PathMember[] {\n const members: PathMember[] = [];\n for (const [url, pathItem] of documentEntries(paths, context.options)) {\n if (!pathItem || typeof pathItem !== \"object\") {\n continue;\n }\n if (\"$ref\" in pathItem) {\n const options = { isReadonly: context.options.immutable };\n const member = documentMember(url, refType(pathItem.$ref), pathItem, options);\n members.push({ url, member, key: literalKey(url), type: member.type });\n continue;\n }\n const startedAt = context.diagnostics.now();\n const path = pathItemMember(url, pathItem, context);\n members.push(path);\n // The original reports only paths written as properties.\n if (path.member.kind === \"property\") {\n context.diagnostics.debug(`Transformed path \"${url}\"`, context.diagnostics.now() - startedAt);\n }\n }\n return members;\n}\n\n/**\n * The `paths` interface body, with only the path items' own types for the overlap check. The\n * document transform checks the overlaps again once the referenced types are declared.\n */\nexport function pathsType(paths: PathsObject, context: TransformContext): IrType {\n return pathsObjectType(pathMembers(paths, context));\n}\n"],"names":["pathItemOperations","OutputFix","replacedTemplateParts","escapeTemplateText","placeholderText","pathItem","pathItemType","createRef","member","irProperty","toPropertyName","irRaw","assignabilityProof","NO_DECLARED_TYPES","coveringTemplateKeys","irObject","irIntersection","documentEntries","documentMember","refType"],"mappings":";;;;;;;;;;;;;;;;;AAqCA,MAAM,iBAAA,GAAoB,YAAA;AAG1B,MAAM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AAI,CAAC,QAAA,IAAY,OAAO,QAAA,KAAa,QAAA,EAAU;AAC7C,MAAA;AAAA,IACF;AACA,IAAA,IAAI,UAAU,QAAA,EAAU;AACtB,MAAA,MAAM,OAAA,GAAU,EAAE,UAAA,EAAY,OAAA,CAAQ,QAAQ,SAAA,EAAU;AACxD,MAAA,MAAM,MAAA,GAASC,8BAAe,GAAA,EAAKC,eAAA,CAAQ,SAAS,IAAI,CAAA,EAAG,UAAU,OAAO,CAAA;AAC5E,MAAA,OAAA,CAAQ,IAAA,CAAK,EAAE,GAAA,EAAK,MAAA,EAAQ,GAAA,EAAK,UAAA,CAAW,GAAG,CAAA,EAAG,IAAA,EAAM,MAAA,CAAO,IAAA,EAAM,CAAA;AACrE,MAAA;AAAA,IACF;AACA,IAAA,MAAM,SAAA,GAAY,OAAA,CAAQ,WAAA,CAAY,GAAA,EAAI;AAC1C,IAAA,MAAM,IAAA,GAAO,cAAA,CAAe,GAAA,EAAK,QAAA,EAAU,OAAO,CAAA;AAClD,IAAA,OAAA,CAAQ,KAAK,IAAI,CAAA;AAEjB,IAAA,IAAI,IAAA,CAAK,MAAA,CAAO,IAAA,KAAS,UAAA,EAAY;AACnC,MAAA,OAAA,CAAQ,WAAA,CAAY,MAAM,CAAA,kBAAA,EAAqB,GAAG,KAAK,OAAA,CAAQ,WAAA,CAAY,GAAA,EAAI,GAAI,SAAS,CAAA;AAAA,IAC9F;AAAA,EACF;AACA,EAAA,OAAO,OAAA;AACT;AAMO,SAAS,SAAA,CAAU,OAAoB,OAAA,EAAmC;AAC/E,EAAA,OAAO,eAAA,CAAgB,WAAA,CAAY,KAAA,EAAO,OAAO,CAAC,CAAA;AACpD;;;;"}
|
|
1
|
+
{"version":3,"file":"paths.cjs","sources":["../../../../src/application/transform/document/paths.ts"],"sourcesContent":["import { irIntersection, irObject, irProperty, irRaw } from \"../../../domain/ir/constructors.js\";\nimport type { IrMember, IrType } from \"../../../domain/ir/nodes.js\";\nimport { toPropertyName } from \"../../../domain/ir/property-name.js\";\nimport { escapeTemplateText } from \"../../../domain/ir/raw-text-escapes.js\";\nimport { createRef } from \"../../../domain/openapi/json-pointer.js\";\nimport type { OperationObject } from \"../../../domain/openapi/operation-object.js\";\nimport type {\n ParameterObject,\n ParameterOrReference,\n} from \"../../../domain/openapi/parameter-object.js\";\nimport {\n type PathItemObject,\n type PathsObject,\n pathItemOperations,\n} from \"../../../domain/openapi/path-item-object.js\";\nimport type { ReferenceSchema, SchemaObject } from \"../../../domain/openapi/schema-object.js\";\nimport { OutputFix } from \"../../../domain/output-fixes.js\";\nimport { documentEntries } from \"../document-entries.js\";\nimport { refType } from \"../ref-type.js\";\nimport type { TransformContext } from \"../transform-context.js\";\nimport { type DeclaredTypeLookup, NO_DECLARED_TYPES } from \"./declared-types.js\";\nimport { documentMember } from \"./document-member.js\";\nimport { pathItemType } from \"./path-item.js\";\nimport {\n type CoverRejection,\n coveringTemplateKeys,\n type PathKey,\n type PathKeyPart,\n type PathPlaceholder,\n} from \"./path-key-overlaps.js\";\nimport {\n type PathParameterMatch,\n placeholderText,\n replacedTemplateParts,\n} from \"./path-template-replace.js\";\nimport { assignabilityProof } from \"./type-assignability.js\";\n\n/** `[^{}]` keeps the scan linear: `[^}]` retried from every `{` of a long run of them. */\nconst PATH_PARAMETER_RE = /\\{[^{}]+\\}/g;\n\n/** Placeholders by parameter schema type; anything else is a string placeholder. */\nconst PLACEHOLDERS: ReadonlyMap<unknown, PathPlaceholder> = new Map([\n [\"number\", \"number\"],\n [\"integer\", \"number\"],\n [\"boolean\", \"boolean\"],\n]);\nconst STRING_PLACEHOLDER: PathPlaceholder = \"string\";\n\nfunction resolvedParameter(\n parameter: ParameterOrReference,\n context: TransformContext,\n): ParameterObject | undefined {\n return \"$ref\" in parameter && parameter.$ref\n ? (context.refs.resolve(parameter.$ref) as ParameterObject | undefined)\n : (parameter as ParameterObject);\n}\n\nfunction addPathParameters(\n parameters: readonly ParameterOrReference[],\n byName: Map<string, ParameterObject>,\n context: TransformContext,\n): void {\n for (const parameter of parameters) {\n const resolved = resolvedParameter(parameter, context);\n if (resolved && resolved.in === \"path\") {\n byName.set(resolved.name, resolved);\n }\n }\n}\n\n/** Path parameters of the path item and of its operations; later definitions win. */\nfunction pathParameters(\n pathItem: PathItemObject,\n context: TransformContext,\n): Map<string, ParameterObject> {\n const byName = new Map<string, ParameterObject>();\n addPathParameters(pathItem.parameters ?? [], byName, context);\n for (const { operation: entry } of pathItemOperations(pathItem)) {\n const operation = (entry as ReferenceSchema).$ref\n ? (context.refs.resolve((entry as ReferenceSchema).$ref) as OperationObject | undefined)\n : (entry as OperationObject);\n addPathParameters(operation?.parameters ?? [], byName, context);\n }\n return byName;\n}\n\n/** Each `{name}` of the URL and its own placeholder, from the URL split at the matches. */\nfunction splitTemplateParts(url: string, matches: readonly PathParameterMatch[]): PathKeyPart[] {\n return url.split(PATH_PARAMETER_RE).flatMap((text, index): PathKeyPart[] => {\n const type = matches[index]?.type;\n const textPart: PathKeyPart = { kind: \"text\", text };\n return type === undefined ? [textPart] : [textPart, { kind: \"placeholder\", type }];\n });\n}\n\n/**\n * `--path-params-as-types`: the URL as template parts, each `{name}` replaced by a placeholder for\n * its parameter's type, the way the original replaces them unless the `path-param-type-names` fix\n * is on (see `replacedTemplateParts`). Null when the URL has no complete `{…}` placeholder.\n */\nfunction pathTemplateParts(\n url: string,\n pathItem: PathItemObject,\n context: TransformContext,\n): PathKeyPart[] | null {\n const found = url.match(PATH_PARAMETER_RE);\n if (!found) {\n return null;\n }\n const parameters = pathParameters(pathItem, context);\n const matches = found.map((match): PathParameterMatch => {\n const schema = parameters.get(match.slice(1, -1))?.schema as SchemaObject | undefined;\n return { match, type: PLACEHOLDERS.get(schema?.type) ?? STRING_PLACEHOLDER };\n });\n return context.options.fixes.has(OutputFix.PATH_PARAM_TYPE_NAMES)\n ? splitTemplateParts(url, matches)\n : replacedTemplateParts(url, matches);\n}\n\n/**\n * The template literal type of the parts. The original escaped only backticks: a backslash or\n * `${` in the URL could end the literal or open a placeholder, and add code to the generated file\n * (intentional fix). So the text parts are escaped.\n */\nfunction templateText(parts: readonly PathKeyPart[]): string {\n const text = parts\n .map((part) =>\n part.kind === \"text\" ? escapeTemplateText(part.text) : placeholderText(part.type),\n )\n .join(\"\");\n return `\\`${text}\\``;\n}\n\n/** A path's member, its key and its path item type for the overlap check. */\nexport interface PathMember {\n readonly url: string;\n readonly member: IrMember;\n readonly key: PathKey;\n readonly type: IrType;\n}\n\nfunction pathItemMember(\n url: string,\n pathItem: PathItemObject,\n context: TransformContext,\n): PathMember {\n const isReadonly = context.options.immutable;\n const type = pathItemType(pathItem, { path: createRef([\"paths\", url]) }, context);\n const parts =\n context.options.pathParamsAsTypes && url.includes(\"{\")\n ? pathTemplateParts(url, pathItem, context)\n : null;\n if (parts === null) {\n const member = irProperty(toPropertyName(url), type, { isReadonly });\n return { url, member, key: literalKey(url), type };\n }\n const member: IrMember = {\n kind: \"indexSignature\",\n parameterName: \"path\",\n parameterType: irRaw(templateText(parts)),\n type,\n isReadonly,\n comments: [],\n };\n return { url, member, key: { kind: \"template\", parts }, type };\n}\n\nfunction literalKey(url: string): PathKey {\n return { kind: \"literal\", url };\n}\n\nconst PATHS_TYPE_NAME = \"paths\";\n\n/** The URL of `paths[\"/apps/{id}\"]`, or null for any other type. */\nfunction pathsIndexOf(type: IrType): string | null {\n if (type.kind !== \"indexedAccess\" || type.index.kind !== \"stringLiteral\") {\n return null;\n }\n const { object } = type;\n const isPaths =\n object.kind === \"reference\" &&\n object.name === PATHS_TYPE_NAME &&\n object.typeArguments.length === 0;\n return isPaths ? type.index.value : null;\n}\n\n/**\n * The lookup, and `paths[\"/apps/{id}\"]` as that path's item. The declarations cannot read it: a\n * templated path is no property of `paths`, and TypeScript finds it through the signature.\n */\nfunction withPathItems(\n paths: readonly PathMember[],\n lookup: DeclaredTypeLookup,\n): DeclaredTypeLookup {\n const byUrl = new Map(paths.map((path) => [path.url, path.type]));\n return (type) => {\n const url = pathsIndexOf(type);\n return (url === null ? undefined : byUrl.get(url)) ?? lookup(type);\n };\n}\n\n/**\n * The rejection of a covered path item: only one proven not assignable to the signature's type\n * counts (ADR-028). A pair the proof cannot decide keeps the original output, which may well\n * compile (`Record<string, never>` fits `{ [key: string]: unknown }`).\n */\nfunction rejectionOf(paths: readonly PathMember[], lookup: DeclaredTypeLookup): CoverRejection {\n const relate = assignabilityProof(withPathItems(paths, lookup));\n return (coveringIndex, coveredIndex) => {\n const coveringPath = paths[coveringIndex];\n const coveredPath = paths[coveredIndex];\n return (\n coveringPath !== undefined &&\n coveredPath !== undefined &&\n relate(coveredPath.type, coveringPath.type) === \"rejected\"\n );\n };\n}\n\n/**\n * The members as one object type. When an index signature covers another key, TypeScript checks\n * the covered key's type against it. Where that check surely fails (TS2411, TS2413), each such\n * signature gets an object of its own in an intersection: an intersection does not check its parts\n * against each other, and every key has the type it had in the one object. Output that compiles\n * keeps the one object. The lookup reads the types that path items reference; without it, a\n * reference decides nothing.\n */\nexport function pathsObjectType(\n paths: readonly PathMember[],\n lookup: DeclaredTypeLookup = NO_DECLARED_TYPES,\n): IrType {\n const keys = paths.map(({ key }) => key);\n const covering = coveringTemplateKeys(keys, rejectionOf(paths, lookup));\n if (covering.size === 0) {\n return irObject(paths.map(({ member }) => member));\n }\n const rest = paths.filter((_, index) => !covering.has(index)).map(({ member }) => member);\n const own = paths\n .filter((_, index) => covering.has(index))\n .map(({ member }) => irObject([member]));\n return irIntersection([irObject(rest), ...own]);\n}\n\n/** The members of the `paths` interface (OpenAPI 3.1 §4.8.8): one per path, by URL. */\nexport function pathMembers(paths: PathsObject, context: TransformContext): PathMember[] {\n const members: PathMember[] = [];\n for (const [url, pathItem] of documentEntries(paths, context.options)) {\n if (!pathItem || typeof pathItem !== \"object\") {\n continue;\n }\n if (\"$ref\" in pathItem) {\n const options = { isReadonly: context.options.immutable };\n const member = documentMember(url, refType(pathItem.$ref), pathItem, options);\n members.push({ url, member, key: literalKey(url), type: member.type });\n continue;\n }\n const startedAt = context.diagnostics.now();\n const path = pathItemMember(url, pathItem, context);\n members.push(path);\n // The original reports only paths written as properties.\n if (path.member.kind === \"property\") {\n context.diagnostics.debug(`Transformed path \"${url}\"`, context.diagnostics.now() - startedAt);\n }\n }\n return members;\n}\n\n/**\n * The `paths` interface body, with only the path items' own types for the overlap check. The\n * document transform checks the overlaps again once the referenced types are declared.\n */\nexport function pathsType(paths: PathsObject, context: TransformContext): IrType {\n return pathsObjectType(pathMembers(paths, context));\n}\n"],"names":["pathItemOperations","OutputFix","replacedTemplateParts","escapeTemplateText","placeholderText","pathItem","pathItemType","createRef","member","irProperty","toPropertyName","irRaw","assignabilityProof","NO_DECLARED_TYPES","coveringTemplateKeys","irObject","irIntersection","documentEntries","documentMember","refType"],"mappings":";;;;;;;;;;;;;;;;;AAsCA,MAAM,iBAAA,GAAoB,aAAA;AAG1B,MAAM,YAAA,uBAA0D,GAAA,CAAI;AAAA,EAClE,CAAC,UAAU,QAAQ,CAAA;AAAA,EACnB,CAAC,WAAW,QAAQ,CAAA;AAAA,EACpB,CAAC,WAAW,SAAS;AACvB,CAAC,CAAA;AACD,MAAM,kBAAA,GAAsC,QAAA;AAE5C,SAAS,iBAAA,CACP,WACA,OAAA,EAC6B;AAC7B,EAAA,OAAO,MAAA,IAAU,aAAa,SAAA,CAAU,IAAA,GACnC,QAAQ,IAAA,CAAK,OAAA,CAAQ,SAAA,CAAU,IAAI,CAAA,GACnC,SAAA;AACP;AAEA,SAAS,iBAAA,CACP,UAAA,EACA,MAAA,EACA,OAAA,EACM;AACN,EAAA,KAAA,MAAW,aAAa,UAAA,EAAY;AAClC,IAAA,MAAM,QAAA,GAAW,iBAAA,CAAkB,SAAA,EAAW,OAAO,CAAA;AACrD,IAAA,IAAI,QAAA,IAAY,QAAA,CAAS,EAAA,KAAO,MAAA,EAAQ;AACtC,MAAA,MAAA,CAAO,GAAA,CAAI,QAAA,CAAS,IAAA,EAAM,QAAQ,CAAA;AAAA,IACpC;AAAA,EAC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A,CAAK,MAAA,CAAO,IAAA,KAAS,UAAA,EAAY;AACnC,MAAA,OAAA,CAAQ,WAAA,CAAY,MAAM,CAAA,kBAAA,EAAqB,GAAG,KAAK,OAAA,CAAQ,WAAA,CAAY,GAAA,EAAI,GAAI,SAAS,CAAA;AAAA,IAC9F;AAAA,EACF;AACA,EAAA,OAAO,OAAA;AACT;AAMO,SAAS,SAAA,CAAU,OAAoB,OAAA,EAAmC;AAC/E,EAAA,OAAO,eAAA,CAAgB,WAAA,CAAY,KAAA,EAAO,OAAO,CAAC,CAAA;AACpD;;;;"}
|
|
@@ -13,7 +13,7 @@ import { coveringTemplateKeys } from './path-key-overlaps.mjs';
|
|
|
13
13
|
import { replacedTemplateParts, placeholderText } from './path-template-replace.mjs';
|
|
14
14
|
import { assignabilityProof } from './type-assignability.mjs';
|
|
15
15
|
|
|
16
|
-
const PATH_PARAMETER_RE = /\{[^}]+\}/g;
|
|
16
|
+
const PATH_PARAMETER_RE = /\{[^{}]+\}/g;
|
|
17
17
|
const PLACEHOLDERS = /* @__PURE__ */ new Map([
|
|
18
18
|
["number", "number"],
|
|
19
19
|
["integer", "number"],
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"paths.mjs","sources":["../../../../src/application/transform/document/paths.ts"],"sourcesContent":["import { irIntersection, irObject, irProperty, irRaw } from \"../../../domain/ir/constructors.js\";\nimport type { IrMember, IrType } from \"../../../domain/ir/nodes.js\";\nimport { toPropertyName } from \"../../../domain/ir/property-name.js\";\nimport { escapeTemplateText } from \"../../../domain/ir/raw-text-escapes.js\";\nimport { createRef } from \"../../../domain/openapi/json-pointer.js\";\nimport type { OperationObject } from \"../../../domain/openapi/operation-object.js\";\nimport type {\n ParameterObject,\n ParameterOrReference,\n} from \"../../../domain/openapi/parameter-object.js\";\nimport {\n type PathItemObject,\n type PathsObject,\n pathItemOperations,\n} from \"../../../domain/openapi/path-item-object.js\";\nimport type { ReferenceSchema, SchemaObject } from \"../../../domain/openapi/schema-object.js\";\nimport { OutputFix } from \"../../../domain/output-fixes.js\";\nimport { documentEntries } from \"../document-entries.js\";\nimport { refType } from \"../ref-type.js\";\nimport type { TransformContext } from \"../transform-context.js\";\nimport { type DeclaredTypeLookup, NO_DECLARED_TYPES } from \"./declared-types.js\";\nimport { documentMember } from \"./document-member.js\";\nimport { pathItemType } from \"./path-item.js\";\nimport {\n type CoverRejection,\n coveringTemplateKeys,\n type PathKey,\n type PathKeyPart,\n type PathPlaceholder,\n} from \"./path-key-overlaps.js\";\nimport {\n type PathParameterMatch,\n placeholderText,\n replacedTemplateParts,\n} from \"./path-template-replace.js\";\nimport { assignabilityProof } from \"./type-assignability.js\";\n\nconst PATH_PARAMETER_RE = /\\{[^}]+\\}/g;\n\n/** Placeholders by parameter schema type; anything else is a string placeholder. */\nconst PLACEHOLDERS: ReadonlyMap<unknown, PathPlaceholder> = new Map([\n [\"number\", \"number\"],\n [\"integer\", \"number\"],\n [\"boolean\", \"boolean\"],\n]);\nconst STRING_PLACEHOLDER: PathPlaceholder = \"string\";\n\nfunction resolvedParameter(\n parameter: ParameterOrReference,\n context: TransformContext,\n): ParameterObject | undefined {\n return \"$ref\" in parameter && parameter.$ref\n ? (context.refs.resolve(parameter.$ref) as ParameterObject | undefined)\n : (parameter as ParameterObject);\n}\n\nfunction addPathParameters(\n parameters: readonly ParameterOrReference[],\n byName: Map<string, ParameterObject>,\n context: TransformContext,\n): void {\n for (const parameter of parameters) {\n const resolved = resolvedParameter(parameter, context);\n if (resolved && resolved.in === \"path\") {\n byName.set(resolved.name, resolved);\n }\n }\n}\n\n/** Path parameters of the path item and of its operations; later definitions win. */\nfunction pathParameters(\n pathItem: PathItemObject,\n context: TransformContext,\n): Map<string, ParameterObject> {\n const byName = new Map<string, ParameterObject>();\n addPathParameters(pathItem.parameters ?? [], byName, context);\n for (const { operation: entry } of pathItemOperations(pathItem)) {\n const operation = (entry as ReferenceSchema).$ref\n ? (context.refs.resolve((entry as ReferenceSchema).$ref) as OperationObject | undefined)\n : (entry as OperationObject);\n addPathParameters(operation?.parameters ?? [], byName, context);\n }\n return byName;\n}\n\n/** Each `{name}` of the URL and its own placeholder, from the URL split at the matches. */\nfunction splitTemplateParts(url: string, matches: readonly PathParameterMatch[]): PathKeyPart[] {\n return url.split(PATH_PARAMETER_RE).flatMap((text, index): PathKeyPart[] => {\n const type = matches[index]?.type;\n const textPart: PathKeyPart = { kind: \"text\", text };\n return type === undefined ? [textPart] : [textPart, { kind: \"placeholder\", type }];\n });\n}\n\n/**\n * `--path-params-as-types`: the URL as template parts, each `{name}` replaced by a placeholder for\n * its parameter's type, the way the original replaces them unless the `path-param-type-names` fix\n * is on (see `replacedTemplateParts`). Null when the URL has no complete `{…}` placeholder.\n */\nfunction pathTemplateParts(\n url: string,\n pathItem: PathItemObject,\n context: TransformContext,\n): PathKeyPart[] | null {\n const found = url.match(PATH_PARAMETER_RE);\n if (!found) {\n return null;\n }\n const parameters = pathParameters(pathItem, context);\n const matches = found.map((match): PathParameterMatch => {\n const schema = parameters.get(match.slice(1, -1))?.schema as SchemaObject | undefined;\n return { match, type: PLACEHOLDERS.get(schema?.type) ?? STRING_PLACEHOLDER };\n });\n return context.options.fixes.has(OutputFix.PATH_PARAM_TYPE_NAMES)\n ? splitTemplateParts(url, matches)\n : replacedTemplateParts(url, matches);\n}\n\n/**\n * The template literal type of the parts. The original escaped only backticks: a backslash or\n * `${` in the URL could end the literal or open a placeholder, and add code to the generated file\n * (intentional fix). So the text parts are escaped.\n */\nfunction templateText(parts: readonly PathKeyPart[]): string {\n const text = parts\n .map((part) =>\n part.kind === \"text\" ? escapeTemplateText(part.text) : placeholderText(part.type),\n )\n .join(\"\");\n return `\\`${text}\\``;\n}\n\n/** A path's member, its key and its path item type for the overlap check. */\nexport interface PathMember {\n readonly url: string;\n readonly member: IrMember;\n readonly key: PathKey;\n readonly type: IrType;\n}\n\nfunction pathItemMember(\n url: string,\n pathItem: PathItemObject,\n context: TransformContext,\n): PathMember {\n const isReadonly = context.options.immutable;\n const type = pathItemType(pathItem, { path: createRef([\"paths\", url]) }, context);\n const parts =\n context.options.pathParamsAsTypes && url.includes(\"{\")\n ? pathTemplateParts(url, pathItem, context)\n : null;\n if (parts === null) {\n const member = irProperty(toPropertyName(url), type, { isReadonly });\n return { url, member, key: literalKey(url), type };\n }\n const member: IrMember = {\n kind: \"indexSignature\",\n parameterName: \"path\",\n parameterType: irRaw(templateText(parts)),\n type,\n isReadonly,\n comments: [],\n };\n return { url, member, key: { kind: \"template\", parts }, type };\n}\n\nfunction literalKey(url: string): PathKey {\n return { kind: \"literal\", url };\n}\n\nconst PATHS_TYPE_NAME = \"paths\";\n\n/** The URL of `paths[\"/apps/{id}\"]`, or null for any other type. */\nfunction pathsIndexOf(type: IrType): string | null {\n if (type.kind !== \"indexedAccess\" || type.index.kind !== \"stringLiteral\") {\n return null;\n }\n const { object } = type;\n const isPaths =\n object.kind === \"reference\" &&\n object.name === PATHS_TYPE_NAME &&\n object.typeArguments.length === 0;\n return isPaths ? type.index.value : null;\n}\n\n/**\n * The lookup, and `paths[\"/apps/{id}\"]` as that path's item. The declarations cannot read it: a\n * templated path is no property of `paths`, and TypeScript finds it through the signature.\n */\nfunction withPathItems(\n paths: readonly PathMember[],\n lookup: DeclaredTypeLookup,\n): DeclaredTypeLookup {\n const byUrl = new Map(paths.map((path) => [path.url, path.type]));\n return (type) => {\n const url = pathsIndexOf(type);\n return (url === null ? undefined : byUrl.get(url)) ?? lookup(type);\n };\n}\n\n/**\n * The rejection of a covered path item: only one proven not assignable to the signature's type\n * counts (ADR-028). A pair the proof cannot decide keeps the original output, which may well\n * compile (`Record<string, never>` fits `{ [key: string]: unknown }`).\n */\nfunction rejectionOf(paths: readonly PathMember[], lookup: DeclaredTypeLookup): CoverRejection {\n const relate = assignabilityProof(withPathItems(paths, lookup));\n return (coveringIndex, coveredIndex) => {\n const coveringPath = paths[coveringIndex];\n const coveredPath = paths[coveredIndex];\n return (\n coveringPath !== undefined &&\n coveredPath !== undefined &&\n relate(coveredPath.type, coveringPath.type) === \"rejected\"\n );\n };\n}\n\n/**\n * The members as one object type. When an index signature covers another key, TypeScript checks\n * the covered key's type against it. Where that check surely fails (TS2411, TS2413), each such\n * signature gets an object of its own in an intersection: an intersection does not check its parts\n * against each other, and every key has the type it had in the one object. Output that compiles\n * keeps the one object. The lookup reads the types that path items reference; without it, a\n * reference decides nothing.\n */\nexport function pathsObjectType(\n paths: readonly PathMember[],\n lookup: DeclaredTypeLookup = NO_DECLARED_TYPES,\n): IrType {\n const keys = paths.map(({ key }) => key);\n const covering = coveringTemplateKeys(keys, rejectionOf(paths, lookup));\n if (covering.size === 0) {\n return irObject(paths.map(({ member }) => member));\n }\n const rest = paths.filter((_, index) => !covering.has(index)).map(({ member }) => member);\n const own = paths\n .filter((_, index) => covering.has(index))\n .map(({ member }) => irObject([member]));\n return irIntersection([irObject(rest), ...own]);\n}\n\n/** The members of the `paths` interface (OpenAPI 3.1 §4.8.8): one per path, by URL. */\nexport function pathMembers(paths: PathsObject, context: TransformContext): PathMember[] {\n const members: PathMember[] = [];\n for (const [url, pathItem] of documentEntries(paths, context.options)) {\n if (!pathItem || typeof pathItem !== \"object\") {\n continue;\n }\n if (\"$ref\" in pathItem) {\n const options = { isReadonly: context.options.immutable };\n const member = documentMember(url, refType(pathItem.$ref), pathItem, options);\n members.push({ url, member, key: literalKey(url), type: member.type });\n continue;\n }\n const startedAt = context.diagnostics.now();\n const path = pathItemMember(url, pathItem, context);\n members.push(path);\n // The original reports only paths written as properties.\n if (path.member.kind === \"property\") {\n context.diagnostics.debug(`Transformed path \"${url}\"`, context.diagnostics.now() - startedAt);\n }\n }\n return members;\n}\n\n/**\n * The `paths` interface body, with only the path items' own types for the overlap check. The\n * document transform checks the overlaps again once the referenced types are declared.\n */\nexport function pathsType(paths: PathsObject, context: TransformContext): IrType {\n return pathsObjectType(pathMembers(paths, context));\n}\n"],"names":["member"],"mappings":";;;;;;;;;;;;;;;AAqCA,MAAM,iBAAA,GAA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QAAA,IAAY,OAAO,QAAA,KAAa,QAAA,EAAU;AAC7C,MAAA;AAAA,IACF;AACA,IAAA,IAAI,UAAU,QAAA,EAAU;AACtB,MAAA,MAAM,OAAA,GAAU,EAAE,UAAA,EAAY,OAAA,CAAQ,QAAQ,SAAA,EAAU;AACxD,MAAA,MAAM,MAAA,GAAS,eAAe,GAAA,EAAK,OAAA,CAAQ,SAAS,IAAI,CAAA,EAAG,UAAU,OAAO,CAAA;AAC5E,MAAA,OAAA,CAAQ,IAAA,CAAK,EAAE,GAAA,EAAK,MAAA,EAAQ,GAAA,EAAK,UAAA,CAAW,GAAG,CAAA,EAAG,IAAA,EAAM,MAAA,CAAO,IAAA,EAAM,CAAA;AACrE,MAAA;AAAA,IACF;AACA,IAAA,MAAM,SAAA,GAAY,OAAA,CAAQ,WAAA,CAAY,GAAA,EAAI;AAC1C,IAAA,MAAM,IAAA,GAAO,cAAA,CAAe,GAAA,EAAK,QAAA,EAAU,OAAO,CAAA;AAClD,IAAA,OAAA,CAAQ,KAAK,IAAI,CAAA;AAEjB,IAAA,IAAI,IAAA,CAAK,MAAA,CAAO,IAAA,KAAS,UAAA,EAAY;AACnC,MAAA,OAAA,CAAQ,WAAA,CAAY,MAAM,CAAA,kBAAA,EAAqB,GAAG,KAAK,OAAA,CAAQ,WAAA,CAAY,GAAA,EAAI,GAAI,SAAS,CAAA;AAAA,IAC9F;AAAA,EACF;AACA,EAAA,OAAO,OAAA;AACT;AAMO,SAAS,SAAA,CAAU,OAAoB,OAAA,EAAmC;AAC/E,EAAA,OAAO,eAAA,CAAgB,WAAA,CAAY,KAAA,EAAO,OAAO,CAAC,CAAA;AACpD;;"}
|
|
1
|
+
{"version":3,"file":"paths.mjs","sources":["../../../../src/application/transform/document/paths.ts"],"sourcesContent":["import { irIntersection, irObject, irProperty, irRaw } from \"../../../domain/ir/constructors.js\";\nimport type { IrMember, IrType } from \"../../../domain/ir/nodes.js\";\nimport { toPropertyName } from \"../../../domain/ir/property-name.js\";\nimport { escapeTemplateText } from \"../../../domain/ir/raw-text-escapes.js\";\nimport { createRef } from \"../../../domain/openapi/json-pointer.js\";\nimport type { OperationObject } from \"../../../domain/openapi/operation-object.js\";\nimport type {\n ParameterObject,\n ParameterOrReference,\n} from \"../../../domain/openapi/parameter-object.js\";\nimport {\n type PathItemObject,\n type PathsObject,\n pathItemOperations,\n} from \"../../../domain/openapi/path-item-object.js\";\nimport type { ReferenceSchema, SchemaObject } from \"../../../domain/openapi/schema-object.js\";\nimport { OutputFix } from \"../../../domain/output-fixes.js\";\nimport { documentEntries } from \"../document-entries.js\";\nimport { refType } from \"../ref-type.js\";\nimport type { TransformContext } from \"../transform-context.js\";\nimport { type DeclaredTypeLookup, NO_DECLARED_TYPES } from \"./declared-types.js\";\nimport { documentMember } from \"./document-member.js\";\nimport { pathItemType } from \"./path-item.js\";\nimport {\n type CoverRejection,\n coveringTemplateKeys,\n type PathKey,\n type PathKeyPart,\n type PathPlaceholder,\n} from \"./path-key-overlaps.js\";\nimport {\n type PathParameterMatch,\n placeholderText,\n replacedTemplateParts,\n} from \"./path-template-replace.js\";\nimport { assignabilityProof } from \"./type-assignability.js\";\n\n/** `[^{}]` keeps the scan linear: `[^}]` retried from every `{` of a long run of them. */\nconst PATH_PARAMETER_RE = /\\{[^{}]+\\}/g;\n\n/** Placeholders by parameter schema type; anything else is a string placeholder. */\nconst PLACEHOLDERS: ReadonlyMap<unknown, PathPlaceholder> = new Map([\n [\"number\", \"number\"],\n [\"integer\", \"number\"],\n [\"boolean\", \"boolean\"],\n]);\nconst STRING_PLACEHOLDER: PathPlaceholder = \"string\";\n\nfunction resolvedParameter(\n parameter: ParameterOrReference,\n context: TransformContext,\n): ParameterObject | undefined {\n return \"$ref\" in parameter && parameter.$ref\n ? (context.refs.resolve(parameter.$ref) as ParameterObject | undefined)\n : (parameter as ParameterObject);\n}\n\nfunction addPathParameters(\n parameters: readonly ParameterOrReference[],\n byName: Map<string, ParameterObject>,\n context: TransformContext,\n): void {\n for (const parameter of parameters) {\n const resolved = resolvedParameter(parameter, context);\n if (resolved && resolved.in === \"path\") {\n byName.set(resolved.name, resolved);\n }\n }\n}\n\n/** Path parameters of the path item and of its operations; later definitions win. */\nfunction pathParameters(\n pathItem: PathItemObject,\n context: TransformContext,\n): Map<string, ParameterObject> {\n const byName = new Map<string, ParameterObject>();\n addPathParameters(pathItem.parameters ?? [], byName, context);\n for (const { operation: entry } of pathItemOperations(pathItem)) {\n const operation = (entry as ReferenceSchema).$ref\n ? (context.refs.resolve((entry as ReferenceSchema).$ref) as OperationObject | undefined)\n : (entry as OperationObject);\n addPathParameters(operation?.parameters ?? [], byName, context);\n }\n return byName;\n}\n\n/** Each `{name}` of the URL and its own placeholder, from the URL split at the matches. */\nfunction splitTemplateParts(url: string, matches: readonly PathParameterMatch[]): PathKeyPart[] {\n return url.split(PATH_PARAMETER_RE).flatMap((text, index): PathKeyPart[] => {\n const type = matches[index]?.type;\n const textPart: PathKeyPart = { kind: \"text\", text };\n return type === undefined ? [textPart] : [textPart, { kind: \"placeholder\", type }];\n });\n}\n\n/**\n * `--path-params-as-types`: the URL as template parts, each `{name}` replaced by a placeholder for\n * its parameter's type, the way the original replaces them unless the `path-param-type-names` fix\n * is on (see `replacedTemplateParts`). Null when the URL has no complete `{…}` placeholder.\n */\nfunction pathTemplateParts(\n url: string,\n pathItem: PathItemObject,\n context: TransformContext,\n): PathKeyPart[] | null {\n const found = url.match(PATH_PARAMETER_RE);\n if (!found) {\n return null;\n }\n const parameters = pathParameters(pathItem, context);\n const matches = found.map((match): PathParameterMatch => {\n const schema = parameters.get(match.slice(1, -1))?.schema as SchemaObject | undefined;\n return { match, type: PLACEHOLDERS.get(schema?.type) ?? STRING_PLACEHOLDER };\n });\n return context.options.fixes.has(OutputFix.PATH_PARAM_TYPE_NAMES)\n ? splitTemplateParts(url, matches)\n : replacedTemplateParts(url, matches);\n}\n\n/**\n * The template literal type of the parts. The original escaped only backticks: a backslash or\n * `${` in the URL could end the literal or open a placeholder, and add code to the generated file\n * (intentional fix). So the text parts are escaped.\n */\nfunction templateText(parts: readonly PathKeyPart[]): string {\n const text = parts\n .map((part) =>\n part.kind === \"text\" ? escapeTemplateText(part.text) : placeholderText(part.type),\n )\n .join(\"\");\n return `\\`${text}\\``;\n}\n\n/** A path's member, its key and its path item type for the overlap check. */\nexport interface PathMember {\n readonly url: string;\n readonly member: IrMember;\n readonly key: PathKey;\n readonly type: IrType;\n}\n\nfunction pathItemMember(\n url: string,\n pathItem: PathItemObject,\n context: TransformContext,\n): PathMember {\n const isReadonly = context.options.immutable;\n const type = pathItemType(pathItem, { path: createRef([\"paths\", url]) }, context);\n const parts =\n context.options.pathParamsAsTypes && url.includes(\"{\")\n ? pathTemplateParts(url, pathItem, context)\n : null;\n if (parts === null) {\n const member = irProperty(toPropertyName(url), type, { isReadonly });\n return { url, member, key: literalKey(url), type };\n }\n const member: IrMember = {\n kind: \"indexSignature\",\n parameterName: \"path\",\n parameterType: irRaw(templateText(parts)),\n type,\n isReadonly,\n comments: [],\n };\n return { url, member, key: { kind: \"template\", parts }, type };\n}\n\nfunction literalKey(url: string): PathKey {\n return { kind: \"literal\", url };\n}\n\nconst PATHS_TYPE_NAME = \"paths\";\n\n/** The URL of `paths[\"/apps/{id}\"]`, or null for any other type. */\nfunction pathsIndexOf(type: IrType): string | null {\n if (type.kind !== \"indexedAccess\" || type.index.kind !== \"stringLiteral\") {\n return null;\n }\n const { object } = type;\n const isPaths =\n object.kind === \"reference\" &&\n object.name === PATHS_TYPE_NAME &&\n object.typeArguments.length === 0;\n return isPaths ? type.index.value : null;\n}\n\n/**\n * The lookup, and `paths[\"/apps/{id}\"]` as that path's item. The declarations cannot read it: a\n * templated path is no property of `paths`, and TypeScript finds it through the signature.\n */\nfunction withPathItems(\n paths: readonly PathMember[],\n lookup: DeclaredTypeLookup,\n): DeclaredTypeLookup {\n const byUrl = new Map(paths.map((path) => [path.url, path.type]));\n return (type) => {\n const url = pathsIndexOf(type);\n return (url === null ? undefined : byUrl.get(url)) ?? lookup(type);\n };\n}\n\n/**\n * The rejection of a covered path item: only one proven not assignable to the signature's type\n * counts (ADR-028). A pair the proof cannot decide keeps the original output, which may well\n * compile (`Record<string, never>` fits `{ [key: string]: unknown }`).\n */\nfunction rejectionOf(paths: readonly PathMember[], lookup: DeclaredTypeLookup): CoverRejection {\n const relate = assignabilityProof(withPathItems(paths, lookup));\n return (coveringIndex, coveredIndex) => {\n const coveringPath = paths[coveringIndex];\n const coveredPath = paths[coveredIndex];\n return (\n coveringPath !== undefined &&\n coveredPath !== undefined &&\n relate(coveredPath.type, coveringPath.type) === \"rejected\"\n );\n };\n}\n\n/**\n * The members as one object type. When an index signature covers another key, TypeScript checks\n * the covered key's type against it. Where that check surely fails (TS2411, TS2413), each such\n * signature gets an object of its own in an intersection: an intersection does not check its parts\n * against each other, and every key has the type it had in the one object. Output that compiles\n * keeps the one object. The lookup reads the types that path items reference; without it, a\n * reference decides nothing.\n */\nexport function pathsObjectType(\n paths: readonly PathMember[],\n lookup: DeclaredTypeLookup = NO_DECLARED_TYPES,\n): IrType {\n const keys = paths.map(({ key }) => key);\n const covering = coveringTemplateKeys(keys, rejectionOf(paths, lookup));\n if (covering.size === 0) {\n return irObject(paths.map(({ member }) => member));\n }\n const rest = paths.filter((_, index) => !covering.has(index)).map(({ member }) => member);\n const own = paths\n .filter((_, index) => covering.has(index))\n .map(({ member }) => irObject([member]));\n return irIntersection([irObject(rest), ...own]);\n}\n\n/** The members of the `paths` interface (OpenAPI 3.1 §4.8.8): one per path, by URL. */\nexport function pathMembers(paths: PathsObject, context: TransformContext): PathMember[] {\n const members: PathMember[] = [];\n for (const [url, pathItem] of documentEntries(paths, context.options)) {\n if (!pathItem || typeof pathItem !== \"object\") {\n continue;\n }\n if (\"$ref\" in pathItem) {\n const options = { isReadonly: context.options.immutable };\n const member = documentMember(url, refType(pathItem.$ref), pathItem, options);\n members.push({ url, member, key: literalKey(url), type: member.type });\n continue;\n }\n const startedAt = context.diagnostics.now();\n const path = pathItemMember(url, pathItem, context);\n members.push(path);\n // The original reports only paths written as properties.\n if (path.member.kind === \"property\") {\n context.diagnostics.debug(`Transformed path \"${url}\"`, context.diagnostics.now() - startedAt);\n }\n }\n return members;\n}\n\n/**\n * The `paths` interface body, with only the path items' own types for the overlap check. The\n * document transform checks the overlaps again once the referenced types are declared.\n */\nexport function pathsType(paths: PathsObject, context: TransformContext): IrType {\n return pathsObjectType(pathMembers(paths, context));\n}\n"],"names":["member"],"mappings":";;;;;;;;;;;;;;;AAsCA,MAAM,iBAAA,GAAoB,aAAA;AAG1B,MAAM,YAAA,uBAA0D,GAAA,CAAI;AAAA,EAClE,CAAC,UAAU,QAAQ,CAAA;AAAA,EACnB,CAAC,WAAW,QAAQ,CAAA;AAAA,EACpB,CAAC,WAAW,SAAS;AACvB,CAAC,CAAA;AACD,MAAM,kBAAA,GAAsC,QAAA;AAE5C,SAAS,iBAAA,CACP,WACA,OAAA,EAC6B;AAC7B,EAAA,OAAO,MAAA,IAAU,aAAa,SAAA,CAAU,IAAA,GACnC,QAAQ,IAAA,CAAK,OAAA,CAAQ,SAAA,CAAU,IAAI,CAAA,GACnC,SAAA;AACP;AAEA,SAAS,iBAAA,CACP,UAAA,EACA,MAAA,EACA,OAAA,EACM;AACN,EAAA,KAAA,MAAW,aAAa,UAAA,EAAY;AAClC,IAAA,MAAM,QAAA,GAAW,iBAAA,CAAkB,SAAA,EAAW,OAAO,CAAA;AACrD,IAAA,IAAI,QAAA,IAAY,QAAA,CAAS,EAAA,KAAO,MAAA,EAAQ;AACtC,MAAA,MAAA,CAAO,GAAA,CAAI,QAAA,CAAS,IAAA,EAAM,QAAQ,CAAA;AAAA,IACpC;AAAA,EACF;AACF;AAGA,SAAS,cAAA,CACP,UACA,OAAA,EAC8B;AAC9B,EAAA,MAAM,MAAA,uBAAa,GAAA,EAA6B;AAChD,EAAA,iBAAA,CAAkB,QAAA,CAAS,UAAA,IAAc,EAAC,EAAG,QAAQ,OAAO,CA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AA,OAAO,OAAA;AACT;AAMO,SAAS,SAAA,CAAU,OAAoB,OAAA,EAAmC;AAC/E,EAAA,OAAO,eAAA,CAAgB,WAAA,CAAY,KAAA,EAAO,OAAO,CAAC,CAAA;AACpD;;"}
|
|
@@ -9,6 +9,13 @@ function unionType(types) {
|
|
|
9
9
|
}
|
|
10
10
|
return types.length === 0 ? NEVER : { kind: "union", types };
|
|
11
11
|
}
|
|
12
|
+
function intersectionType(types) {
|
|
13
|
+
return types.length === 1 ? types[0] : { kind: "intersection", types };
|
|
14
|
+
}
|
|
15
|
+
const NULLISH = /* @__PURE__ */ new Set(["null", "undefined"]);
|
|
16
|
+
function isNullish(type) {
|
|
17
|
+
return type.kind === "keyword" && NULLISH.has(type.keyword);
|
|
18
|
+
}
|
|
12
19
|
function propertyKey(name) {
|
|
13
20
|
return name.kind === "string" ? name.value : name.text;
|
|
14
21
|
}
|
|
@@ -28,6 +35,8 @@ exports.MISSING = MISSING;
|
|
|
28
35
|
exports.NEVER = NEVER;
|
|
29
36
|
exports.OPAQUE = OPAQUE;
|
|
30
37
|
exports.absorbed = absorbed;
|
|
38
|
+
exports.intersectionType = intersectionType;
|
|
39
|
+
exports.isNullish = isNullish;
|
|
31
40
|
exports.propertyKey = propertyKey;
|
|
32
41
|
exports.unionType = unionType;
|
|
33
42
|
//# sourceMappingURL=evaluation.cjs.map
|