Skip to content

Commit

Permalink
fix: spread should not traverse children elements (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbertLucianto authored and gajus committed Mar 22, 2019
1 parent 05c2683 commit 4a47f35
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 33 deletions.
68 changes: 35 additions & 33 deletions src/createSpreadMapper.js
Expand Up @@ -6,7 +6,8 @@ import {
binaryExpression,
stringLiteral,
logicalExpression,
identifier
identifier,
isJSXSpreadAttribute
} from '@babel/types';
import optionsDefaults from './schemas/optionsDefaults';

Expand All @@ -29,43 +30,44 @@ const createSpreadMapper = (path: *, stats: *): { [destinationName: string]: Exp
return pair[0];
});

path.traverse({
JSXSpreadAttribute (spreadPath: *) {
const spread = spreadPath.node;
const spreadAttributes = path.node.openingElement.attributes
.filter((attr) => {
return isJSXSpreadAttribute(attr);
});

for (const attributeKey of attributeKeys) {
const destinationName = attributeNames[attributeKey];
for (const spread of spreadAttributes) {
for (const attributeKey of attributeKeys) {
const destinationName = attributeNames[attributeKey];

if (result[destinationName]) {
result[destinationName] = binaryExpression(
if (result[destinationName]) {
result[destinationName] = binaryExpression(
'+',
result[destinationName],
binaryExpression(
'+',
result[destinationName],
binaryExpression(
'+',
stringLiteral(' '),
logicalExpression(
'||',
memberExpression(
spread.argument,
identifier(destinationName),
),
stringLiteral('')
)
),
);
} else {
result[destinationName] = logicalExpression(
'||',
memberExpression(
spread.argument,
identifier(destinationName),
),
stringLiteral('')
);
}
stringLiteral(' '),
logicalExpression(
'||',
memberExpression(
spread.argument,
identifier(destinationName),
),
stringLiteral('')
)
),
);
} else {
result[destinationName] = logicalExpression(
'||',
memberExpression(
spread.argument,
identifier(destinationName),
),
stringLiteral('')
);
}
}
});
}

return result;
};
Expand Down
Expand Up @@ -20,3 +20,7 @@ const rest2 = {};
// Should not do anything
<div {...rest} {...rest2}></div>;
<div {...rest} {...rest2} className="b"></div>;

<div styleName="a">
<div {...rest} activeStyleName="a"></div>
</div>
Expand Up @@ -14,3 +14,6 @@ const rest2 = {};

<div {...rest} {...rest2}></div>;
<div {...rest} {...rest2} className="b"></div>;
<div className="foo__a">
<div {...rest} activeClassName={"foo__a" + (" " + (rest.activeClassName || ""))}></div>
</div>;

0 comments on commit 4a47f35

Please sign in to comment.