diff --git a/src/program/types/ObjectExpression.js b/src/program/types/ObjectExpression.js index 9dcb80ed..06a09faa 100644 --- a/src/program/types/ObjectExpression.js +++ b/src/program/types/ObjectExpression.js @@ -6,7 +6,6 @@ export default class ObjectExpression extends Node { super.transpile(code, transforms); let firstPropertyStart = this.start + 1; - let regularPropertyCount = 0; let spreadPropertyCount = 0; let computedPropertyCount = 0; let firstSpreadProperty = null; @@ -17,47 +16,44 @@ export default class ObjectExpression extends Node { if (prop.type === 'SpreadElement') { spreadPropertyCount += 1; if (firstSpreadProperty === null) firstSpreadProperty = i; - } else if (prop.computed) { + } else if (prop.computed && transforms.computedProperty) { computedPropertyCount += 1; if (firstComputedProperty === null) firstComputedProperty = i; - } else if (prop.type === 'Property') { - regularPropertyCount += 1; } } - if (spreadPropertyCount && transforms.objectRestSpread) { + if (spreadPropertyCount && !transforms.objectRestSpread && !(computedPropertyCount && transforms.computedProperty)) { + spreadPropertyCount = 0; + firstSpreadProperty = null; + } else if (spreadPropertyCount) { if (!this.program.options.objectAssign) { throw new CompileError( "Object spread operator requires specified objectAssign option with 'Object.assign' or polyfill helper.", this ); } - // enclose run of non-spread properties in curlies let i = this.properties.length; - if (regularPropertyCount && !computedPropertyCount) { - while (i--) { - const prop = this.properties[i]; - - if (prop.type === 'Property' && !prop.computed) { - const lastProp = this.properties[i - 1]; - const nextProp = this.properties[i + 1]; - - if ( - !lastProp || - lastProp.type !== 'Property' || - lastProp.computed - ) { - code.prependRight(prop.start, '{'); - } + while (i--) { + const prop = this.properties[i]; - if ( - !nextProp || - nextProp.type !== 'Property' || - nextProp.computed - ) { - code.appendLeft(prop.end, '}'); - } + // enclose run of non-spread properties in curlies + if (prop.type === 'Property' && !computedPropertyCount) { + const lastProp = this.properties[i - 1]; + const nextProp = this.properties[i + 1]; + + if (!lastProp || lastProp.type !== 'Property') { + code.prependRight(prop.start, '{'); } + + if (!nextProp || nextProp.type !== 'Property') { + code.appendLeft(prop.end, '}'); + } + } + + // Remove ellipsis on spread property + if (prop.type === 'SpreadElement') { + code.remove(prop.start, prop.argument.start); + code.remove(prop.argument.end, prop.end); } } diff --git a/src/program/types/SpreadElement.js b/src/program/types/SpreadElement.js deleted file mode 100644 index 49569aa5..00000000 --- a/src/program/types/SpreadElement.js +++ /dev/null @@ -1,12 +0,0 @@ -import Node from '../Node.js'; - -export default class SpreadElement extends Node { - transpile(code, transforms) { - if (transforms.objectRestSpread && this.parent.type == 'ObjectExpression') { - code.remove(this.start, this.argument.start); - code.remove(this.argument.end, this.end); - } - - super.transpile(code, transforms); - } -} diff --git a/src/program/types/index.js b/src/program/types/index.js index 0b747e51..eb4030a5 100644 --- a/src/program/types/index.js +++ b/src/program/types/index.js @@ -36,7 +36,6 @@ import NewExpression from './NewExpression.js'; import ObjectExpression from './ObjectExpression.js'; import Property from './Property.js'; import ReturnStatement from './ReturnStatement.js'; -import SpreadElement from './SpreadElement.js'; import Super from './Super.js'; import TaggedTemplateExpression from './TaggedTemplateExpression.js'; import TemplateElement from './TemplateElement.js'; @@ -85,7 +84,6 @@ export default { ObjectExpression, Property, ReturnStatement, - SpreadElement, Super, TaggedTemplateExpression, TemplateElement, diff --git a/test/samples/object-rest-spread.js b/test/samples/object-rest-spread.js index bdacf064..abc319ec 100644 --- a/test/samples/object-rest-spread.js +++ b/test/samples/object-rest-spread.js @@ -126,6 +126,34 @@ module.exports = [ var a12 = { ...b, [c]:3, d:4 }; ` }, + { + description: 'supports transpiling spread properties if computed properties shouldn\'t be transpiled', + options: { + objectAssign: 'Object.assign', + transforms: { computedProperty: false, conciseMethodProperty: false } + }, + input: ` + var a0 = { [ x ] : true , ... y }; + `, + output: ` + var a0 = Object.assign({}, {[ x ] : true} , y); + `, + }, + { + description: 'supports transpiling computed properties if spread properties shouldn\'t be transpiled', + options: { + objectAssign: 'Object.assign', + transforms: { objectRestSpread: false, conciseMethodProperty: false } + }, + input: ` + var a0 = { [ x ] : true , ... y }; + `, + output: ` + var obj; + + var a0 = Object.assign(( obj = {}, obj[ x ] = true, obj ), y); + `, + }, { description: 'transpiles inline objects with spread with computed property (#144)',