Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add state-in-constructor rule #1945

Merged
merged 9 commits into from
Jan 21, 2019
Next Next commit
Init rule state-in-constructor
  • Loading branch information
lukyth committed Aug 18, 2018
commit a312b16ea41203fc02054308f590f0e65784e2c3
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const allRules = {
'self-closing-comp': require('./lib/rules/self-closing-comp'),
'sort-comp': require('./lib/rules/sort-comp'),
'sort-prop-types': require('./lib/rules/sort-prop-types'),
'state-in-constructor': require('./lib/rules/state-in-constructor'),
'style-prop-object': require('./lib/rules/style-prop-object'),
'void-dom-elements-no-children': require('./lib/rules/void-dom-elements-no-children')
};
Expand Down
39 changes: 39 additions & 0 deletions lib/rules/state-in-constructor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @fileoverview State initialization in an ES6 class component should be in a constructor
* @author Kanitkorn Sujautra
*/
'use strict';

const Components = require('../util/Components');
const docsUrl = require('../util/docsUrl');

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

module.exports = {
meta: {
docs: {
description: 'State initialization in an ES6 class component should be in a constructor',
category: 'Stylistic Issues',
recommended: false,
url: docsUrl('state-in-constructor')
},
schema: []
},

create: Components.detect((context, components, utils) => ({
ClassProperty: function (node) {
if (
!node.static &&
node.key.name === 'state' &&
utils.isES6Component(node.parent.parent)
) {
context.report({
node,
message: 'State initialization should be in a constructor'
});
}
}
}))
};
155 changes: 155 additions & 0 deletions tests/lib/rules/state-in-constructor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/**
* @fileoverview State initialization in an ES6 class component should be in a constructor
* @author Kanitkorn Sujautra
*/
'use strict';

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

const rule = require('../../../lib/rules/state-in-constructor');
const RuleTester = require('eslint').RuleTester;

const ruleTesterConfig = {
parser: 'babel-eslint',
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
jsx: true
}
}
};

// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------

const ruleTester = new RuleTester(ruleTesterConfig);
ruleTester.run('state-in-constructor', rule, {
valid: [{
code: `
class Foo extends React.Component {
render() {
return <div>Foo</div>
}
}
`
}, {
code: `
class Foo extends React.Component {
constructor(props) {
super(props)
this.state = { bar: 0 }
}
render() {
return <div>Foo</div>
}
}
`
}, {
code: `
class Foo extends React.Component {
constructor(props) {
super(props)
this.state = { bar: 0 }
}
baz = { bar: 0 }
render() {
return <div>Foo</div>
}
}
`
}, {
code: `
class Foo extends React.Component {
constructor(props) {
super(props)
this.baz = { bar: 0 }
}
render() {
return <div>Foo</div>
}
}
`
}, {
code: `
class Foo extends React.Component {
baz = { bar: 0 }
render() {
return <div>Foo</div>
}
}
`
}, {
code: `
const Foo = () => <div>Foo</div>
`
}, {
code: `
function Foo () {
return <div>Foo</div>
}
`
}],

invalid: [{
code: `
class Foo extends React.Component {
state = { bar: 0 }
render() {
return <div>Foo</div>
}
}
`,
errors: [{
message: 'State initialization should be in a constructor'
}]
}, {
code: `
class Foo extends React.Component {
state = { bar: 0 }
baz = { bar: 0 }
render() {
return <div>Foo</div>
}
}
`,
errors: [{
message: 'State initialization should be in a constructor'
}]
}, {
code: `
class Foo extends React.Component {
constructor(props) {
super(props)
this.baz = { bar: 0 }
}
state = { baz: 0 }
render() {
return <div>Foo</div>
}
}
`,
errors: [{
message: 'State initialization should be in a constructor'
}]
}, {
code: `
class Foo extends React.Component {
constructor(props) {
super(props)
this.state = { bar: 0 }
}
state = { baz: 0 }
render() {
return <div>Foo</div>
}
}
`,
errors: [{
message: 'State initialization should be in a constructor'
}]
}]
});