-
Notifications
You must be signed in to change notification settings - Fork 581
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
https://github.com/sebmarkbage/ecmascript-rest-spread/blob/master/Spec.md#spread-properties This add support for ... in object literals which is currently at stage 2. ```js var x = {a: 1}; var y = {...a, b: 2}; // {a: 1, b: 2} ```
- Loading branch information
Showing
15 changed files
with
233 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2016 Traceur Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the 'License'); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an 'AS IS' BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import {SPREAD_EXPRESSION} from '../syntax/trees/ParseTreeType.js'; | ||
import {createObjectLiteral, createArgumentList} from './ParseTreeFactory.js'; | ||
import {parseExpression} from './PlaceholderParser.js'; | ||
import {ParseTreeTransformer} from './ParseTreeTransformer.js'; | ||
|
||
function hasSpread(trees) { | ||
return trees.some((tree) => tree && tree.type === SPREAD_EXPRESSION); | ||
} | ||
|
||
/** | ||
* Adds support for spread properties on object literals | ||
* | ||
* https://github.com/sebmarkbage/ecmascript-rest-spread/blob/master/Spec.md | ||
* | ||
* {a, ...b, c, ...d} | ||
* => | ||
* $spreadProperties({a}, b, {c}, d) | ||
*/ | ||
export class SpreadPropertiesTransformer extends ParseTreeTransformer { | ||
transformObjectLiteral(tree) { | ||
if (!hasSpread(tree.propertyNameAndValues)) { | ||
return super.transformObjectLiteral(tree); | ||
} | ||
|
||
const properties = this.transformList(tree.propertyNameAndValues); | ||
return spreadProperties(properties); | ||
} | ||
} | ||
|
||
export function spreadProperties(properties) { | ||
// Accummulate consecutive properties into a single js property. | ||
let args = []; | ||
let accummulatedProps = null; | ||
for (let i = 0; i < properties.length; i++) { | ||
let property = properties[i]; | ||
if (property.type === SPREAD_EXPRESSION) { | ||
if (accummulatedProps) { | ||
args.push(createObjectLiteral(accummulatedProps)); | ||
accummulatedProps = null; | ||
} | ||
args.push(property.expression); | ||
} else { | ||
if (!accummulatedProps) { | ||
accummulatedProps = []; | ||
} | ||
accummulatedProps.push(property); | ||
} | ||
} | ||
if (accummulatedProps) { | ||
args.push(createObjectLiteral(accummulatedProps)); | ||
} | ||
return parseExpression `$traceurRuntime.spreadProperties(${ | ||
createArgumentList(args)})`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2016 Traceur Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
const { | ||
defineProperty, | ||
getOwnPropertyNames, | ||
getOwnPropertySymbols, | ||
propertyIsEnumerable, | ||
} = Object; | ||
|
||
function createDataProperty(o, p, v) { | ||
defineProperty(o, p, { | ||
configurable: true, | ||
enumerable: true, | ||
value: v, | ||
writable: true, | ||
}); | ||
} | ||
|
||
function copyDataProperties(target, source) { | ||
if (source == null) { // needs to be == | ||
return; | ||
} | ||
|
||
// The spec has a ToObject here but getOwnProperty* does the ToObjext so we | ||
// do not need to call it here. | ||
|
||
const copy = keys => { | ||
for (let i = 0; i < keys.length; i++) { | ||
const nextKey = keys[i]; | ||
if (propertyIsEnumerable.call(source, nextKey)) { | ||
const propValue = source[nextKey]; | ||
createDataProperty(target, nextKey, propValue); | ||
} | ||
} | ||
}; | ||
|
||
copy(getOwnPropertyNames(source)); | ||
copy(getOwnPropertySymbols(source)); | ||
} | ||
|
||
export default function() { | ||
const target = arguments[0]; | ||
for (let i = 1; i < arguments.length; i++) { | ||
copyDataProperties(target, arguments[i]); | ||
} | ||
return target; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Options: --spread-properties | ||
|
||
var n = 0; | ||
var o; | ||
assert.deepEqual(o = {a: 'a', ...{get a() { n++; return 'b'; }}}, {a: 'b'}); | ||
assert.equal(1, n); | ||
assert.equal(o.a, 'b'); | ||
assert.equal(1, n); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Options: --spread-properties | ||
|
||
function fail() { | ||
assert.isTrue(false, 'unreachable'); | ||
} | ||
|
||
var o; | ||
assert.deepEqual({set a(x) {fail()}, ...{a: 'a'}}, {a: 'a'}); | ||
assert.deepEqual(o = {a: 'a', ...{set a(x) {fail()}}}, {a: undefined}); | ||
o.a = 'b'; | ||
assert.equal(o.a, 'b'); | ||
|
||
var n = 0; | ||
var o; | ||
assert.deepEqual(o = {a: 'a', ...{get a() { n++; return 'b'; }}}, {a: 'b'}); | ||
assert.equal(1, n); | ||
assert.equal(o.a, 'b'); | ||
assert.equal(1, n); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Options: --spread-properties | ||
|
||
assert.deepEqual({...{a: 'a'}}, {a: 'a'}); | ||
assert.deepEqual({a: 'a', ...{b: 'b'}}, {a: 'a', b: 'b'}); | ||
assert.deepEqual({a: 'a', ...{b: 'b'}, c: 'c'}, {a: 'a', b: 'b', c: 'c'}); | ||
assert.deepEqual({a: 'a', b: 'b', ...{c: 'c'}}, {a: 'a', b: 'b', c: 'c'}); | ||
assert.deepEqual({a: 'a', b: 'b', c: 'c', ...{}}, {a: 'a', b: 'b', c: 'c'}); | ||
assert.deepEqual({...{a: 'a'}, b: 'b', c: 'c'}, {a: 'a', b: 'b', c: 'c'}); | ||
assert.deepEqual({...{a: 'a'}, ...{b: 'b'}, ...{c: 'c'}}, | ||
{a: 'a', b: 'b', c: 'c'}); | ||
|
||
assert.deepEqual({a: 1, ...{a: 2}}, {a: 2}); | ||
assert.deepEqual({...{a: 1}, a: 2}, {a: 2}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Options: --spread-properties | ||
|
||
var p = { | ||
m() { | ||
return 'm'; | ||
}, | ||
n() { | ||
return 'n'; | ||
}, | ||
}; | ||
|
||
var a = {a: 'a'}; | ||
var b = {b: 'b'}; | ||
|
||
var o = { | ||
__proto__: p, | ||
...a, | ||
m() { | ||
return super.m(); | ||
}, | ||
...b, | ||
n() { | ||
return super.n(); | ||
}, | ||
}; | ||
|
||
assert.equal(o.a, 'a'); | ||
assert.equal(o.b, 'b'); | ||
assert.equal(o.m(), 'm'); | ||
assert.equal(o.n(), 'n'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Options: --spread-properties | ||
|
||
var s1 = Symbol(); | ||
var s2 = Symbol(); | ||
|
||
var o = {[s1]: 1, ...{[s2]: 2}}; | ||
assert.equal(o[s1], 1); | ||
assert.equal(o[s2], 2); |