Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(yaml): inline readBlockSequence() #5776

Merged
merged 1 commit into from
Aug 22, 2024
Merged
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
125 changes: 62 additions & 63 deletions yaml/_loader_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,66 @@
this.result += result;
}
}
readBlockSequence(nodeIndent: number): boolean {
let line: number;
let following: number;
let detected = false;
let ch: number;
const tag = this.tag;
const anchor = this.anchor;
const result: unknown[] = [];

if (this.anchor !== null && typeof this.anchor !== "undefined") {
this.anchorMap.set(this.anchor, result);
}

ch = this.peek();

while (ch !== 0) {
if (ch !== MINUS) {
break;
}

following = this.peek(1);

if (!isWhiteSpaceOrEOL(following)) {
break;
}

detected = true;
this.position++;

if (skipSeparationSpace(this, true, -1)) {
if (this.lineIndent <= nodeIndent) {
result.push(null);
ch = this.peek();
continue;
}

Check warning on line 325 in yaml/_loader_state.ts

View check run for this annotation

Codecov / codecov/patch

yaml/_loader_state.ts#L321-L325

Added lines #L321 - L325 were not covered by tests
}

line = this.line;
composeNode(this, nodeIndent, CONTEXT_BLOCK_IN, false, true);
result.push(this.result);
skipSeparationSpace(this, true, -1);

ch = this.peek();

if ((this.line === line || this.lineIndent > nodeIndent) && ch !== 0) {
return this.throwError("bad indentation of a sequence entry");

Check warning on line 336 in yaml/_loader_state.ts

View check run for this annotation

Codecov / codecov/patch

yaml/_loader_state.ts#L336

Added line #L336 was not covered by tests
} else if (this.lineIndent < nodeIndent) {
break;
}
}

if (detected) {
this.tag = tag;
this.anchor = anchor;
this.kind = "sequence";
this.result = result;
return true;
}
return false;
}

readDocument() {
const documentStart = this.position;
Expand Down Expand Up @@ -1122,67 +1182,6 @@
return true;
}

function readBlockSequence(state: LoaderState, nodeIndent: number): boolean {
let line: number;
let following: number;
let detected = false;
let ch: number;
const tag = state.tag;
const anchor = state.anchor;
const result: unknown[] = [];

if (state.anchor !== null && typeof state.anchor !== "undefined") {
state.anchorMap.set(state.anchor, result);
}

ch = state.peek();

while (ch !== 0) {
if (ch !== MINUS) {
break;
}

following = state.peek(1);

if (!isWhiteSpaceOrEOL(following)) {
break;
}

detected = true;
state.position++;

if (skipSeparationSpace(state, true, -1)) {
if (state.lineIndent <= nodeIndent) {
result.push(null);
ch = state.peek();
continue;
}
}

line = state.line;
composeNode(state, nodeIndent, CONTEXT_BLOCK_IN, false, true);
result.push(state.result);
skipSeparationSpace(state, true, -1);

ch = state.peek();

if ((state.line === line || state.lineIndent > nodeIndent) && ch !== 0) {
return state.throwError("bad indentation of a sequence entry");
} else if (state.lineIndent < nodeIndent) {
break;
}
}

if (detected) {
state.tag = tag;
state.anchor = anchor;
state.kind = "sequence";
state.result = result;
return true;
}
return false;
}

function readBlockMapping(
state: LoaderState,
nodeIndent: number,
Expand Down Expand Up @@ -1594,7 +1593,7 @@
if (indentStatus === 1) {
if (
(allowBlockCollections &&
(readBlockSequence(state, blockIndent) ||
(state.readBlockSequence(blockIndent) ||
readBlockMapping(state, blockIndent, flowIndent))) ||
readFlowCollection(state, flowIndent)
) {
Expand Down Expand Up @@ -1632,7 +1631,7 @@
// Special case: block sequences are allowed to have same indentation level as the parent.
// http://www.yaml.org/spec/1.2/spec.html#id2799784
hasContent = allowBlockCollections &&
readBlockSequence(state, blockIndent);
state.readBlockSequence(blockIndent);
}
}

Expand Down