Skip to content

Commit 7e359e2

Browse files
authored
chore: undo and prevent circular imports (#1748)
* chore: undo and prevent circular imports * fix tests
1 parent 8a2a912 commit 7e359e2

20 files changed

+140
-123
lines changed

.changeset/happy-weeks-ring.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@smithy/shared-ini-file-loader": patch
3+
"@smithy/util-endpoints": patch
4+
"@smithy/util-stream": patch
5+
---
6+
7+
remove and ban circular imports
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* @internal
3+
*/
4+
export const CONFIG_PREFIX_SEPARATOR = ".";

packages/shared-ini-file-loader/src/getConfigData.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ParsedIniData } from "@smithy/types";
22
import { IniSectionType } from "@smithy/types";
33

4-
import { CONFIG_PREFIX_SEPARATOR } from "./loadSharedConfigFiles";
4+
import { CONFIG_PREFIX_SEPARATOR } from "./constants";
55

66
/**
77
* Returns the config data from parsed ini data.

packages/shared-ini-file-loader/src/loadSharedConfigFiles.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ export interface SharedConfigInit {
4040

4141
const swallowError = () => ({});
4242

43-
/**
44-
* @internal
45-
*/
46-
export const CONFIG_PREFIX_SEPARATOR = ".";
43+
export { CONFIG_PREFIX_SEPARATOR } from "./constants";
4744

