Vertispan / j2clmavenplugin

Maven plugin to launch new J2CL compilation
https://vertispan.github.io/j2clmavenplugin/
Apache License 2.0
53 stars 26 forks source link

Possible incorrect J2CL warnings on a correct JSInterop code #254

Closed salmonb closed 5 months ago

salmonb commented 5 months ago

hi,

As discussed on Gitter, I open this issue to report a possible J2CL bug about incorrect warnings on a correct JSInterop code.

Here is a JSInterop class that extends CanvasRenderingContext2D to add the setTransform(DOMMatrixReadOnly) method missing in elemental2 version 1.2.1:

@JsType(isNative = true, namespace = JsPackage.GLOBAL, name = "CanvasRenderingContext2D")
final class CanvasRenderingContext2DWithMatrixSetTransform extends CanvasRenderingContext2D {

    @JsMethod
    public native void setTransform(DOMMatrixReadOnly transform);

}

And here is a simple user code:

public class Main {

    @GWT3EntryPoint
    public void entryPoint() {
        HTMLCanvasElement canvas = Js.cast(DomGlobal.document.createElement("canvas"));
        CanvasRenderingContext2DWithMatrixSetTransform ctx = Js.cast(canvas.getContext("2d"));
        ctx.setTransform(DOMMatrixReadOnly.fromMatrix(DOMMatrixInit.create()));
    }

}

Although everything looks correct, the J2CL reports these warnings:

[WARNING] dev.webfx:overload-jsinterop-j2cl-issue:0.1.0-SNAPSHOT/optimized_js: /Users/bruno/IdeaProjects/overload-jsinterop-j2cl-issue/target/gwt3BuildCache/0.22.0/dev.webfx-overload-jsinterop-j2cl-issue-0.1.0-SNAPSHOT/dfd0c0349f655d48eb15168807b1d279-transpiled_js/results/dev/webfx/issue/Main.impl.java.js:33:2: 
Originally at:
/Users/bruno/IdeaProjects/overload-jsinterop-j2cl-issue/target/gwt3BuildCache/0.22.0/dev.webfx-overload-jsinterop-j2cl-issue-0.1.0-SNAPSHOT/dfd0c0349f655d48eb15168807b1d279-transpiled_js/results/dev/webfx/issue/Main.java:16:8: WARNING - [JSC_WRONG_ARGUMENT_COUNT] Function BaseRenderingContext2D.prototype.setTransform: called with 1 argument(s). Function requires at least 6 argument(s) and no more than 6 argument(s).
        ctx.setTransform(DOMMatrixReadOnly.fromMatrix(DOMMatrixInit.create()));
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

[WARNING] dev.webfx:overload-jsinterop-j2cl-issue:0.1.0-SNAPSHOT/optimized_js: /Users/bruno/IdeaProjects/overload-jsinterop-j2cl-issue/target/gwt3BuildCache/0.22.0/dev.webfx-overload-jsinterop-j2cl-issue-0.1.0-SNAPSHOT/dfd0c0349f655d48eb15168807b1d279-transpiled_js/results/dev/webfx/issue/Main.impl.java.js:33:19: 
Originally at:
/Users/bruno/IdeaProjects/overload-jsinterop-j2cl-issue/target/gwt3BuildCache/0.22.0/dev.webfx-overload-jsinterop-j2cl-issue-0.1.0-SNAPSHOT/dfd0c0349f655d48eb15168807b1d279-transpiled_js/results/dev/webfx/issue/Main.java:16:8: WARNING - [JSC_TYPE_MISMATCH] actual parameter 1 of BaseRenderingContext2D.prototype.setTransform does not match formal parameter
found   : DOMMatrixReadOnly
required: number
        ctx.setTransform(DOMMatrixReadOnly.fromMatrix(DOMMatrixInit.create()));
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I made a Maven project based on the code above as a functioning reproducer available in this repo

zbynek commented 5 months ago

The warning is coming from Closure and is caused by https://github.com/google/closure-compiler/issues/3686 which currently has an open PR.

To work around it, you can create a new file src/main/resources/externs.js with the following content:

``` /** * @externs */ /** * @param {(number|DOMMatrixReadOnly)} m11OrMatrix * @param {number=} m12 * @param {number=} m21 * @param {number=} m22 * @param {number=} dx * @param {number=} dy * @return {undefined} */ CanvasRenderingContext2D.prototype.setTransform = function( m11OrMatrix, m12, m21, m22, dx, dy) {}; // we adds the method to `CanvasRenderingContext2D` because we can't replace methods in `BaseRenderingContext2D` ```

So I think j2cl plugin cannot do anything about this and the issue should be closed.

salmonb commented 5 months ago

You're right, and thanks a lot for your PR on Closure.

I also tried your externs.js and it works indeed.

So I'm closing this issue.

Thank you!