Skip to content

Commit

Permalink
[New] no-did-mount-set-state, no-did-update-set-state: no-op with…
Browse files Browse the repository at this point in the history
… react >= 16.3

Fixes jsx-eslint#1754
  • Loading branch information
ljharb committed Feb 5, 2022
1 parent 1933ac5 commit bd83ea6
Show file tree
Hide file tree
Showing 4 changed files with 451 additions and 409 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
* [`jsx-no-target-blank`]: Improve fixer with option `allowReferrer` ([#3167][] @apepper)
* [`jsx-curly-brace-presence`]: add "propElementValues" config option ([#3191][] @ljharb)
* add [`iframe-missing-sandbox`] rule ([#2753][] @tosmolka @ljharb)
* [`no-did-mount-set-state`], [`no-did-update-set-state`]: no-op with react >= 16.3 ([#1754][] @ljharb)

### Fixed
* [`prop-types`], `propTypes`: add support for exported type inference ([#3163][] @vedadeepta)
Expand Down Expand Up @@ -42,6 +43,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
[#3133]: https://github.com/yannickcr/eslint-plugin-react/pull/3133
[#2921]: https://github.com/yannickcr/eslint-plugin-react/pull/2921
[#2753]: https://github.com/yannickcr/eslint-plugin-react/pull/2753
[#1754]: https://github.com/yannickcr/eslint-plugin-react/issues/1754
[#1046]: https://github.com/yannickcr/eslint-plugin-react/issues/1046
[#620]: https://github.com/yannickcr/eslint-plugin-react/pull/620
[#519]: https://github.com/yannickcr/eslint-plugin-react/issues/519
Expand Down
22 changes: 18 additions & 4 deletions lib/util/makeNoMethodSetStateRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

const docsUrl = require('./docsUrl');
const report = require('./report');
const testReactVersion = require('./version').testReactVersion;

// ------------------------------------------------------------------------------
// Rule Definition
Expand All @@ -29,7 +30,18 @@ const messages = {
noSetState: 'Do not use setState in {{name}}',
};

function makeNoMethodSetStateRule(methodName, shouldCheckUnsafeCb) {
const methodNoopsAsOf = {
componentDidMount: '>= 16.3.0',
componentDidUpdate: '>= 16.3.0',
};

function shouldBeNoop(context, methodName) {
return methodName in methodNoopsAsOf
&& testReactVersion(context, methodNoopsAsOf[methodName])
&& !testReactVersion(context, '999.999.999'); // for when the version is not specified
}

module.exports = function makeNoMethodSetStateRule(methodName, shouldCheckUnsafeCb) {
return {
meta: {
docs: {
Expand Down Expand Up @@ -67,6 +79,10 @@ function makeNoMethodSetStateRule(methodName, shouldCheckUnsafeCb) {

return {
CallExpression(node) {
if (shouldBeNoop(context, methodName)) {
return;
}

const callee = node.callee;
if (
callee.type !== 'MemberExpression'
Expand Down Expand Up @@ -100,6 +116,4 @@ function makeNoMethodSetStateRule(methodName, shouldCheckUnsafeCb) {
};
},
};
}

module.exports = makeNoMethodSetStateRule;
};
Loading

0 comments on commit bd83ea6

Please sign in to comment.