openapi-typesafe 1.0.0-rc.1 → 1.0.0-rc.3
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/node-deprecations/node-deprecations.cjs +14 -0
- package/dist/adapters/node-deprecations/node-deprecations.cjs.map +1 -0
- package/dist/adapters/node-deprecations/node-deprecations.mjs +12 -0
- package/dist/adapters/node-deprecations/node-deprecations.mjs.map +1 -0
- 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 +14 -0
- package/dist/api/default-ports.cjs.map +1 -1
- package/dist/api/default-ports.mjs +12 -1
- package/dist/api/default-ports.mjs.map +1 -1
- package/dist/api/discriminator-conflict-warnings.cjs +1 -1
- package/dist/api/discriminator-conflict-warnings.cjs.map +1 -1
- package/dist/api/discriminator-conflict-warnings.mjs +2 -2
- package/dist/api/discriminator-conflict-warnings.mjs.map +1 -1
- package/dist/api/discriminator-patch.cjs +8 -4
- package/dist/api/discriminator-patch.cjs.map +1 -1
- package/dist/api/discriminator-patch.mjs +9 -5
- package/dist/api/discriminator-patch.mjs.map +1 -1
- package/dist/api/discriminator-scan.cjs +2 -2
- package/dist/api/discriminator-scan.cjs.map +1 -1
- package/dist/api/discriminator-scan.mjs +3 -3
- package/dist/api/discriminator-scan.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 +6 -3
- package/dist/api/document-run.cjs.map +1 -1
- package/dist/api/document-run.mjs +6 -3
- 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 +7 -1
- package/dist/api/legacy-callback-warning.cjs.map +1 -1
- package/dist/api/legacy-callback-warning.mjs +8 -2
- 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 +3 -2
- package/dist/api/openapi-ts.cjs.map +1 -1
- package/dist/api/openapi-ts.mjs +4 -3
- package/dist/api/openapi-ts.mjs.map +1 -1
- package/dist/api/string-to-ast.cjs +7 -9
- package/dist/api/string-to-ast.cjs.map +1 -1
- package/dist/api/string-to-ast.mjs +8 -9
- package/dist/api/string-to-ast.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 +2 -2
- package/dist/api/transform-schema.cjs.map +1 -1
- package/dist/api/transform-schema.mjs +3 -3
- 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/components.cjs +1 -1
- package/dist/application/transform/document/components.cjs.map +1 -1
- package/dist/application/transform/document/components.mjs +2 -2
- package/dist/application/transform/document/components.mjs.map +1 -1
- package/dist/application/transform/document/parameters.cjs +2 -2
- package/dist/application/transform/document/parameters.cjs.map +1 -1
- package/dist/application/transform/document/parameters.mjs +3 -3
- package/dist/application/transform/document/parameters.mjs.map +1 -1
- package/dist/application/transform/document/paths.cjs +2 -2
- package/dist/application/transform/document/paths.cjs.map +1 -1
- package/dist/application/transform/document/paths.mjs +3 -3
- 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/application/transform/ref-pointer/pointer-type.cjs +1 -1
- package/dist/application/transform/ref-pointer/pointer-type.cjs.map +1 -1
- package/dist/application/transform/ref-pointer/pointer-type.mjs +2 -2
- package/dist/application/transform/ref-pointer/pointer-type.mjs.map +1 -1
- package/dist/application/transform/schema/access-flags.cjs +1 -1
- package/dist/application/transform/schema/access-flags.cjs.map +1 -1
- package/dist/application/transform/schema/access-flags.mjs +2 -2
- package/dist/application/transform/schema/access-flags.mjs.map +1 -1
- package/dist/application/transform/schema/array-type.cjs +2 -2
- package/dist/application/transform/schema/array-type.cjs.map +1 -1
- package/dist/application/transform/schema/array-type.mjs +3 -3
- package/dist/application/transform/schema/array-type.mjs.map +1 -1
- package/dist/application/transform/schema/composition-type.cjs +3 -3
- package/dist/application/transform/schema/composition-type.cjs.map +1 -1
- package/dist/application/transform/schema/composition-type.mjs +4 -4
- package/dist/application/transform/schema/composition-type.mjs.map +1 -1
- package/dist/application/transform/schema/enum-rule.cjs +3 -5
- package/dist/application/transform/schema/enum-rule.cjs.map +1 -1
- package/dist/application/transform/schema/enum-rule.mjs +4 -6
- package/dist/application/transform/schema/enum-rule.mjs.map +1 -1
- package/dist/application/transform/schema/presence-members.cjs +1 -1
- package/dist/application/transform/schema/presence-members.cjs.map +1 -1
- package/dist/application/transform/schema/presence-members.mjs +2 -2
- package/dist/application/transform/schema/presence-members.mjs.map +1 -1
- package/dist/application/transform/schema/required-variants.cjs +2 -2
- package/dist/application/transform/schema/required-variants.cjs.map +1 -1
- package/dist/application/transform/schema/required-variants.mjs +3 -3
- package/dist/application/transform/schema/required-variants.mjs.map +1 -1
- package/dist/application/transform/schema/unevaluated-properties.cjs +1 -1
- package/dist/application/transform/schema/unevaluated-properties.cjs.map +1 -1
- package/dist/application/transform/schema/unevaluated-properties.mjs +2 -2
- package/dist/application/transform/schema/unevaluated-properties.mjs.map +1 -1
- package/dist/application/transform/schema-comment.cjs +1 -1
- package/dist/application/transform/schema-comment.cjs.map +1 -1
- package/dist/application/transform/schema-comment.mjs +2 -2
- package/dist/application/transform/schema-comment.mjs.map +1 -1
- 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/deprecations.cjs +11 -0
- package/dist/domain/deprecations.cjs.map +1 -0
- package/dist/domain/deprecations.mjs +9 -0
- package/dist/domain/deprecations.mjs.map +1 -0
- 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/output-fixes.cjs +11 -14
- package/dist/domain/output-fixes.cjs.map +1 -1
- package/dist/domain/output-fixes.mjs +11 -13
- package/dist/domain/output-fixes.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 +8 -1
- package/dist/domain/warnings.cjs.map +1 -1
- package/dist/domain/warnings.mjs +8 -1
- package/dist/domain/warnings.mjs.map +1 -1
- package/dist/index.cjs +4 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1185 -59
- package/dist/index.d.mts +1185 -59
- package/dist/index.d.ts +1185 -59
- package/dist/index.mjs +4 -2
- 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 +5 -4
- package/src/adapters/node-deprecations/node-deprecations.ts +14 -0
- 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 +23 -0
- package/src/api/discriminator-conflict-warnings.ts +2 -2
- package/src/api/discriminator-patch.ts +11 -7
- package/src/api/discriminator-scan.ts +3 -3
- package/src/api/document-refs.ts +19 -15
- package/src/api/document-run.ts +14 -6
- package/src/api/footer-sink.ts +34 -2
- package/src/api/legacy-callback-warning.ts +16 -5
- package/src/api/legacy-ts-helpers.ts +7 -5
- package/src/api/legacy-utils.ts +1 -1
- package/src/api/openapi-ts.ts +8 -2
- package/src/api/run-context.ts +37 -0
- package/src/api/string-to-ast.ts +11 -15
- package/src/api/transform-context.ts +10 -8
- package/src/api/transform-schema.ts +9 -8
- package/src/application/ports/deprecation-reporter.ts +9 -0
- package/src/application/print/type-printer.ts +2 -1
- package/src/application/transform/document/components.ts +2 -2
- package/src/application/transform/document/parameters.ts +4 -3
- package/src/application/transform/document/paths.ts +4 -3
- 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/application/transform/ref-pointer/pointer-type.ts +2 -4
- package/src/application/transform/schema/access-flags.ts +2 -2
- package/src/application/transform/schema/array-type.ts +3 -5
- package/src/application/transform/schema/composition-type.ts +4 -4
- package/src/application/transform/schema/enum-rule.ts +4 -6
- package/src/application/transform/schema/presence-members.ts +2 -2
- package/src/application/transform/schema/required-variants.ts +3 -3
- package/src/application/transform/schema/unevaluated-properties.ts +2 -2
- package/src/application/transform/schema-comment.ts +2 -2
- 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/deprecations.ts +15 -0
- package/src/domain/errors.ts +16 -4
- 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/output-fixes.ts +16 -16
- package/src/domain/url-credentials.ts +18 -0
- package/src/domain/warnings.ts +7 -0
- package/src/index.ts +3 -6
- 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":"presence-members.cjs","sources":["../../../../src/application/transform/schema/presence-members.ts"],"sourcesContent":["import type { Schema, SchemaObject } from \"../../../domain/openapi/schema-object.js\";\nimport {
|
|
1
|
+
{"version":3,"file":"presence-members.cjs","sources":["../../../../src/application/transform/schema/presence-members.ts"],"sourcesContent":["import type { Schema, SchemaObject } from \"../../../domain/openapi/schema-object.js\";\nimport { FIX } from \"../../../domain/output-fixes.js\";\nimport type { SchemaRequest } from \"./schema-rule.js\";\n\n/**\n * The `allof-required` fix (ADR-021, roadmap R6). An `allOf` member such as `{ required: [name] }`\n * or `{ type: object }` says nothing about the shape of the object; it only asks that keys are\n * present. Typed as an object of its own, it adds `& Record<string, never>` or `& unknown`. With\n * the fix, such members are left out and their keys are made required in the other members, the\n * same way the parent's `required` is (upstream #1474, #1520, #2165).\n */\n\n/**\n * Keywords that do not change the shape: `type: object`, `required`, and annotations that the\n * generator does not read on an `allOf` member. `default`, `readOnly` and `writeOnly` are left out:\n * they change types elsewhere.\n */\nconst PRESENCE_KEYWORDS: ReadonlySet<string> = new Set([\n \"$comment\",\n \"deprecated\",\n \"description\",\n \"example\",\n \"examples\",\n \"required\",\n \"title\",\n \"type\",\n]);\n\nconst EXTENSION_PREFIX = \"x-\";\nconst OBJECT_TYPE = \"object\";\n\nfunction isStringList(value: unknown): value is readonly string[] {\n return Array.isArray(value) && value.every((entry) => typeof entry === \"string\");\n}\n\nfunction isPresenceKeyword([key, value]: readonly [string, unknown]): boolean {\n if (key === \"type\") {\n return value === OBJECT_TYPE;\n }\n if (key === \"required\") {\n return isStringList(value);\n }\n return PRESENCE_KEYWORDS.has(key) || key.startsWith(EXTENSION_PREFIX);\n}\n\nfunction isPresenceObject(schema: unknown): schema is SchemaObject {\n return (\n typeof schema === \"object\" &&\n schema !== null &&\n !Array.isArray(schema) &&\n !(\"$ref\" in schema) &&\n Object.entries(schema).every(isPresenceKeyword)\n );\n}\n\n/**\n * The schema a member stands for: itself, or the target of a `$ref` with no sibling keywords. The\n * discriminator scan has already added the discriminator property to a mapped target, so such a\n * target is never presence-only.\n */\nfunction memberSchema(member: Schema, request: SchemaRequest): unknown {\n if (typeof member !== \"object\" || member === null || !(\"$ref\" in member)) {\n return member;\n }\n const ref = (member as { readonly $ref: unknown }).$ref;\n if (typeof ref !== \"string\" || Object.keys(member).length !== 1) {\n return undefined;\n }\n return request.context.refs.resolve(ref);\n}\n\n/** The `allOf` members to type, and the keys the left-out members make required. */\nexport interface AllOfMembers {\n readonly members: readonly Schema[];\n readonly required: readonly string[];\n}\n\n/**\n * The members of `allOf` with presence-only ones left out, when the fix is on and another member\n * is left to carry the keys; otherwise the members as they are.\n */\nexport function allOfMembers(members: readonly Schema[], request: SchemaRequest): AllOfMembers {\n const unchanged = { members, required: [] };\n if (!request.context.options.fixes.has(FIX.ALLOF_REQUIRED)) {\n return unchanged;\n }\n const schemas = members.map((member) => memberSchema(member, request));\n const kept = members.filter((_, index) => !isPresenceObject(schemas[index]));\n if (kept.length === 0 || kept.length === members.length) {\n return unchanged;\n }\n const required = schemas.flatMap((schema) =>\n isPresenceObject(schema) ? (schema.required ?? []) : [],\n );\n return { members: kept, required };\n}\n"],"names":["FIX"],"mappings":";;;;AAiBA,MAAM,iBAAA,uBAA6C,GAAA,CAAI;AAAA,EACrD,UAAA;AAAA,EACA,YAAA;AAAA,EACA,aAAA;AAAA,EACA,SAAA;AAAA,EACA,UAAA;AAAA,EACA,UAAA;AAAA,EACA,OAAA;AAAA,EACA;AACF,CAAC,CAAA;AAED,MAAM,gBAAA,GAAmB,IAAA;AACzB,MAAM,WAAA,GAAc,QAAA;AAEpB,SAAS,aAAa,KAAA,EAA4C;AAChE,EAAA,OAAO,KAAA,CAAM,OAAA,CAAQ,KAAK,CAAA,IAAK,KAAA,CAAM,MAAM,CAAC,KAAA,KAAU,OAAO,KAAA,KAAU,QAAQ,CAAA;AACjF;AAEA,SAAS,iBAAA,CAAkB,CAAC,GAAA,EAAK,KAAK,CAAA,EAAwC;AAC5E,EAAA,IAAI,QAAQ,MAAA,EAAQ;AAClB,IAAA,OAAO,KAAA,KAAU,WAAA;AAAA,EACnB;AACA,EAAA,IAAI,QAAQ,UAAA,EAAY;AACtB,IAAA,OAAO,aAAa,KAAK,CAAA;AAAA,EAC3B;AACA,EAAA,OAAO,kBAAkB,GAAA,CAAI,GAAG,CAAA,IAAK,GAAA,CAAI,WAAW,gBAAgB,CAAA;AACtE;AAEA,SAAS,iBAAiB,MAAA,EAAyC;AACjE,EAAA,OACE,OAAO,MAAA,KAAW,QAAA,IAClB,WAAW,IAAA,IAC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,WAAW,OAAA,CAAQ,OAAA;AAAA,IAAQ,CAAC,WAChC,gBAAA,CAAiB,MAAM,IAAK,MAAA,CAAO,QAAA,IAAY,EAAC,GAAK;AAAC,GACxD;AACA,EAAA,OAAO,EAAE,OAAA,EAAS,IAAA,EAAM,QAAA,EAAS;AACnC;;"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { FIX } from '../../../domain/output-fixes.mjs';
|
|
2
2
|
|
|
3
3
|
const PRESENCE_KEYWORDS = /* @__PURE__ */ new Set([
|
|
4
4
|
"$comment",
|
|
@@ -39,7 +39,7 @@ function memberSchema(member, request) {
|
|
|
39
39
|
}
|
|
40
40
|
function allOfMembers(members, request) {
|
|
41
41
|
const unchanged = { members, required: [] };
|
|
42
|
-
if (!request.context.options.fixes.has(
|
|
42
|
+
if (!request.context.options.fixes.has(FIX.ALLOF_REQUIRED)) {
|
|
43
43
|
return unchanged;
|
|
44
44
|
}
|
|
45
45
|
const schemas = members.map((member) => memberSchema(member, request));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"presence-members.mjs","sources":["../../../../src/application/transform/schema/presence-members.ts"],"sourcesContent":["import type { Schema, SchemaObject } from \"../../../domain/openapi/schema-object.js\";\nimport {
|
|
1
|
+
{"version":3,"file":"presence-members.mjs","sources":["../../../../src/application/transform/schema/presence-members.ts"],"sourcesContent":["import type { Schema, SchemaObject } from \"../../../domain/openapi/schema-object.js\";\nimport { FIX } from \"../../../domain/output-fixes.js\";\nimport type { SchemaRequest } from \"./schema-rule.js\";\n\n/**\n * The `allof-required` fix (ADR-021, roadmap R6). An `allOf` member such as `{ required: [name] }`\n * or `{ type: object }` says nothing about the shape of the object; it only asks that keys are\n * present. Typed as an object of its own, it adds `& Record<string, never>` or `& unknown`. With\n * the fix, such members are left out and their keys are made required in the other members, the\n * same way the parent's `required` is (upstream #1474, #1520, #2165).\n */\n\n/**\n * Keywords that do not change the shape: `type: object`, `required`, and annotations that the\n * generator does not read on an `allOf` member. `default`, `readOnly` and `writeOnly` are left out:\n * they change types elsewhere.\n */\nconst PRESENCE_KEYWORDS: ReadonlySet<string> = new Set([\n \"$comment\",\n \"deprecated\",\n \"description\",\n \"example\",\n \"examples\",\n \"required\",\n \"title\",\n \"type\",\n]);\n\nconst EXTENSION_PREFIX = \"x-\";\nconst OBJECT_TYPE = \"object\";\n\nfunction isStringList(value: unknown): value is readonly string[] {\n return Array.isArray(value) && value.every((entry) => typeof entry === \"string\");\n}\n\nfunction isPresenceKeyword([key, value]: readonly [string, unknown]): boolean {\n if (key === \"type\") {\n return value === OBJECT_TYPE;\n }\n if (key === \"required\") {\n return isStringList(value);\n }\n return PRESENCE_KEYWORDS.has(key) || key.startsWith(EXTENSION_PREFIX);\n}\n\nfunction isPresenceObject(schema: unknown): schema is SchemaObject {\n return (\n typeof schema === \"object\" &&\n schema !== null &&\n !Array.isArray(schema) &&\n !(\"$ref\" in schema) &&\n Object.entries(schema).every(isPresenceKeyword)\n );\n}\n\n/**\n * The schema a member stands for: itself, or the target of a `$ref` with no sibling keywords. The\n * discriminator scan has already added the discriminator property to a mapped target, so such a\n * target is never presence-only.\n */\nfunction memberSchema(member: Schema, request: SchemaRequest): unknown {\n if (typeof member !== \"object\" || member === null || !(\"$ref\" in member)) {\n return member;\n }\n const ref = (member as { readonly $ref: unknown }).$ref;\n if (typeof ref !== \"string\" || Object.keys(member).length !== 1) {\n return undefined;\n }\n return request.context.refs.resolve(ref);\n}\n\n/** The `allOf` members to type, and the keys the left-out members make required. */\nexport interface AllOfMembers {\n readonly members: readonly Schema[];\n readonly required: readonly string[];\n}\n\n/**\n * The members of `allOf` with presence-only ones left out, when the fix is on and another member\n * is left to carry the keys; otherwise the members as they are.\n */\nexport function allOfMembers(members: readonly Schema[], request: SchemaRequest): AllOfMembers {\n const unchanged = { members, required: [] };\n if (!request.context.options.fixes.has(FIX.ALLOF_REQUIRED)) {\n return unchanged;\n }\n const schemas = members.map((member) => memberSchema(member, request));\n const kept = members.filter((_, index) => !isPresenceObject(schemas[index]));\n if (kept.length === 0 || kept.length === members.length) {\n return unchanged;\n }\n const required = schemas.flatMap((schema) =>\n isPresenceObject(schema) ? (schema.required ?? []) : [],\n );\n return { members: kept, required };\n}\n"],"names":[],"mappings":";;AAiBA,MAAM,iBAAA,uBAA6C,GAAA,CAAI;AAAA,EACrD,UAAA;AAAA,EACA,YAAA;AAAA,EACA,aAAA;AAAA,EACA,SAAA;AAAA,EACA,UAAA;AAAA,EACA,UAAA;AAAA,EACA,OAAA;AAAA,EACA;AACF,CAAC,CAAA;AAED,MAAM,gBAAA,GAAmB,IAAA;AACzB,MAAM,WAAA,GAAc,QAAA;AAEpB,SAAS,aAAa,KAAA,EAA4C;AAChE,EAAA,OAAO,KAAA,CAAM,OAAA,CAAQ,KAAK,CAAA,IAAK,KAAA,CAAM,MAAM,CAAC,KAAA,KAAU,OAAO,KAAA,KAAU,QAAQ,CAAA;AACjF;AAEA,SAAS,iBAAA,CAAkB,CAAC,GAAA,EAAK,KAAK,CAAA,EAAwC;AAC5E,EAAA,IAAI,QAAQ,MAAA,EAAQ;AAClB,IAAA,OAAO,KAAA,KAAU,WAAA;AAAA,EACnB;AACA,EAAA,IAAI,QAAQ,UAAA,EAAY;AACtB,IAAA,OAAO,aAAa,KAAK,CAAA;AAAA,EAC3B;AACA,EAAA,OAAO,kBAAkB,GAAA,CAAI,GAAG,CAAA,IAAK,GAAA,CAAI,WAAW,gBAAgB,CAAA;AACtE;AAEA,SAAS,iBAAiB,MAAA,EAAyC;AACjE,EAAA,OACE,OAAO,MAAA,KAAW,QAAA,IAClB,WAAW,IAAA,IACX,CAAC,MAAM,OAAA,CAAQ,MAAM,CAAA,IACrB,EAAE,UAAU,MAAA,CAAA,IACZ,MAAA,CAAO,QAAQ,MAAM,CAAA,CAAE,MAAM,iBAAiB,CAAA;AAElD;AAOA,SAAS,YAAA,CAAa,QAAgB,OAAA,EAAiC;AACrE,EAAA,IAAI,OAAO,MAAA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10 @@ function requiredKeyLists(schema, variants, request) {
|
|
|
23
23
|
return isApplicable ? keyLists : null;
|
|
24
24
|
}
|
|
25
25
|
function hasRequiredVariants(schema, variants, request) {
|
|
26
|
-
return request.context.options.fixes.has(outputFixes.
|
|
26
|
+
return request.context.options.fixes.has(outputFixes.FIX.ANYOF_ONEOF_REQUIRED) && isObjectTyped(schema) && requiredKeyLists(schema, variants, request) !== null;
|
|
27
27
|
}
|
|
28
28
|
function requiredVariantsType(previous, schema, variants, request) {
|
|
29
|
-
if (!request.context.options.fixes.has(outputFixes.
|
|
29
|
+
if (!request.context.options.fixes.has(outputFixes.FIX.ANYOF_ONEOF_REQUIRED) || previous === null) {
|
|
30
30
|
return null;
|
|
31
31
|
}
|
|
32
32
|
const keyLists = isObjectTyped(schema) ? requiredKeyLists(schema, variants, request) : null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"required-variants.cjs","sources":["../../../../src/application/transform/schema/required-variants.ts"],"sourcesContent":["import type { IrType } from \"../../../domain/ir/nodes.js\";\nimport type { Schema, SchemaObject } from \"../../../domain/openapi/schema-object.js\";\nimport {
|
|
1
|
+
{"version":3,"file":"required-variants.cjs","sources":["../../../../src/application/transform/schema/required-variants.ts"],"sourcesContent":["import type { IrType } from \"../../../domain/ir/nodes.js\";\nimport type { Schema, SchemaObject } from \"../../../domain/openapi/schema-object.js\";\nimport { FIX } from \"../../../domain/output-fixes.js\";\nimport { unionOf, withRequiredKeys } from \"../type-combinators.js\";\nimport { isKeptProperty } from \"./object-type.js\";\nimport type { SchemaRequest } from \"./schema-rule.js\";\n\n/**\n * The `anyof-oneof-required` fix. `anyOf: [{ required: [a] }, { required: [b] }]` next to\n * `properties` says which of the properties must be there. Each such variant has no type of its\n * own (`unknown`), so the original gives `Props | unknown`, which is `unknown`. With the fix each\n * variant is the parent with its keys required:\n * `WithRequired<Props, \"a\"> | WithRequired<Props, \"b\">`.\n */\n\n/** Keywords that do not change a value's type: a variant of these and `required` counts. */\nconst ANNOTATION_KEYS: ReadonlySet<string> = new Set([\"required\", \"description\", \"title\"]);\n\n/** The `required` keys of a variant that has nothing else, or null for any other variant. */\nfunction requiredOnlyKeys(variant: Schema): readonly string[] | null {\n if (typeof variant !== \"object\" || variant === null || \"$ref\" in variant) {\n return null;\n }\n const { required } = variant as SchemaObject;\n const isRequiredOnly = Object.keys(variant).every((key) => ANNOTATION_KEYS.has(key));\n return isRequiredOnly && Array.isArray(required) && required.length > 0 ? required : null;\n}\n\n/** Whether the schema's type is an object: no `type`, `object`, or a list with `object`. */\nfunction isObjectTyped(schema: SchemaObject): boolean {\n const { type } = schema;\n return (\n type === undefined || type === \"object\" || (Array.isArray(type) && type.includes(\"object\"))\n );\n}\n\n/** The variants' key lists, or null unless each requires only properties the type keeps. */\nfunction requiredKeyLists(\n schema: SchemaObject,\n variants: readonly Schema[],\n request: SchemaRequest,\n): (readonly string[])[] | null {\n const keyLists = variants.map(requiredOnlyKeys);\n // A key of a property the type leaves out (`--exclude-deprecated`) is no key of `WithRequired`.\n const isApplicable =\n keyLists.length > 0 &&\n keyLists.every((keys) => keys?.every((key) => isKeptProperty(schema, key, request)) === true);\n return isApplicable ? (keyLists as (readonly string[])[]) : null;\n}\n\n/** Whether the fix gives `variants` of the schema their keys of its properties. */\nexport function hasRequiredVariants(\n schema: SchemaObject,\n variants: readonly Schema[],\n request: SchemaRequest,\n): boolean {\n return (\n request.context.options.fixes.has(FIX.ANYOF_ONEOF_REQUIRED) &&\n isObjectTyped(schema) &&\n requiredKeyLists(schema, variants, request) !== null\n );\n}\n\n/**\n * The union of the parent type with each variant's keys required, or null where the fix is off\n * or the variants are not all required-only ones over the parent's own, kept properties. Only an\n * object has the keys. For a `type` list, the `object` member has required them already\n * (`multipleTypes`), so the list is the whole type.\n */\nexport function requiredVariantsType(\n previous: IrType | null,\n schema: SchemaObject,\n variants: readonly Schema[],\n request: SchemaRequest,\n): IrType | null {\n if (!request.context.options.fixes.has(FIX.ANYOF_ONEOF_REQUIRED) || previous === null) {\n return null;\n }\n const keyLists = isObjectTyped(schema) ? requiredKeyLists(schema, variants, request) : null;\n if (keyLists === null) {\n return null;\n }\n if (Array.isArray(schema.type)) {\n return previous;\n }\n request.context.footer.requireWithRequired();\n return unionOf(keyLists.map((keys) => withRequiredKeys(previous, keys)));\n}\n"],"names":["isKeptProperty","FIX","unionOf","withRequiredKeys"],"mappings":";;;;;;AAgBA,MAAM,kCAAuC,IAAI,GAAA,CAAI,CAAC,UAAA,EAAY,aAAA,EAAe,OAAO,CAAC,CAAA;AAGzF,SAAS,iBAAiB,OAAA,EAA2C;AACnE,EAAA,IAAI,OAAO,OAAA,KAAY,QAAA,IAAY,OAAA,KAAY,IAAA,IAAQ,UAAU,OAAA,EAAS;AACxE,IAAA,OAAO,IAAA;AAAA,EACT;AACA,EAAA,MAAM,EAAE,UAAS,GAAI,OAAA;AACrB,EAAA,MAAM,cAAA,GAAiB,MAAA,CAAO,IAAA,CAAK,OAAO,CAAA,CAAE,KAAA,CAAM,CAAC,GAAA,KAAQ,eAAA,CAAgB,GAAA,CAAI,GAAG,CAAC,CAAA;AACnF,EAAA,OAAO,cAAA,IAAkB,MAAM,OAAA,CAAQ,QAAQ,KAAK,QAAA,CAAS,MAAA,GAAS,IAAI,QAAA,GAAW,IAAA;AACvF;AAGA,SAAS,cAAc,MAAA,EAA+B;AACpD,EAAA,MAAM,EAAE,MAAK,GAAI,MAAA;AACjB,EAAA,OACE,IAAA,KAAS,MAAA,IAAa,IAAA,KAAS,QAAA,IAAa,KAAA,CAAM,QAAQ,IAAI,CAAA,IAAK,IAAA,CAAK,QAAA,CAAS,QAAQ,CAAA;AAE7F;AAGA,SAAS,gBAAA,CACP,MAAA,EACA,QAAA,EACA,OAAA,EAC8B;AAC9B,EAAA,MAAM,QAAA,GAAW,QAAA,CAAS,GAAA,CAAI,gBAAgB,CAAA;AAE9C,EAAA,MAAM,eACJ,QAAA,CAAS,MAAA,GAAS,KAClB,QAAA,CAAS,KAAA,CAA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.mjs';
|
|
2
2
|
import { unionOf, withRequiredKeys } from '../type-combinators.mjs';
|
|
3
3
|
import { isKeptProperty } from './object-type.mjs';
|
|
4
4
|
|
|
@@ -21,10 +21,10 @@ function requiredKeyLists(schema, variants, request) {
|
|
|
21
21
|
return isApplicable ? keyLists : null;
|
|
22
22
|
}
|
|
23
23
|
function hasRequiredVariants(schema, variants, request) {
|
|
24
|
-
return request.context.options.fixes.has(
|
|
24
|
+
return request.context.options.fixes.has(FIX.ANYOF_ONEOF_REQUIRED) && isObjectTyped(schema) && requiredKeyLists(schema, variants, request) !== null;
|
|
25
25
|
}
|
|
26
26
|
function requiredVariantsType(previous, schema, variants, request) {
|
|
27
|
-
if (!request.context.options.fixes.has(
|
|
27
|
+
if (!request.context.options.fixes.has(FIX.ANYOF_ONEOF_REQUIRED) || previous === null) {
|
|
28
28
|
return null;
|
|
29
29
|
}
|
|
30
30
|
const keyLists = isObjectTyped(schema) ? requiredKeyLists(schema, variants, request) : null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"required-variants.mjs","sources":["../../../../src/application/transform/schema/required-variants.ts"],"sourcesContent":["import type { IrType } from \"../../../domain/ir/nodes.js\";\nimport type { Schema, SchemaObject } from \"../../../domain/openapi/schema-object.js\";\nimport {
|
|
1
|
+
{"version":3,"file":"required-variants.mjs","sources":["../../../../src/application/transform/schema/required-variants.ts"],"sourcesContent":["import type { IrType } from \"../../../domain/ir/nodes.js\";\nimport type { Schema, SchemaObject } from \"../../../domain/openapi/schema-object.js\";\nimport { FIX } from \"../../../domain/output-fixes.js\";\nimport { unionOf, withRequiredKeys } from \"../type-combinators.js\";\nimport { isKeptProperty } from \"./object-type.js\";\nimport type { SchemaRequest } from \"./schema-rule.js\";\n\n/**\n * The `anyof-oneof-required` fix. `anyOf: [{ required: [a] }, { required: [b] }]` next to\n * `properties` says which of the properties must be there. Each such variant has no type of its\n * own (`unknown`), so the original gives `Props | unknown`, which is `unknown`. With the fix each\n * variant is the parent with its keys required:\n * `WithRequired<Props, \"a\"> | WithRequired<Props, \"b\">`.\n */\n\n/** Keywords that do not change a value's type: a variant of these and `required` counts. */\nconst ANNOTATION_KEYS: ReadonlySet<string> = new Set([\"required\", \"description\", \"title\"]);\n\n/** The `required` keys of a variant that has nothing else, or null for any other variant. */\nfunction requiredOnlyKeys(variant: Schema): readonly string[] | null {\n if (typeof variant !== \"object\" || variant === null || \"$ref\" in variant) {\n return null;\n }\n const { required } = variant as SchemaObject;\n const isRequiredOnly = Object.keys(variant).every((key) => ANNOTATION_KEYS.has(key));\n return isRequiredOnly && Array.isArray(required) && required.length > 0 ? required : null;\n}\n\n/** Whether the schema's type is an object: no `type`, `object`, or a list with `object`. */\nfunction isObjectTyped(schema: SchemaObject): boolean {\n const { type } = schema;\n return (\n type === undefined || type === \"object\" || (Array.isArray(type) && type.includes(\"object\"))\n );\n}\n\n/** The variants' key lists, or null unless each requires only properties the type keeps. */\nfunction requiredKeyLists(\n schema: SchemaObject,\n variants: readonly Schema[],\n request: SchemaRequest,\n): (readonly string[])[] | null {\n const keyLists = variants.map(requiredOnlyKeys);\n // A key of a property the type leaves out (`--exclude-deprecated`) is no key of `WithRequired`.\n const isApplicable =\n keyLists.length > 0 &&\n keyLists.every((keys) => keys?.every((key) => isKeptProperty(schema, key, request)) === true);\n return isApplicable ? (keyLists as (readonly string[])[]) : null;\n}\n\n/** Whether the fix gives `variants` of the schema their keys of its properties. */\nexport function hasRequiredVariants(\n schema: SchemaObject,\n variants: readonly Schema[],\n request: SchemaRequest,\n): boolean {\n return (\n request.context.options.fixes.has(FIX.ANYOF_ONEOF_REQUIRED) &&\n isObjectTyped(schema) &&\n requiredKeyLists(schema, variants, request) !== null\n );\n}\n\n/**\n * The union of the parent type with each variant's keys required, or null where the fix is off\n * or the variants are not all required-only ones over the parent's own, kept properties. Only an\n * object has the keys. For a `type` list, the `object` member has required them already\n * (`multipleTypes`), so the list is the whole type.\n */\nexport function requiredVariantsType(\n previous: IrType | null,\n schema: SchemaObject,\n variants: readonly Schema[],\n request: SchemaRequest,\n): IrType | null {\n if (!request.context.options.fixes.has(FIX.ANYOF_ONEOF_REQUIRED) || previous === null) {\n return null;\n }\n const keyLists = isObjectTyped(schema) ? requiredKeyLists(schema, variants, request) : null;\n if (keyLists === null) {\n return null;\n }\n if (Array.isArray(schema.type)) {\n return previous;\n }\n request.context.footer.requireWithRequired();\n return unionOf(keyLists.map((keys) => withRequiredKeys(previous, keys)));\n}\n"],"names":[],"mappings":";;;;AAgBA,MAAM,kCAAuC,IAAI,GAAA,CAAI,CAAC,UAAA,EAAY,aAAA,EAAe,OAAO,CAAC,CAAA;AAGzF,SAAS,iBAAiB,OAAA,EAA2C;AACnE,EAAA,IAAI,OAAO,OAAA,KAAY,QAAA,IAAY,OAAA,KAAY,IAAA,IAAQ,UAAU,OAAA,EAAS;AACxE,IAAA,OAAO,IAAA;AAAA,EACT;AACA,EAAA,MAAM,EAAE,UAAS,GAAI,OAAA;AACrB,EAAA,MAAM,cAAA,GAAiB,MAAA,CAAO,IAAA,CAAK,OAAO,CAAA,CAAE,KAAA,CAAM,CAAC,GAAA,KAAQ,eAAA,CAAgB,GAAA,CAAI,GAAG,CAAC,CAAA;AACnF,EAAA,OAAO,cAAA,IAAkB,MAAM,OAAA,CAAQ,QAAQ,KAAK,QAAA,CAAS,MAAA,GAAS,IAAI,QAAA,GAAW,IAAA;AACvF;AAGA,SAAS,cAAc,MAAA,EAA+B;AACpD,EAAA,MAAM,EAAE,MAAK,GAAI,MAAA;AACjB,EAAA,OACE,IAAA,KAAS,MAAA,IAAa,IAAA,KAAS,QAAA,IAAa,KAAA,CAAM,QAAQ,IAAI,CAAA,IAAK,IAAA,CAAK,QAAA,CAAS,QAAQ,CAAA;AAE7F;AAGA,SAAS,gBAAA,CACP,MAAA,EACA,QAAA,EACA,OAAA,EAC8B;AAC9B,EAAA,MAAM,QAAA,GAAW,QAAA,CAAS,GAAA,CAAI,gBAAgB,CAAA;AAE9C,EAAA,MAA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const typeCombinators = require('../type-combinators.cjs');
|
|
|
8
8
|
|
|
9
9
|
function unevaluatedSchemaOf(schema, request) {
|
|
10
10
|
const { unevaluatedProperties } = schema;
|
|
11
|
-
if (!request.context.options.fixes.has(outputFixes.
|
|
11
|
+
if (!request.context.options.fixes.has(outputFixes.FIX.UNEVALUATED_PROPERTIES) || unevaluatedProperties === void 0 || unevaluatedProperties === false || schema.additionalProperties !== void 0) {
|
|
12
12
|
return null;
|
|
13
13
|
}
|
|
14
14
|
return schema.type === void 0 || schema.type === "object" ? unevaluatedProperties : null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"unevaluated-properties.cjs","sources":["../../../../src/application/transform/schema/unevaluated-properties.ts"],"sourcesContent":["import {\n irKeyword,\n irObject,\n irReference,\n irStringIndexSignature,\n} from \"../../../domain/ir/constructors.js\";\nimport type { IrMember, IrType } from \"../../../domain/ir/nodes.js\";\nimport type { Schema, SchemaObject } from \"../../../domain/openapi/schema-object.js\";\nimport {
|
|
1
|
+
{"version":3,"file":"unevaluated-properties.cjs","sources":["../../../../src/application/transform/schema/unevaluated-properties.ts"],"sourcesContent":["import {\n irKeyword,\n irObject,\n irReference,\n irStringIndexSignature,\n} from \"../../../domain/ir/constructors.js\";\nimport type { IrMember, IrType } from \"../../../domain/ir/nodes.js\";\nimport type { Schema, SchemaObject } from \"../../../domain/openapi/schema-object.js\";\nimport { FIX } from \"../../../domain/output-fixes.js\";\nimport { HELPER_TYPES } from \"../helper-types.js\";\nimport { UNKNOWN_TYPE } from \"../shared-types.js\";\nimport { unionOf } from \"../type-combinators.js\";\nimport type { SchemaRequest } from \"./schema-rule.js\";\n\n/**\n * The `unevaluated-properties` fix (ADR-021, roadmap R11; upstream #2839, #2880). Without it\n * `unevaluatedProperties` is ignored, so its value type is lost and an object with only that\n * keyword is `Record<string, never>`. With it the composed type is intersected with an index\n * signature, as `additionalProperties` gives: every key that no other keyword declares has the\n * `unevaluatedProperties` type. TypeScript checks that index signature against every key of a\n * value, declared ones included, so its type also holds the types of the declared properties\n * (of this schema and of its `allOf`, `anyOf` and `oneOf` members); otherwise a valid value would\n * not be assignable. `false` adds nothing, like `additionalProperties: false`: TypeScript has no\n * closed object type.\n */\n\n/**\n * Whether `unevaluatedProperties` applies. It types the keys left over at this level, so it\n * does nothing next to `additionalProperties` (which already types them all), and only an object\n * schema has keys. A `type` list is typed one listed type at a time, so its object gets it.\n */\nfunction unevaluatedSchemaOf(schema: SchemaObject, request: SchemaRequest): Schema | null {\n const { unevaluatedProperties } = schema;\n if (\n !request.context.options.fixes.has(FIX.UNEVALUATED_PROPERTIES) ||\n unevaluatedProperties === undefined ||\n unevaluatedProperties === false ||\n schema.additionalProperties !== undefined\n ) {\n return null;\n }\n return schema.type === undefined || schema.type === \"object\" ? unevaluatedProperties : null;\n}\n\nfunction isAnySchema(schema: Schema): boolean {\n return schema === true || (typeof schema === \"object\" && Object.keys(schema).length === 0);\n}\n\nfunction memberValueTypes(member: IrMember): IrType[] {\n if (member.kind === \"indexSignature\") {\n return [member.type];\n }\n return member.isOptional ? [member.type, irKeyword(\"undefined\")] : [member.type];\n}\n\n/**\n * The value types a key of the composed type can have, read from its IR: the members of object\n * types, through unions and intersections. A reference (a `$ref`, `Omit`, `WithRequired`) or a raw\n * type is read with `PropertyValues`. Primitives, literals and arrays declare no keys.\n */\nfunction declaredValueTypes(type: IrType, request: SchemaRequest): IrType[] {\n switch (type.kind) {\n case \"object\":\n return type.members.flatMap(memberValueTypes);\n case \"union\":\n case \"intersection\":\n return type.types.flatMap((member) => declaredValueTypes(member, request));\n case \"reference\":\n case \"indexedAccess\":\n case \"raw\":\n request.context.footer.requirePropertyValues();\n return [irReference(HELPER_TYPES.propertyValues.name, [type])];\n case \"keyword\":\n case \"stringLiteral\":\n case \"numberLiteral\":\n case \"booleanLiteral\":\n case \"array\":\n case \"tuple\":\n case \"readonly\":\n return [];\n }\n}\n\n/**\n * The index signature object for `unevaluatedProperties`, given the composed type of the other\n * keywords (null when there is none). Null when the fix is off or the keyword does not apply.\n */\nexport function unevaluatedPropertiesType(\n schema: SchemaObject,\n composed: IrType | null,\n request: SchemaRequest,\n): IrType | null {\n const unevaluated = unevaluatedSchemaOf(schema, request);\n if (unevaluated === null) {\n return null;\n }\n const valueTypes = isAnySchema(unevaluated)\n ? [UNKNOWN_TYPE]\n : [\n request.transformNested(unevaluated, request.location, true),\n ...(composed === null ? [] : declaredValueTypes(composed, request)),\n ];\n const { immutable } = request.context.options;\n return irObject([irStringIndexSignature(\"key\", unionOf(valueTypes), immutable)]);\n}\n"],"names":["FIX","irKeyword","irReference","HELPER_TYPES","UNKNOWN_TYPE","irObject","irStringIndexSignature","unionOf"],"mappings":";;;;;;;;AA+BA,SAAS,mBAAA,CAAoB,QAAsB,OAAA,EAAuC;AACxF,EAAA,MAAM,EAAE,uBAAsB,GAAI,MAAA;AAClC,EAAA,IACE,CAAC,OAAA,CAAQ,OAAA,CAAQ,OAAA,CAAQ,MAAM,GAAA,CAAIA,eAAA,CAAI,sBAAsB,CAAA,IAC7D,0BAA0B,MAAA,IAC1B,qBAAA,KAA0B,KAAA,IAC1B,MAAA,CAAO,yBAAyB,MAAA,EAChC;AACA,IAAA,OAAO,IAAA;AAAA,EACT;AACA,EAAA,OAAO,OAAO,IAAA,KAAS,MAAA,IAAa,MAAA,CAAO,IAAA,KAAS,WAAW,qBAAA,GAAwB,IAAA;AACzF;AAEA,SAAS,YAAY,MAAA,EAAyB;AAC5C,EAAA,OAAO,MAAA,KAAW,QAAS,OAAO,MAAA,KAAW,YAAY,MAAA,CAAO,IAAA,CAAK,MAAM,CAAA,CAAE,MAAA,KAAW,CAAA;AAC1F;AAEA,SAAS,iBAAiB,MAAA,EAA4B;AACpD,EAAA,IAAI,MAAA,CAAO,SAAS,gBAAA,EAAkB;AACpC,IAAA,OAAO,CAAC,OAAO,IAAI,CAAA;AAAA,EACrB;AACA,EAAA,OAAO,MAAA,CAAO,UAAA,GAAa,CAAC,MAAA,CAAO,IAAA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AC,GAAI,kBAAA,CAAmB,UAAU,OAAO;AAAA,GACnE;AACJ,EAAA,MAAM,EAAE,SAAA,EAAU,GAAI,OAAA,CAAQ,OAAA,CAAQ,OAAA;AACtC,EAAA,OAAOC,qBAAA,CAAS,CAACC,mCAAA,CAAuB,KAAA,EAAOC,wBAAQ,UAAU,CAAA,EAAG,SAAS,CAAC,CAAC,CAAA;AACjF;;"}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { irObject, irStringIndexSignature, irReference, irKeyword } from '../../../domain/ir/constructors.mjs';
|
|
2
|
-
import {
|
|
2
|
+
import { FIX } from '../../../domain/output-fixes.mjs';
|
|
3
3
|
import { HELPER_TYPES } from '../helper-types.mjs';
|
|
4
4
|
import { UNKNOWN_TYPE } from '../shared-types.mjs';
|
|
5
5
|
import { unionOf } from '../type-combinators.mjs';
|
|
6
6
|
|
|
7
7
|
function unevaluatedSchemaOf(schema, request) {
|
|
8
8
|
const { unevaluatedProperties } = schema;
|
|
9
|
-
if (!request.context.options.fixes.has(
|
|
9
|
+
if (!request.context.options.fixes.has(FIX.UNEVALUATED_PROPERTIES) || unevaluatedProperties === void 0 || unevaluatedProperties === false || schema.additionalProperties !== void 0) {
|
|
10
10
|
return null;
|
|
11
11
|
}
|
|
12
12
|
return schema.type === void 0 || schema.type === "object" ? unevaluatedProperties : null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"unevaluated-properties.mjs","sources":["../../../../src/application/transform/schema/unevaluated-properties.ts"],"sourcesContent":["import {\n irKeyword,\n irObject,\n irReference,\n irStringIndexSignature,\n} from \"../../../domain/ir/constructors.js\";\nimport type { IrMember, IrType } from \"../../../domain/ir/nodes.js\";\nimport type { Schema, SchemaObject } from \"../../../domain/openapi/schema-object.js\";\nimport {
|
|
1
|
+
{"version":3,"file":"unevaluated-properties.mjs","sources":["../../../../src/application/transform/schema/unevaluated-properties.ts"],"sourcesContent":["import {\n irKeyword,\n irObject,\n irReference,\n irStringIndexSignature,\n} from \"../../../domain/ir/constructors.js\";\nimport type { IrMember, IrType } from \"../../../domain/ir/nodes.js\";\nimport type { Schema, SchemaObject } from \"../../../domain/openapi/schema-object.js\";\nimport { FIX } from \"../../../domain/output-fixes.js\";\nimport { HELPER_TYPES } from \"../helper-types.js\";\nimport { UNKNOWN_TYPE } from \"../shared-types.js\";\nimport { unionOf } from \"../type-combinators.js\";\nimport type { SchemaRequest } from \"./schema-rule.js\";\n\n/**\n * The `unevaluated-properties` fix (ADR-021, roadmap R11; upstream #2839, #2880). Without it\n * `unevaluatedProperties` is ignored, so its value type is lost and an object with only that\n * keyword is `Record<string, never>`. With it the composed type is intersected with an index\n * signature, as `additionalProperties` gives: every key that no other keyword declares has the\n * `unevaluatedProperties` type. TypeScript checks that index signature against every key of a\n * value, declared ones included, so its type also holds the types of the declared properties\n * (of this schema and of its `allOf`, `anyOf` and `oneOf` members); otherwise a valid value would\n * not be assignable. `false` adds nothing, like `additionalProperties: false`: TypeScript has no\n * closed object type.\n */\n\n/**\n * Whether `unevaluatedProperties` applies. It types the keys left over at this level, so it\n * does nothing next to `additionalProperties` (which already types them all), and only an object\n * schema has keys. A `type` list is typed one listed type at a time, so its object gets it.\n */\nfunction unevaluatedSchemaOf(schema: SchemaObject, request: SchemaRequest): Schema | null {\n const { unevaluatedProperties } = schema;\n if (\n !request.context.options.fixes.has(FIX.UNEVALUATED_PROPERTIES) ||\n unevaluatedProperties === undefined ||\n unevaluatedProperties === false ||\n schema.additionalProperties !== undefined\n ) {\n return null;\n }\n return schema.type === undefined || schema.type === \"object\" ? unevaluatedProperties : null;\n}\n\nfunction isAnySchema(schema: Schema): boolean {\n return schema === true || (typeof schema === \"object\" && Object.keys(schema).length === 0);\n}\n\nfunction memberValueTypes(member: IrMember): IrType[] {\n if (member.kind === \"indexSignature\") {\n return [member.type];\n }\n return member.isOptional ? [member.type, irKeyword(\"undefined\")] : [member.type];\n}\n\n/**\n * The value types a key of the composed type can have, read from its IR: the members of object\n * types, through unions and intersections. A reference (a `$ref`, `Omit`, `WithRequired`) or a raw\n * type is read with `PropertyValues`. Primitives, literals and arrays declare no keys.\n */\nfunction declaredValueTypes(type: IrType, request: SchemaRequest): IrType[] {\n switch (type.kind) {\n case \"object\":\n return type.members.flatMap(memberValueTypes);\n case \"union\":\n case \"intersection\":\n return type.types.flatMap((member) => declaredValueTypes(member, request));\n case \"reference\":\n case \"indexedAccess\":\n case \"raw\":\n request.context.footer.requirePropertyValues();\n return [irReference(HELPER_TYPES.propertyValues.name, [type])];\n case \"keyword\":\n case \"stringLiteral\":\n case \"numberLiteral\":\n case \"booleanLiteral\":\n case \"array\":\n case \"tuple\":\n case \"readonly\":\n return [];\n }\n}\n\n/**\n * The index signature object for `unevaluatedProperties`, given the composed type of the other\n * keywords (null when there is none). Null when the fix is off or the keyword does not apply.\n */\nexport function unevaluatedPropertiesType(\n schema: SchemaObject,\n composed: IrType | null,\n request: SchemaRequest,\n): IrType | null {\n const unevaluated = unevaluatedSchemaOf(schema, request);\n if (unevaluated === null) {\n return null;\n }\n const valueTypes = isAnySchema(unevaluated)\n ? [UNKNOWN_TYPE]\n : [\n request.transformNested(unevaluated, request.location, true),\n ...(composed === null ? [] : declaredValueTypes(composed, request)),\n ];\n const { immutable } = request.context.options;\n return irObject([irStringIndexSignature(\"key\", unionOf(valueTypes), immutable)]);\n}\n"],"names":[],"mappings":";;;;;;AA+BA,SAAS,mBAAA,CAAoB,QAAsB,OAAA,EAAuC;AACxF,EAAA,MAAM,EAAE,uBAAsB,GAAI,MAAA;AAClC,EAAA,IACE,CAAC,OAAA,CAAQ,OAAA,CAAQ,OAAA,CAAQ,MAAM,GAAA,CAAI,GAAA,CAAI,sBAAsB,CAAA,IAC7D,0BAA0B,MAAA,IAC1B,qBAAA,KAA0B,KAAA,IAC1B,MAAA,CAAO,yBAAyB,MAAA,EAChC;AACA,IAAA,OAAO,IAAA;AAAA,EACT;AACA,EAAA,OAAO,OAAO,IAAA,KAAS,MAAA,IAAa,MAAA,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AEd;AAMO,SAAS,yBAAA,CACd,MAAA,EACA,QAAA,EACA,OAAA,EACe;AACf,EAAA,MAAM,WAAA,GAAc,mBAAA,CAAoB,MAAA,EAAQ,OAAO,CAAA;AACvD,EAAA,IAAI,gBAAgB,IAAA,EAAM;AACxB,IAAA,OAAO,IAAA;AAAA,EACT;AACA,EAAA,MAAM,aAAa,WAAA,CAAY,WAAW,CAAA,GACtC,CAAC,YAAY,CAAA,GACb;AAAA,IACE,OAAA,CAAQ,eAAA,CAAgB,WAAA,EAAa,OAAA,CAAQ,UAAU,IAAI,CAAA;AAAA,IAC3D,GAAI,QAAA,KAAa,IAAA,GAAO,EAAC,GAAI,kBAAA,CAAmB,UAAU,OAAO;AAAA,GACnE;AACJ,EAAA,MAAM,EAAE,SAAA,EAAU,GAAI,OAAA,CAAQ,OAAA,CAAQ,OAAA;AACtC,EAAA,OAAO,QAAA,CAAS,CAAC,sBAAA,CAAuB,KAAA,EAAO,QAAQ,UAAU,CAAA,EAAG,SAAS,CAAC,CAAC,CAAA;AACjF;;"}
|
|
@@ -15,7 +15,7 @@ const MEMBER_LISTS = [
|
|
|
15
15
|
const BULLET_CONTINUATION = "\n ";
|
|
16
16
|
const ORIGINAL_SCHEMA_COMMENT = { hasMemberDescriptions: false };
|
|
17
17
|
function schemaCommentOptionsOf(fixes) {
|
|
18
|
-
return { hasMemberDescriptions: fixes.has(outputFixes.
|
|
18
|
+
return { hasMemberDescriptions: fixes.has(outputFixes.FIX.ANYOF_ONEOF_DESCRIPTIONS) };
|
|
19
19
|
}
|
|
20
20
|
function formatValue(value) {
|
|
21
21
|
const text = typeof value === "object" ? JSON.stringify(value, null, JSON_INDENT) : value;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema-comment.cjs","sources":["../../../src/application/transform/schema-comment.ts"],"sourcesContent":["import type { IrComment, IrPropertySignature } from \"../../domain/ir/nodes.js\";\nimport { OutputFix } from \"../../domain/output-fixes.js\";\n\n/** Schema fields that produce JSDoc lines (the original `AnnotatedSchemaObject`). */\nexport interface AnnotatedSchema {\n readonly const?: unknown;\n readonly default?: unknown;\n readonly deprecated?: boolean;\n readonly description?: string;\n readonly enum?: unknown[];\n readonly example?: unknown;\n readonly examples?: unknown;\n readonly format?: string;\n readonly nullable?: boolean;\n readonly summary?: string;\n readonly title?: string;\n readonly type?: string | string[];\n}\n\nconst LINE_BREAK_RE = /\\r?\\n/g;\n/** Continuation lines of a multi-line value are indented under the tag. */\nconst CONTINUATION = \"\\n * \";\nconst COMMENT_END_RE = /\\*\\//g;\n/** Tags written as `@tag value`; an empty string is kept only where it is a meaningful value. */\nconst VALUE_TAGS = [\"description\", \"default\", \"example\"] as const;\nconst TAGS_KEEPING_EMPTY_STRING: ReadonlySet<string> = new Set([\"default\", \"example\"]);\nconst JSON_INDENT = 2;\n/** The member lists whose descriptions `anyof-oneof-descriptions` adds, with their headings. */\nconst MEMBER_LISTS = [\n [\"anyOf\", \"Any of:\"],\n [\"oneOf\", \"One of:\"],\n] as const;\n/** The later lines of a member's description are indented under its bullet. */\nconst BULLET_CONTINUATION = \"\\n \";\n\n/** How a schema's JSDoc is written. */\nexport interface SchemaCommentOptions {\n /** `anyof-oneof-descriptions`: the descriptions of `anyOf` and `oneOf` members are listed. */\n readonly hasMemberDescriptions: boolean;\n}\n\n/** The JSDoc the original writes. */\nexport const ORIGINAL_SCHEMA_COMMENT: SchemaCommentOptions = { hasMemberDescriptions: false };\n\n/** How the fixes turned on write a schema's JSDoc. */\nexport function schemaCommentOptionsOf(fixes: ReadonlySet<OutputFix>): SchemaCommentOptions {\n return { hasMemberDescriptions: fixes.has(OutputFix.ANYOF_ONEOF_DESCRIPTIONS) };\n}\n\nfunction formatValue(value: unknown): string {\n const text = typeof value === \"object\" ? JSON.stringify(value, null, JSON_INDENT) : value;\n return String(text).trim().replace(LINE_BREAK_RE, CONTINUATION);\n}\n\nfunction plainLines(schema: AnnotatedSchema): string[] {\n const lines: string[] = [];\n for (const text of [schema.title, schema.summary]) {\n // The document is not validated against the schema dialect: a property named `summary`\n // written without `properties:` makes `summary` an object (upstream #2349).\n if (typeof text === \"string\" && text) {\n lines.push(text.trim().replace(LINE_BREAK_RE, CONTINUATION));\n }\n }\n if (schema.format) {\n lines.push(`Format: ${schema.format}`);\n }\n if (schema.deprecated) {\n lines.push(\"@deprecated\");\n }\n return lines;\n}\n\nfunction valueTagLines(schema: AnnotatedSchema): string[] {\n const lines: string[] = [];\n for (const tag of VALUE_TAGS) {\n const value = schema[tag];\n if (value === undefined || (value === \"\" && !TAGS_KEEPING_EMPTY_STRING.has(tag))) {\n continue;\n }\n lines.push(`@${tag} ${formatValue(value)}`);\n }\n if (Array.isArray(schema.examples)) {\n lines.push(...schema.examples.map((example: unknown) => `@example ${formatValue(example)}`));\n }\n return lines;\n}\n\nfunction enumTypeName(schema: AnnotatedSchema): string {\n if (Array.isArray(schema.type)) {\n return schema.type.join(\"|\");\n }\n return typeof schema.type === \"string\" ? schema.type : \"unknown\";\n}\n\nfunction constantAndEnumLines(schema: AnnotatedSchema): string[] {\n const lines: string[] = [];\n if (\"const\" in schema) {\n lines.push(\"@constant\");\n }\n if (schema.enum) {\n lines.push(`@enum {${enumTypeName(schema)}${schema.nullable ? \"|null\" : \"\"}}`);\n }\n return lines;\n}\n\n/** The own, non-blank `description` of each member; a member without one is left out. */\nfunction memberDescriptions(members: unknown): string[] {\n if (!Array.isArray(members)) {\n return [];\n }\n return members.flatMap((member: unknown) => {\n const description = isSchemaObject(member) ? member.description : undefined;\n return typeof description === \"string\" && description.trim() !== \"\" ? [description.trim()] : [];\n });\n}\n\n/** Each member list with a described member, as a heading and one bullet per description. */\nfunction memberDescriptionLists(schema: SchemaRecord): string[] {\n return MEMBER_LISTS.flatMap(([keyword, heading]) => {\n const descriptions = memberDescriptions(schema[keyword]);\n if (descriptions.length === 0) {\n return [];\n }\n const bullets = descriptions.map(\n (description) => `- ${description.replace(LINE_BREAK_RE, BULLET_CONTINUATION)}`,\n );\n return [[heading, ...bullets].join(\"\\n\")];\n });\n}\n\n/**\n * The schema with the descriptions of its `anyOf` and `oneOf` members after its own description\n * (upstream #2142). A member's description holds only where that member applies, so each one is\n * listed under its keyword rather than merged into one sentence. A `description` that is not a\n * string is kept as it is.\n */\nfunction withMemberDescriptions(schema: SchemaRecord): AnnotatedSchema {\n const lists = memberDescriptionLists(schema);\n const own = schema.description;\n if (lists.length === 0 || (own !== undefined && typeof own !== \"string\")) {\n // As the original, the keywords are read as they are, without validating them.\n return schema as AnnotatedSchema;\n }\n const ownLines = typeof own === \"string\" && own.trim() !== \"\" ? [own.trim()] : [];\n return { ...(schema as AnnotatedSchema), description: [...ownLines, ...lists].join(\"\\n\") };\n}\n\n/** A schema object read without knowing its keywords. */\ninterface SchemaRecord {\n readonly description?: unknown;\n readonly [keyword: string]: unknown;\n}\n\nfunction isSchemaObject(value: unknown): value is SchemaRecord {\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\n/**\n * The JSDoc comment the original writes for a schema (`addJSDocComment`): title, summary and\n * format, then `@deprecated`, `@description`, `@default`, `@example`, `@constant` and `@enum`\n * tags. One short line stays on the `/**` line; anything else is a block. Returns null when there\n * is nothing to say, or when the value is not a schema object.\n */\nexport function schemaComment(\n schema: unknown,\n options: SchemaCommentOptions = ORIGINAL_SCHEMA_COMMENT,\n): IrComment | null {\n if (!isSchemaObject(schema)) {\n return null;\n }\n const annotated = options.hasMemberDescriptions\n ? withMemberDescriptions(schema)\n : (schema as AnnotatedSchema);\n const lines = [\n ...plainLines(annotated),\n ...valueTagLines(annotated),\n ...constantAndEnumLines(annotated),\n ];\n if (lines.length === 0) {\n return null;\n }\n const isSingleLine = lines.length === 1 && !lines.some((line) => line.includes(\"\\n\"));\n const text = isSingleLine ? `* ${lines.join(\"\\n\")} ` : `*\\n * ${lines.join(\"\\n * \")}\\n `;\n // A `*/` inside a value would end the comment early.\n return { kind: \"block\", text: text.replace(COMMENT_END_RE, \"*\\\\/\") };\n}\n\n/** The property with the schema's JSDoc added after the comments it already has. */\nexport function withSchemaComment(\n property: IrPropertySignature,\n schema: unknown,\n options: SchemaCommentOptions = ORIGINAL_SCHEMA_COMMENT,\n): IrPropertySignature {\n const comment = schemaComment(schema, options);\n return comment === null ? property : { ...property, comments: [...property.comments, comment] };\n}\n"],"names":["OutputFix"],"mappings":";;;;AAmBA,MAAM,aAAA,GAAgB,QAAA;AAEtB,MAAM,YAAA,GAAe,WAAA;AACrB,MAAM,cAAA,GAAiB,OAAA;AAEvB,MAAM,UAAA,GAAa,CAAC,aAAA,EAAe,SAAA,EAAW,SAAS,CAAA;AACvD,MAAM,4CAAiD,IAAI,GAAA,CAAI,CAAC,SAAA,EAAW,SAAS,CAAC,CAAA;AACrF,MAAM,WAAA,GAAc,CAAA;AAEpB,MAAM,YAAA,GAAe;AAAA,EACnB,CAAC,SAAS,SAAS,CAAA;AAAA,EACnB,CAAC,SAAS,SAAS;AACrB,CAAA;AAEA,MAAM,mBAAA,GAAsB,MAAA;AASrB,MAAM,uBAAA,GAAgD,EAAE,qBAAA,EAAuB,KAAA;AAG/E,SAAS,uBAAuB,KAAA,EAAqD;AAC1F,EAAA,OAAO,EAAE,qBAAA,EAAuB,KAAA,CAAM,GAAA,CAAIA,qBAAA,CAAU,wBAAwB,CAAA,EAAE;AAChF;AAEA,SAAS,YAAY,KAAA,EAAwB;AAC3C,EAAA,MAAM,IAAA,GAAO,OAAO,KAAA,KAAU,QAAA,GAAW,KAAK,SAAA,CAAU,KAAA,EAAO,IAAA,EAAM,WAAW,CAAA,GAAI,KAAA;AACpF,EAAA,OAAO,OAAO,IAAI,CAAA,CAAE,MAAK,CAAE,OAAA,CAAQ,eAAe,YAAY,CAAA;AAChE;AAEA,SAAS,WAAW,MAAA,EAAmC;AACrD,EAAA,MAAM,QAAkB,EAAC;AACzB,EAAA,KAAA,MAAW,QAAQ,CAAC,MAAA,CAAO,KAAA,EAAO,MAAA,CAAO,OAAO,CAAA,EAAG;AAGjD,IAAA,IAAI,OA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A,MAAM,KAAA,GAAQ;AAAA,IACZ,GAAG,WAAW,SAAS,CAAA;AAAA,IACvB,GAAG,cAAc,SAAS,CAAA;AAAA,IAC1B,GAAG,qBAAqB,SAAS;AAAA,GACnC;AACA,EAAA,IAAI,KAAA,CAAM,WAAW,CAAA,EAAG;AACtB,IAAA,OAAO,IAAA;AAAA,EACT;AACA,EAAA,MAAM,YAAA,GAAe,KAAA,CAAM,MAAA,KAAW,CAAA,IAAK,CAAC,KAAA,CAAM,IAAA,CAAK,CAAC,IAAA,KAAS,IAAA,CAAK,QAAA,CAAS,IAAI,CAAC,CAAA;AACpF,EAAA,MAAM,OAAO,YAAA,GAAe,CAAA,EAAA,EAAK,MAAM,IAAA,CAAK,IAAI,CAAC,CAAA,CAAA,CAAA,GAAM,CAAA;AAAA,GAAA,EAAS,KAAA,CAAM,IAAA,CAAK,OAAO,CAAC;AAAA,CAAA,CAAA;AAEnF,EAAA,OAAO,EAAE,MAAM,OAAA,EAAS,IAAA,EAAM,KAAK,OAAA,CAAQ,cAAA,EAAgB,MAAM,CAAA,EAAE;AACrE;AAGO,SAAS,iBAAA,CACd,QAAA,EACA,MAAA,EACA,OAAA,GAAgC,uBAAA,EACX;AACrB,EAAA,MAAM,OAAA,GAAU,aAAA,CAAc,MAAA,EAAQ,OAAO,CAAA;AAC7C,EAAA,OAAO,OAAA,KAAY,IAAA,GAAO,QAAA,GAAW,EAAE,GAAG,QAAA,EAAU,QAAA,EAAU,CAAC,GAAG,QAAA,CAAS,QAAA,EAAU,OAAO,CAAA,EAAE;AAChG;;;;;"}
|
|
1
|
+
{"version":3,"file":"schema-comment.cjs","sources":["../../../src/application/transform/schema-comment.ts"],"sourcesContent":["import type { IrComment, IrPropertySignature } from \"../../domain/ir/nodes.js\";\nimport { FIX, type OutputFix } from \"../../domain/output-fixes.js\";\n\n/** Schema fields that produce JSDoc lines (the original `AnnotatedSchemaObject`). */\nexport interface AnnotatedSchema {\n readonly const?: unknown;\n readonly default?: unknown;\n readonly deprecated?: boolean;\n readonly description?: string;\n readonly enum?: unknown[];\n readonly example?: unknown;\n readonly examples?: unknown;\n readonly format?: string;\n readonly nullable?: boolean;\n readonly summary?: string;\n readonly title?: string;\n readonly type?: string | string[];\n}\n\nconst LINE_BREAK_RE = /\\r?\\n/g;\n/** Continuation lines of a multi-line value are indented under the tag. */\nconst CONTINUATION = \"\\n * \";\nconst COMMENT_END_RE = /\\*\\//g;\n/** Tags written as `@tag value`; an empty string is kept only where it is a meaningful value. */\nconst VALUE_TAGS = [\"description\", \"default\", \"example\"] as const;\nconst TAGS_KEEPING_EMPTY_STRING: ReadonlySet<string> = new Set([\"default\", \"example\"]);\nconst JSON_INDENT = 2;\n/** The member lists whose descriptions `anyof-oneof-descriptions` adds, with their headings. */\nconst MEMBER_LISTS = [\n [\"anyOf\", \"Any of:\"],\n [\"oneOf\", \"One of:\"],\n] as const;\n/** The later lines of a member's description are indented under its bullet. */\nconst BULLET_CONTINUATION = \"\\n \";\n\n/** How a schema's JSDoc is written. */\nexport interface SchemaCommentOptions {\n /** `anyof-oneof-descriptions`: the descriptions of `anyOf` and `oneOf` members are listed. */\n readonly hasMemberDescriptions: boolean;\n}\n\n/** The JSDoc the original writes. */\nexport const ORIGINAL_SCHEMA_COMMENT: SchemaCommentOptions = { hasMemberDescriptions: false };\n\n/** How the fixes turned on write a schema's JSDoc. */\nexport function schemaCommentOptionsOf(fixes: ReadonlySet<OutputFix>): SchemaCommentOptions {\n return { hasMemberDescriptions: fixes.has(FIX.ANYOF_ONEOF_DESCRIPTIONS) };\n}\n\nfunction formatValue(value: unknown): string {\n const text = typeof value === \"object\" ? JSON.stringify(value, null, JSON_INDENT) : value;\n return String(text).trim().replace(LINE_BREAK_RE, CONTINUATION);\n}\n\nfunction plainLines(schema: AnnotatedSchema): string[] {\n const lines: string[] = [];\n for (const text of [schema.title, schema.summary]) {\n // The document is not validated against the schema dialect: a property named `summary`\n // written without `properties:` makes `summary` an object (upstream #2349).\n if (typeof text === \"string\" && text) {\n lines.push(text.trim().replace(LINE_BREAK_RE, CONTINUATION));\n }\n }\n if (schema.format) {\n lines.push(`Format: ${schema.format}`);\n }\n if (schema.deprecated) {\n lines.push(\"@deprecated\");\n }\n return lines;\n}\n\nfunction valueTagLines(schema: AnnotatedSchema): string[] {\n const lines: string[] = [];\n for (const tag of VALUE_TAGS) {\n const value = schema[tag];\n if (value === undefined || (value === \"\" && !TAGS_KEEPING_EMPTY_STRING.has(tag))) {\n continue;\n }\n lines.push(`@${tag} ${formatValue(value)}`);\n }\n if (Array.isArray(schema.examples)) {\n lines.push(...schema.examples.map((example: unknown) => `@example ${formatValue(example)}`));\n }\n return lines;\n}\n\nfunction enumTypeName(schema: AnnotatedSchema): string {\n if (Array.isArray(schema.type)) {\n return schema.type.join(\"|\");\n }\n return typeof schema.type === \"string\" ? schema.type : \"unknown\";\n}\n\nfunction constantAndEnumLines(schema: AnnotatedSchema): string[] {\n const lines: string[] = [];\n if (\"const\" in schema) {\n lines.push(\"@constant\");\n }\n if (schema.enum) {\n lines.push(`@enum {${enumTypeName(schema)}${schema.nullable ? \"|null\" : \"\"}}`);\n }\n return lines;\n}\n\n/** The own, non-blank `description` of each member; a member without one is left out. */\nfunction memberDescriptions(members: unknown): string[] {\n if (!Array.isArray(members)) {\n return [];\n }\n return members.flatMap((member: unknown) => {\n const description = isSchemaObject(member) ? member.description : undefined;\n return typeof description === \"string\" && description.trim() !== \"\" ? [description.trim()] : [];\n });\n}\n\n/** Each member list with a described member, as a heading and one bullet per description. */\nfunction memberDescriptionLists(schema: SchemaRecord): string[] {\n return MEMBER_LISTS.flatMap(([keyword, heading]) => {\n const descriptions = memberDescriptions(schema[keyword]);\n if (descriptions.length === 0) {\n return [];\n }\n const bullets = descriptions.map(\n (description) => `- ${description.replace(LINE_BREAK_RE, BULLET_CONTINUATION)}`,\n );\n return [[heading, ...bullets].join(\"\\n\")];\n });\n}\n\n/**\n * The schema with the descriptions of its `anyOf` and `oneOf` members after its own description\n * (upstream #2142). A member's description holds only where that member applies, so each one is\n * listed under its keyword rather than merged into one sentence. A `description` that is not a\n * string is kept as it is.\n */\nfunction withMemberDescriptions(schema: SchemaRecord): AnnotatedSchema {\n const lists = memberDescriptionLists(schema);\n const own = schema.description;\n if (lists.length === 0 || (own !== undefined && typeof own !== \"string\")) {\n // As the original, the keywords are read as they are, without validating them.\n return schema as AnnotatedSchema;\n }\n const ownLines = typeof own === \"string\" && own.trim() !== \"\" ? [own.trim()] : [];\n return { ...(schema as AnnotatedSchema), description: [...ownLines, ...lists].join(\"\\n\") };\n}\n\n/** A schema object read without knowing its keywords. */\ninterface SchemaRecord {\n readonly description?: unknown;\n readonly [keyword: string]: unknown;\n}\n\nfunction isSchemaObject(value: unknown): value is SchemaRecord {\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\n/**\n * The JSDoc comment the original writes for a schema (`addJSDocComment`): title, summary and\n * format, then `@deprecated`, `@description`, `@default`, `@example`, `@constant` and `@enum`\n * tags. One short line stays on the `/**` line; anything else is a block. Returns null when there\n * is nothing to say, or when the value is not a schema object.\n */\nexport function schemaComment(\n schema: unknown,\n options: SchemaCommentOptions = ORIGINAL_SCHEMA_COMMENT,\n): IrComment | null {\n if (!isSchemaObject(schema)) {\n return null;\n }\n const annotated = options.hasMemberDescriptions\n ? withMemberDescriptions(schema)\n : (schema as AnnotatedSchema);\n const lines = [\n ...plainLines(annotated),\n ...valueTagLines(annotated),\n ...constantAndEnumLines(annotated),\n ];\n if (lines.length === 0) {\n return null;\n }\n const isSingleLine = lines.length === 1 && !lines.some((line) => line.includes(\"\\n\"));\n const text = isSingleLine ? `* ${lines.join(\"\\n\")} ` : `*\\n * ${lines.join(\"\\n * \")}\\n `;\n // A `*/` inside a value would end the comment early.\n return { kind: \"block\", text: text.replace(COMMENT_END_RE, \"*\\\\/\") };\n}\n\n/** The property with the schema's JSDoc added after the comments it already has. */\nexport function withSchemaComment(\n property: IrPropertySignature,\n schema: unknown,\n options: SchemaCommentOptions = ORIGINAL_SCHEMA_COMMENT,\n): IrPropertySignature {\n const comment = schemaComment(schema, options);\n return comment === null ? property : { ...property, comments: [...property.comments, comment] };\n}\n"],"names":["FIX"],"mappings":";;;;AAmBA,MAAM,aAAA,GAAgB,QAAA;AAEtB,MAAM,YAAA,GAAe,WAAA;AACrB,MAAM,cAAA,GAAiB,OAAA;AAEvB,MAAM,UAAA,GAAa,CAAC,aAAA,EAAe,SAAA,EAAW,SAAS,CAAA;AACvD,MAAM,4CAAiD,IAAI,GAAA,CAAI,CAAC,SAAA,EAAW,SAAS,CAAC,CAAA;AACrF,MAAM,WAAA,GAAc,CAAA;AAEpB,MAAM,YAAA,GAAe;AAAA,EACnB,CAAC,SAAS,SAAS,CAAA;AAAA,EACnB,CAAC,SAAS,SAAS;AACrB,CAAA;AAEA,MAAM,mBAAA,GAAsB,MAAA;AASrB,MAAM,uBAAA,GAAgD,EAAE,qBAAA,EAAuB,KAAA;AAG/E,SAAS,uBAAuB,KAAA,EAAqD;AAC1F,EAAA,OAAO,EAAE,qBAAA,EAAuB,KAAA,CAAM,GAAA,CAAIA,eAAA,CAAI,wBAAwB,CAAA,EAAE;AAC1E;AAEA,SAAS,YAAY,KAAA,EAAwB;AAC3C,EAAA,MAAM,IAAA,GAAO,OAAO,KAAA,KAAU,QAAA,GAAW,KAAK,SAAA,CAAU,KAAA,EAAO,IAAA,EAAM,WAAW,CAAA,GAAI,KAAA;AACpF,EAAA,OAAO,OAAO,IAAI,CAAA,CAAE,MAAK,CAAE,OAAA,CAAQ,eAAe,YAAY,CAAA;AAChE;AAEA,SAAS,WAAW,MAAA,EAAmC;AACrD,EAAA,MAAM,QAAkB,EAAC;AACzB,EAAA,KAAA,MAAW,QAAQ,CAAC,MAAA,CAAO,KAAA,EAAO,MAAA,CAAO,OAAO,CAAA,EAAG;AAGjD,IAAA,IAAI,OAAO,IAAA,KAAS,QAAA,IAAY,IAAA,EAAM;AACpC,MAAA,KAAA,CAAM,KAAK,IAAA,CAAK,IAAA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,CAAA;AAAA,IAC1B,GAAG,qBAAqB,SAAS;AAAA,GACnC;AACA,EAAA,IAAI,KAAA,CAAM,WAAW,CAAA,EAAG;AACtB,IAAA,OAAO,IAAA;AAAA,EACT;AACA,EAAA,MAAM,YAAA,GAAe,KAAA,CAAM,MAAA,KAAW,CAAA,IAAK,CAAC,KAAA,CAAM,IAAA,CAAK,CAAC,IAAA,KAAS,IAAA,CAAK,QAAA,CAAS,IAAI,CAAC,CAAA;AACpF,EAAA,MAAM,OAAO,YAAA,GAAe,CAAA,EAAA,EAAK,MAAM,IAAA,CAAK,IAAI,CAAC,CAAA,CAAA,CAAA,GAAM,CAAA;AAAA,GAAA,EAAS,KAAA,CAAM,IAAA,CAAK,OAAO,CAAC;AAAA,CAAA,CAAA;AAEnF,EAAA,OAAO,EAAE,MAAM,OAAA,EAAS,IAAA,EAAM,KAAK,OAAA,CAAQ,cAAA,EAAgB,MAAM,CAAA,EAAE;AACrE;AAGO,SAAS,iBAAA,CACd,QAAA,EACA,MAAA,EACA,OAAA,GAAgC,uBAAA,EACX;AACrB,EAAA,MAAM,OAAA,GAAU,aAAA,CAAc,MAAA,EAAQ,OAAO,CAAA;AAC7C,EAAA,OAAO,OAAA,KAAY,IAAA,GAAO,QAAA,GAAW,EAAE,GAAG,QAAA,EAAU,QAAA,EAAU,CAAC,GAAG,QAAA,CAAS,QAAA,EAAU,OAAO,CAAA,EAAE;AAChG;;;;;"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { FIX } from '../../domain/output-fixes.mjs';
|
|
2
2
|
|
|
3
3
|
const LINE_BREAK_RE = /\r?\n/g;
|
|
4
4
|
const CONTINUATION = "\n * ";
|
|
@@ -13,7 +13,7 @@ const MEMBER_LISTS = [
|
|
|
13
13
|
const BULLET_CONTINUATION = "\n ";
|
|
14
14
|
const ORIGINAL_SCHEMA_COMMENT = { hasMemberDescriptions: false };
|
|
15
15
|
function schemaCommentOptionsOf(fixes) {
|
|
16
|
-
return { hasMemberDescriptions: fixes.has(
|
|
16
|
+
return { hasMemberDescriptions: fixes.has(FIX.ANYOF_ONEOF_DESCRIPTIONS) };
|
|
17
17
|
}
|
|
18
18
|
function formatValue(value) {
|
|
19
19
|
const text = typeof value === "object" ? JSON.stringify(value, null, JSON_INDENT) : value;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema-comment.mjs","sources":["../../../src/application/transform/schema-comment.ts"],"sourcesContent":["import type { IrComment, IrPropertySignature } from \"../../domain/ir/nodes.js\";\nimport { OutputFix } from \"../../domain/output-fixes.js\";\n\n/** Schema fields that produce JSDoc lines (the original `AnnotatedSchemaObject`). */\nexport interface AnnotatedSchema {\n readonly const?: unknown;\n readonly default?: unknown;\n readonly deprecated?: boolean;\n readonly description?: string;\n readonly enum?: unknown[];\n readonly example?: unknown;\n readonly examples?: unknown;\n readonly format?: string;\n readonly nullable?: boolean;\n readonly summary?: string;\n readonly title?: string;\n readonly type?: string | string[];\n}\n\nconst LINE_BREAK_RE = /\\r?\\n/g;\n/** Continuation lines of a multi-line value are indented under the tag. */\nconst CONTINUATION = \"\\n * \";\nconst COMMENT_END_RE = /\\*\\//g;\n/** Tags written as `@tag value`; an empty string is kept only where it is a meaningful value. */\nconst VALUE_TAGS = [\"description\", \"default\", \"example\"] as const;\nconst TAGS_KEEPING_EMPTY_STRING: ReadonlySet<string> = new Set([\"default\", \"example\"]);\nconst JSON_INDENT = 2;\n/** The member lists whose descriptions `anyof-oneof-descriptions` adds, with their headings. */\nconst MEMBER_LISTS = [\n [\"anyOf\", \"Any of:\"],\n [\"oneOf\", \"One of:\"],\n] as const;\n/** The later lines of a member's description are indented under its bullet. */\nconst BULLET_CONTINUATION = \"\\n \";\n\n/** How a schema's JSDoc is written. */\nexport interface SchemaCommentOptions {\n /** `anyof-oneof-descriptions`: the descriptions of `anyOf` and `oneOf` members are listed. */\n readonly hasMemberDescriptions: boolean;\n}\n\n/** The JSDoc the original writes. */\nexport const ORIGINAL_SCHEMA_COMMENT: SchemaCommentOptions = { hasMemberDescriptions: false };\n\n/** How the fixes turned on write a schema's JSDoc. */\nexport function schemaCommentOptionsOf(fixes: ReadonlySet<OutputFix>): SchemaCommentOptions {\n return { hasMemberDescriptions: fixes.has(OutputFix.ANYOF_ONEOF_DESCRIPTIONS) };\n}\n\nfunction formatValue(value: unknown): string {\n const text = typeof value === \"object\" ? JSON.stringify(value, null, JSON_INDENT) : value;\n return String(text).trim().replace(LINE_BREAK_RE, CONTINUATION);\n}\n\nfunction plainLines(schema: AnnotatedSchema): string[] {\n const lines: string[] = [];\n for (const text of [schema.title, schema.summary]) {\n // The document is not validated against the schema dialect: a property named `summary`\n // written without `properties:` makes `summary` an object (upstream #2349).\n if (typeof text === \"string\" && text) {\n lines.push(text.trim().replace(LINE_BREAK_RE, CONTINUATION));\n }\n }\n if (schema.format) {\n lines.push(`Format: ${schema.format}`);\n }\n if (schema.deprecated) {\n lines.push(\"@deprecated\");\n }\n return lines;\n}\n\nfunction valueTagLines(schema: AnnotatedSchema): string[] {\n const lines: string[] = [];\n for (const tag of VALUE_TAGS) {\n const value = schema[tag];\n if (value === undefined || (value === \"\" && !TAGS_KEEPING_EMPTY_STRING.has(tag))) {\n continue;\n }\n lines.push(`@${tag} ${formatValue(value)}`);\n }\n if (Array.isArray(schema.examples)) {\n lines.push(...schema.examples.map((example: unknown) => `@example ${formatValue(example)}`));\n }\n return lines;\n}\n\nfunction enumTypeName(schema: AnnotatedSchema): string {\n if (Array.isArray(schema.type)) {\n return schema.type.join(\"|\");\n }\n return typeof schema.type === \"string\" ? schema.type : \"unknown\";\n}\n\nfunction constantAndEnumLines(schema: AnnotatedSchema): string[] {\n const lines: string[] = [];\n if (\"const\" in schema) {\n lines.push(\"@constant\");\n }\n if (schema.enum) {\n lines.push(`@enum {${enumTypeName(schema)}${schema.nullable ? \"|null\" : \"\"}}`);\n }\n return lines;\n}\n\n/** The own, non-blank `description` of each member; a member without one is left out. */\nfunction memberDescriptions(members: unknown): string[] {\n if (!Array.isArray(members)) {\n return [];\n }\n return members.flatMap((member: unknown) => {\n const description = isSchemaObject(member) ? member.description : undefined;\n return typeof description === \"string\" && description.trim() !== \"\" ? [description.trim()] : [];\n });\n}\n\n/** Each member list with a described member, as a heading and one bullet per description. */\nfunction memberDescriptionLists(schema: SchemaRecord): string[] {\n return MEMBER_LISTS.flatMap(([keyword, heading]) => {\n const descriptions = memberDescriptions(schema[keyword]);\n if (descriptions.length === 0) {\n return [];\n }\n const bullets = descriptions.map(\n (description) => `- ${description.replace(LINE_BREAK_RE, BULLET_CONTINUATION)}`,\n );\n return [[heading, ...bullets].join(\"\\n\")];\n });\n}\n\n/**\n * The schema with the descriptions of its `anyOf` and `oneOf` members after its own description\n * (upstream #2142). A member's description holds only where that member applies, so each one is\n * listed under its keyword rather than merged into one sentence. A `description` that is not a\n * string is kept as it is.\n */\nfunction withMemberDescriptions(schema: SchemaRecord): AnnotatedSchema {\n const lists = memberDescriptionLists(schema);\n const own = schema.description;\n if (lists.length === 0 || (own !== undefined && typeof own !== \"string\")) {\n // As the original, the keywords are read as they are, without validating them.\n return schema as AnnotatedSchema;\n }\n const ownLines = typeof own === \"string\" && own.trim() !== \"\" ? [own.trim()] : [];\n return { ...(schema as AnnotatedSchema), description: [...ownLines, ...lists].join(\"\\n\") };\n}\n\n/** A schema object read without knowing its keywords. */\ninterface SchemaRecord {\n readonly description?: unknown;\n readonly [keyword: string]: unknown;\n}\n\nfunction isSchemaObject(value: unknown): value is SchemaRecord {\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\n/**\n * The JSDoc comment the original writes for a schema (`addJSDocComment`): title, summary and\n * format, then `@deprecated`, `@description`, `@default`, `@example`, `@constant` and `@enum`\n * tags. One short line stays on the `/**` line; anything else is a block. Returns null when there\n * is nothing to say, or when the value is not a schema object.\n */\nexport function schemaComment(\n schema: unknown,\n options: SchemaCommentOptions = ORIGINAL_SCHEMA_COMMENT,\n): IrComment | null {\n if (!isSchemaObject(schema)) {\n return null;\n }\n const annotated = options.hasMemberDescriptions\n ? withMemberDescriptions(schema)\n : (schema as AnnotatedSchema);\n const lines = [\n ...plainLines(annotated),\n ...valueTagLines(annotated),\n ...constantAndEnumLines(annotated),\n ];\n if (lines.length === 0) {\n return null;\n }\n const isSingleLine = lines.length === 1 && !lines.some((line) => line.includes(\"\\n\"));\n const text = isSingleLine ? `* ${lines.join(\"\\n\")} ` : `*\\n * ${lines.join(\"\\n * \")}\\n `;\n // A `*/` inside a value would end the comment early.\n return { kind: \"block\", text: text.replace(COMMENT_END_RE, \"*\\\\/\") };\n}\n\n/** The property with the schema's JSDoc added after the comments it already has. */\nexport function withSchemaComment(\n property: IrPropertySignature,\n schema: unknown,\n options: SchemaCommentOptions = ORIGINAL_SCHEMA_COMMENT,\n): IrPropertySignature {\n const comment = schemaComment(schema, options);\n return comment === null ? property : { ...property, comments: [...property.comments, comment] };\n}\n"],"names":[],"mappings":";;AAmBA,MAAM,aAAA,GAAgB,QAAA;AAEtB,MAAM,YAAA,GAAe,WAAA;AACrB,MAAM,cAAA,GAAiB,OAAA;AAEvB,MAAM,UAAA,GAAa,CAAC,aAAA,EAAe,SAAA,EAAW,SAAS,CAAA;AACvD,MAAM,4CAAiD,IAAI,GAAA,CAAI,CAAC,SAAA,EAAW,SAAS,CAAC,CAAA;AACrF,MAAM,WAAA,GAAc,CAAA;AAEpB,MAAM,YAAA,GAAe;AAAA,EACnB,CAAC,SAAS,SAAS,CAAA;AAAA,EACnB,CAAC,SAAS,SAAS;AACrB,CAAA;AAEA,MAAM,mBAAA,GAAsB,MAAA;AASrB,MAAM,uBAAA,GAAgD,EAAE,qBAAA,EAAuB,KAAA;AAG/E,SAAS,uBAAuB,KAAA,EAAqD;AAC1F,EAAA,OAAO,EAAE,qBAAA,EAAuB,KAAA,CAAM,GAAA,CAAI,SAAA,CAAU,wBAAwB,CAAA,EAAE;AAChF;AAEA,SAAS,YAAY,KAAA,EAAwB;AAC3C,EAAA,MAAM,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QAAA,GAAW,EAAE,GAAG,QAAA,EAAU,QAAA,EAAU,CAAC,GAAG,QAAA,CAAS,QAAA,EAAU,OAAO,CAAA,EAAE;AAChG;;"}
|
|
1
|
+
{"version":3,"file":"schema-comment.mjs","sources":["../../../src/application/transform/schema-comment.ts"],"sourcesContent":["import type { IrComment, IrPropertySignature } from \"../../domain/ir/nodes.js\";\nimport { FIX, type OutputFix } from \"../../domain/output-fixes.js\";\n\n/** Schema fields that produce JSDoc lines (the original `AnnotatedSchemaObject`). */\nexport interface AnnotatedSchema {\n readonly const?: unknown;\n readonly default?: unknown;\n readonly deprecated?: boolean;\n readonly description?: string;\n readonly enum?: unknown[];\n readonly example?: unknown;\n readonly examples?: unknown;\n readonly format?: string;\n readonly nullable?: boolean;\n readonly summary?: string;\n readonly title?: string;\n readonly type?: string | string[];\n}\n\nconst LINE_BREAK_RE = /\\r?\\n/g;\n/** Continuation lines of a multi-line value are indented under the tag. */\nconst CONTINUATION = \"\\n * \";\nconst COMMENT_END_RE = /\\*\\//g;\n/** Tags written as `@tag value`; an empty string is kept only where it is a meaningful value. */\nconst VALUE_TAGS = [\"description\", \"default\", \"example\"] as const;\nconst TAGS_KEEPING_EMPTY_STRING: ReadonlySet<string> = new Set([\"default\", \"example\"]);\nconst JSON_INDENT = 2;\n/** The member lists whose descriptions `anyof-oneof-descriptions` adds, with their headings. */\nconst MEMBER_LISTS = [\n [\"anyOf\", \"Any of:\"],\n [\"oneOf\", \"One of:\"],\n] as const;\n/** The later lines of a member's description are indented under its bullet. */\nconst BULLET_CONTINUATION = \"\\n \";\n\n/** How a schema's JSDoc is written. */\nexport interface SchemaCommentOptions {\n /** `anyof-oneof-descriptions`: the descriptions of `anyOf` and `oneOf` members are listed. */\n readonly hasMemberDescriptions: boolean;\n}\n\n/** The JSDoc the original writes. */\nexport const ORIGINAL_SCHEMA_COMMENT: SchemaCommentOptions = { hasMemberDescriptions: false };\n\n/** How the fixes turned on write a schema's JSDoc. */\nexport function schemaCommentOptionsOf(fixes: ReadonlySet<OutputFix>): SchemaCommentOptions {\n return { hasMemberDescriptions: fixes.has(FIX.ANYOF_ONEOF_DESCRIPTIONS) };\n}\n\nfunction formatValue(value: unknown): string {\n const text = typeof value === \"object\" ? JSON.stringify(value, null, JSON_INDENT) : value;\n return String(text).trim().replace(LINE_BREAK_RE, CONTINUATION);\n}\n\nfunction plainLines(schema: AnnotatedSchema): string[] {\n const lines: string[] = [];\n for (const text of [schema.title, schema.summary]) {\n // The document is not validated against the schema dialect: a property named `summary`\n // written without `properties:` makes `summary` an object (upstream #2349).\n if (typeof text === \"string\" && text) {\n lines.push(text.trim().replace(LINE_BREAK_RE, CONTINUATION));\n }\n }\n if (schema.format) {\n lines.push(`Format: ${schema.format}`);\n }\n if (schema.deprecated) {\n lines.push(\"@deprecated\");\n }\n return lines;\n}\n\nfunction valueTagLines(schema: AnnotatedSchema): string[] {\n const lines: string[] = [];\n for (const tag of VALUE_TAGS) {\n const value = schema[tag];\n if (value === undefined || (value === \"\" && !TAGS_KEEPING_EMPTY_STRING.has(tag))) {\n continue;\n }\n lines.push(`@${tag} ${formatValue(value)}`);\n }\n if (Array.isArray(schema.examples)) {\n lines.push(...schema.examples.map((example: unknown) => `@example ${formatValue(example)}`));\n }\n return lines;\n}\n\nfunction enumTypeName(schema: AnnotatedSchema): string {\n if (Array.isArray(schema.type)) {\n return schema.type.join(\"|\");\n }\n return typeof schema.type === \"string\" ? schema.type : \"unknown\";\n}\n\nfunction constantAndEnumLines(schema: AnnotatedSchema): string[] {\n const lines: string[] = [];\n if (\"const\" in schema) {\n lines.push(\"@constant\");\n }\n if (schema.enum) {\n lines.push(`@enum {${enumTypeName(schema)}${schema.nullable ? \"|null\" : \"\"}}`);\n }\n return lines;\n}\n\n/** The own, non-blank `description` of each member; a member without one is left out. */\nfunction memberDescriptions(members: unknown): string[] {\n if (!Array.isArray(members)) {\n return [];\n }\n return members.flatMap((member: unknown) => {\n const description = isSchemaObject(member) ? member.description : undefined;\n return typeof description === \"string\" && description.trim() !== \"\" ? [description.trim()] : [];\n });\n}\n\n/** Each member list with a described member, as a heading and one bullet per description. */\nfunction memberDescriptionLists(schema: SchemaRecord): string[] {\n return MEMBER_LISTS.flatMap(([keyword, heading]) => {\n const descriptions = memberDescriptions(schema[keyword]);\n if (descriptions.length === 0) {\n return [];\n }\n const bullets = descriptions.map(\n (description) => `- ${description.replace(LINE_BREAK_RE, BULLET_CONTINUATION)}`,\n );\n return [[heading, ...bullets].join(\"\\n\")];\n });\n}\n\n/**\n * The schema with the descriptions of its `anyOf` and `oneOf` members after its own description\n * (upstream #2142). A member's description holds only where that member applies, so each one is\n * listed under its keyword rather than merged into one sentence. A `description` that is not a\n * string is kept as it is.\n */\nfunction withMemberDescriptions(schema: SchemaRecord): AnnotatedSchema {\n const lists = memberDescriptionLists(schema);\n const own = schema.description;\n if (lists.length === 0 || (own !== undefined && typeof own !== \"string\")) {\n // As the original, the keywords are read as they are, without validating them.\n return schema as AnnotatedSchema;\n }\n const ownLines = typeof own === \"string\" && own.trim() !== \"\" ? [own.trim()] : [];\n return { ...(schema as AnnotatedSchema), description: [...ownLines, ...lists].join(\"\\n\") };\n}\n\n/** A schema object read without knowing its keywords. */\ninterface SchemaRecord {\n readonly description?: unknown;\n readonly [keyword: string]: unknown;\n}\n\nfunction isSchemaObject(value: unknown): value is SchemaRecord {\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\n/**\n * The JSDoc comment the original writes for a schema (`addJSDocComment`): title, summary and\n * format, then `@deprecated`, `@description`, `@default`, `@example`, `@constant` and `@enum`\n * tags. One short line stays on the `/**` line; anything else is a block. Returns null when there\n * is nothing to say, or when the value is not a schema object.\n */\nexport function schemaComment(\n schema: unknown,\n options: SchemaCommentOptions = ORIGINAL_SCHEMA_COMMENT,\n): IrComment | null {\n if (!isSchemaObject(schema)) {\n return null;\n }\n const annotated = options.hasMemberDescriptions\n ? withMemberDescriptions(schema)\n : (schema as AnnotatedSchema);\n const lines = [\n ...plainLines(annotated),\n ...valueTagLines(annotated),\n ...constantAndEnumLines(annotated),\n ];\n if (lines.length === 0) {\n return null;\n }\n const isSingleLine = lines.length === 1 && !lines.some((line) => line.includes(\"\\n\"));\n const text = isSingleLine ? `* ${lines.join(\"\\n\")} ` : `*\\n * ${lines.join(\"\\n * \")}\\n `;\n // A `*/` inside a value would end the comment early.\n return { kind: \"block\", text: text.replace(COMMENT_END_RE, \"*\\\\/\") };\n}\n\n/** The property with the schema's JSDoc added after the comments it already has. */\nexport function withSchemaComment(\n property: IrPropertySignature,\n schema: unknown,\n options: SchemaCommentOptions = ORIGINAL_SCHEMA_COMMENT,\n): IrPropertySignature {\n const comment = schemaComment(schema, options);\n return comment === null ? property : { ...property, comments: [...property.comments, comment] };\n}\n"],"names":[],"mappings":";;AAmBA,MAAM,aAAA,GAAgB,QAAA;AAEtB,MAAM,YAAA,GAAe,WAAA;AACrB,MAAM,cAAA,GAAiB,OAAA;AAEvB,MAAM,UAAA,GAAa,CAAC,aAAA,EAAe,SAAA,EAAW,SAAS,CAAA;AACvD,MAAM,4CAAiD,IAAI,GAAA,CAAI,CAAC,SAAA,EAAW,SAAS,CAAC,CAAA;AACrF,MAAM,WAAA,GAAc,CAAA;AAEpB,MAAM,YAAA,GAAe;AAAA,EACnB,CAAC,SAAS,SAAS,CAAA;AAAA,EACnB,CAAC,SAAS,SAAS;AACrB,CAAA;AAEA,MAAM,mBAAA,GAAsB,MAAA;AASrB,MAAM,uBAAA,GAAgD,EAAE,qBAAA,EAAuB,KAAA;AAG/E,SAAS,uBAAuB,KAAA,EAAqD;AAC1F,EAAA,OAAO,EAAE,qBAAA,EAAuB,KAAA,CAAM,GAAA,CAAI,GAAA,CAAI,wBAAwB,CAAA,EAAE;AAC1E;AAEA,SAAS,YAAY,KAAA,EAAwB;AAC3C,EAAA,MAAM,IAAA,GAAO,OAAO,KAAA,KAAU,QAAA,GAAW,KAAK,SAAA,CAAU,KAAA,EAAO,IAAA,EAAM,WAAW,CAAA,GA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AE;AAChG;;"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const scule = require('scule');
|
|
4
|
+
const warnings = require('../domain/warnings.cjs');
|
|
5
|
+
const cliArguments = require('./cli-arguments.cjs');
|
|
6
|
+
const cliErrors = require('./cli-errors.cjs');
|
|
7
|
+
const cliOutput = require('./cli-output.cjs');
|
|
8
|
+
|
|
9
|
+
const OUTPUT_SETTING = "output";
|
|
10
|
+
const FIX_SETTING = "fix";
|
|
11
|
+
const LOCAL_REF_ROOT_SETTING = "localRefRoot";
|
|
12
|
+
const REMOTE_REF_HOSTS_SETTING = "remoteRefHosts";
|
|
13
|
+
const IGNORED_SETTINGS = ["contentNever"];
|
|
14
|
+
const CAMEL_CASE_SETTINGS = [
|
|
15
|
+
OUTPUT_SETTING,
|
|
16
|
+
FIX_SETTING,
|
|
17
|
+
LOCAL_REF_ROOT_SETTING,
|
|
18
|
+
REMOTE_REF_HOSTS_SETTING,
|
|
19
|
+
...cliArguments.GENERATOR_FLAGS,
|
|
20
|
+
...IGNORED_SETTINGS
|
|
21
|
+
];
|
|
22
|
+
const KNOWN_SETTINGS = /* @__PURE__ */ new Set([
|
|
23
|
+
...CAMEL_CASE_SETTINGS,
|
|
24
|
+
...CAMEL_CASE_SETTINGS.map((name) => scule.kebabCase(name))
|
|
25
|
+
]);
|
|
26
|
+
function unknownSettingWarnings(apiName, settings) {
|
|
27
|
+
return Object.keys(settings).filter((key) => !KNOWN_SETTINGS.has(key)).map(
|
|
28
|
+
(key) => warnings.warningText(
|
|
29
|
+
`API ${apiName}: the setting \`${key}\` is not an openapi-typesafe option and is ignored`,
|
|
30
|
+
warnings.WARNING_CODES.CONFIG_SETTING_UNKNOWN
|
|
31
|
+
)
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
function settingValue(settings, name) {
|
|
35
|
+
return name in settings ? settings[name] : settings[scule.kebabCase(name)];
|
|
36
|
+
}
|
|
37
|
+
function hasSetting(settings, name) {
|
|
38
|
+
return name in settings || scule.kebabCase(name) in settings;
|
|
39
|
+
}
|
|
40
|
+
function invalidSetting(apiName, name, expected) {
|
|
41
|
+
return cliErrors.cliError(
|
|
42
|
+
"CLI_CONFIG_INVALID",
|
|
43
|
+
`API ${apiName}: \`${scule.kebabCase(name)}\` in x-openapi-ts must be ${expected}`
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
function localRefRootOf(apiName, settings, configRoot) {
|
|
47
|
+
if (!hasSetting(settings, LOCAL_REF_ROOT_SETTING)) {
|
|
48
|
+
return {};
|
|
49
|
+
}
|
|
50
|
+
const value = settingValue(settings, LOCAL_REF_ROOT_SETTING) ?? "";
|
|
51
|
+
if (typeof value !== "string") {
|
|
52
|
+
throw invalidSetting(apiName, LOCAL_REF_ROOT_SETTING, "a directory path");
|
|
53
|
+
}
|
|
54
|
+
return { localRefRoot: cliOutput.resolveLocationUrl(value === "" ? "." : value, configRoot) };
|
|
55
|
+
}
|
|
56
|
+
function remoteRefHostsOf(apiName, settings) {
|
|
57
|
+
if (!hasSetting(settings, REMOTE_REF_HOSTS_SETTING)) {
|
|
58
|
+
return {};
|
|
59
|
+
}
|
|
60
|
+
const value = settingValue(settings, REMOTE_REF_HOSTS_SETTING) ?? [];
|
|
61
|
+
const entries = [value].flat();
|
|
62
|
+
if (entries.some((entry) => typeof entry !== "string")) {
|
|
63
|
+
throw invalidSetting(apiName, REMOTE_REF_HOSTS_SETTING, "a host list");
|
|
64
|
+
}
|
|
65
|
+
return { remoteRefHosts: cliArguments.hostList(entries) ?? [] };
|
|
66
|
+
}
|
|
67
|
+
function apiRefLimitOptions(apiName, settings, configRoot) {
|
|
68
|
+
return {
|
|
69
|
+
...localRefRootOf(apiName, settings, configRoot),
|
|
70
|
+
...remoteRefHostsOf(apiName, settings)
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
exports.FIX_SETTING = FIX_SETTING;
|
|
75
|
+
exports.OUTPUT_SETTING = OUTPUT_SETTING;
|
|
76
|
+
exports.apiRefLimitOptions = apiRefLimitOptions;
|
|
77
|
+
exports.unknownSettingWarnings = unknownSettingWarnings;
|
|
78
|
+
//# sourceMappingURL=api-settings.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api-settings.cjs","sources":["../../src/cli/api-settings.ts"],"sourcesContent":["import { kebabCase } from \"scule\";\nimport { WARNING_CODES, warningText } from \"../domain/warnings.js\";\nimport { GENERATOR_FLAGS, hostList } from \"./cli-arguments.js\";\nimport { cliError } from \"./cli-errors.js\";\nimport { resolveLocationUrl } from \"./cli-output.js\";\nimport type { RefLimitOptions } from \"./ref-limits.js\";\n\n/**\n * The settings of one API in `redocly.yaml` (`apis.<name>.x-openapi-ts`). A setting has the name\n * of its CLI flag, in kebab-case or camelCase (ADR-021, decision 1); a setting the CLI does not\n * read gets a warning, as an unknown flag gets a notice.\n */\n\nexport type ApiSettings = Readonly<Record<string, unknown>>;\n\n/** The setting that names the output file. */\nexport const OUTPUT_SETTING = \"output\";\n\n/** The setting that lists the API's output fixes. */\nexport const FIX_SETTING = \"fix\";\n\nconst LOCAL_REF_ROOT_SETTING = \"localRefRoot\";\nconst REMOTE_REF_HOSTS_SETTING = \"remoteRefHosts\";\n\n/** `--content-never` is accepted and has no effect, as on the command line. */\nconst IGNORED_SETTINGS = [\"contentNever\"] as const;\n\nconst CAMEL_CASE_SETTINGS = [\n OUTPUT_SETTING,\n FIX_SETTING,\n LOCAL_REF_ROOT_SETTING,\n REMOTE_REF_HOSTS_SETTING,\n ...GENERATOR_FLAGS,\n ...IGNORED_SETTINGS,\n];\n\nconst KNOWN_SETTINGS = new Set<string>([\n ...CAMEL_CASE_SETTINGS,\n ...CAMEL_CASE_SETTINGS.map((name) => kebabCase(name)),\n]);\n\n/** The warning for each setting of an API that no code reads. */\nexport function unknownSettingWarnings(apiName: string, settings: ApiSettings): string[] {\n return Object.keys(settings)\n .filter((key) => !KNOWN_SETTINGS.has(key))\n .map((key) =>\n warningText(\n `API ${apiName}: the setting \\`${key}\\` is not an openapi-typesafe option and is ignored`,\n WARNING_CODES.CONFIG_SETTING_UNKNOWN,\n ),\n );\n}\n\n/** The value of a setting in camelCase, else in kebab-case; `undefined` when it is absent. */\nfunction settingValue(settings: ApiSettings, name: string): unknown {\n return name in settings ? settings[name] : settings[kebabCase(name)];\n}\n\nfunction hasSetting(settings: ApiSettings, name: string): boolean {\n return name in settings || kebabCase(name) in settings;\n}\n\nfunction invalidSetting(apiName: string, name: string, expected: string): Error {\n return cliError(\n \"CLI_CONFIG_INVALID\",\n `API ${apiName}: \\`${kebabCase(name)}\\` in x-openapi-ts must be ${expected}`,\n );\n}\n\n/** `local-ref-root: dir`, relative to the config file; empty means the config file's directory. */\nfunction localRefRootOf(apiName: string, settings: ApiSettings, configRoot: URL): RefLimitOptions {\n if (!hasSetting(settings, LOCAL_REF_ROOT_SETTING)) {\n return {};\n }\n const value = settingValue(settings, LOCAL_REF_ROOT_SETTING) ?? \"\";\n if (typeof value !== \"string\") {\n throw invalidSetting(apiName, LOCAL_REF_ROOT_SETTING, \"a directory path\");\n }\n return { localRefRoot: resolveLocationUrl(value === \"\" ? \".\" : value, configRoot) };\n}\n\n/** `remote-ref-hosts: [a, b]` or `a,b`; an empty list or value allows no host. */\nfunction remoteRefHostsOf(apiName: string, settings: ApiSettings): RefLimitOptions {\n if (!hasSetting(settings, REMOTE_REF_HOSTS_SETTING)) {\n return {};\n }\n const value = settingValue(settings, REMOTE_REF_HOSTS_SETTING) ?? [];\n const entries = [value].flat();\n if (entries.some((entry) => typeof entry !== \"string\")) {\n throw invalidSetting(apiName, REMOTE_REF_HOSTS_SETTING, \"a host list\");\n }\n return { remoteRefHosts: hostList(entries) ?? [] };\n}\n\n/**\n * The `$ref` limits an API sets (threat model R1, R2). Each one replaces the CLI flag of the same\n * name for this API, as its boolean settings and `fix` do; fails with `CLI_CONFIG_INVALID` for a\n * value of another type.\n */\nexport function apiRefLimitOptions(\n apiName: string,\n settings: ApiSettings,\n configRoot: URL,\n): RefLimitOptions {\n return {\n ...localRefRootOf(apiName, settings, configRoot),\n ...remoteRefHostsOf(apiName, settings),\n };\n}\n"],"names":["GENERATOR_FLAGS","kebabCase","warningText","WARNING_CODES","cliError","resolveLocationUrl","hostList"],"mappings":";;;;;;;;AAgBO,MAAM,cAAA,GAAiB;AAGvB,MAAM,WAAA,GAAc;AAE3B,MAAM,sBAAA,GAAyB,cAAA;AAC/B,MAAM,wBAAA,GAA2B,gBAAA;AAGjC,MAAM,gBAAA,GAAmB,CAAC,cAAc,CAAA;AAExC,MAAM,mBAAA,GAAsB;AAAA,EAC1B,cAAA;AAAA,EACA,WAAA;AAAA,EACA,sBAAA;AAAA,EACA,wBAAA;AAAA,EACA,GAAGA,4BAAA;AAAA,EACH,GAAG;AACL,CAAA;AAEA,MAAM,cAAA,uBAAqB,GAAA,CAAY;AAAA,EACrC,GAAG,mBAAA;AAAA,EACH,GAAG,mBAAA,CAAoB,GAAA,CAAI,CAAC,IAAA,KAASC,eAAA,CAAU,IAAI,CAAC;AACtD,CAAC,CAAA;AAGM,SAAS,sBAAA,CAAuB,SAAiB,QAAA,EAAiC;AACvF,EAAA,OAAO,MAAA,CAAO,IAAA,CAAK,QAAQ,CAAA,CACxB,MAAA,CAAO,CAAC,GAAA,KAAQ,CAAC,cAAA,CAAe,GAAA,CAAI,GAAG,CAAC,CAAA,CACxC,GAAA;AAAA,IAAI,CAAC,GAAA,KACJC,oBAAA;AAAA,MACE,CAAA,IAAA,EAAO,OAAO,CAAA,gBAAA,EAAmB,GAAG,CAAA,mDAAA,CAAA;AAAA,MACpCC,sBAAA,CAAc;AAAA;AAChB,GACF;AACJ;AAGA,SAAS,YAAA,CAAa,UAAuB,IAAA,EAAuB;AAClE,EAAA,OAAO,IAAA,IAAQ,WAAW,QAAA,CAAS,IAAI,IAAI,QAAA,CAASF,eAAA,CAAU,IAAI,CAAC,CAAA;AACrE;AAEA,SAAS,UAAA,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AAA,EAA0B,aAAa,CAAA;AAAA,EACvE;AACA,EAAA,OAAO,EAAE,cAAA,EAAgBC,qBAAA,CAAS,OAAO,CAAA,IAAK,EAAC,EAAE;AACnD;AAOO,SAAS,kBAAA,CACd,OAAA,EACA,QAAA,EACA,UAAA,EACiB;AACjB,EAAA,OAAO;AAAA,IACL,GAAG,cAAA,CAAe,OAAA,EAAS,QAAA,EAAU,UAAU,CAAA;AAAA,IAC/C,GAAG,gBAAA,CAAiB,OAAA,EAAS,QAAQ;AAAA,GACvC;AACF;;;;;"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { kebabCase } from 'scule';
|
|
2
|
+
import { warningText, WARNING_CODES } from '../domain/warnings.mjs';
|
|
3
|
+
import { GENERATOR_FLAGS, hostList } from './cli-arguments.mjs';
|
|
4
|
+
import { cliError } from './cli-errors.mjs';
|
|
5
|
+
import { resolveLocationUrl } from './cli-output.mjs';
|
|
6
|
+
|
|
7
|
+
const OUTPUT_SETTING = "output";
|
|
8
|
+
const FIX_SETTING = "fix";
|
|
9
|
+
const LOCAL_REF_ROOT_SETTING = "localRefRoot";
|
|
10
|
+
const REMOTE_REF_HOSTS_SETTING = "remoteRefHosts";
|
|
11
|
+
const IGNORED_SETTINGS = ["contentNever"];
|
|
12
|
+
const CAMEL_CASE_SETTINGS = [
|
|
13
|
+
OUTPUT_SETTING,
|
|
14
|
+
FIX_SETTING,
|
|
15
|
+
LOCAL_REF_ROOT_SETTING,
|
|
16
|
+
REMOTE_REF_HOSTS_SETTING,
|
|
17
|
+
...GENERATOR_FLAGS,
|
|
18
|
+
...IGNORED_SETTINGS
|
|
19
|
+
];
|
|
20
|
+
const KNOWN_SETTINGS = /* @__PURE__ */ new Set([
|
|
21
|
+
...CAMEL_CASE_SETTINGS,
|
|
22
|
+
...CAMEL_CASE_SETTINGS.map((name) => kebabCase(name))
|
|
23
|
+
]);
|
|
24
|
+
function unknownSettingWarnings(apiName, settings) {
|
|
25
|
+
return Object.keys(settings).filter((key) => !KNOWN_SETTINGS.has(key)).map(
|
|
26
|
+
(key) => warningText(
|
|
27
|
+
`API ${apiName}: the setting \`${key}\` is not an openapi-typesafe option and is ignored`,
|
|
28
|
+
WARNING_CODES.CONFIG_SETTING_UNKNOWN
|
|
29
|
+
)
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
function settingValue(settings, name) {
|
|
33
|
+
return name in settings ? settings[name] : settings[kebabCase(name)];
|
|
34
|
+
}
|
|
35
|
+
function hasSetting(settings, name) {
|
|
36
|
+
return name in settings || kebabCase(name) in settings;
|
|
37
|
+
}
|
|
38
|
+
function invalidSetting(apiName, name, expected) {
|
|
39
|
+
return cliError(
|
|
40
|
+
"CLI_CONFIG_INVALID",
|
|
41
|
+
`API ${apiName}: \`${kebabCase(name)}\` in x-openapi-ts must be ${expected}`
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
function localRefRootOf(apiName, settings, configRoot) {
|
|
45
|
+
if (!hasSetting(settings, LOCAL_REF_ROOT_SETTING)) {
|
|
46
|
+
return {};
|
|
47
|
+
}
|
|
48
|
+
const value = settingValue(settings, LOCAL_REF_ROOT_SETTING) ?? "";
|
|
49
|
+
if (typeof value !== "string") {
|
|
50
|
+
throw invalidSetting(apiName, LOCAL_REF_ROOT_SETTING, "a directory path");
|
|
51
|
+
}
|
|
52
|
+
return { localRefRoot: resolveLocationUrl(value === "" ? "." : value, configRoot) };
|
|
53
|
+
}
|
|
54
|
+
function remoteRefHostsOf(apiName, settings) {
|
|
55
|
+
if (!hasSetting(settings, REMOTE_REF_HOSTS_SETTING)) {
|
|
56
|
+
return {};
|
|
57
|
+
}
|
|
58
|
+
const value = settingValue(settings, REMOTE_REF_HOSTS_SETTING) ?? [];
|
|
59
|
+
const entries = [value].flat();
|
|
60
|
+
if (entries.some((entry) => typeof entry !== "string")) {
|
|
61
|
+
throw invalidSetting(apiName, REMOTE_REF_HOSTS_SETTING, "a host list");
|
|
62
|
+
}
|
|
63
|
+
return { remoteRefHosts: hostList(entries) ?? [] };
|
|
64
|
+
}
|
|
65
|
+
function apiRefLimitOptions(apiName, settings, configRoot) {
|
|
66
|
+
return {
|
|
67
|
+
...localRefRootOf(apiName, settings, configRoot),
|
|
68
|
+
...remoteRefHostsOf(apiName, settings)
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export { FIX_SETTING, OUTPUT_SETTING, apiRefLimitOptions, unknownSettingWarnings };
|
|
73
|
+
//# sourceMappingURL=api-settings.mjs.map
|