From 5469f4a374ecee393cffc6cd7899e971349781a2 Mon Sep 17 00:00:00 2001 From: Mate Pek Date: Sat, 30 Jul 2022 15:15:33 +0800 Subject: [PATCH] fix broken version --- CHANGELOG.md | 6 +- src/util/ResolveRule.ts | 150 ++++++++++++++++++---------------------- 2 files changed, 73 insertions(+), 83 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a29df41..b1fdcf74 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,9 +6,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [Unreleased] +## [4.2.5] + +Fixed broken version. + ## [4.2.4] - 2022-07-29 -I won't even mention the change here.. Let's see how it works out. +! BROKEN ! ## [4.2.3] - 2022-07-22 diff --git a/src/util/ResolveRule.ts b/src/util/ResolveRule.ts index b78bf467..e6b4ab1f 100644 --- a/src/util/ResolveRule.ts +++ b/src/util/ResolveRule.ts @@ -2,11 +2,7 @@ import * as pathlib from 'path'; /// -function _mapAllStrings( - value: any /*eslint-disable-line*/, - parent: any /*eslint-disable-line*/, - mapperFunc: (s: string, parent: any) => any /*eslint-disable-line*/, -): any /*eslint-disable-line*/ { +function _mapAllStrings(value: unknown, parent: unknown, mapperFunc: (s: string, parent: unknown) => unknown): unknown { if (value === null) return null; switch (typeof value) { case 'bigint': @@ -21,7 +17,7 @@ function _mapAllStrings( } case 'object': { if (Array.isArray(value)) { - const newValue: any[] = []; /*eslint-disable-line*/ + const newValue: unknown[] = []; for (const v of value) { const res = _mapAllStrings(v, value, mapperFunc); if (res !== undefined) newValue.push(res); @@ -31,7 +27,7 @@ function _mapAllStrings( const newValue = Object.create(Object.getPrototypeOf(value)); Object.defineProperties(newValue, Object.getOwnPropertyDescriptors(value)); for (const prop in value) { - const res = _mapAllStrings(value[prop], value, mapperFunc); + const res = _mapAllStrings((value as Record)[prop], value, mapperFunc); if (res !== undefined) newValue[prop] = res; } return newValue; @@ -43,10 +39,10 @@ function _mapAllStrings( const _flatResolved = Symbol('special value which means that the veriable was flat resolved'); async function _mapAllStringsAsync( - value: unknown /*eslint-disable-line*/, - parent: any /*eslint-disable-line*/, - mapperFunc: (s: string, parent: unknown) => Promise /*eslint-disable-line*/, -): Promise /*eslint-disable-line*/ { + value: unknown, + parent: unknown, + mapperFunc: (s: string, parent: unknown) => Promise, +): Promise { if (value === null) return null; switch (typeof value) { case 'bigint': @@ -57,24 +53,11 @@ async function _mapAllStringsAsync( case 'undefined': return value; case 'string': { - let prevMappedValue = await mapperFunc(value, parent); - /* this check is a hack. at this point we cannot assume that mapperFunc resolves variables only with '$'. - * but good enough for now. Should saves some resources. https://xkcd.com/1691/ */ - if (typeof prevMappedValue === 'string' && prevMappedValue.indexOf('$') === -1) return prevMappedValue; - - let nextMappedValue = await _mapAllStringsAsync(prevMappedValue, parent, mapperFunc); - while ( - typeof prevMappedValue === 'string' /*this limits to string but object comparison is more complicated*/ && - prevMappedValue !== (nextMappedValue = await _mapAllStringsAsync(prevMappedValue, parent, mapperFunc)) - ) { - prevMappedValue = nextMappedValue; - } - - return nextMappedValue; + return mapperFunc(value, parent); } case 'object': { if (Array.isArray(value)) { - const newValue: any[] = []; /*eslint-disable-line*/ + const newValue: unknown[] = []; for (const v of value) { const res = await _mapAllStringsAsync(v, newValue, mapperFunc); if (res !== _flatResolved) newValue.push(res); @@ -160,72 +143,75 @@ export type ResolveRuleAsync = | ResolveRegexRuleAsync; // eslint-disable-next-line -export function resolveVariablesAsync(value: T, varValue: readonly ResolveRuleAsync[]): Promise { - return _mapAllStringsAsync( - value, - undefined, - // eslint-disable-next-line - async (s: string, parent: any): Promise => { - for (let i = 0; i < varValue.length; ++i) { - const { resolve, rule, isFlat } = varValue[i]; - - if (typeof resolve == 'string') { - if (s === resolve) { - if (typeof rule == 'string') { - return rule; - } /* rule is callable */ else { - const ruleV = await (rule as () => Promise)(); // eslint-disable-line - if (isFlat) { - if (Array.isArray(parent)) { - if (Array.isArray(ruleV)) { - parent.push(...ruleV); - return _flatResolved; - } - } else if (typeof parent === 'object') { - if (typeof ruleV === 'object') { - Object.assign(parent, ruleV); - return _flatResolved; - } +export async function resolveVariablesAsync(value: T, varValue: readonly ResolveRuleAsync[]): Promise { + const mapper = async (s: string, parent: unknown): Promise => { + for (let i = 0; i < varValue.length; ++i) { + const { resolve, rule, isFlat } = varValue[i]; + + if (typeof resolve == 'string') { + if (s === resolve) { + if (typeof rule == 'string') { + return rule; + } /* rule is callable */ else { + const ruleV = await (rule as () => Promise)(); + if (isFlat) { + if (Array.isArray(parent)) { + if (Array.isArray(ruleV)) { + parent.push(...ruleV); + return _flatResolved; + } + } else if (typeof parent === 'object' && parent !== null) { + if (typeof ruleV === 'object') { + Object.assign(parent, ruleV); + return _flatResolved; } - throw Error( - `resolveVariablesAsync: coudn't flat-resolve because ${typeof parent} != ${typeof ruleV} for ${s}`, - ); - } else { - return ruleV; } - } - } else if (typeof rule == 'string') { - s = replaceAllString(s, resolve, rule); - } /* rule is callable */ else { - if (s.indexOf(resolve) != -1) { - const ruleV = await (rule as () => Promise)(); // eslint-disable-line - s = replaceAllString(s, resolve, ruleV); + throw Error( + `resolveVariablesAsync: coudn't flat-resolve because ${typeof parent} != ${typeof ruleV} for ${s}`, + ); + } else { + return ruleV; } } - } else { - const ruleF = rule as (m: RegExpMatchArray) => Promise; - // resolve as RegExp && rule as Function - // eslint-disable-next-line - if (rule.length > 1) { - throw Error('resolveVariables regex func should expect 1 argument'); + } else if (typeof rule == 'string') { + s = replaceAllString(s, resolve, rule); + } /* rule is callable */ else { + if (s.indexOf(resolve) != -1) { + const ruleV = await (rule as () => Promise)(); + s = replaceAllString(s, resolve, ruleV); } + } + } else { + const ruleF = rule as (m: RegExpMatchArray) => Promise; + // resolve as RegExp && rule as Function + // eslint-disable-next-line + if (rule.length > 1) { + throw Error('resolveVariables regex func should expect 1 argument'); + } - const match = s.match(resolve); - - if (match) { - // whole input matches - if (match.index === 0 && match[0].length === s.length) { - return ruleF(match); - } + const match = s.match(resolve); - s = await replaceAllRegExp(s, resolve, ruleF, match); + if (match) { + // whole input matches + if (match.index === 0 && match[0].length === s.length) { + return ruleF(match); } + + s = await replaceAllRegExp(s, resolve, ruleF, match); } } + } - return s; - }, - ); + return s; + }; + + let resolved = await _mapAllStringsAsync(value, undefined, mapper); + + // adding an extra level resolution + if (typeof resolved === 'string' && resolved.indexOf('$') !== -1) + resolved = await _mapAllStringsAsync(resolved, undefined, mapper); + + return resolved as T; } const _normalizedEnvCache: Record = @@ -260,7 +246,7 @@ export function resolveOSEnvironmentVariables(value: T, strictAllowed: boolea s = s.substring(match.index! + match[0].length); } - }); + }) as T; } export const PythonIndexerRegexStr = '(?:\\[(?:(-?[0-9]+)|(-?[0-9]+)?:(-?[0-9]+)?)\\])';