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

[New] no-unused-state: Support ignore option #3881

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/rules/no-unused-state.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,16 @@ var UnusedGetInitialStateTest = createReactClass({
}
})
```

## Rule Options

This rule can take one argument to ignore some specific states during validation.

```js
...
"react/no-unused-state": [<enabled>, { ignore: <ignore> }]
...
```

- `enabled`: for enabling the rule. 0=off, 1=warn, 2=error. Defaults to 0.
- `ignore`: optional array of states name to ignore during validation.
28 changes: 26 additions & 2 deletions lib/rules/no-unused-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,34 @@ module.exports = {

messages,

schema: [],
schema: [{
type: 'object',
properties: {
ignore: {
type: 'array',
items: {
type: 'string',
},
uniqueItems: true,
},
},
additionalProperties: false,
}],
},

create(context) {
const defaults = { skipShapeProps: true, customValidators: [], ignore: [] };
const configuration = Object.assign({}, defaults, context.options[0] || {});

/**
* Checks if the state is ignored
* @param {string} name Name of the state to check.
* @returns {boolean} True if the state is ignored, false if not.
*/
function isIgnored(name) {
return configuration.ignore.indexOf(name) !== -1;
}

// Non-null when we are inside a React component ClassDeclaration and we have
// not yet encountered any use of this.state which we have chosen not to
// analyze. If we encounter any such usage (like this.state being spread as
Expand Down Expand Up @@ -223,7 +247,7 @@ module.exports = {
// Report all unused state fields.
classInfo.stateFields.forEach((node) => {
const name = getName(node.key);
if (!classInfo.usedStateFields.has(name)) {
if (!classInfo.usedStateFields.has(name) && !isIgnored(name)) {
report(context, messages.unusedStateField, 'unusedStateField', {
node,
data: {
Expand Down
108 changes: 108 additions & 0 deletions tests/lib/rules/no-unused-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,58 @@ eslintTester.run('no-unused-state', rule, {
}
`,
features: ['types', 'class fields'],
},
{
code: `
var UnusedGetInitialStateTest = createReactClass({
getInitialState: function() {
return { foo: 0 };
},
render: function() {
return <SomeComponent />;
}
})
`,
options: [{ ignore: ['foo'] }],
},
{
code: `
var UnusedComputedStringLiteralKeyStateTest = createReactClass({
getInitialState: function() {
return { ['foo']: 0 };
},
render: function() {
return <SomeComponent />;
}
})
`,
options: [{ ignore: ['foo'] }],
},
{
code: `
class UnusedCtorStateTest extends React.Component {
constructor() {
this.state = { foo: 0 };
}
render() {
return <SomeComponent />;
}
}
`,
options: [{ ignore: ['foo'] }],
},
{
code: `
class UnusedCtorStateTest extends React.Component {
constructor() {
this.state = { ['foo']: 0 };
}
render() {
return <SomeComponent />;
}
}
`,
options: [{ ignore: ['foo'] }],
}
)),

Expand Down Expand Up @@ -1616,5 +1668,61 @@ eslintTester.run('no-unused-state', rule, {
'thisDestructStateDestructPropUnused',
]),
},
{
code: `
var UnusedGetInitialStateTest = createReactClass({
getInitialState: function() {
return { foo: 0, dummy: 0 };
},
render: function() {
return <SomeComponent />;
}
})
`,
options: [{ ignore: ['foo'] }],
errors: getErrorMessages(['dummy']),
},
{
code: `
var UnusedGetInitialStateTest = createReactClass({
getInitialState: function() {
return { ['foo']: 0, ['dummy']: 0 };
},
render: function() {
return <SomeComponent />;
}
})
`,
options: [{ ignore: ['foo'] }],
errors: getErrorMessages(['dummy']),
},
{
code: `
class UnusedCtorStateTest extends React.Component {
constructor() {
this.state = { foo: 0, dummy: 0 };
}
render() {
return <SomeComponent />;
}
}
`,
options: [{ ignore: ['foo'] }],
errors: getErrorMessages(['dummy']),
},
{
code: `
class UnusedCtorStateTest extends React.Component {
constructor() {
this.state = { ['foo']: 0, ['dummy']: 0 };
}
render() {
return <SomeComponent />;
}
}
`,
options: [{ ignore: ['foo'] }],
errors: getErrorMessages(['dummy']),
},
]),
});
Loading