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): prepare for noUncheckedIndexedAccess #4457

Merged
merged 4 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
76 changes: 19 additions & 57 deletions yaml/_dumper/dumper.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.

import { YAMLError } from "../_error.ts";
import type { RepresentFn, StyleVariant, Type } from "../type.ts";
import type { RepresentFn } from "../type.ts";
import * as common from "../_utils.ts";
import { DumperState, type DumperStateOptions } from "./dumper_state.ts";

Expand Down Expand Up @@ -129,20 +129,7 @@
}

function testImplicitResolving(state: DumperState, str: string): boolean {
let type: Type;
for (
let index = 0, length = state.implicitTypes.length;
index < length;
index += 1
) {
type = state.implicitTypes[index];

if (type.resolve(str)) {
return true;
}
}

return false;
return state.implicitTypes.some((type) => type.resolve(str));
}

// [33] s-white ::= s-space | s-tab
Expand Down Expand Up @@ -379,7 +366,7 @@
// tslint:disable-next-line:no-conditional-assignment
while ((match = lineRe.exec(string))) {
const prefix = match[1],
line = match[2];
line = match[2] || "";

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

View check run for this annotation

Codecov / codecov/patch

yaml/_dumper/dumper.ts#L369

Added line #L369 was not covered by tests
moreIndented = line[0] === " ";
result += prefix +
(!prevMoreIndented && !moreIndented && line !== "" ? "\n" : "") +
Expand Down Expand Up @@ -572,18 +559,12 @@
const _tag = state.tag,
objectKeyList = Object.keys(object);

let pairBuffer: string, objectKey: string, objectValue: Any;
for (
let index = 0, length = objectKeyList.length;
index < length;
index += 1
) {
pairBuffer = state.condenseFlow ? '"' : "";
for (const [index, objectKey] of objectKeyList.entries()) {
let pairBuffer = state.condenseFlow ? '"' : "";

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

View check run for this annotation

Codecov / codecov/patch

yaml/_dumper/dumper.ts#L562-L563

Added lines #L562 - L563 were not covered by tests

if (index !== 0) pairBuffer += ", ";

objectKey = objectKeyList[index];
objectValue = object[objectKey];
const objectValue = object[objectKey];

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

View check run for this annotation

Codecov / codecov/patch

yaml/_dumper/dumper.ts#L567

Added line #L567 was not covered by tests

if (!writeNode(state, level, objectKey, false, false)) {
continue; // Skip this pair because of invalid key;
Expand Down Expand Up @@ -631,29 +612,20 @@
throw new YAMLError("sortKeys must be a boolean or a function");
}

let pairBuffer = "",
objectKey: string,
objectValue: Any,
explicitPair: boolean;
for (
let index = 0, length = objectKeyList.length;
index < length;
index += 1
) {
pairBuffer = "";
for (const [index, objectKey] of objectKeyList.entries()) {
let pairBuffer = "";

if (!compact || index !== 0) {
pairBuffer += generateNextLine(state, level);
}

objectKey = objectKeyList[index];
objectValue = object[objectKey];
const objectValue = object[objectKey];

if (!writeNode(state, level + 1, objectKey, true, true, true)) {
continue; // Skip this pair because of invalid key.
}

explicitPair = (state.tag !== null && state.tag !== "?") ||
const explicitPair = (state.tag !== null && state.tag !== "?") ||
(state.dump && state.dump.length > 1024);

if (explicitPair) {
Expand Down Expand Up @@ -697,11 +669,8 @@
): boolean {
const typeList = explicit ? state.explicitTypes : state.implicitTypes;

let type: Type;
let style: StyleVariant;
let _result: string;
for (let index = 0, length = typeList.length; index < length; index += 1) {
type = typeList[index];
for (const type of typeList) {
let _result: string;

if (
(type.instanceOf || type.predicate) &&
Expand All @@ -712,12 +681,12 @@
state.tag = explicit ? type.tag : "?";

if (type.represent) {
style = state.styleMap[type.tag] || type.defaultStyle;
const style = state.styleMap[type.tag]! || type.defaultStyle;

if (_toString.call(type.represent) === "[object Function]") {
_result = (type.represent as RepresentFn)(object, style);
} else if (hasOwn(type.represent, style)) {
_result = (type.represent as ArrayObject<RepresentFn>)[style](
_result = (type.represent as ArrayObject<RepresentFn>)[style]!(
object,
style,
);
Expand Down Expand Up @@ -845,14 +814,8 @@
inspectNode(object[idx], objects, duplicatesIndexes);
}
} else {
const objectKeyList = Object.keys(object);

for (
let idx = 0, length = objectKeyList.length;
idx < length;
idx += 1
) {
inspectNode(object[objectKeyList[idx]], objects, duplicatesIndexes);
for (const objectKey of Object.keys(object)) {
inspectNode(object[objectKey], objects, duplicatesIndexes);
}
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
}
}
Expand All @@ -868,11 +831,10 @@

inspectNode(object, objects, duplicatesIndexes);

const length = duplicatesIndexes.length;
for (let index = 0; index < length; index += 1) {
state.duplicates.push(objects[duplicatesIndexes[index]]);
for (const idx of duplicatesIndexes) {
state.duplicates.push(objects[idx]);

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

View check run for this annotation

Codecov / codecov/patch

yaml/_dumper/dumper.ts#L835

Added line #L835 was not covered by tests
}
state.usedDuplicates = Array.from({ length });
state.usedDuplicates = Array.from({ length: duplicatesIndexes.length });
}

export function dump(input: Any, options?: DumperStateOptions): string {
Expand Down
10 changes: 3 additions & 7 deletions yaml/_dumper/dumper_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@
): ArrayObject<StyleVariant> {
if (typeof map === "undefined" || map === null) return {};

let type: Type;
const result: ArrayObject<StyleVariant> = {};
const keys = Object.keys(map);
let tag: string, style: StyleVariant;
for (let index = 0, length = keys.length; index < length; index += 1) {
tag = keys[index];
style = String(map[tag]) as StyleVariant;
for (let tag of Object.keys(map)) {
let style = String(map[tag]) as StyleVariant;

Check warning on line 21 in yaml/_dumper/dumper_state.ts

View check run for this annotation

Codecov / codecov/patch

yaml/_dumper/dumper_state.ts#L20-L21

Added lines #L20 - L21 were not covered by tests
if (tag.slice(0, 2) === "!!") {
tag = `tag:yaml.org,2002:${tag.slice(2)}`;
}
type = schema.compiledTypeMap.fallback[tag];
const type = schema.compiledTypeMap.fallback[tag];

Check warning on line 25 in yaml/_dumper/dumper_state.ts

View check run for this annotation

Codecov / codecov/patch

yaml/_dumper/dumper_state.ts#L25

Added line #L25 was not covered by tests

if (
type &&
Expand Down
20 changes: 9 additions & 11 deletions yaml/_loader/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,13 @@
return throwError(state, "YAML directive accepts exactly one argument");
}

const match = /^([0-9]+)\.([0-9]+)$/.exec(args[0]);
const match = /^([0-9]+)\.([0-9]+)$/.exec(args[0]!);

Check warning on line 205 in yaml/_loader/loader.ts

View check run for this annotation

Codecov / codecov/patch

yaml/_loader/loader.ts#L205

Added line #L205 was not covered by tests
if (match === null) {
return throwError(state, "ill-formed argument of the YAML directive");
}

const major = parseInt(match[1], 10);
const minor = parseInt(match[2], 10);
const major = parseInt(match[1]!, 10);
const minor = parseInt(match[2]!, 10);

Check warning on line 211 in yaml/_loader/loader.ts

View check run for this annotation

Codecov / codecov/patch

yaml/_loader/loader.ts#L210-L211

Added lines #L210 - L211 were not covered by tests
if (major !== 1) {
return throwError(state, "unacceptable YAML version of the document");
}
Expand All @@ -225,8 +225,8 @@
return throwError(state, "TAG directive accepts exactly two arguments");
}

const handle = args[0];
const prefix = args[1];
const handle = args[0]!;
const prefix = args[1]!;

Check warning on line 229 in yaml/_loader/loader.ts

View check run for this annotation

Codecov / codecov/patch

yaml/_loader/loader.ts#L228-L229

Added lines #L228 - L229 were not covered by tests

if (!PATTERN_TAG_HANDLE.test(handle)) {
return throwError(
Expand Down Expand Up @@ -300,9 +300,7 @@
);
}

const keys = Object.keys(source);
for (let i = 0, len = keys.length; i < len; i++) {
const key = keys[i];
for (const key in Object.keys(source)) {

Check warning on line 303 in yaml/_loader/loader.ts

View check run for this annotation

Codecov / codecov/patch

yaml/_loader/loader.ts#L303

Added line #L303 was not covered by tests
if (!hasOwn(destination, key)) {
Object.defineProperty(destination, key, {
value: source[key],
Expand Down Expand Up @@ -1562,7 +1560,7 @@
typeIndex < typeQuantity;
typeIndex++
) {
type = state.implicitTypes[typeIndex];
type = state.implicitTypes[typeIndex]!;

// Implicit resolving is not allowed for non-scalar types, and '?'
// non-specific tag is only assigned to plain scalars. So, it isn't
Expand All @@ -1581,7 +1579,7 @@
} else if (
hasOwn(state.typeMap[state.kind || "fallback"], state.tag)
) {
type = state.typeMap[state.kind || "fallback"][state.tag];
type = state.typeMap[state.kind || "fallback"][state.tag]!;

if (state.result !== null && type.kind !== state.kind) {
return throwError(
Expand Down Expand Up @@ -1679,7 +1677,7 @@
if (ch !== 0) readLineBreak(state);

if (hasOwn(directiveHandlers, directiveName)) {
directiveHandlers[directiveName](state, directiveName, ...directiveArgs);
directiveHandlers[directiveName]!(state, directiveName, ...directiveArgs);

Check warning on line 1680 in yaml/_loader/loader.ts

View check run for this annotation

Codecov / codecov/patch

yaml/_loader/loader.ts#L1680

Added line #L1680 was not covered by tests
} else {
throwWarning(state, `unknown document directive "${directiveName}"`);
}
Expand Down
2 changes: 1 addition & 1 deletion yaml/_type/binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function representYamlBinary(object: Uint8Array): string {
result += map[bits & 0x3f];
}

bits = (bits << 8) + object[idx];
bits = (bits << 8) + object[idx]!;
}

// Dump tail
Expand Down
2 changes: 1 addition & 1 deletion yaml/_type/float.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
const sign = value[0] === "-" ? -1 : 1;
const digits: number[] = [];

if ("+-".indexOf(value[0]) >= 0) {
if (value[0] && "+-".indexOf(value[0]) >= 0) {

Check warning on line 41 in yaml/_type/float.ts

View check run for this annotation

Codecov / codecov/patch

yaml/_type/float.ts#L41

Added line #L41 was not covered by tests
value = value.slice(1);
}

Expand Down
6 changes: 2 additions & 4 deletions yaml/_type/pairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
function resolveYamlPairs(data: Any[][]): boolean {
const result = Array.from({ length: data.length });

for (let index = 0; index < data.length; index++) {
const pair = data[index];

for (const [index, pair] of data.entries()) {

Check warning on line 14 in yaml/_type/pairs.ts

View check run for this annotation

Codecov / codecov/patch

yaml/_type/pairs.ts#L14

Added line #L14 was not covered by tests
if (_toString.call(pair) !== "[object Object]") return false;

const keys = Object.keys(pair);
Expand All @@ -32,7 +30,7 @@
const result = Array.from({ length: data.length });

for (let index = 0; index < data.length; index += 1) {
const pair = data[index];
const pair = data[index]!;

Check warning on line 33 in yaml/_type/pairs.ts

View check run for this annotation

Codecov / codecov/patch

yaml/_type/pairs.ts#L33

Added line #L33 was not covered by tests

const keys = Object.keys(pair);

Expand Down
12 changes: 6 additions & 6 deletions yaml/_type/timestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@

// match: [1] year [2] month [3] day

const year = +match[1];
const month = +match[2] - 1; // JS month starts with 0
const day = +match[3];
const year = +match[1]!;
const month = +match[2]! - 1; // JS month starts with 0
const day = +match[3]!;

Check warning on line 44 in yaml/_type/timestamp.ts

View check run for this annotation

Codecov / codecov/patch

yaml/_type/timestamp.ts#L42-L44

Added lines #L42 - L44 were not covered by tests

if (!match[4]) {
// no hour
Expand All @@ -51,8 +51,8 @@
// match: [4] hour [5] minute [6] second [7] fraction

const hour = +match[4];
const minute = +match[5];
const second = +match[6];
const minute = +match[5]!;
const second = +match[6]!;

Check warning on line 55 in yaml/_type/timestamp.ts

View check run for this annotation

Codecov / codecov/patch

yaml/_type/timestamp.ts#L54-L55

Added lines #L54 - L55 were not covered by tests

let fraction = 0;
if (match[7]) {
Expand All @@ -67,7 +67,7 @@
// match: [8] tz [9] tz_sign [10] tz_hour [11] tz_minute

let delta = null;
if (match[9]) {
if (match[9] && match[10]) {

Check warning on line 70 in yaml/_type/timestamp.ts

View check run for this annotation

Codecov / codecov/patch

yaml/_type/timestamp.ts#L70

Added line #L70 was not covered by tests
const tzHour = +match[10];
const tzMinute = +(match[11] || 0);
delta = (tzHour * 60 + tzMinute) * 60000; // delta in milli-seconds
Expand Down
7 changes: 1 addition & 6 deletions yaml/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ function compileList(
}

for (const currentType of schema[name]) {
for (
let previousIndex = 0;
previousIndex < result.length;
previousIndex++
) {
const previousType = result[previousIndex];
for (const [previousIndex, previousType] of result.entries()) {
if (
previousType.tag === currentType.tag &&
previousType.kind === currentType.kind
Expand Down
Loading