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): share char constants #5246

Merged
merged 6 commits into from
Jul 2, 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
28 changes: 28 additions & 0 deletions yaml/_chars.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

export const TAB = 0x09; /* Tab */
export const LINE_FEED = 0x0a; /* LF */
export const CARRIAGE_RETURN = 0x0d; /* CR */
export const SPACE = 0x20; /* Space */
export const EXCLAMATION = 0x21; /* ! */
export const DOUBLE_QUOTE = 0x22; /* " */
export const SHARP = 0x23; /* # */
export const PERCENT = 0x25; /* % */
export const AMPERSAND = 0x26; /* & */
export const SINGLE_QUOTE = 0x27; /* ' */
export const ASTERISK = 0x2a; /* * */
export const COMMA = 0x2c; /* , */
export const MINUS = 0x2d; /* - */
export const COLON = 0x3a; /* : */
export const GREATER_THAN = 0x3e; /* > */
export const QUESTION = 0x3f; /* ? */
export const COMMERCIAL_AT = 0x40; /* @ */
export const LEFT_SQUARE_BRACKET = 0x5b; /* [ */
export const RIGHT_SQUARE_BRACKET = 0x5d; /* ] */
export const GRAVE_ACCENT = 0x60; /* ` */
export const LEFT_CURLY_BRACKET = 0x7b; /* { */
export const VERTICAL_LINE = 0x7c; /* | */
export const RIGHT_CURLY_BRACKET = 0x7d; /* } */
109 changes: 55 additions & 54 deletions yaml/_dumper/dumper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,30 @@
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import {
AMPERSAND,
ASTERISK,
COLON,
COMMA,
COMMERCIAL_AT,
DOUBLE_QUOTE,
EXCLAMATION,
GRAVE_ACCENT,
GREATER_THAN,
LEFT_CURLY_BRACKET,
LEFT_SQUARE_BRACKET,
LINE_FEED,
MINUS,
PERCENT,
QUESTION,
RIGHT_CURLY_BRACKET,
RIGHT_SQUARE_BRACKET,
SHARP,
SINGLE_QUOTE,
SPACE,
TAB,
VERTICAL_LINE,
} from "../_chars.ts";
import { YamlError } from "../_error.ts";
import type { RepresentFn } from "../_type.ts";
import * as common from "../_utils.ts";
Expand All @@ -11,29 +35,6 @@
type Any = common.Any;
type ArrayObject<T = Any> = common.ArrayObject<T>;

const CHAR_TAB = 0x09; /* Tab */
const CHAR_LINE_FEED = 0x0a; /* LF */
const CHAR_SPACE = 0x20; /* Space */
const CHAR_EXCLAMATION = 0x21; /* ! */
const CHAR_DOUBLE_QUOTE = 0x22; /* " */
const CHAR_SHARP = 0x23; /* # */
const CHAR_PERCENT = 0x25; /* % */
const CHAR_AMPERSAND = 0x26; /* & */
const CHAR_SINGLE_QUOTE = 0x27; /* ' */
const CHAR_ASTERISK = 0x2a; /* * */
const CHAR_COMMA = 0x2c; /* , */
const CHAR_MINUS = 0x2d; /* - */
const CHAR_COLON = 0x3a; /* : */
const CHAR_GREATER_THAN = 0x3e; /* > */
const CHAR_QUESTION = 0x3f; /* ? */
const CHAR_COMMERCIAL_AT = 0x40; /* @ */
const CHAR_LEFT_SQUARE_BRACKET = 0x5b; /* [ */
const CHAR_RIGHT_SQUARE_BRACKET = 0x5d; /* ] */
const CHAR_GRAVE_ACCENT = 0x60; /* ` */
const CHAR_LEFT_CURLY_BRACKET = 0x7b; /* { */
const CHAR_VERTICAL_LINE = 0x7c; /* | */
const CHAR_RIGHT_CURLY_BRACKET = 0x7d; /* } */

const ESCAPE_SEQUENCES = new Map<number, string>([
[0x00, "\\0"],
[0x07, "\\a"],
Expand Down Expand Up @@ -131,7 +132,7 @@

// [33] s-white ::= s-space | s-tab
function isWhitespace(c: number): boolean {
return c === CHAR_SPACE || c === CHAR_TAB;
return c === SPACE || c === TAB;
}

// Returns true if the character can be printed without escaping.
Expand All @@ -155,14 +156,14 @@
isPrintable(c) &&
c !== 0xfeff &&
// - c-flow-indicator
c !== CHAR_COMMA &&
c !== CHAR_LEFT_SQUARE_BRACKET &&
c !== CHAR_RIGHT_SQUARE_BRACKET &&
c !== CHAR_LEFT_CURLY_BRACKET &&
c !== CHAR_RIGHT_CURLY_BRACKET &&
c !== COMMA &&
c !== LEFT_SQUARE_BRACKET &&
c !== RIGHT_SQUARE_BRACKET &&
c !== LEFT_CURLY_BRACKET &&
c !== RIGHT_CURLY_BRACKET &&
// - ":" - "#"
c !== CHAR_COLON &&
c !== CHAR_SHARP
c !== COLON &&
c !== SHARP
);
}

