Skip to content

Commit b20e52d

Browse files
Julian RosseGeoffreyBooth
Julian Rosse
authored andcommitted
[CS2] use _extends utility instead of Object.assign() for object spreads (#4675)
* _extends utility instead of Object.assign() * eqJS test for _extends * Test that a user-defined function named `_extends` doesn’t conflict with our utility function * IE8 polyfill note in docs
1 parent 5525b2b commit b20e52d

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

documentation/sections/es2015plus_output.md

+2
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ npm install --global coffeescript@next
1111
npm install --save-dev coffeescript@next babel-cli babel-preset-env
1212
coffee --print *.coffee | babel --presets env > app.js
1313
```
14+
15+
Note that [babel-preset-env](https://babeljs.io/docs/plugins/preset-env/) doesn’t automatically supply polyfills for your code. CoffeeScript itself will output [`Array.indexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf) if you use the `in` operator, or destructuring or spread/rest syntax; and [`Function.bind`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind) if you use a bound (`=>`) method in a class. Both are supported in Internet Explorer 9+ and all more recent browsers, but you will need to supply polyfills if you need to support Internet Explorer 8 or below and are using features that would cause these methods to be output, or in your own code are using similarly modern methods. One option is [`babel-polyfill`](https://babeljs.io/docs/usage/polyfill/), though there are many [other](https://hackernoon.com/polyfills-everything-you-ever-wanted-to-know-or-maybe-a-bit-less-7c8de164e423) [strategies](https://philipwalton.com/articles/loading-polyfills-only-when-needed/).

lib/coffeescript/nodes.js

+6-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/nodes.coffee

+15-1
Original file line numberDiff line numberDiff line change
@@ -1530,7 +1530,8 @@ exports.Obj = class Obj extends Base
15301530
propSlices.push prop
15311531
addSlice()
15321532
slices.unshift new Obj unless slices[0] instanceof Obj
1533-
(new Call new Literal('Object.assign'), slices).compileToFragments o
1533+
_extends = new Value new Literal utility '_extends', o
1534+
(new Call _extends, slices).compileToFragments o
15341535

15351536
compileCSXAttributes: (o) ->
15361537
props = @properties
@@ -3728,6 +3729,19 @@ UTILITIES =
37283729
}
37293730
}
37303731
"
3732+
_extends: -> "
3733+
Object.assign || function (target) {
3734+
for (var i = 1; i < arguments.length; i++) {
3735+
var source = arguments[i];
3736+
for (var key in source) {
3737+
if (Object.prototype.hasOwnProperty.call(source, key)) {
3738+
target[key] = source[key];
3739+
}
3740+
}
3741+
}
3742+
return target;
3743+
}
3744+
"
37313745

37323746
# Shortcuts to speed up the lookup time for native functions.
37333747
hasProp: -> '{}.hasOwnProperty'

test/assignment.coffee

+18
Original file line numberDiff line numberDiff line change
@@ -897,3 +897,21 @@ test "#4566: destructuring with nested default values", ->
897897

898898
{e: {f = 5} = {}} = {}
899899
eq 5, f
900+
901+
test "#4674: _extends utility for object spreads 1", ->
902+
eqJS(
903+
"{a, b..., c..., d}"
904+
"""
905+
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
906+
907+
_extends({a}, b, c, {d});
908+
"""
909+
)
910+
911+
test "#4674: _extends utility for object spreads 2", ->
912+
_extends = -> 3
913+
a = b: 1
914+
c = d: 2
915+
e = {a..., c...}
916+
eq e.b, 1
917+
eq e.d, 2

0 commit comments

Comments
 (0)