haxiomic / dts2hx

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

@:native("$eval") for function DollarEval not converted to $eval in Puppeteer #60

Closed rb1147x closed 3 years ago

rb1147x commented 3 years ago

Using Puppeteer there is a function: page.$eval and several other functions using the $ character (more info: https://github.com/puppeteer/puppeteer/blob/v5.3.1/docs/api.md#pageselector).

Externs get generated as page.DollarEval, however, the JS output keeps DollarEval instead of converting to $eval

Example:

From Puppeteer externs generated by dts2hx:

@:native("$eval")
    @:overload(function<R, X1>(selector:String, pageFunction:(element:js.html.DOMElement, x1:UnwrapElementHandle<X1>) -> ts.AnyOf2<js.lib.Promise<R>, R>, x1:X1):js.lib.Promise<WrapElementHandle<R>> { })
    @:overload(function<R, X1, X2>(selector:String, pageFunction:(element:js.html.DOMElement, x1:UnwrapElementHandle<X1>, x2:UnwrapElementHandle<X2>) -> ts.AnyOf2<js.lib.Promise<R>, R>, x1:X1, x2:X2):js.lib.Promise<WrapElementHandle<R>> { })
    @:overload(function<R, X1, X2, X3>(selector:String, pageFunction:(element:js.html.DOMElement, x1:UnwrapElementHandle<X1>, x2:UnwrapElementHandle<X2>, x3:UnwrapElementHandle<X3>) -> ts.AnyOf2<js.lib.Promise<R>, R>, x1:X1, x2:X2, x3:X3):js.lib.Promise<WrapElementHandle<R>> { })
    @:overload(function<R>(selector:String, pageFunction:(element:js.html.DOMElement, args:haxe.extern.Rest<Dynamic>) -> ts.AnyOf2<js.lib.Promise<R>, R>, args:haxe.extern.Rest<SerializableOrJSHandle>):js.lib.Promise<WrapElementHandle<R>> { })
    function DollarEval<R>(selector:String, pageFunction:(element:js.html.DOMElement) -> ts.AnyOf2<js.lib.Promise<R>, R>):js.lib.Promise<WrapElementHandle<R>>;

Usage:

page.DollarEval('div[itemscope="itemscope"]', function (el) {
   return el.innerHTML;
}).then(function(v) {
   trace(v);
});

JS output:

page.DollarEval("div[itemscope=\"itemscope\"]",function(el) {
  return el.innerHTML;
}).then(function(v) {
  console.log("src/Main.hx:36:",v);
});

Is this maybe a larger Haxe issue not supporting the $ character in @:native?

Thanks.

haxiomic commented 3 years ago

Hey @rb1147x, this is indeed an issue in haxe, the problem is @:native isn't yet supported on typedefs. There are proposals and PRs to enable this like https://github.com/HaxeFoundation/haxe/pull/9433 and https://github.com/HaxeFoundation/haxe/issues/5105 but nothing has landed in the compiler yet

You may be able to change these to interfaces to fix for now (although it's not something dts2hx will do automatically because most of the time we need the properties of typedefs for other parts of the externs to work)

I agree this is a pain and it's one of the major long-term issues with haxe js externs

haxiomic commented 3 years ago

Closing as a duplicate https://github.com/haxiomic/dts2hx/issues/48 https://github.com/haxiomic/dts2hx/issues/55

rb1147x commented 3 years ago

Oh, okay, thanks for the quick response!