haxiomic / dts2hx

Converts TypeScript definition files (d.ts) to haxe externs (.hx) via the TypeScript compiler API
MIT License
131 stars 8 forks source link

enum abstract enumeration convertion is incorrect if started not from 0 #104

Closed serjek closed 2 years ago

serjek commented 2 years ago
  1. npx dts2hx react-native-rate
  2. Observe generated AndroidMarket.hx file. It has following enum abstract declatation:
    @:jsRequire("react-native-rate", "AndroidMarket") @:enum extern abstract AndroidMarket(Int) from Int to Int {
    var Google;
    var Amazon;
    var Other;
    }
  3. Observe original source, it has following enum declaration:
    export const AndroidMarket = {
    Google: 1,
    Amazon: 2,
    Other: 3,
    }

Expected: resulting enum should be correctly enumerated like so:

var Google = 1;
var Amazon = 2;
var Other = 3;

Experienced: resulting enumeration starts with 0 which results in incorrect params passed to lib.

haxiomic commented 2 years ago

The generated code references the source enum, for example: https://try.haxe.org/#aAcC35B5

@:jsRequire("react-native-rate", "AndroidMarket") @:enum extern abstract AndroidMarket(Int) from Int to Int {
    var Google;
    var Amazon;
    var Other;
}

class Test {
  static function main() {
    var x: AndroidMarket = Google;
    trace(x);
  }
}

Generates

console.log("Test.hx:11:",AndroidMarket.Google);

Is it generating something different for you? If so, what are your compiler arguments and haxe version?

serjek commented 2 years ago

Hey sorry for false alarm, it works as expected. For some reason I got removed the @:jsRequire part when first checked, generated again and it's working now. Closing the issue!

haxiomic commented 2 years ago

No worries :), glad it's working