Search in sources :

Example 1 with Polyfill

use of com.google.javascript.jscomp.PolyfillUsageFinder.Polyfill in project closure-compiler by google.

the class IsolatePolyfills method rewritePolyfill.

/**
 * Rewrites a potential access of a polyfilled class or method to first look for the non-global,
 * polyfilled version.
 */
private void rewritePolyfill(PolyfillUsage polyfillUsage) {
    final Polyfill polyfill = polyfillUsage.polyfill();
    // won't need to be isolated.
    if (compiler.getOptions().getOutputFeatureSet().contains(FeatureSet.valueOf(polyfill.nativeVersion))) {
        return;
    }
    final Node polyfillAccess = polyfillUsage.node();
    final Node parent = polyfillUsage.node().getParent();
    if (FILES_ALLOWED_UNQUALIFIED_POLYFILL_ACCESSES.contains(polyfillAccess.getSourceFileName())) {
        // TODO(b/156776817): crash on early references to polyfills that are not in our allowlist.
        return;
    }
    // tmp[$maybePolyfillProp(tmp, 'includes)] = intercept;
    if (NodeUtil.isLValue(polyfillAccess) || (parent.isAssign() && polyfillAccess.isFirstChildOf(parent))) {
        return;
    }
    final String name = polyfillUsage.name();
    boolean isGlobalClass = name.indexOf('.') == -1 && polyfill.kind.equals(Polyfill.Kind.STATIC);
    if (isGlobalClass) {
        // e.g. Symbol, Map, window.Map, or goog.global.Map
        polyfillAccess.replaceWith(// $jscomp.polyfills['Map']
        IR.getelem(jscompPolyfillsObject.cloneTree(), IR.string(name)).srcrefTree(polyfillAccess));
    } else if (parent.isCall() && polyfillAccess.isFirstChildOf(parent)) {
        // e.g. getStr().includes('x')
        rewritePolyfillInCall(polyfillAccess);
    } else {
        // e.g. [].includes.call(myIter, 0)
        Node methodName = IR.string(polyfillAccess.getString()).srcref(polyfillAccess);
        Node receiver = polyfillAccess.removeFirstChild();
        polyfillAccess.replaceWith(createPolyfillMethodLookup(receiver, methodName).srcrefTree(polyfillAccess));
    }
    compiler.reportChangeToEnclosingScope(parent);
}
Also used : Node(com.google.javascript.rhino.Node) Polyfill(com.google.javascript.jscomp.PolyfillUsageFinder.Polyfill)

Example 2 with Polyfill

use of com.google.javascript.jscomp.PolyfillUsageFinder.Polyfill in project closure-compiler by google.

the class RewritePolyfills method inject.

private void inject(PolyfillUsage polyfillUsage) {
    Polyfill polyfill = polyfillUsage.polyfill();
    final FeatureSet outputFeatureSet = compiler.getOptions().getOutputFeatureSet();
    final FeatureSet featuresRequiredByPolyfill = FeatureSet.valueOf(polyfill.polyfillVersion);
    if (polyfill.kind.equals(Polyfill.Kind.STATIC) && !outputFeatureSet.contains(featuresRequiredByPolyfill)) {
        compiler.report(JSError.make(polyfillUsage.node(), INSUFFICIENT_OUTPUT_VERSION_ERROR, polyfillUsage.name(), outputFeatureSet.version()));
    }
    // version that introduced this symbol?"
    if (!outputFeatureSet.contains(FeatureSet.valueOf(polyfill.nativeVersion)) && !polyfill.library.isEmpty()) {
        libraries.add(polyfill.library);
    }
}
Also used : Polyfill(com.google.javascript.jscomp.PolyfillUsageFinder.Polyfill) FeatureSet(com.google.javascript.jscomp.parsing.parser.FeatureSet)

Aggregations

Polyfill (com.google.javascript.jscomp.PolyfillUsageFinder.Polyfill)2 FeatureSet (com.google.javascript.jscomp.parsing.parser.FeatureSet)1 Node (com.google.javascript.rhino.Node)1