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 1 commit
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
31 changes: 12 additions & 19 deletions yaml/_dumper/dumper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
index < length;
index += 1
) {
type = state.implicitTypes[index];
type = state.implicitTypes[index]!;
gabelluardo marked this conversation as resolved.
Show resolved Hide resolved

if (type.resolve(str)) {
return true;
Expand Down Expand Up @@ -379,7 +379,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 382 in yaml/_dumper/dumper.ts

View check run for this annotation

Codecov / codecov/patch

yaml/_dumper/dumper.ts#L382

Added line #L382 was not covered by tests
gabelluardo marked this conversation as resolved.
Show resolved Hide resolved
moreIndented = line[0] === " ";
result += prefix +
(!prevMoreIndented && !moreIndented && line !== "" ? "\n" : "") +
Expand Down Expand Up @@ -582,7 +582,7 @@

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

objectKey = objectKeyList[index];
objectKey = objectKeyList[index]!;

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

View check run for this annotation

Codecov / codecov/patch

yaml/_dumper/dumper.ts#L585

Added line #L585 was not covered by tests
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
objectValue = object[objectKey];

if (!writeNode(state, level, objectKey, false, false)) {
Expand Down Expand Up @@ -646,7 +646,7 @@
pairBuffer += generateNextLine(state, level);
}

objectKey = objectKeyList[index];
objectKey = objectKeyList[index]!;
gabelluardo marked this conversation as resolved.
Show resolved Hide resolved
objectValue = object[objectKey];

if (!writeNode(state, level + 1, objectKey, true, true, true)) {
Expand Down Expand Up @@ -701,7 +701,7 @@
let style: StyleVariant;
let _result: string;
for (let index = 0, length = typeList.length; index < length; index += 1) {
type = typeList[index];
type = typeList[index]!;
gabelluardo marked this conversation as resolved.
Show resolved Hide resolved
gabelluardo marked this conversation as resolved.
Show resolved Hide resolved

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

if (type.represent) {
style = state.styleMap[type.tag] || type.defaultStyle;
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 +845,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 +862,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 866 in yaml/_dumper/dumper.ts

View check run for this annotation

Codecov / codecov/patch

yaml/_dumper/dumper.ts#L866

Added line #L866 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
4 changes: 2 additions & 2 deletions yaml/_dumper/dumper_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
const keys = Object.keys(map);
let tag: string, style: StyleVariant;
for (let index = 0, length = keys.length; index < length; index += 1) {
tag = keys[index];
tag = keys[index]!;

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

View check run for this annotation

Codecov / codecov/patch

yaml/_dumper/dumper_state.ts#L24

Added line #L24 was not covered by tests
gabelluardo marked this conversation as resolved.
Show resolved Hide resolved
style = String(map[tag]) as StyleVariant;
if (tag.slice(0, 2) === "!!") {
tag = `tag:yaml.org,2002:${tag.slice(2)}`;
}
type = schema.compiledTypeMap.fallback[tag];
type = schema.compiledTypeMap.fallback[tag]!;

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

View check run for this annotation

Codecov / codecov/patch

yaml/_dumper/dumper_state.ts#L29

Added line #L29 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 @@ -68,7 +68,7 @@

let delta = null;
if (match[9]) {
const tzHour = +match[10];
const tzHour = +match[10]!;

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

View check run for this annotation

Codecov / codecov/patch

yaml/_type/timestamp.ts#L71

Added line #L71 was not covered by tests
gabelluardo marked this conversation as resolved.
Show resolved Hide resolved
const tzMinute = +(match[11] || 0);
delta = (tzHour * 60 + tzMinute) * 60000; // delta in milli-seconds
if (match[9] === "-") delta = -delta;
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