Skip to content

Commit

Permalink
Fixed usage of any
Browse files Browse the repository at this point in the history
  • Loading branch information
KyrylR committed Nov 21, 2024
1 parent c91d416 commit a1d4371
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
28 changes: 21 additions & 7 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { SimpleIdentifierListContext } from "../generated";
import { VariableContext, VariableContextWithNull } from "../types";
import {
CircomValueType,
VariableContext,
VariableContextWithNull,
} from "../types";

export function parseSimpleIdentifierList(
ctx: SimpleIdentifierListContext,
Expand All @@ -19,7 +23,7 @@ export function parseSimpleIdentifierList(

export function buildVariableContext(
names: string[],
values: any[],
values: CircomValueType[],
): VariableContext {
if (names.length !== values.length) {
throw new Error("Names and values must have the same length");
Expand All @@ -44,7 +48,7 @@ export function buildVariableContext(
return context;
}

export function getArrayDimensions(value: any): number[] {
export function getArrayDimensions(value: CircomValueType): number[] {
if (Array.isArray(value)) {
return [value.length, ...getArrayDimensions(value[0])];
}
Expand All @@ -55,7 +59,7 @@ export function getArrayDimensions(value: any): number[] {
export function bindVariableContext(
variableName: string,
dimensions: number[],
values: any,
values: CircomValueType,
): VariableContextWithNull {
const context: VariableContextWithNull = {};
const resolved = resolveDimensions(variableName, dimensions);
Expand All @@ -82,7 +86,10 @@ export function resolveDimensions(
}

// reference MUST be similar to [0][1]
function parseVariable(value: any, reference: string): bigint {
function parseVariable(
value: CircomValueType,
reference: string,
): CircomValueType {
const parts = reference
.split("[")
.map((part) => part.replace("]", ""))
Expand All @@ -92,9 +99,16 @@ function parseVariable(value: any, reference: string): bigint {
return getReferenceValueInternal(value, parts);
}

function getReferenceValueInternal(value: any, reference: number[]): bigint {
function getReferenceValueInternal(
value: CircomValueType,
reference: number[],
): CircomValueType {
if (reference.length === 0) {
return BigInt(value);
return value;
}

if (!Array.isArray(value)) {
throw new Error("INTERNAL ERROR! Reference is invalid");
}

return getReferenceValueInternal(value[reference[0]], reference.slice(1));
Expand Down
10 changes: 5 additions & 5 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { bindVariableContext } from "../src";
describe("utils", () => {
describe("resolve dimensions", () => {
it("should resolve the case: [1, 2], when value: [[3, 5]]", () => {
expect(bindVariableContext("a", [1, 2], [[3, 5]])).to.deep.equal({
expect(bindVariableContext("a", [1, 2], [[3n, 5n]])).to.deep.equal({
"a[0][0]": 3n,
"a[0][1]": 5n,
});
Expand All @@ -19,13 +19,13 @@ describe("utils", () => {
});

it("should resolve the case: [], when value: 1", () => {
expect(bindVariableContext("a", [], 1)).to.deep.equal({
expect(bindVariableContext("a", [], 1n)).to.deep.equal({
a: 1n,
});
});

it("should resolve the case: [1], when value: 1", () => {
expect(bindVariableContext("a", [1], 1)).to.deep.equal({
expect(bindVariableContext("a", [1], 1n)).to.deep.equal({
"a[0]": null,
});
});
Expand All @@ -36,8 +36,8 @@ describe("utils", () => {
"a",
[2, 2],
[
[1, 2],
[3, 4],
[1n, 2n],
[3n, 4n],
],
),
).to.deep.equal({
Expand Down

0 comments on commit a1d4371

Please sign in to comment.