Skip to content

Commit

Permalink
[Fix] no-unused-state: handle optional chaining
Browse files Browse the repository at this point in the history
Fixes #2515
  • Loading branch information
golopot authored and ljharb committed Mar 8, 2020
1 parent 9861469 commit da7a045
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/rules/no-unused-state.js
Expand Up @@ -415,7 +415,7 @@ module.exports = {
handleAssignment(node.id, node.init);
},

MemberExpression(node) {
'MemberExpression, OptionalMemberExpression'(node) {
if (!classInfo) {
return;
}
Expand Down
10 changes: 9 additions & 1 deletion tests/helpers/parsers.js
@@ -1,11 +1,19 @@
'use strict';

const path = require('path');
const semver = require('semver');
const version = require('eslint/package.json').version;

const NODE_MODULES = '../../node_modules';

module.exports = {
BABEL_ESLINT: path.join(__dirname, NODE_MODULES, 'babel-eslint'),
TYPESCRIPT_ESLINT: path.join(__dirname, NODE_MODULES, 'typescript-eslint-parser'),
'@TYPESCRIPT_ESLINT': path.join(__dirname, NODE_MODULES, '@typescript-eslint/parser')
'@TYPESCRIPT_ESLINT': path.join(__dirname, NODE_MODULES, '@typescript-eslint/parser'),
TS: function TS(tests) {
if (semver.satisfies(version, '>= 5')) {
return tests;
}
return [];
}
};
13 changes: 2 additions & 11 deletions tests/lib/rules/jsx-no-comment-textnodes.js
Expand Up @@ -9,20 +9,11 @@
// Requirements
// ------------------------------------------------------------------------------

const semver = require('semver');
const version = require('eslint/package.json').version;
const RuleTester = require('eslint').RuleTester;
const rule = require('../../../lib/rules/jsx-no-comment-textnodes');

const parsers = require('../../helpers/parsers');

function TS(tests) {
if (semver.satisfies(version, '>= 5')) {
return tests;
}
return [];
}

const parserOptions = {
ecmaVersion: 2018,
sourceType: 'module',
Expand Down Expand Up @@ -182,7 +173,7 @@ ruleTester.run('jsx-no-comment-textnodes', rule, {
code: '<pre>&#x2F;&#42; TODO: Write perfect code &#42;&#x2F;</pre>',
parser: parsers.BABEL_ESLINT
}
].concat(TS([
].concat(parsers.TS([
{
code: `
class Comp1 extends Component {
Expand Down Expand Up @@ -388,7 +379,7 @@ ruleTester.run('jsx-no-comment-textnodes', rule, {
parser: parsers.BABEL_ESLINT,
errors: [{message: 'Comments inside children section of tag should be placed inside braces'}]
}
].concat(TS([
].concat(parsers.TS([
{
code: `
class Comp1 extends Component {
Expand Down
26 changes: 24 additions & 2 deletions tests/lib/rules/no-unused-state.js
Expand Up @@ -25,7 +25,7 @@ function getErrorMessages(unusedFields) {
}

eslintTester.run('no-unused-state', rule, {
valid: [
valid: [].concat(
`function StatelessFnUnaffectedTest(props) {
return <SomeComponent foo={props.foo} />;
};`,
Expand Down Expand Up @@ -231,6 +231,28 @@ eslintTester.run('no-unused-state', rule, {
}
}`,
parser: parsers.BABEL_ESLINT
}, parsers.TS([{
code: `
class OptionalChaining extends React.Component {
constructor() {
this.state = { foo: 0 };
}
render() {
return <SomeComponent foo={this.state?.foo} />;
}
}`,
parser: parsers['@TYPESCRIPT_ESLINT']
}]), {
code: `
class OptionalChaining extends React.Component {
constructor() {
this.state = { foo: 0 };
}
render() {
return <SomeComponent foo={this.state?.foo} />;
}
}`,
parser: parsers.BABEL_ESLINT
},
`class VariableDeclarationTest extends React.Component {
constructor() {
Expand Down Expand Up @@ -795,7 +817,7 @@ eslintTester.run('no-unused-state', rule, {
`,
parser: parsers.TYPESCRIPT_ESLINT
}
],
),

invalid: [
{
Expand Down

0 comments on commit da7a045

Please sign in to comment.