Expand All @@ -176,27 +177,27 @@
!isWhitespace(c) && // - s-white
// - (c-indicator ::=
// “-” | “?” | “:” | “,” | “[” | “]” | “{” | “}”
c !== CHAR_MINUS &&
c !== CHAR_QUESTION &&
c !== CHAR_COLON &&
c !== CHAR_COMMA &&
c !== CHAR_LEFT_SQUARE_BRACKET &&
c !== CHAR_RIGHT_SQUARE_BRACKET &&
c !== CHAR_LEFT_CURLY_BRACKET &&
c !== CHAR_RIGHT_CURLY_BRACKET &&
c !== MINUS &&
c !== QUESTION &&
c !== COLON &&
c !== COMMA &&
c !== LEFT_SQUARE_BRACKET &&
c !== RIGHT_SQUARE_BRACKET &&
c !== LEFT_CURLY_BRACKET &&
c !== RIGHT_CURLY_BRACKET &&
// | “#” | “&” | “*” | “!” | “|” | “>” | “'” | “"”
c !== CHAR_SHARP &&
c !== CHAR_AMPERSAND &&
c !== CHAR_ASTERISK &&
c !== CHAR_EXCLAMATION &&
c !== CHAR_VERTICAL_LINE &&
c !== CHAR_GREATER_THAN &&
c !== CHAR_SINGLE_QUOTE &&
c !== CHAR_DOUBLE_QUOTE &&
c !== SHARP &&
c !== AMPERSAND &&
c !== ASTERISK &&
c !== EXCLAMATION &&
c !== VERTICAL_LINE &&
c !== GREATER_THAN &&
c !== SINGLE_QUOTE &&
c !== DOUBLE_QUOTE &&
// | “%” | “@” | “`”)
c !== CHAR_PERCENT &&
c !== CHAR_COMMERCIAL_AT &&
c !== CHAR_GRAVE_ACCENT
c !== PERCENT &&
c !== COMMERCIAL_AT &&
c !== GRAVE_ACCENT
);
}

Expand Down Expand Up @@ -249,7 +250,7 @@
// Case: block styles permitted.
for (i = 0; i < string.length; i++) {
char = string.charCodeAt(i);
if (char === CHAR_LINE_FEED) {
if (char === LINE_FEED) {
hasLineBreak = true;
// Check if any line can be folded.
if (shouldTrackWidth) {
Expand Down Expand Up @@ -535,7 +536,7 @@
_result += generateNextLine(state, level);
}

if (state.dump && CHAR_LINE_FEED === state.dump.charCodeAt(0)) {
if (state.dump && LINE_FEED === state.dump.charCodeAt(0)) {
_result += "-";
} else {
_result += "- ";
Expand Down Expand Up @@ -628,7 +629,7 @@
(state.dump && state.dump.length > 1024);

if (explicitPair) {
if (state.dump && CHAR_LINE_FEED === state.dump.charCodeAt(0)) {
if (state.dump && LINE_FEED === state.dump.charCodeAt(0)) {

Check warning on line 632 in yaml/_dumper/dumper.ts

View check run for this annotation

Codecov / codecov/patch

yaml/_dumper/dumper.ts#L632

Added line #L632 was not covered by tests
pairBuffer += "?";
} else {
pairBuffer += "? ";
Expand All @@ -645,7 +646,7 @@
continue; // Skip this pair because of invalid value.
}

if (state.dump && CHAR_LINE_FEED === state.dump.charCodeAt(0)) {
if (state.dump && LINE_FEED === state.dump.charCodeAt(0)) {
pairBuffer += ":";
} else {
pairBuffer += ": ";
Expand Down
33 changes: 22 additions & 11 deletions yaml/_loader/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import {
CARRIAGE_RETURN,
COMMA,
LEFT_CURLY_BRACKET,
LEFT_SQUARE_BRACKET,
LINE_FEED,
RIGHT_CURLY_BRACKET,
RIGHT_SQUARE_BRACKET,
SPACE,
TAB,
} from "../_chars.ts";
import { YamlError } from "../_error.ts";
import { Mark } from "../_mark.ts";
import type { Type } from "../_type.ts";
Expand Down Expand Up @@ -39,29 +50,29 @@ function _class(obj: unknown): string {
}

function isEOL(c: number): boolean {
return c === 0x0a || /* LF */ c === 0x0d /* CR */;
return c === LINE_FEED || c === CARRIAGE_RETURN;
}

function isWhiteSpace(c: number): boolean {
return c === 0x09 || /* Tab */ c === 0x20 /* Space */;
return c === TAB || c === SPACE;
}

function isWsOrEol(c: number): boolean {
return (
c === 0x09 /* Tab */ ||
c === 0x20 /* Space */ ||
c === 0x0a /* LF */ ||
c === 0x0d /* CR */
c === TAB ||
c === SPACE ||
c === LINE_FEED ||
c === CARRIAGE_RETURN
);
}

function isFlowIndicator(c: number): boolean {
return (
c === 0x2c /* , */ ||
c === 0x5b /* [ */ ||
c === 0x5d /* ] */ ||
c === 0x7b /* { */ ||
c === 0x7d /* } */
c === COMMA ||
c === LEFT_SQUARE_BRACKET ||
c === RIGHT_SQUARE_BRACKET ||
c === LEFT_CURLY_BRACKET ||
c === RIGHT_CURLY_BRACKET
);
}

Expand Down