Skip to content

Commit

Permalink
Add instance-methods and instance-variables to sort-comp.
Browse files Browse the repository at this point in the history
  • Loading branch information
RDGthree committed Oct 10, 2017
1 parent a120758 commit b2fefdc
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/rules/sort-comp.js
Expand Up @@ -171,6 +171,20 @@ module.exports = {
}
}

if (indexes.length === 0 && method.instanceVariable) {
const annotationIndex = methodsOrder.indexOf('instance-variables');
if (annotationIndex >= 0) {
indexes.push(annotationIndex);
}
}

if (indexes.length === 0 && method.instanceMethod) {
const annotationIndex = methodsOrder.indexOf('instance-methods');
if (annotationIndex >= 0) {
indexes.push(annotationIndex);
}
}

// No matching pattern, return 'everything-else' index
if (indexes.length === 0) {
for (i = 0, j = methodsOrder.length; i < j; i++) {
Expand Down Expand Up @@ -384,6 +398,8 @@ module.exports = {
getter: node.kind === 'get',
setter: node.kind === 'set',
static: node.static,
instanceVariable: node.value && node.value.type !== 'ArrowFunctionExpression' && node.value.type !== 'FunctionExpression',
instanceMethod: node.value && (node.value.type === 'ArrowFunctionExpression' || node.value.type === 'FunctionExpression'),
typeAnnotation: !!node.typeAnnotation && node.value === null
}));

Expand Down
88 changes: 88 additions & 0 deletions tests/lib/rules/sort-comp.js
Expand Up @@ -310,6 +310,49 @@ ruleTester.run('sort-comp', rule, {
'render'
]
}]
}, {
// Instance methods should be at the top
code: [
'class Hello extends React.Component {',
' foo = () => {}',
' constructor() {}',
' render() {',
' return <div>{this.props.text}</div>;',
' }',
'}'
].join('\n'),
parser: 'babel-eslint',
options: [{
order: [
'instance-methods',
'static-methods',
'lifecycle',
'everything-else',
'render'
]
}]
}, {
// Instance variables should be at the top
code: [
'class Hello extends React.Component {',
' foo = "bar"',
' constructor() {}',
' state = {}',
' render() {',
' return <div>{this.props.text}</div>;',
' }',
'}'
].join('\n'),
parser: 'babel-eslint',
options: [{
order: [
'instance-variables',
'static-methods',
'lifecycle',
'everything-else',
'render'
]
}]
}],

invalid: [{
Expand Down Expand Up @@ -515,5 +558,50 @@ ruleTester.run('sort-comp', rule, {
'render'
]
}]
}, {
// Instance methods should not be at the top
code: [
'class Hello extends React.Component {',
' constructor() {}',
' foo = () => {}',
' render() {',
' return <div>{this.props.text}</div>;',
' }',
'}'
].join('\n'),
parser: 'babel-eslint',
errors: [{message: 'constructor should be placed after foo'}],
options: [{
order: [
'instance-methods',
'static-methods',
'lifecycle',
'everything-else',
'render'
]
}]
}, {
// Instance variables should not be at the top
code: [
'class Hello extends React.Component {',
' constructor() {}',
' state = {}',
' foo = "bar"',
' render() {',
' return <div>{this.props.text}</div>;',
' }',
'}'
].join('\n'),
parser: 'babel-eslint',
errors: [{message: 'foo should be placed before constructor'}],
options: [{
order: [
'instance-variables',
'static-methods',
'lifecycle',
'everything-else',
'render'
]
}]
}]
});

0 comments on commit b2fefdc

Please sign in to comment.