Skip to content

Commit

Permalink
build: organize imports in ts and spec files with prettier (#21)
Browse files Browse the repository at this point in the history
# Motivation

To ease the review of PR and to improve our DX, automatically organize and sort the `import`.

# Changes

- add [prettier-plugin-organize-imports](https://github.com/simonhaenisch/prettier-plugin-organize-imports)

# Note

We use the plugin in [nns-dapp](dfinity/nns-dapp#404) too.
  • Loading branch information
peterpeterparker authored Feb 9, 2022
1 parent 1faff43 commit be3d658
Show file tree
Hide file tree
Showing 19 changed files with 5,407 additions and 7,670 deletions.
2 changes: 1 addition & 1 deletion candid/governance.idl.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { IDL } from "@dfinity/candid";
import { _SERVICE } from "./governanceTypes";
export const idlFactory: IDL.InterfaceFactory;

import { _SERVICE } from "./governanceTypes";
export { _SERVICE as GovernanceService };
7 changes: 3 additions & 4 deletions esbuild.mjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#!/usr/bin/env node

import esbuild from "esbuild";
import {
existsSync,
mkdirSync,
readdirSync,
statSync,
writeFileSync,
existsSync,
mkdirSync,
} from "fs";
import { join } from "path";

import esbuild from "esbuild";

const dist = join(process.cwd(), "dist");

if (!existsSync(dist)) {
Expand Down
12,911 changes: 5,331 additions & 7,580 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
"@dfinity/agent": "^0.10.2",
"@dfinity/candid": "^0.10.2",
"@dfinity/principal": "^0.10.2",
"crc-32": "^1.2.0",
"crc": "^3.8.0",
"crc-32": "^1.2.0",
"google-protobuf": "^3.19.1",
"js-sha256": "^0.9.0"
},
Expand All @@ -45,6 +45,7 @@
"jest": "^27.3.1",
"jest-mock-extended": "^2.0.4",
"prettier": "^2.5.0",
"prettier-plugin-organize-imports": "^2.3.4",
"text-encoding": "^0.7.0",
"ts-jest": "^27.0.7",
"ts-protoc-gen": "^0.15.0",
Expand Down
2 changes: 1 addition & 1 deletion scripts/rmdir.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

import { rmSync, existsSync } from "fs";
import { existsSync, rmSync } from "fs";
import { join } from "path";

const rmDir = () => {
Expand Down
2 changes: 1 addition & 1 deletion src/account_identifier.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Principal } from "@dfinity/principal";
import { expect, describe, it } from "@jest/globals";
import { describe, expect, it } from "@jest/globals";
import { AccountIdentifier } from ".";
import { SubAccount } from "./account_identifier";

Expand Down
4 changes: 2 additions & 2 deletions src/account_identifier.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AccountIdentifier as AccountIdentifierPb } from "../proto/ledger_pb";
import { sha224 } from "js-sha256";
import { Principal } from "@dfinity/principal";
import { sha224 } from "js-sha256";
import { AccountIdentifier as AccountIdentifierPb } from "../proto/ledger_pb";
import {
asciiStringToByteArray,
calculateCrc32,
Expand Down
2 changes: 1 addition & 1 deletion src/canisters/governance/payloads.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Option } from "../../types/option";
import { IDL, JsonObject } from "@dfinity/candid";
import { Buffer } from "buffer";
import {
Expand All @@ -19,6 +18,7 @@ import {
UpdateSubnetReplicaVersionPayload,
UpdateUnassignedNodesConfigPayload,
} from "../../../candid/payloads.idl";
import { Option } from "../../types/option";

export const getNnsFunctionName = (nnsFunction: number): string => {
switch (nnsFunction) {
Expand Down
46 changes: 18 additions & 28 deletions src/canisters/governance/request.converters.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,45 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { Principal } from "@dfinity/principal";
import {
accountIdentifierToBytes,
arrayBufferToArrayOfNumber,
} from "../../utils/converter.utils";
AccountIdentifier as RawAccountIdentifier,
Action as RawAction,
Amount,
By as RawBy,
Change as RawChange,
Command as RawCommand,
Followees as RawFollowees,
ListProposalInfo,
ManageNeuron as RawManageNeuron,
NeuronId as RawNeuronId,
NeuronIdOrSubaccount as RawNeuronIdOrSubaccount,
NodeProvider as RawNodeProvider,
Operation as RawOperation,
RewardMode as RawRewardMode,
} from "../../../candid/governanceTypes.d";
import { AccountIdentifier, E8s, NeuronId } from "../../types/common";
import {
Action,
AddHotKeyRequest,
By,
Change,
ClaimNeuronRequest,
ClaimOrRefreshNeuronRequest,
Command,
DisburseRequest,
DisburseToNeuronRequest,
FollowRequest,
IncreaseDissolveDelayRequest,
ListProposalsRequest,
MakeProposalRequest,
ManageNeuron,
MergeMaturityRequest,
NeuronIdOrSubaccount,
NodeProvider,
Operation,
ProposalId,
RegisterVoteRequest,
RemoveHotKeyRequest,
RewardMode,
SpawnRequest,
SplitRequest,
StartDissolvingRequest,
StopDissolvingRequest,
UnsupportedValueError,
} from "../../types/governance_converters";
import {
AccountIdentifier as RawAccountIdentifier,
Action as RawAction,
Amount,
By as RawBy,
Change as RawChange,
Command as RawCommand,
Followees as RawFollowees,
ListProposalInfo,
ManageNeuron as RawManageNeuron,
NeuronId as RawNeuronId,
NeuronIdOrSubaccount as RawNeuronIdOrSubaccount,
NodeProvider as RawNodeProvider,
Operation as RawOperation,
RewardMode as RawRewardMode,
} from "../../../candid/governanceTypes.d";
accountIdentifierToBytes,
arrayBufferToArrayOfNumber,
} from "../../utils/converter.utils";
// Protobuf is not supported yet.
// import { AccountIdentifier as PbAccountIdentifier } from "../../proto/ledger_pb";
// import { ManageNeuron as PbManageNeuron } from "../../proto/governance_pb";
Expand Down
65 changes: 31 additions & 34 deletions src/canisters/governance/response.converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,31 @@

import { Principal } from "@dfinity/principal";
import {
accountIdentifierFromBytes,
arrayOfNumberToArrayBuffer,
arrayOfNumberToUint8Array,
principalToAccountIdentifier,
} from "../../utils/converter.utils";
AccountIdentifier as RawAccountIdentifier,
Action as RawAction,
Amount as RawAmount,
Ballot as RawBallot,
BallotInfo as RawBallotInfo,
By as RawBy,
Change as RawChange,
Command as RawCommand,
DissolveState as RawDissolveState,
Followees as RawFollowees,
KnownNeuron as RawKnownNeuron,
ListNeuronsResponse as RawListNeuronsResponse,
ListProposalInfoResponse as RawListProposalInfoResponse,
ManageNeuronResponse as RawManageNeuronResponse,
Neuron as RawNeuron,
NeuronId as RawNeuronId,
NeuronIdOrSubaccount as RawNeuronIdOrSubaccount,
NeuronInfo as RawNeuronInfo,
NodeProvider as RawNodeProvider,
Operation as RawOperation,
Proposal as RawProposal,
ProposalInfo as RawProposalInfo,
RewardMode as RawRewardMode,
Tally as RawTally,
} from "../../../candid/governanceTypes.d";
import { GOVERNANCE_CANISTER_ID } from "../../constants/canister_ids";
import { AccountIdentifier, E8s, NeuronId } from "../../types/common";
import {
Expand All @@ -18,14 +38,12 @@ import {
By,
Change,
Command,
DisburseResponse,
DisburseToNeuronResponse,
DissolveState,
Followees,
KnownNeuron,
ListProposalsResponse,
MakeProposalResponse,
MergeMaturityResponse,
Neuron,
NeuronIdOrSubaccount,
NeuronInfo,
Expand All @@ -34,37 +52,16 @@ import {
Proposal,
ProposalInfo,
RewardMode,
SpawnResponse,
Tally,
UnsupportedValueError,
} from "../../types/governance_converters";
import {
AccountIdentifier as RawAccountIdentifier,
Action as RawAction,
Amount as RawAmount,
Ballot as RawBallot,
BallotInfo as RawBallotInfo,
By as RawBy,
Change as RawChange,
Command as RawCommand,
DissolveState as RawDissolveState,
Followees as RawFollowees,
KnownNeuron as RawKnownNeuron,
ListNeuronsResponse as RawListNeuronsResponse,
ListProposalInfoResponse as RawListProposalInfoResponse,
ManageNeuronResponse as RawManageNeuronResponse,
Neuron as RawNeuron,
NeuronId as RawNeuronId,
NeuronIdOrSubaccount as RawNeuronIdOrSubaccount,
NeuronInfo as RawNeuronInfo,
NodeProvider as RawNodeProvider,
Operation as RawOperation,
Proposal as RawProposal,
ProposalInfo as RawProposalInfo,
RewardMode as RawRewardMode,
Tally as RawTally,
} from "../../../candid/governanceTypes.d";
import { Option } from "../../types/option";
import {
accountIdentifierFromBytes,
arrayOfNumberToArrayBuffer,
arrayOfNumberToUint8Array,
principalToAccountIdentifier,
} from "../../utils/converter.utils";
// Protobuf is not supported yet:
// import { ManageNeuronResponse as PbManageNeuronResponse } from "../../proto/governance_pb";
import { convertNnsFunctionPayload, getNnsFunctionName } from "./payloads";
Expand Down
2 changes: 1 addition & 1 deletion src/governance.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { mock } from "jest-mock-extended";
import { GovernanceService } from "../candid/governance.idl";
import { ListKnownNeuronsResponse } from "../candid/governanceTypes";
import { GovernanceCanister } from "./governance";
import { GovernanceService } from "../candid/governance.idl";

describe("GovernanceCanister.listKnownNeurons", () => {
it("populates all KnownNeuron fields correctly", async () => {
Expand Down
6 changes: 3 additions & 3 deletions src/governance.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Actor } from "@dfinity/agent";
import { GovernanceService, idlFactory } from "../candid/governance.idl";
import { idlFactory as certifiedIdlFactory } from "../candid/governance.certified.idl";
import { GovernanceService, idlFactory } from "../candid/governance.idl";
import { RequestConverters } from "./canisters/governance/request.converters";
import { ResponseConverters } from "./canisters/governance/response.converters";
import { MAINNET_GOVERNANCE_CANISTER_ID } from "./constants/canister_ids";
import { GovernanceCanisterOptions } from "./types/governance";
import {
KnownNeuron,
ListProposalsRequest,
ListProposalsResponse,
} from "./types/governance_converters";
import { MAINNET_GOVERNANCE_CANISTER_ID } from "./constants/canister_ids";
import { defaultAgent } from "./utils/agent.utils";
import { GovernanceCanisterOptions } from "./types/governance";

export class GovernanceCanister {
private constructor(
Expand Down
2 changes: 1 addition & 1 deletion src/icp.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, describe, it } from "@jest/globals";
import { describe, expect, it } from "@jest/globals";
import { ICP } from ".";
import { FromICPStringError } from "./types/icp";

Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export { GovernanceCanister } from "./governance";
export { LedgerCanister } from "./ledger";
export { AccountIdentifier, SubAccount } from "./account_identifier";
export { GovernanceCanister } from "./governance";
export { ICP } from "./icp";
export { LedgerCanister } from "./ledger";
export * from "./types/governance";
export * from "./types/governance_converters";
export * from "./types/icp";
Expand Down
8 changes: 4 additions & 4 deletions src/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import {
SendRequest,
} from "../proto/ledger_pb";
import { AccountIdentifier } from "./account_identifier";
import { ICP } from "./icp";
import { queryCall, updateCall } from "./utils/proto.utils";
import { MAINNET_LEDGER_CANISTER_ID } from "./constants/canister_ids";
import { defaultAgent } from "./utils/agent.utils";
import { ICP } from "./icp";
import { BlockHeight } from "./types/common";
import {
Fetcher,
InsufficientFunds,
Expand All @@ -23,7 +22,8 @@ import {
TxDuplicate,
TxTooOld,
} from "./types/ledger";
import { BlockHeight } from "./types/common";
import { defaultAgent } from "./utils/agent.utils";
import { queryCall, updateCall } from "./utils/proto.utils";

export class LedgerCanister {
private constructor(
Expand Down
4 changes: 2 additions & 2 deletions src/types/governance_converters.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { DerEncodedPublicKey } from "@dfinity/agent";
import { Option } from "./option";
import { Principal } from "@dfinity/principal";
import {
AccountIdentifier,
CanisterIdString,
E8s,
NeuronId,
PrincipalString,
} from "./common";
import { Principal } from "@dfinity/principal";
import { Option } from "./option";

export type Action =
| { RegisterKnownNeuron: KnownNeuron }
Expand Down
2 changes: 1 addition & 1 deletion src/types/ledger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ICP } from "../icp";
import { Agent } from "@dfinity/agent";
import { Principal } from "@dfinity/principal";
import { ICP } from "../icp";

export type BlockHeight = bigint;

Expand Down
2 changes: 1 addition & 1 deletion src/utils/converter.utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Principal } from "@dfinity/principal";
import { sha224 } from "js-sha256";
import { Buffer } from "buffer";
/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-ignore (no type definitions for crc are available)
import crc from "crc";
import { sha224 } from "js-sha256";
/* eslint-enable @typescript-eslint/ban-ts-comment */
import { SUB_ACCOUNT_BYTE_LENGTH } from "../constants/constants";
import { AccountIdentifier, SubAccount } from "../types/common";
Expand Down
3 changes: 1 addition & 2 deletions src/utils/proto.utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Agent } from "@dfinity/agent";
import { Agent, polling } from "@dfinity/agent";
import { Principal } from "@dfinity/principal";
import { polling } from "@dfinity/agent";

/**
* Submits an update call to the IC.
Expand Down

0 comments on commit be3d658

Please sign in to comment.