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 readAlias() #5856

Merged
merged 1 commit into from
Aug 28, 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
57 changes: 29 additions & 28 deletions yaml/_loader_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,34 @@ export class LoaderState {
this.anchor = this.input.slice(position, this.position);
return true;
}
readAlias(): boolean {
if (this.peek() !== ASTERISK) return false;

let ch = this.next();

const position = this.position;

while (ch !== 0 && !isWhiteSpaceOrEOL(ch) && !isFlowIndicator(ch)) {
ch = this.next();
}

if (this.position === position) {
return this.throwError(
"Cannot read alias: alias name must contain at least one character",
);
}

const alias = this.input.slice(position, this.position);
if (!this.anchorMap.has(alias)) {
return this.throwError(
`Cannot read alias: unidentified alias "${alias}"`,
);
}

this.result = this.anchorMap.get(alias);
this.skipSeparationSpace(true, -1);
return true;
}

readDocument() {
const documentStart = this.position;
Expand Down Expand Up @@ -1531,33 +1559,6 @@ export class LoaderState {
}
}

function readAlias(state: LoaderState): boolean {
if (state.peek() !== ASTERISK) return false;

let ch = state.next();

const position = state.position;

while (ch !== 0 && !isWhiteSpaceOrEOL(ch) && !isFlowIndicator(ch)) {
ch = state.next();
}

if (state.position === position) {
return state.throwError(
"Cannot read alias: alias name must contain at least one character",
);
}

const alias = state.input.slice(position, state.position);
if (!state.anchorMap.has(alias)) {
return state.throwError(`Cannot read alias: unidentified alias "${alias}"`);
}

state.result = state.anchorMap.get(alias);
state.skipSeparationSpace(true, -1);
return true;
}

function composeNode(
state: LoaderState,
parentIndent: number,
Expand Down Expand Up @@ -1642,7 +1643,7 @@ function composeNode(
state.readDoubleQuotedScalar(flowIndent)
) {
hasContent = true;
} else if (readAlias(state)) {
} else if (state.readAlias()) {
hasContent = true;

if (state.tag !== null || state.anchor !== null) {
Expand Down