4845
/**
4946
* Loads the config and credentials files.

packages/shared-ini-file-loader/src/parseIni.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ParsedIniData } from "@smithy/types";
22
import { IniSectionType } from "@smithy/types";
33

4-
import { CONFIG_PREFIX_SEPARATOR } from "./loadSharedConfigFiles";
4+
import { CONFIG_PREFIX_SEPARATOR } from "./constants";
55

66
const prefixKeyRegex = /^([\w-]+)\s(["'])?([\w-@\+\.%:/]+)\2$/;
77
const profileNameBlockList = ["__proto__", "profile __proto__"];

packages/util-endpoints/src/utils/callFunction.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { afterEach, beforeEach, describe, expect, test as it, vi } from "vitest";
22

3-
import { callFunction } from "./callFunction";
43
import { customEndpointFunctions } from "./customEndpointFunctions";
54
import { endpointFunctions } from "./endpointFunctions";
6-
import { evaluateExpression } from "./evaluateExpression";
7-
8-
vi.mock("./evaluateExpression");
5+
import { callFunction, group } from "./evaluateExpression";
96

107
describe(callFunction.name, () => {
8+
vi.spyOn(group, "evaluateExpression").mockImplementation(vi.fn());
9+
const { evaluateExpression } = group;
10+
1111
const mockOptions = {
1212
endpointParams: {},
1313
referenceRecord: {},
Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,2 @@
1-
import type { EvaluateOptions, Expression, FunctionObject, FunctionReturn } from "../types";
2-
import { customEndpointFunctions } from "./customEndpointFunctions";
3-
import { endpointFunctions } from "./endpointFunctions";
4-
import { evaluateExpression } from "./evaluateExpression";
5-
6-
export const callFunction = ({ fn, argv }: FunctionObject, options: EvaluateOptions): FunctionReturn => {
7-
const evaluatedArgs = argv.map((arg) =>
8-
["boolean", "number"].includes(typeof arg) ? arg : evaluateExpression(arg as Expression, "arg", options)
9-
);
10-
const fnSegments = fn.split(".");
11-
if (fnSegments[0] in customEndpointFunctions && fnSegments[1] != null) {
12-
// @ts-ignore Element implicitly has an 'any' type
13-
return customEndpointFunctions[fnSegments[0]][fnSegments[1]](...evaluatedArgs);
14-
}
15-
// @ts-ignore Element implicitly has an 'any' type
16-
return endpointFunctions[fn](...evaluatedArgs);
17-
};
1+
// breaks circular import
2+
export { callFunction } from "./evaluateExpression";

packages/util-endpoints/src/utils/evaluateExpression.spec.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { afterEach, describe, expect, test as it, vi } from "vitest";
22

33
import { EndpointError } from "../types";
4-
import { callFunction } from "./callFunction";
5-
import { evaluateExpression } from "./evaluateExpression";
4+
import { evaluateExpression, group } from "./evaluateExpression";
65
import { evaluateTemplate } from "./evaluateTemplate";
76
import { getReferenceValue } from "./getReferenceValue";
87

9-
vi.mock("./callFunction");
108
vi.mock("./getReferenceValue");
119
vi.mock("./evaluateTemplate");
1210

1311
describe(evaluateExpression.name, () => {
12+
vi.spyOn(group, "callFunction").mockImplementation(vi.fn());
13+
const { callFunction } = group;
14+
1415
const mockOptions = {
1516
endpointParams: {},
1617
referenceRecord: {},
Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,35 @@
1-
import type { EvaluateOptions, Expression, FunctionObject, ReferenceObject } from "../types";
1+
import type { EvaluateOptions, Expression, FunctionObject, FunctionReturn, ReferenceObject } from "../types";
22
import { EndpointError } from "../types";
3-
import { callFunction } from "./callFunction";
3+
import { customEndpointFunctions } from "./customEndpointFunctions";
4+
import { endpointFunctions } from "./endpointFunctions";
45
import { evaluateTemplate } from "./evaluateTemplate";
56
import { getReferenceValue } from "./getReferenceValue";
67

78
export const evaluateExpression = (obj: Expression, keyName: string, options: EvaluateOptions) => {
89
if (typeof obj === "string") {
910
return evaluateTemplate(obj, options);
1011
} else if ((obj as FunctionObject)["fn"]) {
11-
return callFunction(obj as FunctionObject, options);
12+
return group.callFunction(obj as FunctionObject, options);
1213
} else if ((obj as ReferenceObject)["ref"]) {
1314
return getReferenceValue(obj as ReferenceObject, options);
1415
}
1516
throw new EndpointError(`'${keyName}': ${String(obj)} is not a string, function or reference.`);
1617
};
18+
19+
export const callFunction = ({ fn, argv }: FunctionObject, options: EvaluateOptions): FunctionReturn => {
20+
const evaluatedArgs = argv.map((arg) =>
21+
["boolean", "number"].includes(typeof arg) ? arg : group.evaluateExpression(arg as Expression, "arg", options)
22+
);
23+
const fnSegments = fn.split(".");
24+
if (fnSegments[0] in customEndpointFunctions && fnSegments[1] != null) {
25+
// @ts-ignore Element implicitly has an 'any' type
26+
return customEndpointFunctions[fnSegments[0]][fnSegments[1]](...evaluatedArgs);
27+
}
28+
// @ts-ignore Element implicitly has an 'any' type
29+
return endpointFunctions[fn](...evaluatedArgs);
30+
};
31+
32+
export const group = {
33+
evaluateExpression,
34+
callFunction,
35+
};

packages/util-endpoints/src/utils/evaluateRules.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ import type { EndpointRuleObject, ErrorRuleObject, TreeRuleObject } from "../typ
44
import { EndpointError } from "../types";
55
import { evaluateEndpointRule } from "./evaluateEndpointRule";
66
import { evaluateErrorRule } from "./evaluateErrorRule";
7-
import { evaluateRules } from "./evaluateRules";
8-
import { evaluateTreeRule } from "./evaluateTreeRule";
7+
import { evaluateRules, group } from "./evaluateRules";
98

109
vi.mock("./evaluateEndpointRule");
1110
vi.mock("./evaluateErrorRule");
1211
vi.mock("./evaluateTreeRule");
1312

1413
describe(evaluateRules.name, () => {
14+
vi.spyOn(group, "evaluateTreeRule").mockImplementation(vi.fn());
15+
const { evaluateTreeRule } = group;
16+
1517
const mockOptions = {
1618
endpointParams: {},
1719
referenceRecord: {},

0 commit comments

Comments
 (0)