Skip to content

Commit 07b6373

Browse files
authored
Merge pull request #1082 from webOS101/sort-comp-classexpression
Fix sort-comp ClassExpression
2 parents f67f4a9 + f87646f commit 07b6373

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

lib/rules/sort-comp.js

+1
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ module.exports = {
290290
*/
291291
function getComponentProperties(node) {
292292
switch (node.type) {
293+
case 'ClassExpression':
293294
case 'ClassDeclaration':
294295
return node.body.body;
295296
case 'ObjectExpression':

tests/lib/rules/sort-comp.js

+42
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,34 @@ ruleTester.run('sort-comp', rule, {
240240
'render'
241241
]
242242
}]
243+
}, {
244+
// Non-react classes should be ignored, even in expressions
245+
code: [
246+
'return class Hello {',
247+
' render() {',
248+
' return <div>{this.props.text}</div>;',
249+
' }',
250+
' props: { text: string };',
251+
' constructor() {}',
252+
' state: Object = {};',
253+
'}'
254+
].join('\n'),
255+
parser: 'babel-eslint',
256+
parserOptions: parserOptions
257+
}, {
258+
// Non-react classes should be ignored, even in expressions
259+
code: [
260+
'return class {',
261+
' render() {',
262+
' return <div>{this.props.text}</div>;',
263+
' }',
264+
' props: { text: string };',
265+
' constructor() {}',
266+
' state: Object = {};',
267+
'}'
268+
].join('\n'),
269+
parser: 'babel-eslint',
270+
parserOptions: parserOptions
243271
}],
244272

245273
invalid: [{
@@ -275,6 +303,20 @@ ruleTester.run('sort-comp', rule, {
275303
'});'
276304
].join('\n'),
277305
errors: [{message: 'render should be placed after onClick'}]
306+
}, {
307+
// Must force a custom method to be placed before render, even in function
308+
code: [
309+
'var Hello = () => {',
310+
' return class Test extends React.Component {',
311+
' render () {',
312+
' return <div>Hello</div>;',
313+
' }',
314+
' onClick () {}',
315+
' }',
316+
'};'
317+
].join('\n'),
318+
parserOptions: parserOptions,
319+
errors: [{message: 'render should be placed after onClick'}]
278320
}, {
279321
// Must force a custom method to be placed after render if no 'everything-else' group is specified
280322
code: [

0 commit comments

Comments
 (0)