Skip to content

Commit

Permalink
Sort imports
Browse files Browse the repository at this point in the history
  • Loading branch information
ericglau committed Nov 29, 2023
1 parent c890dbe commit 5c6815f
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions packages/core-cairo/src/print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,34 @@ function withSemicolons(lines: string[]): string[] {
}

function printImports(contract: Contract) {
const lines = [];
for (const component of contract.components) {
lines.push(`use ${component.path}::${component.name}`);
}
for (const standaloneImport of contract.standaloneImports) {
lines.push(`use ${standaloneImport}`);
const { ozImports, otherImports, superImports } = sortImports(contract);

const lines: string[] = [];
ozImports.forEach(i => lines.push(`use ${i}`));
otherImports.forEach(i => lines.push(`use ${i}`));
superImports.forEach(i => lines.push(`use ${i}`));
return withSemicolons(lines);
}

function sortImports(contract: Contract) {
const componentImports = contract.components.flatMap(c => `${c.path}::${c.name}`);
const combined = componentImports.concat(contract.standaloneImports);

const ozImports = [];
const otherImports = [];
const superImports = [];

for (const importStatement of combined) {
if (importStatement.startsWith('openzeppelin')) {
ozImports.push(importStatement);
} else {
otherImports.push(importStatement);
}
}
if (contract.superVariables.length > 0) {
lines.push(`use super::{${contract.superVariables.map(v => v.name).join(', ')}}`);
superImports.push(`super::{${contract.superVariables.map(v => v.name).join(', ')}}`);
}
return withSemicolons(lines);
return { ozImports, otherImports, superImports };
}

function printComponentDeclarations(contract: Contract) {
Expand Down

0 comments on commit 5c6815f

Please sign in to comment.