Skip to content

Commit e875edd

Browse files
graingertwyze
authored andcommitted
Avoid crash on block-arrow components with hoc wrapper (#9)
* add test to check crash on block-arrow components with hoc wrapper * avoid crash on block-arrow components with hoc wrapper
1 parent e98586f commit e875edd

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

src/index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,19 @@ export default ({ types: t }: { types: BabelTypes }): Visitor => ({
8888
return
8989
}
9090

91-
// Ignore the `if` since I can't come up with a test case to satisfy it.
92-
/* istanbul ignore if */
93-
if ( !variable ) {
91+
const name = (() => {
92+
try {
93+
return variable.get('id.name').node
94+
} catch (errr) {
95+
return undefined
96+
}
97+
})()
98+
99+
if ( name == null ) {
94100
return
95101
}
96102

97103
const statement = variable.getStatementParent()
98-
const { node: name } = variable.get('id.name')
99104

100105
// check to make sure we don't set displayName when already set
101106
if ( isDisplayNameSet(statement, name) ) {

test/__snapshots__/index.js.snap

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,26 @@ App.displayName = \'App\';
2525
exports.default = App;"
2626
`;
2727

28+
exports[`does not crash on block-arrow components with hoc wrapper 1`] = `
29+
"\'use strict\';
30+
31+
Object.defineProperty(exports, \"__esModule\", {
32+
value: true
33+
});
34+
35+
var _react = require(\'react\');
36+
37+
var _react2 = _interopRequireDefault(_react);
38+
39+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
40+
41+
exports.default = function () {
42+
return fn(function (survey) {
43+
return _react2.default.createElement(\'div\', null);
44+
});
45+
};"
46+
`;
47+
2848
exports[`ignores default function block when no return statement 1`] = `
2949
"\'use strict\';
3050
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from 'react';
2+
3+
export default () => fn(survey => {
4+
return <div />;
5+
});

test/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,9 @@ test(
162162
snapshotMacro,
163163
'ignore-jsx-expression-blocks',
164164
)
165+
166+
test(
167+
'does not crash on block-arrow components with hoc wrapper',
168+
snapshotMacro,
169+
'export-default-hoc-block-arrow',
170+
)

0 commit comments

Comments
 (0)