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): move exports to import file #5651

Merged
merged 3 commits into from
Aug 8, 2024
Merged
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
17 changes: 0 additions & 17 deletions yaml/_dumper.ts → yaml/_dumper_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -903,20 +903,3 @@ export class DumperState {
this.usedDuplicates = new Set();
}
}

// deno-lint-ignore no-explicit-any
export function dump(input: any, options: DumperStateOptions = {}): string {
const state = new DumperState(options);

if (state.useAnchors) state.getDuplicateReferences(input);

if (
state.writeNode(0, input, {
block: true,
compact: true,
isKey: false,
})
) return `${state.dump}\n`;

return "";
}
43 changes: 2 additions & 41 deletions yaml/_loader.ts → yaml/_loader_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const PATTERN_TAG_HANDLE = /^(?:!|!!|![a-z\-]+!)$/i;
const PATTERN_TAG_URI =
/^(?:!|[^,\[\]\{\}])(?:%[0-9a-f]{2}|[0-9a-z\-#;\/\?:@&=\+\$,_\.!~\*'\(\)\[\]])*$/i;

interface LoaderStateOptions {
export interface LoaderStateOptions {
/** specifies a schema to use. */
schema?: Schema;
/** compatibility with JSON.parse behaviour. */
Expand Down Expand Up @@ -136,7 +136,7 @@ function codepointToChar(codepoint: number): string {
);
}

class LoaderState {
export class LoaderState {
schema: Schema;
input: string;
length: number;
Expand Down Expand Up @@ -1691,42 +1691,3 @@ function composeNode(

return state.tag !== null || state.anchor !== null || hasContent;
}

function sanitizeInput(input: string) {
input = String(input);

if (input.length > 0) {
// Add tailing `\n` if not exists
if (!isEOL(input.charCodeAt(input.length - 1))) input += "\n";

// Strip BOM
if (input.charCodeAt(0) === 0xfeff) input = input.slice(1);
}

// Use 0 as string terminator. That significantly simplifies bounds check.
input += "\0";

return input;
}

export function loadDocuments(
input: string,
options: LoaderStateOptions = {},
): unknown[] {
input = sanitizeInput(input);
const state = new LoaderState(input, options);
return [...state.readDocuments()];
}

export function load(input: string, options: LoaderStateOptions = {}): unknown {
input = sanitizeInput(input);
const state = new LoaderState(input, options);
const documentGenerator = state.readDocuments();
const document = documentGenerator.next().value;
if (!documentGenerator.next().done) {
throw new SyntaxError(
"expected a single document in the stream, but found more",
);
}
return document ?? null;
}
38 changes: 35 additions & 3 deletions yaml/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

import { load, loadDocuments } from "./_loader.ts";
import { isEOL } from "./_chars.ts";
import { LoaderState } from "./_loader_state.ts";
import { SCHEMA_MAP, type SchemaType } from "./_schema.ts";

export type { SchemaType };
Expand All @@ -31,6 +32,23 @@ export interface ParseOptions {
onWarning?(error: Error): void;
}

function sanitizeInput(input: string) {
input = String(input);

if (input.length > 0) {
// Add tailing `\n` if not exists
if (!isEOL(input.charCodeAt(input.length - 1))) input += "\n";

// Strip BOM
if (input.charCodeAt(0) === 0xfeff) input = input.slice(1);
}

// Use 0 as string terminator. That significantly simplifies bounds check.
input += "\0";

return input;
}

/**
* Parse and return a YAML string as a parsed YAML document object.
*
Expand Down Expand Up @@ -58,7 +76,19 @@ export function parse(
content: string,
options: ParseOptions = {},
): unknown {
return load(content, { ...options, schema: SCHEMA_MAP.get(options.schema!) });
content = sanitizeInput(content);
const state = new LoaderState(content, {
...options,
schema: SCHEMA_MAP.get(options.schema!),
});
const documentGenerator = state.readDocuments();
const document = documentGenerator.next().value;
if (!documentGenerator.next().done) {
throw new SyntaxError(
"expected a single document in the stream, but found more",
);
}
return document ?? null;
}

/**
Expand Down Expand Up @@ -89,8 +119,10 @@ export function parse(
* @returns Array of parsed documents.
*/
export function parseAll(content: string, options: ParseOptions = {}): unknown {
return loadDocuments(content, {
content = sanitizeInput(content);
const state = new LoaderState(content, {
...options,
schema: SCHEMA_MAP.get(options.schema!),
});
return [...state.readDocuments()];
}
15 changes: 13 additions & 2 deletions yaml/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

import { dump } from "./_dumper.ts";
import { DumperState } from "./_dumper_state.ts";
import { SCHEMA_MAP, type SchemaType } from "./_schema.ts";
import type { StyleVariant } from "./_type.ts";

Expand Down Expand Up @@ -110,5 +110,16 @@
data: unknown,
options: StringifyOptions = {},
): string {
return dump(data, { ...options, schema: SCHEMA_MAP.get(options.schema!) });
const state = new DumperState({
...options,
schema: SCHEMA_MAP.get(options.schema!),
});

// deno-lint-ignore no-explicit-any
if (state.useAnchors) state.getDuplicateReferences(data as any);

if (state.writeNode(0, data, { block: true, compact: true, isKey: false })) {
return `${state.dump}\n`;
}
return "";

Check warning on line 124 in yaml/stringify.ts

View check run for this annotation

Codecov / codecov/patch

yaml/stringify.ts#L124

Added line #L124 was not covered by tests
}