diff --git a/yaml/_dumper_state.ts b/yaml/_dumper_state.ts index 7771f9ad32b4..44e506a7ac7f 100644 --- a/yaml/_dumper_state.ts +++ b/yaml/_dumper_state.ts @@ -28,7 +28,7 @@ import { VERTICAL_LINE, } from "./_chars.ts"; import { DEFAULT_SCHEMA, type Schema } from "./_schema.ts"; -import type { KindType, RepresentFn, StyleVariant, Type } from "./_type.ts"; +import type { KindType, StyleVariant, Type } from "./_type.ts"; import { isObject } from "./_utils.ts"; const STYLE_PLAIN = 1; @@ -740,13 +740,13 @@ export class DumperState { if (typeof type.represent === "function") { return type.represent(value, style); } - if (Object.hasOwn(type.represent, style)) { - const represent = type.represent[style] as RepresentFn; - return represent(value, style); + const represent = type.represent[style]; + if (!represent) { + throw new TypeError( + `!<${type.tag}> tag resolver accepts not "${style}" style`, + ); } - throw new TypeError( - `!<${type.tag}> tag resolver accepts not "${style}" style`, - ); + return represent(value, style); } detectType(value: unknown): { tag: string | null; value: unknown } { diff --git a/yaml/stringify_test.ts b/yaml/stringify_test.ts index 55ef4e6a59e2..791dab6698ac 100644 --- a/yaml/stringify_test.ts +++ b/yaml/stringify_test.ts @@ -124,6 +124,12 @@ Deno.test({ stringify([true, false], { styles: { "!!bool": "uppercase" } }), "- TRUE\n- FALSE\n", ); + + assertThrows( + () => stringify([true, false], { styles: { "!!bool": "octal" } }), + TypeError, + '! tag resolver accepts not "octal" style', + ); }, });