Skip to content

Commit e98586f

Browse files
dagjaneirowyze
authored andcommitted
Ignore JSX Expression blocks (#7)
We should ignore JSX elements rendered inside expression blocks since they do not represent individual components.
1 parent 12dbbf9 commit e98586f

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,15 @@ export default ({ types: t }: { types: BabelTypes }): Visitor => ({
7979
}
8080

8181
const variable = path.find(( node: NodePath ): NodePath =>
82-
node.isVariableDeclarator() || node.isExportDefaultDeclaration(),
82+
node.isVariableDeclarator() || node.isExportDefaultDeclaration() ||
83+
node.isJSXExpressionContainer(),
8384
)
8485

86+
// Ignore JSX elements inside JSX expression blocks
87+
if ( t.isJSXExpressionContainer(variable) ) {
88+
return
89+
}
90+
8591
// Ignore the `if` since I can't come up with a test case to satisfy it.
8692
/* istanbul ignore if */
8793
if ( !variable ) {

test/__snapshots__/index.js.snap

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,43 @@ exports.default = Hello;
139139
Hello.displayName = \'Hello\';"
140140
`;
141141
142+
exports[`ignores jsx expression blocks 1`] = `
143+
"'use strict';
144+
145+
Object.defineProperty(exports, "__esModule", {
146+
value: true
147+
});
148+
149+
var _react = require('react');
150+
151+
var _react2 = _interopRequireDefault(_react);
152+
153+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
154+
155+
var HelloWorld = function HelloWorld() {
156+
var content = _react2.default.createElement(
157+
'div',
158+
null,
159+
[1, 2, 3].map(function (n) {
160+
return _react2.default.createElement(
161+
'h1',
162+
null,
163+
n
164+
);
165+
})
166+
);
167+
168+
return _react2.default.createElement(
169+
'div',
170+
null,
171+
content
172+
);
173+
};
174+
175+
HelloWorld.displayName = 'HelloWorld';
176+
exports.default = HelloWorld;"
177+
`;
178+
142179
exports[`ignores when displayName is already set on default export function 1`] = `
143180
"\'use strict\';
144181
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React from 'react'
2+
3+
const HelloWorld = () => {
4+
const content = (
5+
<div>
6+
{
7+
[1, 2, 3].map((n) => { return <h1>{n}</h1> })
8+
}
9+
</div>
10+
)
11+
12+
return <div>{content}</div>
13+
}
14+
15+
export default HelloWorld

test/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,9 @@ test(
156156
snapshotMacro,
157157
'named-export-block',
158158
)
159+
160+
test(
161+
'ignores jsx expression blocks',
162+
snapshotMacro,
163+
'ignore-jsx-expression-blocks',
164+
)

0 commit comments

Comments
 (0)