google / j2cl

Java to Closure JavaScript transpiler
Apache License 2.0
1.23k stars 144 forks source link

Adding new/experimental ECMA definitions #124

Closed rendaw closed 3 years ago

rendaw commented 3 years ago

Currently there's some ECMA objects that are new or experimental that aren't available in Elemental2: ClipboardItem, Intl.Segmenter, etc.

I tried to add my own shims like:

@JsType(isNative = true, name = "Intl.Segmenter", namespace = GLOBAL)
public class Segmenter {
  public Segmenter(String lang, JsPropertyMap<Object> options) {}

  public native JsArray<Segment> segment(String text);
}

but I get various errors

Segment$$Overlay.impl.java.js:13: ERROR - variable Segment is undeclared
  return instance instanceof Segment;
                             ^
  ProTip: "JSC_UNDEFINED_VARIABLE" or "checkVars" or "missingSourcesWarnings" or "undefinedVars" can be added to the `suppress` attribute of:
ERROR - Bad type annotation. Unknown type ClipboardItem
  ProTip: "JSC_UNRECOGNIZED_TYPE_ERROR" or "checkTypes" or "unrecognizedTypeError" can be added to the `suppress` attribute
ERROR - Property segment never defined on segmenter
  ProTip: "JSC_POSSIBLE_INEXISTENT_PROPERTY" or "checkTypes" or "globallyMissingProperties" or "missingProperties" or "missingSourcesWarnings" can be added to the `suppress` attribute

etc.

Since I have very minimal javascript I added the above to js_suppress, but is there a better/safer way to do this? I assume the reason elemental2 works while this doesn't is that elemental2 is checked against an internal js engine or something that doesn't have these defined (yet).

tbroyer commented 3 years ago

AFAICT, you need externs for all browser APIs. If these are not in Closure Library externs, then you have to write those and give them to the Closure Compiler.

rendaw commented 3 years ago

Thanks! That pointed me in the right direction.