Skip to content

Commit b2fefdc

Browse files
committed
Add instance-methods and instance-variables to sort-comp.
1 parent a120758 commit b2fefdc

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

lib/rules/sort-comp.js

+16
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,20 @@ module.exports = {
171171
}
172172
}
173173

174+
if (indexes.length === 0 && method.instanceVariable) {
175+
const annotationIndex = methodsOrder.indexOf('instance-variables');
176+
if (annotationIndex >= 0) {
177+
indexes.push(annotationIndex);
178+
}
179+
}
180+
181+
if (indexes.length === 0 && method.instanceMethod) {
182+
const annotationIndex = methodsOrder.indexOf('instance-methods');
183+
if (annotationIndex >= 0) {
184+
indexes.push(annotationIndex);
185+
}
186+
}
187+
174188
// No matching pattern, return 'everything-else' index
175189
if (indexes.length === 0) {
176190
for (i = 0, j = methodsOrder.length; i < j; i++) {
@@ -384,6 +398,8 @@ module.exports = {
384398
getter: node.kind === 'get',
385399
setter: node.kind === 'set',
386400
static: node.static,
401+
instanceVariable: node.value && node.value.type !== 'ArrowFunctionExpression' && node.value.type !== 'FunctionExpression',
402+
instanceMethod: node.value && (node.value.type === 'ArrowFunctionExpression' || node.value.type === 'FunctionExpression'),
387403
typeAnnotation: !!node.typeAnnotation && node.value === null
388404
}));
389405

tests/lib/rules/sort-comp.js

+88
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,49 @@ ruleTester.run('sort-comp', rule, {
310310
'render'
311311
]
312312
}]
313+
}, {
314+
// Instance methods should be at the top
315+
code: [
316+
'class Hello extends React.Component {',
317+
' foo = () => {}',
318+
' constructor() {}',
319+
' render() {',
320+
' return <div>{this.props.text}</div>;',
321+
' }',
322+
'}'
323+
].join('\n'),
324+
parser: 'babel-eslint',
325+
options: [{
326+
order: [
327+
'instance-methods',
328+
'static-methods',
329+
'lifecycle',
330+
'everything-else',
331+
'render'
332+
]
333+
}]
334+
}, {
335+
// Instance variables should be at the top
336+
code: [
337+
'class Hello extends React.Component {',
338+
' foo = "bar"',
339+
' constructor() {}',
340+
' state = {}',
341+
' render() {',
342+
' return <div>{this.props.text}</div>;',
343+
' }',
344+
'}'
345+
].join('\n'),
346+
parser: 'babel-eslint',
347+
options: [{
348+
order: [
349+
'instance-variables',
350+
'static-methods',
351+
'lifecycle',
352+
'everything-else',
353+
'render'
354+
]
355+
}]
313356
}],
314357

315358
invalid: [{
@@ -515,5 +558,50 @@ ruleTester.run('sort-comp', rule, {
515558
'render'
516559
]
517560
}]
561+
}, {
562+
// Instance methods should not be at the top
563+
code: [
564+
'class Hello extends React.Component {',
565+
' constructor() {}',
566+
' foo = () => {}',
567+
' render() {',
568+
' return <div>{this.props.text}</div>;',
569+
' }',
570+
'}'
571+
].join('\n'),
572+
parser: 'babel-eslint',
573+
errors: [{message: 'constructor should be placed after foo'}],
574+
options: [{
575+
order: [
576+
'instance-methods',
577+
'static-methods',
578+
'lifecycle',
579+
'everything-else',
580+
'render'
581+
]
582+
}]
583+
}, {
584+
// Instance variables should not be at the top
585+
code: [
586+
'class Hello extends React.Component {',
587+
' constructor() {}',
588+
' state = {}',
589+
' foo = "bar"',
590+
' render() {',
591+
' return <div>{this.props.text}</div>;',
592+
' }',
593+
'}'
594+
].join('\n'),
595+
parser: 'babel-eslint',
596+
errors: [{message: 'foo should be placed before constructor'}],
597+
options: [{
598+
order: [
599+
'instance-variables',
600+
'static-methods',
601+
'lifecycle',
602+
'everything-else',
603+
'render'
604+
]
605+
}]
518606
}]
519607
});

0 commit comments

Comments
 (0)