Skip to content

Commit c943de3

Browse files
PCreationsgajus
authored andcommitted
fix: generate className when using runtime resolution (#18) (#19)
* fix: added escape hatch when testing attribute that do not have a name property (#11) * fix: object expression generated on dynamic resolution now use quote to escape properties' name (#14) * fix: getClassName now works as expected when multiple file use runtime resolution * fix: getClassName now works as expected when multiple file use runtime resolution * fix: className attribute is now correctly generated when using runtime resolution (#18)
1 parent 4ab70fb commit c943de3

File tree

8 files changed

+31
-13
lines changed

8 files changed

+31
-13
lines changed

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ export default ({
114114
}
115115
replaceJsxExpressionContainer(
116116
t,
117+
path,
117118
styleNameAttribute,
118119
filenameMap[filename].importedHelperIndentifier,
119120
filenameMap[filename].styleModuleImportMapIdentifier

src/replaceJsxExpressionContainer.js

+25-8
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,33 @@ import BabelTypes, {
55
Identifier
66
} from 'babel-types';
77

8-
export default (t: BabelTypes, styleNameAttribute: JSXAttribute, importedHelperIndentifier: Identifier, styleModuleImportMapIdentifier: Identifier): void => {
8+
export default (t: BabelTypes, path: Object, styleNameAttribute: JSXAttribute, importedHelperIndentifier: Identifier, styleModuleImportMapIdentifier: Identifier): void => {
99
const expressionContainerValue = styleNameAttribute.value;
10+
const classNameAttribute = path.node.openingElement.attributes
11+
.find((attribute) => {
12+
return typeof attribute.name !== 'undefined' && attribute.name.name === 'className';
13+
});
14+
const classNameAttributeValue = classNameAttribute ? classNameAttribute.value.value : '';
15+
16+
if (classNameAttribute) {
17+
path.node.openingElement.attributes.splice(path.node.openingElement.attributes.indexOf(classNameAttribute), 1);
18+
}
19+
20+
const styleNameExpression = t.callExpression(
21+
importedHelperIndentifier,
22+
[
23+
expressionContainerValue.expression,
24+
styleModuleImportMapIdentifier
25+
]
26+
);
1027

1128
styleNameAttribute.value = t.jSXExpressionContainer(
12-
t.callExpression(
13-
importedHelperIndentifier,
14-
[
15-
expressionContainerValue.expression,
16-
styleModuleImportMapIdentifier
17-
]
18-
)
29+
classNameAttribute ?
30+
t.binaryExpression(
31+
'+',
32+
t.stringLiteral(classNameAttributeValue + ' '),
33+
styleNameExpression
34+
) : styleNameExpression
1935
);
36+
styleNameAttribute.name.name = 'className';
2037
};

test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName bis/actual.js renamed to test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName (with already existing className)/actual.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ const styleNameBar = 'bar.a-b';
55
const styleNameFoo = 'a-b';
66

77
<div styleName={styleNameBar}></div>;
8-
<div styleName={styleNameFoo}></div>;
8+
<div className='global' styleName={styleNameFoo}></div>;

test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName bis/expected.js renamed to test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName (with already existing className)/expected.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ const _styleModuleImportMap = {
1313
const styleNameBar = 'bar.a-b';
1414
const styleNameFoo = 'a-b';
1515

16-
<div styleName={_getClassName(styleNameBar, _styleModuleImportMap)}></div>;
17-
<div styleName={_getClassName(styleNameFoo, _styleModuleImportMap)}></div>;
16+
<div className={_getClassName(styleNameBar, _styleModuleImportMap)}></div>;
17+
<div className={'global ' + _getClassName(styleNameFoo, _styleModuleImportMap)}></div>;

test/fixtures/react-css-modules/uses getClassName to resolve non-literal styleName/expected.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ const _styleModuleImportMap = {
1313
const styleNameBar = 'bar.a-b';
1414
const styleNameFoo = 'a-b';
1515

16-
<div styleName={_getClassName(styleNameBar, _styleModuleImportMap)}></div>;
17-
<div styleName={_getClassName(styleNameFoo, _styleModuleImportMap)}></div>;
16+
<div className={_getClassName(styleNameBar, _styleModuleImportMap)}></div>;
17+
<div className={_getClassName(styleNameFoo, _styleModuleImportMap)}></div>;

0 commit comments

Comments
 (0)