Skip to content

Commit

Permalink
Merge pull request #1411 from jackyho112/make-no-unused-state-rule-ha…
Browse files Browse the repository at this point in the history
…ndle-arrow-function-class-method

[no-unused-state] Add ability to handle arrow function class method
  • Loading branch information
ljharb authored Oct 18, 2017
2 parents 773e0fe + e616d7f commit 952da85
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/rules/no-unused-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,27 @@ module.exports = {
) {
addStateFields(node.value);
}

if (
!node.static &&
node.value &&
node.value.type === 'ArrowFunctionExpression'
) {
// Create a new set for this.state aliases local to this method.
classInfo.aliases = new Set();
}
},

'ClassProperty:exit'(node) {
if (
classInfo &&
!node.static &&
node.value &&
node.value.type === 'ArrowFunctionExpression'
) {
// Forget our set of local aliases.
classInfo.aliases = null;
}
},

MethodDefinition() {
Expand Down
97 changes: 97 additions & 0 deletions tests/lib/rules/no-unused-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,22 @@ eslintTester.run('no-unused-state', rule, {
return <SomeComponent foo={foo} />;
}
}`,
`class NonRenderClassMethodFalseNegativeTest extends React.Component {
constructor() {
this.state = { foo: 0, bar: 0 };
}
doSomething() {
const { foo } = this.state;
return this.state.foo;
}
doSomethingElse() {
const { state: { bar }} = this;
return bar;
}
render() {
return <SomeComponent />;
}
}`,
{
code: `class TypeCastExpressionSpreadFalseNegativeTest extends React.Component {
constructor() {
Expand All @@ -397,6 +413,72 @@ eslintTester.run('no-unused-state', rule, {
}
}`,
parser: 'babel-eslint'
},
{
code: `class ArrowFunctionClassMethodDestructuringFalseNegativeTest extends React.Component {
constructor() {
this.state = { foo: 0 };
}
doSomething = () => {
const { state: { foo } } = this;
return foo;
}
render() {
return <SomeComponent />;
}
}`,
parser: 'babel-eslint'
},
{
code: `class ArrowFunctionClassMethodWithClassPropertyTransformFalseNegativeTest extends React.Component {
state = { foo: 0 };
doSomething = () => {
const { state:{ foo } } = this;
return foo;
}
render() {
return <SomeComponent />;
}
}`,
parser: 'babel-eslint'
},
{
code: `class ArrowFunctionClassMethodDeepDestructuringFalseNegativeTest extends React.Component {
state = { foo: { bar: 0 } };
doSomething = () => {
const { state: { foo: { bar }}} = this;
return bar;
}
render() {
return <SomeComponent />;
}
}`,
parser: 'babel-eslint'
},
{
code: `class ArrowFunctionClassMethodDestructuringAssignmentFalseNegativeTest extends React.Component {
state = { foo: 0 };
doSomething = () => {
const { state: { foo: bar }} = this;
return bar;
}
render() {
return <SomeComponent />;
}
}`,
parser: 'babel-eslint'
}
],

Expand Down Expand Up @@ -642,6 +724,21 @@ eslintTester.run('no-unused-state', rule, {
}`,
errors: getErrorMessages(['foo'])
},
{
code: `class UnusedStateArrowFunctionMethodTest extends React.Component {
constructor() {
this.state = { foo: 0 };
}
doSomething = () => {
return null;
}
render() {
return <SomeComponent />;
}
}`,
errors: getErrorMessages(['foo']),
parser: 'babel-eslint'
},
{
code: `class TypeCastExpressionTest extends React.Component {
constructor() {
Expand Down

0 comments on commit 952da85

Please sign in to comment.