Skip to content

Commit

Permalink
fix broken version
Browse files Browse the repository at this point in the history
  • Loading branch information
matepek committed Jul 30, 2022
1 parent ba4c53d commit 5469f4a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 83 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
150 changes: 68 additions & 82 deletions src/util/ResolveRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand All @@ -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);
Expand All @@ -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<string, unknown>)[prop], value, mapperFunc);
if (res !== undefined) newValue[prop] = res;
}
return newValue;
Expand All @@ -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<any> /*eslint-disable-line*/,
): Promise<any> /*eslint-disable-line*/ {
value: unknown,
parent: unknown,
mapperFunc: (s: string, parent: unknown) => Promise<unknown>,
): Promise<unknown> {
if (value === null) return null;
switch (typeof value) {
case 'bigint':
Expand All @@ -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);
Expand Down Expand Up @@ -160,72 +143,75 @@ export type ResolveRuleAsync<R = any> =
| ResolveRegexRuleAsync;

// eslint-disable-next-line
export function resolveVariablesAsync<T>(value: T, varValue: readonly ResolveRuleAsync<any>[]): Promise<T> {
return _mapAllStringsAsync(
value,
undefined,
// eslint-disable-next-line
async (s: string, parent: any): Promise<any> => {
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<any>)(); // 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<T>(value: T, varValue: readonly ResolveRuleAsync<any>[]): Promise<T> {
const mapper = async (s: string, parent: unknown): Promise<unknown> => {
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<unknown>)();
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<any>)(); // 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<string>;
// 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<string>)();
s = replaceAllString(s, resolve, ruleV);
}
}
} else {
const ruleF = rule as (m: RegExpMatchArray) => Promise<string>;
// 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<string, string | undefined> =
Expand Down Expand Up @@ -260,7 +246,7 @@ export function resolveOSEnvironmentVariables<T>(value: T, strictAllowed: boolea

s = s.substring(match.index! + match[0].length);
}
});
}) as T;
}

export const PythonIndexerRegexStr = '(?:\\[(?:(-?[0-9]+)|(-?[0-9]+)?:(-?[0-9]+)?)\\])';
Expand Down

0 comments on commit 5469f4a

Please sign in to comment.