Skip to content

Commit a1e5007

Browse files
authored
Merge pull request #60 from jnwng/jw/fix-spread-property
Adding support for `objectRestSpread` within props
2 parents e4c0312 + 2eefda3 commit a1e5007

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

__tests__/helper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const parser = require('babylon');
44

55
function parse(code) {
66
return parser.parse(code, {
7-
plugins: ['jsx', 'functionBind', 'estree'],
7+
plugins: ['jsx', 'functionBind', 'estree', 'objectRestSpread'],
88
});
99
}
1010

__tests__/src/getPropValue-test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,15 @@ describe('getPropValue', () => {
768768

769769
assert.deepEqual(expected, actual);
770770
});
771+
772+
it('should evaluate to a correct representation of the object, ignore spread properties', () => {
773+
const prop = extractProp('<div foo={{bar: "baz", ...{baz: "bar", foo: {...{bar: "meh"}}}}} />');
774+
775+
const expected = { bar: 'baz', baz: 'bar', foo: { bar: 'meh' } };
776+
const actual = getPropValue(prop);
777+
778+
assert.deepEqual(expected, actual);
779+
});
771780
});
772781

773782
describe('New expression', () => {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsx-ast-utils",
3-
"version": "2.0.1",
3+
"version": "2.0.2",
44
"description": "AST utility module for statically analyzing JSX",
55
"main": "lib/index.js",
66
"scripts": {

src/values/expressions/ObjectExpression.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ import getValue from './index';
99
export default function extractValueFromObjectExpression(value) {
1010
return value.properties.reduce((obj, property) => {
1111
const object = Object.assign({}, obj);
12-
object[getValue(property.key)] = getValue(property.value);
12+
if (property.type === 'SpreadProperty') {
13+
if (property.argument.type === 'ObjectExpression') {
14+
return Object.assign(object, extractValueFromObjectExpression(property.argument));
15+
}
16+
} else {
17+
object[getValue(property.key)] = getValue(property.value);
18+
}
1319
return object;
1420
}, {});
1521
}

0 commit comments

Comments
 (0)