Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: spread should not traverse children elements #245

Merged
merged 1 commit into from Mar 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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>;