Skip to content

Commit

Permalink
Improve function args format
Browse files Browse the repository at this point in the history
  • Loading branch information
ericglau committed Nov 29, 2023
1 parent 8ea9863 commit c890dbe
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions packages/core-cairo/src/print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ function printFunction(fn: ContractFunction) {
const body = spaceBetween(
withSemicolons(fn.codeBefore?.concat(fn.code) ?? fn.code),
);
return printFunction2(head, [], args, fn.kind, undefined, undefined, body);
return printFunction2(head, args, fn.kind, undefined, undefined, body);
}


Expand Down Expand Up @@ -210,7 +210,6 @@ function printConstructor(contract: Contract): Lines[] {

const constructor = printFunction2(
head,
[],
args.map(a => printArgument(a)),
modifier,
undefined,
Expand Down Expand Up @@ -317,31 +316,28 @@ export function printValue(value: Value): string {
// }

// generic for functions and constructors
// kindedName = 'func foo'
function printFunction2(kindedName: string, implicitArgs: string[], args: string[], kind: string | undefined, returns: string[] | undefined, returnLine: string | undefined, code: Lines[]): Lines[] {
// kindedName = 'fn foo'
function printFunction2(kindedName: string, args: string[], kind: string | undefined, returns: string[] | undefined, returnLine: string | undefined, code: Lines[]): Lines[] {
const fn = [];

if (kind !== undefined) {
fn.push(`#[${kind}]`);
}

let accum = kindedName;

const allArgs = implicitArgs.concat(args);
let accum = `${kindedName}(`;

if (allArgs.length > 0) {
fn.push(`${accum}(`);
let formattedArgs = allArgs.join(', ');
if (args.length > 0) {
let formattedArgs = args.join(', ');
if (formattedArgs.length > 80) {
fn.push(accum);
accum = '';
// print each arg in a separate line
fn.push(allArgs.map(arg => `${arg},`));
fn.push(args.map(arg => `${arg},`));
} else {
fn.push([formattedArgs]);
accum += `${formattedArgs}`;
}
accum = ')';
} else {
accum += '()';
}
accum += ')';

if (returns === undefined) {
accum += ' {';
Expand Down

0 comments on commit c890dbe

Please sign in to comment.