-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrongly added Array.prototype.keys
polyfill when Object.keys
is used
#2448
Comments
In general we will sometimes add polyfills we don't need, though we can probably do better in this case. cc @shicks |
This didn't use to be an issue for us, but after updating to "use strict";
var $jscomp = $jscomp || {};
$jscomp.scope = {};
$jscomp.owns = function(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
};
$jscomp.ASSUME_ES5 = !1;
$jscomp.ASSUME_NO_NATIVE_MAP = !1;
$jscomp.ASSUME_NO_NATIVE_SET = !1;
$jscomp.defineProperty =
$jscomp.ASSUME_ES5 || "function" == typeof Object.defineProperties
? Object.defineProperty
: function(target, property, descriptor) {
target != Array.prototype &&
target != Object.prototype &&
(target[property] = descriptor.value);
};
$jscomp.getGlobal = function(maybeGlobal) {
return "undefined" != typeof window && window === maybeGlobal
? maybeGlobal
: "undefined" != typeof global && null != global ? global : maybeGlobal;
};
$jscomp.global = $jscomp.getGlobal(this);
$jscomp.polyfill = function(target, polyfill) {
if (polyfill) {
var obj = $jscomp.global;
target = target.split(".");
for (var i = 0; i < target.length - 1; i++) {
var key = target[i];
key in obj || (obj[key] = {});
obj = obj[key];
}
target = target[target.length - 1];
i = obj[target];
polyfill = polyfill(i);
polyfill != i &&
null != polyfill &&
$jscomp.defineProperty(obj, target, {
configurable: !0,
writable: !0,
value: polyfill
});
}
};
$jscomp.polyfill(
"Object.assign",
function(orig) {
return orig
? orig
: function(target, var_args) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
if (source)
for (var key in source)
$jscomp.owns(source, key) && (target[key] = source[key]);
}
return target;
};
},
"es6",
"es3"
);
$jscomp.SYMBOL_PREFIX = "jscomp_symbol_";
$jscomp.initSymbol = function() {
$jscomp.initSymbol = function() {};
$jscomp.global.Symbol || ($jscomp.global.Symbol = $jscomp.Symbol);
};
$jscomp.Symbol = (function() {
var counter = 0;
return function(opt_description) {
return $jscomp.SYMBOL_PREFIX + (opt_description || "") + counter++;
};
})();
$jscomp.initSymbolIterator = function() {
$jscomp.initSymbol();
var symbolIterator = $jscomp.global.Symbol.iterator;
symbolIterator ||
(symbolIterator = $jscomp.global.Symbol.iterator = $jscomp.global.Symbol(
"iterator"
));
"function" != typeof Array.prototype[symbolIterator] &&
$jscomp.defineProperty(Array.prototype, symbolIterator, {
configurable: !0,
writable: !0,
value: function() {
return $jscomp.arrayIterator(this);
}
});
$jscomp.initSymbolIterator = function() {};
};
$jscomp.arrayIterator = function(array) {
var index = 0;
return $jscomp.iteratorPrototype(function() {
return index < array.length
? { done: !1, value: array[index++] }
: { done: !0 };
});
};
$jscomp.iteratorPrototype = function(next) {
$jscomp.initSymbolIterator();
next = { next: next };
next[$jscomp.global.Symbol.iterator] = function() {
return this;
};
return next;
};
$jscomp.iteratorFromArray = function(array, transform) {
$jscomp.initSymbolIterator();
array instanceof String && (array += "");
var i = 0,
iter = {
next: function() {
if (i < array.length) {
var index = i++;
return { value: transform(index, array[index]), done: !1 };
}
iter.next = function() {
return { done: !0, value: void 0 };
};
return iter.next();
}
};
iter[Symbol.iterator] = function() {
return iter;
};
return iter;
};
$jscomp.polyfill(
"Array.prototype.keys",
function(orig) {
return orig
? orig
: function() {
return $jscomp.iteratorFromArray(this, function(i) {
return i;
});
};
},
"es6",
"es3"
); I didn't change any options. You can reproduce by installing Yarn and then running
The build output is in This is a blocker for updating for us: we can't afford to ship any polyfills. |
@gaearon After I filled the issue, I found the solution a few months ago. If |
I’m using the JS version that claims |
This is still an issue with |
I was unable to reproduce this with the current compiler. My guess is that the type information on If this is still a problem, please reopen. |
I am still getting this issue while using function test(o) {
return Object["keys"](o);
} |
When
Object.keys()
is used,closure compiler
addsArray.prototype.keys
polyfill. Test file:Output:
I believe it's incorrect.
The text was updated successfully, but these errors were encountered: