GithubHelp home page GithubHelp logo

stellar / dts-xdr Goto Github PK

View Code? Open in Web Editor NEW
3.0 21.0 3.0 61 KB

A library for generating TypeScript declarations (.d.ts) for js-xdr auto-generated files.

Home Page: https://github.com/stellar/dts-xdr

License: Apache License 2.0

JavaScript 100.00%
xdr typescript stellar

dts-xdr's Introduction

dts-xdr

Stellar

dts-xdr is a library for generating TypeScript declarations file (.d.ts) for js-xdr auto-generated files.

This library uses jscodeshift to generate the definitions, follow the steps below to generate definitions.

Setup

git clone https://github.com/stellar/dts-xdr.git
cd dts-xdr
yarn install

Usage

You can use this library in two mode:

Source replacement

The first one is using inline replacement which is the default mode when you call jscodeshift. The following will replace the given file with the generated code:

npx jscodeshift -t src/transform.js sample/stellar-xdr_generated.js

After you run the command above, sample/stellar-xdr_generated.js will have the type definitions.

Output mode

The second mode is specifying an output file, this mode won't change the source file. The following command will generate a new file called stellar-xdr_generated.d.ts with the TypeScript declarations:

OUT=stellar-xdr_generated.d.ts npx jscodeshift -t src/transform.js sample/stellar-xdr_generated.js

dts-xdr's People

Contributors

abuiles avatar paulbellamy avatar shaptic avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dts-xdr's Issues

xdr union switch on number causes invalid typescript class generation

Describe the bug
ts generated code for an xdr union switch based on number, the resulting ts class generates an invalid x(arm value): union static factory method for any switched values beyond 0, the method name is just the number x, which is invalid js syntax.

What version are you on?
1.3.0

To Reproduce
https://github.com/stellar/js-stellar-base/blob/soroban/types/next.d.ts#L11589 has generated ts code for TransactionExt class with static 1(value: SorobanTransactionData): TransactionExt; which is not a callable method in js.

https://github.com/stellar/js-stellar-base/blob/soroban/src/generated/next_generated.js#L5368 has the generated js code defining the mapping for xdr union and switch values.

the source xdr defining this union with numeric switch values - https://github.com/stellar/stellar-xdr/blob/next/Stellar-transaction.x#L869, excerpt here:

struct Transaction
{
   ...
    union switch (int v)
    {
    case 0:
        void;
    case 1:
        SorobanTransactionData sorobanData;
    }
    ext;
};

to reproduce the behavior locally, use js-stellar-base as it uses dts-xdr and compiles this xdr def:

  1. checkout github.com/stellar/js-stellar-base
  2. run make reset-xdr from checked out directory

in either case, looking at the resulting js-stellar-base/types/next.d.ts, will see TransactionExt with the incorrectly named factory method.

Expected behavior
a static ts method declaration for the armed switch name such as TransactionExt.sorobanData(value: SorobanTransactionData): TransactionExt

Additional context
What is the expected way to use the generated code to create an instance of one of these unions given you have an arm value, it seems like these static methods named after the switch value are intended for that, otherwise, the default constructor on the ts class doesn't work, I tried that first, which does create an object, but when using the class instance method provided to set the arm value sorobanData(sorobanTransactionData) it doesn't appear to do anything as the value() return undefined afterwards.

one potential work around is to create instance from the static factory method fromXDR(string) and use a hardcoded serialized xdr string representing an empty instance if possible.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.