Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow disabling prettier #14

Merged
merged 1 commit into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const name = "MyInterface";
export const license = "UNLICENSED";
export const solidityVersion = ">=0.5.0 <0.8.0";
export const prettifyOutput = true;
27 changes: 18 additions & 9 deletions lib/solidity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,34 @@ export interface GenerateSolidityOptions {
name?: string;
solidityVersion?: string;
license?: string;
prettifyOutput?: boolean;
}

export const generateSolidity = ({
abi,
...options
name = defaults.name,
solidityVersion = defaults.solidityVersion,
license = defaults.license,
prettifyOutput = defaults.prettifyOutput,
}: GenerateSolidityOptions) => {
const generated = dispatch({
node: abi,
visitor: new SolidityGenerator({
...options,
name,
solidityVersion,
license,
declarations: collectDeclarations(abi),
}),
});

if (!prettifyOutput) {
return generated;
}

try {
return prettier.format(generated, {
plugins: ["prettier-plugin-solidity"],
// @ts-ignore
// @ts-expect-error
parser: "solidity-parse",
});
} catch (error) {
Expand All @@ -50,9 +60,8 @@ interface Context {

type Visit<N extends Node> = VisitOptions<N, Context | undefined>;

type ConstructorOptions = {declarations: Declarations} & Omit<
GenerateSolidityOptions,
"abi"
type ConstructorOptions = {declarations: Declarations} & Required<
Omit<GenerateSolidityOptions, "abi" | "prettifyOutput">
>;

class SolidityGenerator implements Visitor<string, Context | undefined> {
Expand All @@ -66,9 +75,9 @@ class SolidityGenerator implements Visitor<string, Context | undefined> {

constructor({
declarations,
name = defaults.name,
license = defaults.license,
solidityVersion = defaults.solidityVersion,
name,
license,
solidityVersion,
}: ConstructorOptions) {
this.name = name;
this.license = license;
Expand Down