Skip to content

Commit 1a1e307

Browse files
author
Supriya S
committed
jsx resolution when using var strings
1 parent 1b9f7eb commit 1a1e307

File tree

5 files changed

+13
-6
lines changed

5 files changed

+13
-6
lines changed

.flowconfig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
[ignore]
22
<PROJECT_ROOT>/node_modules/config-chain/test/broken.json
3-
<PROJECT_ROOT>/npm/node_modules/config-chain/test/broken.json
43
<PROJECT_ROOT>/node_modules/conventional-changelog-core/test/fixtures/_malformation.json
54
<PROJECT_ROOT>/node_modules/npmconf/test/fixtures/package.json
65

demo/src/components/JSXExpressionStyleResolution.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import classnames from 'classnames';
33
import './table.css';
44

55
export default () => {
6-
var cname = classnames({'row': true});
6+
var cname;
7+
cname = classnames({'row': true});
78

8-
return <div styleName={classnames('table')}>
9+
return <div className="kjdhf" styleName={classnames('table')}>
910
<div styleName={cname}>
1011
<div styleName='cell'>A2</div>
1112
<div styleName='cell'>B2</div>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"scripts": {
5555
"build": "rm -fr ./dist && NODE_ENV=production babel ./src --out-dir ./dist --source-maps --copy-files && npm run build-helper",
5656
"build-helper": "mkdir -p ./dist/browser && NODE_ENV=production babel ./src/getClassName.js --out-file ./dist/browser/getClassName.js --source-maps --no-babelrc --plugins transform-es2015-modules-commonjs,transform-flow-strip-types --presets es2015",
57-
"lint": "eslint ./src --fix && flow",
57+
"lint": "eslint ./src && flow",
5858
"precommit": "npm run test && npm run lint",
5959
"test": " NODE_ENV=test mocha --require babel-core/register"
6060
},

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ export default ({
214214
if (t.isCallExpression(attribute.value.expression) || t.isIdentifier(attribute.value.expression)) {
215215
resolveJsxExpression(
216216
path,
217+
stats,
217218
filenameMap[filename].styleModuleImportMap,
218219
attribute,
219220
destinationName,

src/resolveJSXExpression.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ type OptionsType = {|
2323
*/
2424
export default (
2525
path: *,
26+
stats: *,
2627
styleModuleImportMap: StyleModuleImportMapType,
2728
sourceAttribute: JSXAttribute,
2829
destinationName: string,
2930
options: OptionsType): void => {
31+
3032
const jsxExpression = sourceAttribute.value.expression;
3133
const replaceCallArguments = function (callExpressionArguments) {
3234
for (const argument of callExpressionArguments) {
@@ -46,10 +48,14 @@ export default (
4648
replaceCallArguments(sourceAttribute.value.expression.arguments);
4749
} else if (isIdentifier(jsxExpression)) {
4850
const variableDeclaration = path.scope.getBinding(jsxExpression.name).path.node;
49-
51+
5052
if (isVariableDeclarator(variableDeclaration)) {
5153
if (isCallExpression(variableDeclaration.init)) {
5254
replaceCallArguments(variableDeclaration.init.arguments);
55+
} else if(isStringLiteral(variableDeclaration.init)) {
56+
variableDeclaration.init.value = getClassName(variableDeclaration.init.value, styleModuleImportMap, options);
57+
} else {
58+
throw new Error(`When using variable references for attribute values, make sure that these are declared and initialized with either a string or a function call. Variable assignments for such identifiers are not supported as of now!!Check line ${variableDeclaration.loc.start.line} in ${stats.file.opts.filename}`);
5359
}
5460
}
5561
}
@@ -61,7 +67,7 @@ export default (
6167

6268
// the desination attribute cannot be already present on the Jsx
6369
if (destinationAttribute) {
64-
throw new Error('Destination Attribute cannot be present on JSX Element when using JSX Expressions');
70+
throw new Error(`${destinationName} cannot be already present on a JSX Element when using JSX Expressions. Check line ${destinationAttribute.loc.start.line} in ${stats.file.opts.filename}`);
6571
} else {
6672
sourceAttribute.name.name = destinationName;
6773
}

0 commit comments

Comments
 (0)