Skip to content

Commit 7b625f0

Browse files
committed
Fix sort-comp crash on stateless components (fixes #285)
1 parent fc4173d commit 7b625f0

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

lib/rules/sort-comp.js

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,6 @@ module.exports = Components.detect(function(context, components) {
7575
}
7676
}, context.options[0]);
7777

78-
/**
79-
* Checks if the component must be validated
80-
* @param {Object} component The component to process
81-
* @returns {Boolean} True if the component must be validated, false if not.
82-
*/
83-
function mustBeValidated(component) {
84-
return (
85-
component &&
86-
!component.hasDisplayName
87-
);
88-
}
89-
9078
// --------------------------------------------------------------------------
9179
// Public
9280
// --------------------------------------------------------------------------
@@ -236,10 +224,14 @@ module.exports = Components.detect(function(context, components) {
236224
* @returns {Array} Properties array.
237225
*/
238226
function getComponentProperties(node) {
239-
if (node.type === 'ClassDeclaration') {
240-
return node.body.body;
227+
switch (node.type) {
228+
case 'ClassDeclaration':
229+
return node.body.body;
230+
case 'ObjectExpression':
231+
return node.properties;
232+
default:
233+
return [];
241234
}
242-
return node.properties;
243235
}
244236

245237
/**
@@ -346,7 +338,7 @@ module.exports = Components.detect(function(context, components) {
346338
'Program:exit': function() {
347339
var list = components.list();
348340
for (var component in list) {
349-
if (!list.hasOwnProperty(component) || !mustBeValidated(list[component])) {
341+
if (!list.hasOwnProperty(component)) {
350342
continue;
351343
}
352344
var properties = getComponentProperties(list[component].node);

tests/lib/rules/sort-comp.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,22 @@ ruleTester.run('sort-comp', rule, {
189189
classes: true,
190190
jsx: true
191191
}
192+
}, {
193+
// Must ignore stateless components
194+
code: [
195+
'function Hello(props) {',
196+
' return <div>Hello {props.name}</div>',
197+
'}'
198+
].join('\n'),
199+
parser: 'babel-eslint'
200+
}, {
201+
// Must ignore stateless components (arrow function with explicit return)
202+
code: [
203+
'var Hello = props => (',
204+
' <div>Hello {props.name}</div>',
205+
')'
206+
].join('\n'),
207+
parser: 'babel-eslint'
192208
}],
193209

194210
invalid: [{

0 commit comments

Comments
 (0)