Skip to content

Small syntactic fixes to JsLIGO grammar to get closer to TS

Christian Rinderknecht requested to merge rinderknecht@jsligo_fixes into dev

Motivation and Context

Related Issue(s): #1611 (closed)

Description

This is now accepted:

type planet = {
  name: string;
  planetType: planetType;
  lord: option<address>;
};

Also this:

type planetType =
  | { kind: "Tellurian" }
  | { kind: "Gaseous" }
  | { kind: "Other" };

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Performance improvement (non-breaking change that improves performance)
  • None (change with no changelog)

Changelog

Semicolons can now be used in describing objects (records), as well as commas. Discriminated unions can now have a leading vertical bar.

For a contract like

type planetType =
  | { kind: "Tellurian" }
  | { kind: "Gaseous" }
  | { kind: "Other" };

type planet = {
  name: string;
  planetType: planetType;
  lord: option<address>;
};


const main = (p: planet, _: int): [list<operation>, int] => {
    let state = 0;
    let planetType = p.planetType;
    switch(planetType.kind) {
    case "Tellurian":
      state += 1;
      break
    case "Gaseous":
      state -= 2;
      break
    case "Other":
      state = 0;
      break
    }
    return [list([]), state]
} 

Before

$ ligo.59 compile contract y.jsligo 
File "y.jsligo", line 2, characters 4-5:
  1 | type planetType =
  2 |   | { kind: "Tellurian" }
  3 |   | { kind: "Gaseous" }
Ill-formed variant of a sum type.
At this point, one of the following is expected:
  * attributes for the variant;
  * an opening bracket '[' followed by a data constructor as a string.

After

$ ligo compile contract  y.jsligo  
{ parameter
    (pair (pair (option %lord address) (string %name))
          (or %planetType (or (unit %gaseous) (unit %other)) (unit %tellurian))) ;
  storage int ;
  code { CAR ;
         PUSH int 0 ;
         SWAP ;
         CDR ;
         IF_LEFT
           { IF_LEFT { DROP ; PUSH int 2 ; SWAP ; SUB } { DROP 2 ; PUSH int 0 } }
           { DROP ; PUSH int 1 ; ADD } ;
         NIL operation ;
         PAIR } }

Checklist:

  • Changes follow the existing coding style (use dune @fmt to check).
  • Tests for the changes have been added (for bug fixes / feature).
  • Documentation has been updated.
  • Changelog description has been added (if appropriate).
  • Start titles under ## Changelog section with #### (if appropriate).
  • There is no image or uploaded file in changelog
  • Examples in changed behaviour have been added to the changelog (for breaking change / feature).
Edited by Melwyn Saldanha

Merge request reports