dfinity / prettier-plugin-motoko

A code formatter for the Motoko smart contract language.
Apache License 2.0
28 stars 1 forks source link

Adding a comma in a strange place that cause a compile issue #127

Closed skilesare closed 8 months ago

skilesare commented 8 months ago

ccing @jorgenbuilder as he knows a bit more about what is going on.

The linter seems to be adding a comma in my file for some strange reason. This file was generated using the candid file for the cycles_ledger. It likes to put a comma after the first item in the achived_transactions type of public type GetTransactionsResult = {.

file:

module {
  public type Account = { owner : Principal; subaccount : ?Blob };
  public type Allowance = { allowance : Nat; expires_at : ?Nat64 };
  public type AllowanceArgs = { account : Account; spender : Account };
  public type ApproveArgs = {
    fee : ?Nat;
    memo : ?Blob;
    from_subaccount : ?Blob;
    created_at_time : ?Nat64;
    amount : Nat;
    expected_allowance : ?Nat;
    expires_at : ?Nat64;
    spender : Account;
  };
  public type ApproveError = {
    #GenericError : { message : Text; error_code : Nat };
    #TemporarilyUnavailable;
    #Duplicate : { duplicate_of : Nat };
    #BadFee : { expected_fee : Nat };
    #AllowanceChanged : { current_allowance : Nat };
    #CreatedInFuture : { ledger_time : Nat64 };
    #TooOld;
    #Expired : { ledger_time : Nat64 };
    #InsufficientFunds : { balance : Nat };
  };
  public type BlockIndex = Nat;
  public type CanisterSettings = {
    freezing_threshold : ?Nat;
    controllers : ?[Principal];
    reserved_cycles_limit : ?Nat;
    memory_allocation : ?Nat;
    compute_allocation : ?Nat;
  };
  public type ChangeIndexId = { #SetTo : Principal; #Unset };
  public type CmcCreateCanisterArgs = {
    subnet_selection : ?SubnetSelection;
    settings : ?CanisterSettings;
  };
  public type CreateCanisterArgs = {
    from_subaccount : ?Blob;
    created_at_time : ?Nat64;
    amount : Nat;
    creation_args : ?CmcCreateCanisterArgs;
  };
  public type CreateCanisterError = {
    #GenericError : { message : Text; error_code : Nat };
    #TemporarilyUnavailable;
    #Duplicate : { duplicate_of : Nat; canister_id : ?Principal };
    #CreatedInFuture : { ledger_time : Nat64 };
    #FailedToCreate : {
      error : Text;
      refund_block : ?BlockIndex;
      fee_block : ?BlockIndex;
    };
    #TooOld;
    #InsufficientFunds : { balance : Nat };
  };
  public type CreateCanisterSuccess = {
    block_id : BlockIndex;
    canister_id : Principal;
  };
  public type DataCertificate = { certificate : Blob; hash_tree : Blob };
  public type DepositArgs = { to : Account; memo : ?Blob };
  public type DepositResult = { balance : Nat; block_index : BlockIndex };
  public type GetTransactionsArgs = [{ start : Nat; length : Nat }];
  public type GetTransactionsResult = {
    log_length : Nat;
    transactions : [{ id : Nat; transaction : Value }];
    archived_transactions : [
      {
        args : GetTransactionsArgs;
        callback : shared query GetTransactionsArgs -> async GetTransactionsResult;
      }
    ];
  };
  public type HttpRequest = {
    url : Text;
    method : Text;
    body : Blob;
    headers : [(Text, Text)];
  };
  public type HttpResponse = {
    body : Blob;
    headers : [(Text, Text)];
    status_code : Nat16;
  };
  public type InitArgs = {
    max_transactions_per_request : Nat64;
    index_id : ?Principal;
  };
  public type LedgerArgs = { #Upgrade : ?UpgradeArgs; #Init : InitArgs };
  public type MetadataValue = {
    #Int : Int;
    #Nat : Nat;
    #Blob : Blob;
    #Text : Text;
  };
  public type RejectionCode = {
    #NoError;
    #CanisterError;
    #SysTransient;
    #DestinationInvalid;
    #Unknown;
    #SysFatal;
    #CanisterReject;
  };
  public type SendArgs = {
    to : Principal;
    from_subaccount : ?Blob;
    created_at_time : ?Nat64;
    amount : Nat;
  };
  public type SendError = {
    #GenericError : { message : Text; error_code : Nat };
    #TemporarilyUnavailable;
    #FailedToSend : {
      rejection_code : RejectionCode;
      fee_block : ?Nat;
      rejection_reason : Text;
    };
    #Duplicate : { duplicate_of : Nat };
    #BadFee : { expected_fee : Nat };
    #InvalidReceiver : { receiver : Principal };
    #CreatedInFuture : { ledger_time : Nat64 };
    #TooOld;
    #InsufficientFunds : { balance : Nat };
  };
  public type SubnetFilter = { subnet_type : ?Text };
  public type SubnetSelection = {
    #Filter : SubnetFilter;
    #Subnet : { subnet : Principal };
  };
  public type SupportedStandard = { url : Text; name : Text };
  public type TransferArgs = {
    to : Account;
    fee : ?Nat;
    memo : ?Blob;
    from_subaccount : ?Blob;
    created_at_time : ?Nat64;
    amount : Nat;
  };
  public type TransferError = {
    #GenericError : { message : Text; error_code : Nat };
    #TemporarilyUnavailable;
    #BadBurn : { min_burn_amount : Nat };
    #Duplicate : { duplicate_of : Nat };
    #BadFee : { expected_fee : Nat };
    #CreatedInFuture : { ledger_time : Nat64 };
    #TooOld;
    #InsufficientFunds : { balance : Nat };
  };
  public type TransferFromArgs = {
    to : Account;
    fee : ?Nat;
    spender_subaccount : ?Blob;
    from : Account;
    memo : ?Blob;
    created_at_time : ?Nat64;
    amount : Nat;
  };
  public type TransferFromError = {
    #GenericError : { message : Text; error_code : Nat };
    #TemporarilyUnavailable;
    #InsufficientAllowance : { allowance : Nat };
    #BadBurn : { min_burn_amount : Nat };
    #Duplicate : { duplicate_of : Nat };
    #BadFee : { expected_fee : Nat };
    #CreatedInFuture : { ledger_time : Nat64 };
    #TooOld;
    #InsufficientFunds : { balance : Nat };
  };
  public type UpgradeArgs = {
    change_index_id : ?ChangeIndexId;
    max_transactions_per_request : ?Nat64;
  };
  public type Value = {
    #Int : Int;
    #Map : [(Text, Value)];
    #Nat : Nat;
    #Nat64 : Nat64;
    #Blob : Blob;
    #Text : Text;
    #Array : [Value];
  };

  public type Service = actor {
    create_canister : shared CreateCanisterArgs -> async {
      #Ok : CreateCanisterSuccess;
      #Err : CreateCanisterError;
    };
    deposit : shared DepositArgs -> async DepositResult;
    http_request : shared query HttpRequest -> async HttpResponse;
    icrc1_balance_of : shared query Account -> async Nat;
    icrc1_decimals : shared query () -> async Nat8;
    icrc1_fee : shared query () -> async Nat;
    icrc1_metadata : shared query () -> async [(Text, MetadataValue)];
    icrc1_minting_account : shared query () -> async ?Account;
    icrc1_name : shared query () -> async Text;
    icrc1_supported_standards : shared query () -> async [SupportedStandard];
    icrc1_symbol : shared query () -> async Text;
    icrc1_total_supply : shared query () -> async Nat;
    icrc1_transfer : shared TransferArgs -> async {
      #Ok : BlockIndex;
      #Err : TransferError;
    };
    icrc2_allowance : shared query AllowanceArgs -> async Allowance;
    icrc2_approve : shared ApproveArgs -> async {
      #Ok : Nat;
      #Err : ApproveError;
    };
    icrc2_transfer_from : shared TransferFromArgs -> async {
      #Ok : Nat;
      #Err : TransferFromError;
    };
    icrc3_get_tip_certificate : shared query () -> async ?DataCertificate;
    icrc3_get_transactions : shared query GetTransactionsArgs -> async GetTransactionsResult;
    send : shared SendArgs -> async { #Ok : BlockIndex; #Err : SendError };
  };
};
rvanasa commented 8 months ago

Just to make sure I'm following, are you referring to this comma which appears after running the formatter?

archived_transactions : [
  {
    args : GetTransactionsArgs;
    callback : shared query GetTransactionsArgs -> async GetTransactionsResult;
  }, <-- added comma
];

This indeed seems like a bug in the formatter; I'll move this issue to the prettier-plugin-motoko repository. As a temporary workaround, adding the following .prettierrc config file to your project should remove the trailing commas:

{
    "overrides": [{
        "files": "*.mo",
        "options": {
            "trailingComma": "none"
        }
    }]
}
skilesare commented 8 months ago

Thank you!

rvanasa commented 8 months ago

This is now fixed in version 0.15.1 of the VS Code extension. Cheers!