refactor(yaml): inline readFlowCollection() (#5840)

This commit is contained in:
Tim Reichen 2024-08-27 10:40:56 +02:00 committed by GitHub
parent 3dbccc5a84
commit ab2c1cd7ca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -825,6 +825,124 @@ export class LoaderState {
"Unexpected end of the stream within a double quoted scalar",
);
}
readFlowCollection(nodeIndent: number): boolean {
let ch = this.peek();
let terminator: number;
let isMapping = true;
let result = {};
if (ch === LEFT_SQUARE_BRACKET) {
terminator = RIGHT_SQUARE_BRACKET;
isMapping = false;
result = [];
} else if (ch === LEFT_CURLY_BRACKET) {
terminator = RIGHT_CURLY_BRACKET;
} else {
return false;
}
if (this.anchor !== null && typeof this.anchor !== "undefined") {
this.anchorMap.set(this.anchor, result);
}
ch = this.next();
const tag = this.tag;
const anchor = this.anchor;
let readNext = true;
let valueNode = null;
let keyNode = null;
let keyTag: string | null = null;
let isExplicitPair = false;
let isPair = false;
let following = 0;
let line = 0;
const overridableKeys = new Set<string>();
while (ch !== 0) {
this.skipSeparationSpace(true, nodeIndent);
ch = this.peek();
if (ch === terminator) {
this.position++;
this.tag = tag;
this.anchor = anchor;
this.kind = isMapping ? "mapping" : "sequence";
this.result = result;
return true;
}
if (!readNext) {
return this.throwError(
"Cannot read flow collection: missing comma between flow collection entries",
);
}
keyTag = keyNode = valueNode = null;
isPair = isExplicitPair = false;
if (ch === QUESTION) {
following = this.peek(1);
if (isWhiteSpaceOrEOL(following)) {
isPair = isExplicitPair = true;
this.position++;
this.skipSeparationSpace(true, nodeIndent);
}
}
line = this.line;
composeNode(this, nodeIndent, CONTEXT_FLOW_IN, false, true);
keyTag = this.tag || null;
keyNode = this.result;
this.skipSeparationSpace(true, nodeIndent);
ch = this.peek();
if ((isExplicitPair || this.line === line) && ch === COLON) {
isPair = true;
ch = this.next();
this.skipSeparationSpace(true, nodeIndent);
composeNode(this, nodeIndent, CONTEXT_FLOW_IN, false, true);
valueNode = this.result;
}
if (isMapping) {
this.storeMappingPair(
result as Record<string, unknown>,
overridableKeys,
keyTag,
keyNode,
valueNode,
);
} else if (isPair) {
(result as Record<string, unknown>[]).push(
this.storeMappingPair(
{},
overridableKeys,
keyTag,
keyNode,
valueNode,
),
);
} else {
(result as unknown[]).push(keyNode);
}
this.skipSeparationSpace(true, nodeIndent);
ch = this.peek();
if (ch === COMMA) {
readNext = true;
ch = this.next();
} else {
readNext = false;
}
}
return this.throwError(
"Cannot read flow collection: unexpected end of the stream within a flow collection",
);
}
readDocument() {
const documentStart = this.position;
@ -954,125 +1072,6 @@ export class LoaderState {
}
}
function readFlowCollection(state: LoaderState, nodeIndent: number): boolean {
let ch = state.peek();
let terminator: number;
let isMapping = true;
let result = {};
if (ch === LEFT_SQUARE_BRACKET) {
terminator = RIGHT_SQUARE_BRACKET;
isMapping = false;
result = [];
} else if (ch === LEFT_CURLY_BRACKET) {
terminator = RIGHT_CURLY_BRACKET;
} else {
return false;
}
if (state.anchor !== null && typeof state.anchor !== "undefined") {
state.anchorMap.set(state.anchor, result);
}
ch = state.next();
const tag = state.tag;
const anchor = state.anchor;
let readNext = true;
let valueNode = null;
let keyNode = null;
let keyTag: string | null = null;
let isExplicitPair = false;
let isPair = false;
let following = 0;
let line = 0;
const overridableKeys = new Set<string>();
while (ch !== 0) {
state.skipSeparationSpace(true, nodeIndent);
ch = state.peek();
if (ch === terminator) {
state.position++;
state.tag = tag;
state.anchor = anchor;
state.kind = isMapping ? "mapping" : "sequence";
state.result = result;
return true;
}
if (!readNext) {
return state.throwError(
"Cannot read flow collection: missing comma between flow collection entries",
);
}
keyTag = keyNode = valueNode = null;
isPair = isExplicitPair = false;
if (ch === QUESTION) {
following = state.peek(1);
if (isWhiteSpaceOrEOL(following)) {
isPair = isExplicitPair = true;
state.position++;
state.skipSeparationSpace(true, nodeIndent);
}
}
line = state.line;
composeNode(state, nodeIndent, CONTEXT_FLOW_IN, false, true);
keyTag = state.tag || null;
keyNode = state.result;
state.skipSeparationSpace(true, nodeIndent);
ch = state.peek();
if ((isExplicitPair || state.line === line) && ch === COLON) {
isPair = true;
ch = state.next();
state.skipSeparationSpace(true, nodeIndent);
composeNode(state, nodeIndent, CONTEXT_FLOW_IN, false, true);
valueNode = state.result;
}
if (isMapping) {
state.storeMappingPair(
result as Record<string, unknown>,
overridableKeys,
keyTag,
keyNode,
valueNode,
);
} else if (isPair) {
(result as Record<string, unknown>[]).push(
state.storeMappingPair(
{},
overridableKeys,
keyTag,
keyNode,
valueNode,
),
);
} else {
(result as unknown[]).push(keyNode);
}
state.skipSeparationSpace(true, nodeIndent);
ch = state.peek();
if (ch === COMMA) {
readNext = true;
ch = state.next();
} else {
readNext = false;
}
}
return state.throwError(
"Cannot read flow collection: unexpected end of the stream within a flow collection",
);
}
// Handles block scaler styles: e.g. '|', '>', '|-' and '>-'.
// https://yaml.org/spec/1.2.2/#81-block-scalar-styles
function readBlockScalar(state: LoaderState, nodeIndent: number): boolean {
@ -1645,7 +1644,7 @@ function composeNode(
(allowBlockCollections &&
(state.readBlockSequence(blockIndent) ||
readBlockMapping(state, blockIndent, flowIndent))) ||
readFlowCollection(state, flowIndent)
state.readFlowCollection(flowIndent)
) {
hasContent = true;
} else {