Skip to content

Commit

Permalink
Adding support for objectRestSpread within props
Browse files Browse the repository at this point in the history
  • Loading branch information
jnwng committed Dec 6, 2017
1 parent e4c0312 commit a5b9461
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion __tests__/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const parser = require('babylon');

function parse(code) {
return parser.parse(code, {
plugins: ['jsx', 'functionBind', 'estree'],
plugins: ['jsx', 'functionBind', 'estree', 'objectRestSpread'],
});
}

Expand Down
9 changes: 9 additions & 0 deletions __tests__/src/getPropValue-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,15 @@ describe('getPropValue', () => {

assert.deepEqual(expected, actual);
});

it('should evaluate to a correct representation of the object, ignore spread properties', () => {
const prop = extractProp('<div foo={{bar: "baz", ...{baz: "bar", foo: {...{bar: "meh"}}}}} />');

const expected = { bar: 'baz', baz: 'bar', foo: { bar: 'meh' } };
const actual = getPropValue(prop);

assert.deepEqual(expected, actual);
});
});

describe('New expression', () => {
Expand Down
8 changes: 7 additions & 1 deletion src/values/expressions/ObjectExpression.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ import getValue from './index';
export default function extractValueFromObjectExpression(value) {
return value.properties.reduce((obj, property) => {
const object = Object.assign({}, obj);
object[getValue(property.key)] = getValue(property.value);
if (property.type === 'SpreadProperty') {
if (property.argument.type === 'ObjectExpression') {
return Object.assign(object, extractValueFromObjectExpression(property.argument));
}
} else {
object[getValue(property.key)] = getValue(property.value);
}
return object;
}, {});
}

0 comments on commit a5b9461

Please sign in to comment.