Skip to content

Commit 4a47f35

Browse files
AlbertLuciantogajus
authored andcommitted
fix: spread should not traverse children elements (#245)
1 parent 05c2683 commit 4a47f35

File tree

3 files changed

+42
-33
lines changed

3 files changed

+42
-33
lines changed

src/createSpreadMapper.js

+35-33
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
binaryExpression,
77
stringLiteral,
88
logicalExpression,
9-
identifier
9+
identifier,
10+
isJSXSpreadAttribute
1011
} from '@babel/types';
1112
import optionsDefaults from './schemas/optionsDefaults';
1213

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

32-
path.traverse({
33-
JSXSpreadAttribute (spreadPath: *) {
34-
const spread = spreadPath.node;
33+
const spreadAttributes = path.node.openingElement.attributes
34+
.filter((attr) => {
35+
return isJSXSpreadAttribute(attr);
36+
});
3537

36-
for (const attributeKey of attributeKeys) {
37-
const destinationName = attributeNames[attributeKey];
38+
for (const spread of spreadAttributes) {
39+
for (const attributeKey of attributeKeys) {
40+
const destinationName = attributeNames[attributeKey];
3841

39-
if (result[destinationName]) {
40-
result[destinationName] = binaryExpression(
42+
if (result[destinationName]) {
43+
result[destinationName] = binaryExpression(
44+
'+',
45+
result[destinationName],
46+
binaryExpression(
4147
'+',
42-
result[destinationName],
43-
binaryExpression(
44-
'+',
45-
stringLiteral(' '),
46-
logicalExpression(
47-
'||',
48-
memberExpression(
49-
spread.argument,
50-
identifier(destinationName),
51-
),
52-
stringLiteral('')
53-
)
54-
),
55-
);
56-
} else {
57-
result[destinationName] = logicalExpression(
58-
'||',
59-
memberExpression(
60-
spread.argument,
61-
identifier(destinationName),
62-
),
63-
stringLiteral('')
64-
);
65-
}
48+
stringLiteral(' '),
49+
logicalExpression(
50+
'||',
51+
memberExpression(
52+
spread.argument,
53+
identifier(destinationName),
54+
),
55+
stringLiteral('')
56+
)
57+
),
58+
);
59+
} else {
60+
result[destinationName] = logicalExpression(
61+
'||',
62+
memberExpression(
63+
spread.argument,
64+
identifier(destinationName),
65+
),
66+
stringLiteral('')
67+
);
6668
}
6769
}
68-
});
70+
}
6971

7072
return result;
7173
};

test/fixtures/react-css-modules/handle spread attributes/input.js

+4
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@ const rest2 = {};
2020
// Should not do anything
2121
<div {...rest} {...rest2}></div>;
2222
<div {...rest} {...rest2} className="b"></div>;
23+
24+
<div styleName="a">
25+
<div {...rest} activeStyleName="a"></div>
26+
</div>

test/fixtures/react-css-modules/handle spread attributes/output.js

+3
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ const rest2 = {};
1414

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

0 commit comments

Comments
 (0)