Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion yaml/_loader_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,13 @@ export class LoaderState {
"Cannot read flow collection: missing comma between flow collection entries",
);
}
if (ch === COMMA) {
// "flow collection entries can never be completely empty",
// as per YAML 1.2, section 7.4
throw this.#createError(
"Cannot read flow collection: expected the node content, but found ','",
);
}

keyTag = keyNode = valueNode = null;
isPair = isExplicitPair = false;
Expand Down Expand Up @@ -1515,8 +1522,11 @@ export class LoaderState {
resolveTag(state: State) {
switch (state.tag) {
case null:
case "!":
case "!": {
const { anchor } = state;
if (anchor !== null) this.anchorMap.set(anchor, state.result);
return state;
}
case "?": {
for (const type of this.implicitTypes) {
// Implicit resolving is not allowed for non-scalar types, and '?'
Expand Down
21 changes: 18 additions & 3 deletions yaml/_type/int.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ function isDecCode(c: number): boolean {
}

function resolveYamlInteger(data: string): boolean {
if (data === null) return false;

const max = data.length;
let index = 0;
let hasDigits = false;
Expand Down Expand Up @@ -73,6 +75,19 @@ function resolveYamlInteger(data: string): boolean {
return hasDigits && ch !== "_";
}

if (ch === "o") {
// base 8
index++;

for (; index < max; index++) {
ch = data[index];
if (ch === "_") continue;
if (!isOctCode(data.charCodeAt(index))) return false;
hasDigits = true;
}
return hasDigits && ch !== "_";
}

// base 8
for (; index < max; index++) {
ch = data[index];
Expand All @@ -83,7 +98,7 @@ function resolveYamlInteger(data: string): boolean {
return hasDigits && ch !== "_";
}

// base 10 (except 0) or base 60
// base 10

// value should not start with `_`;
if (ch === "_") return false;
Expand All @@ -100,8 +115,7 @@ function resolveYamlInteger(data: string): boolean {
// Should have digits and should not end with `_`
if (!hasDigits || ch === "_") return false;

// base60 almost not used, no needs to optimize
return /^(:[0-5]?[0-9])+$/.test(data.slice(index));
return true;
}

function constructYamlInteger(data: string): number {
Expand All @@ -124,6 +138,7 @@ function constructYamlInteger(data: string): number {
if (ch === "0") {
if (value[1] === "b") return sign * parseInt(value.slice(2), 2);
if (value[1] === "x") return sign * parseInt(value, 16);
if (value[1] === "o") return sign * parseInt(value.slice(2), 8);
return sign * parseInt(value, 8);
}

Expand Down
1 change: 1 addition & 0 deletions yaml/_type/nil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const nil: Type<"scalar", null> = {
predicate: (object: unknown): object is null => object === null,
construct: () => null,
resolve: (data: string): boolean => {
if (data === null) return true;
return (
data === "~" ||
data === "null" ||
Expand Down
4 changes: 3 additions & 1 deletion yaml/_type/omap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import type { Type } from "../_type.ts";
import { isPlainObject } from "../_utils.ts";

function resolveYamlOmap(data: Record<string, unknown>[]): boolean {
if (data === null) return true;

const objectKeys = new Set();
for (const object of data) {
if (!isPlainObject(object)) return false;
Expand All @@ -25,6 +27,6 @@ export const omap: Type<"sequence", Record<PropertyKey, unknown>[]> = {
kind: "sequence",
resolve: resolveYamlOmap,
construct(data) {
return data;
return data !== null ? data : [];
},
};
80 changes: 79 additions & 1 deletion yaml/parse_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,24 @@ Deno.test({
- 012_34
- 0x1A
- 0x1A_2B
- 0o17
- 0o777
`),
[10, 170, 10, 668, 26, 6699],
[10, 170, 10, 668, 26, 6699, 15, 511],
);

// explicit !!int tag on resolvable scalars
assertEquals(
parse(`
- !!int 42
- !!int -42
- !!int "42"
- !!int 0b1010
- !!int 012
- !!int 0x1A
- !!int 0o17
`),
[42, -42, 42, 10, 10, 26, 15],
);

// empty string is invalid integer
Expand Down Expand Up @@ -422,6 +438,13 @@ Deno.test({
SyntaxError,
"Cannot resolve a node with !<tag:yaml.org,2002:int> explicit tag at line 2, column 1:\n \n ^",
);

// empty value with !!int tag fails with a syntax error, not a TypeError
assertThrows(
() => parse("x: !!int\n"),
SyntaxError,
"Cannot resolve a node with !<tag:yaml.org,2002:int> explicit tag",
);
},
});

Expand Down Expand Up @@ -499,6 +522,22 @@ Deno.test({
SyntaxError,
"Cannot resolve a node with !<tag:yaml.org,2002:omap> explicit tag",
);

// empty omap resolves to an empty sequence
assertEquals(parse("x: !!omap\n"), { x: [] });
},
});

Deno.test({
name: "parse() handles null type",
fn() {
assertEquals(parse("x: null\ny: ~\nz:\n"), {
x: null,
y: null,
z: null,
});
// explicit !!null tag on an empty value resolves to null
assertEquals(parse("x: !!null\n"), { x: null });
},
});

Expand Down Expand Up @@ -597,6 +636,32 @@ Deno.test("parse() handles anchors and aliases", () => {
new Uint8Array([72, 101, 108, 108, 111]),
],
);
// anchor on the key line binds the indented scalar value
assertEquals(
parse(`a: &anchor
value
ref: *anchor`),
{ a: "value", ref: "value" },
);
assertEquals(
parse(`a: &anchor
"quoted"
ref: *anchor`),
{ a: "quoted", ref: "quoted" },
);
assertEquals(
parse(`a: &anchor
42
ref: *anchor`),
{ a: 42, ref: 42 },
);
// anchor with empty value binds null
assertEquals(
parse(`a: &anchor

ref: *anchor`),
{ a: null, ref: null },
);
assertThrows(
() =>
parse(`- &anchor Foo
Expand All @@ -613,6 +678,19 @@ Deno.test("parse() handles anchors and aliases", () => {
);
});

Deno.test("parse() rejects empty flow collection entries", () => {
// YAML 1.2, section 7.4: flow collection entries can never be empty
for (const doc of ["[, 4]", "[1, , 3]", "[, ]", "{, a: 1}"]) {
assertThrows(
() => parse(doc),
SyntaxError,
"expected the node content, but found ','",
);
}
// a trailing comma is allowed
assertEquals(parse("[a,]"), ["a"]);
});

Deno.test("parse() handles escaped strings in double quotes", () => {
assertEquals(parse('"\\"bar\\""'), '"bar"');
assertEquals(parse('"\\x30\\x31"'), "01");
Expand Down
Loading