From 93c8ba61d153dc713c4c6b1c74d12d71a14fc286 Mon Sep 17 00:00:00 2001 From: Gabriele Belluardo Date: Sat, 9 Mar 2024 01:51:18 +0100 Subject: [PATCH 1/4] refactor(yaml): prepare for `noUncheckedIndexedAccess` --- yaml/_dumper/dumper.ts | 31 ++++++++++++------------------- yaml/_dumper/dumper_state.ts | 4 ++-- yaml/_loader/loader.ts | 20 +++++++++----------- yaml/_type/binary.ts | 2 +- yaml/_type/float.ts | 2 +- yaml/_type/pairs.ts | 6 ++---- yaml/_type/timestamp.ts | 12 ++++++------ yaml/schema.ts | 7 +------ 8 files changed, 34 insertions(+), 50 deletions(-) diff --git a/yaml/_dumper/dumper.ts b/yaml/_dumper/dumper.ts index c1338a75f6a6..5ead73f96334 100644 --- a/yaml/_dumper/dumper.ts +++ b/yaml/_dumper/dumper.ts @@ -135,7 +135,7 @@ function testImplicitResolving(state: DumperState, str: string): boolean { index < length; index += 1 ) { - type = state.implicitTypes[index]; + type = state.implicitTypes[index]!; if (type.resolve(str)) { return true; @@ -379,7 +379,7 @@ function foldString(string: string, width: number): string { // tslint:disable-next-line:no-conditional-assignment while ((match = lineRe.exec(string))) { const prefix = match[1], - line = match[2]; + line = match[2]!; moreIndented = line[0] === " "; result += prefix + (!prevMoreIndented && !moreIndented && line !== "" ? "\n" : "") + @@ -582,7 +582,7 @@ function writeFlowMapping( if (index !== 0) pairBuffer += ", "; - objectKey = objectKeyList[index]; + objectKey = objectKeyList[index]!; objectValue = object[objectKey]; if (!writeNode(state, level, objectKey, false, false)) { @@ -646,7 +646,7 @@ function writeBlockMapping( pairBuffer += generateNextLine(state, level); } - objectKey = objectKeyList[index]; + objectKey = objectKeyList[index]!; objectValue = object[objectKey]; if (!writeNode(state, level + 1, objectKey, true, true, true)) { @@ -701,7 +701,7 @@ function detectType( let style: StyleVariant; let _result: string; for (let index = 0, length = typeList.length; index < length; index += 1) { - type = typeList[index]; + type = typeList[index]!; if ( (type.instanceOf || type.predicate) && @@ -712,12 +712,12 @@ function detectType( 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)[style]( + _result = (type.represent as ArrayObject)[style]!( object, style, ); @@ -845,14 +845,8 @@ function inspectNode( 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); } } } @@ -868,11 +862,10 @@ function getDuplicateReferences( 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]); } - state.usedDuplicates = Array.from({ length }); + state.usedDuplicates = Array.from({ length: duplicatesIndexes.length }); } export function dump(input: Any, options?: DumperStateOptions): string { diff --git a/yaml/_dumper/dumper_state.ts b/yaml/_dumper/dumper_state.ts index f0a1aac75bf2..1f37e1e6e7c5 100644 --- a/yaml/_dumper/dumper_state.ts +++ b/yaml/_dumper/dumper_state.ts @@ -21,12 +21,12 @@ function compileStyleMap( 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]!; 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]!; if ( type && diff --git a/yaml/_loader/loader.ts b/yaml/_loader/loader.ts index 1f78c8ad7fbf..eb7058f98c79 100644 --- a/yaml/_loader/loader.ts +++ b/yaml/_loader/loader.ts @@ -202,13 +202,13 @@ const directiveHandlers: DirectiveHandlers = { 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]!); 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); if (major !== 1) { return throwError(state, "unacceptable YAML version of the document"); } @@ -225,8 +225,8 @@ const directiveHandlers: DirectiveHandlers = { 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]!; if (!PATTERN_TAG_HANDLE.test(handle)) { return throwError( @@ -300,9 +300,7 @@ function mergeMappings( ); } - 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)) { if (!hasOwn(destination, key)) { Object.defineProperty(destination, key, { value: source[key], @@ -1562,7 +1560,7 @@ function composeNode( 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 @@ -1581,7 +1579,7 @@ function composeNode( } 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( @@ -1679,7 +1677,7 @@ function readDocument(state: LoaderState) { if (ch !== 0) readLineBreak(state); if (hasOwn(directiveHandlers, directiveName)) { - directiveHandlers[directiveName](state, directiveName, ...directiveArgs); + directiveHandlers[directiveName]!(state, directiveName, ...directiveArgs); } else { throwWarning(state, `unknown document directive "${directiveName}"`); } diff --git a/yaml/_type/binary.ts b/yaml/_type/binary.ts index 589b71070192..bbdc3d1b33b2 100644 --- a/yaml/_type/binary.ts +++ b/yaml/_type/binary.ts @@ -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 diff --git a/yaml/_type/float.ts b/yaml/_type/float.ts index bf82cc9a12e7..9c93781cef8a 100644 --- a/yaml/_type/float.ts +++ b/yaml/_type/float.ts @@ -38,7 +38,7 @@ function constructYamlFloat(data: string): number { const sign = value[0] === "-" ? -1 : 1; const digits: number[] = []; - if ("+-".indexOf(value[0]) >= 0) { + if (value[0] && "+-".indexOf(value[0]) >= 0) { value = value.slice(1); } diff --git a/yaml/_type/pairs.ts b/yaml/_type/pairs.ts index 342e5b23dc76..62942f8e7fae 100644 --- a/yaml/_type/pairs.ts +++ b/yaml/_type/pairs.ts @@ -11,9 +11,7 @@ const _toString = Object.prototype.toString; 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()) { if (_toString.call(pair) !== "[object Object]") return false; const keys = Object.keys(pair); @@ -32,7 +30,7 @@ function constructYamlPairs(data: string): Any[] { const result = Array.from({ length: data.length }); for (let index = 0; index < data.length; index += 1) { - const pair = data[index]; + const pair = data[index]!; const keys = Object.keys(pair); diff --git a/yaml/_type/timestamp.ts b/yaml/_type/timestamp.ts index 0047bb0ab609..660c76614701 100644 --- a/yaml/_type/timestamp.ts +++ b/yaml/_type/timestamp.ts @@ -39,9 +39,9 @@ function constructYamlTimestamp(data: string): Date { // 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]!; if (!match[4]) { // no hour @@ -51,8 +51,8 @@ function constructYamlTimestamp(data: string): Date { // 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]!; let fraction = 0; if (match[7]) { @@ -68,7 +68,7 @@ function constructYamlTimestamp(data: string): Date { let delta = null; if (match[9]) { - const tzHour = +match[10]; + const tzHour = +match[10]!; const tzMinute = +(match[11] || 0); delta = (tzHour * 60 + tzMinute) * 60000; // delta in milli-seconds if (match[9] === "-") delta = -delta; diff --git a/yaml/schema.ts b/yaml/schema.ts index 90701ecbd69a..072870473ee5 100644 --- a/yaml/schema.ts +++ b/yaml/schema.ts @@ -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 From 5559afa85f1caedf7d4e1bd7004fb168a0769d6f Mon Sep 17 00:00:00 2001 From: Gabriele Belluardo Date: Mon, 11 Mar 2024 02:06:47 +0100 Subject: [PATCH 2/4] fix: requested changes --- yaml/_dumper/dumper.ts | 57 ++++++++---------------------------- yaml/_dumper/dumper_state.ts | 10 ++----- yaml/_type/timestamp.ts | 4 +-- 3 files changed, 18 insertions(+), 53 deletions(-) diff --git a/yaml/_dumper/dumper.ts b/yaml/_dumper/dumper.ts index 5ead73f96334..a46c75c87abd 100644 --- a/yaml/_dumper/dumper.ts +++ b/yaml/_dumper/dumper.ts @@ -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"; @@ -129,20 +129,7 @@ function generateNextLine(state: DumperState, level: number): string { } 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 @@ -379,7 +366,7 @@ function foldString(string: string, width: number): string { // tslint:disable-next-line:no-conditional-assignment while ((match = lineRe.exec(string))) { const prefix = match[1], - line = match[2]!; + line = match[2] || ""; moreIndented = line[0] === " "; result += prefix + (!prevMoreIndented && !moreIndented && line !== "" ? "\n" : "") + @@ -572,18 +559,12 @@ function writeFlowMapping( 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 ? '"' : ""; if (index !== 0) pairBuffer += ", "; - objectKey = objectKeyList[index]!; - objectValue = object[objectKey]; + const objectValue = object[objectKey]; if (!writeNode(state, level, objectKey, false, false)) { continue; // Skip this pair because of invalid key; @@ -631,29 +612,20 @@ function writeBlockMapping( 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) { @@ -697,11 +669,8 @@ function detectType( ): 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) && @@ -712,7 +681,7 @@ function detectType( 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); diff --git a/yaml/_dumper/dumper_state.ts b/yaml/_dumper/dumper_state.ts index 1f37e1e6e7c5..ee35743e396a 100644 --- a/yaml/_dumper/dumper_state.ts +++ b/yaml/_dumper/dumper_state.ts @@ -16,17 +16,13 @@ function compileStyleMap( ): ArrayObject { if (typeof map === "undefined" || map === null) return {}; - let type: Type; const result: ArrayObject = {}; - 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; if (tag.slice(0, 2) === "!!") { tag = `tag:yaml.org,2002:${tag.slice(2)}`; } - type = schema.compiledTypeMap.fallback[tag]!; + const type = schema.compiledTypeMap.fallback[tag]; if ( type && diff --git a/yaml/_type/timestamp.ts b/yaml/_type/timestamp.ts index 660c76614701..d268a1ecb687 100644 --- a/yaml/_type/timestamp.ts +++ b/yaml/_type/timestamp.ts @@ -67,8 +67,8 @@ function constructYamlTimestamp(data: string): Date { // match: [8] tz [9] tz_sign [10] tz_hour [11] tz_minute let delta = null; - if (match[9]) { - const tzHour = +match[10]!; + if (match[9] && match[10]) { + const tzHour = +match[10]; const tzMinute = +(match[11] || 0); delta = (tzHour * 60 + tzMinute) * 60000; // delta in milli-seconds if (match[9] === "-") delta = -delta; From ec9bd21e108e5093e2d4b0e0fe5f415ef597b0da Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Mon, 11 Mar 2024 17:03:30 +1100 Subject: [PATCH 3/4] Update yaml/_dumper/dumper.ts --- yaml/_dumper/dumper.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/yaml/_dumper/dumper.ts b/yaml/_dumper/dumper.ts index a46c75c87abd..23398496a6c7 100644 --- a/yaml/_dumper/dumper.ts +++ b/yaml/_dumper/dumper.ts @@ -814,8 +814,8 @@ function inspectNode( inspectNode(object[idx], objects, duplicatesIndexes); } } else { - for (const objectKey of Object.keys(object)) { - inspectNode(object[objectKey], objects, duplicatesIndexes); + for (const object of Object.values(object)) { + inspectNode(object, objects, duplicatesIndexes); } } } From 509231c9d96085ad1be13ea7298f2f977a56506c Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Mon, 11 Mar 2024 17:06:46 +1100 Subject: [PATCH 4/4] Revert "Update yaml/_dumper/dumper.ts" This reverts commit ec9bd21e108e5093e2d4b0e0fe5f415ef597b0da. --- yaml/_dumper/dumper.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/yaml/_dumper/dumper.ts b/yaml/_dumper/dumper.ts index 23398496a6c7..a46c75c87abd 100644 --- a/yaml/_dumper/dumper.ts +++ b/yaml/_dumper/dumper.ts @@ -814,8 +814,8 @@ function inspectNode( inspectNode(object[idx], objects, duplicatesIndexes); } } else { - for (const object of Object.values(object)) { - inspectNode(object, objects, duplicatesIndexes); + for (const objectKey of Object.keys(object)) { + inspectNode(object[objectKey], objects, duplicatesIndexes); } } }