-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This will check for `shouldComponentUpdate` defined in a class that extends React.PureComponent.
- Loading branch information
Showing
5 changed files
with
289 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Prevent usage of shouldComponentUpdate when extending React.PureComponent (react/no-useless-scu) | ||
|
||
Warns if you have `shouldComponentUpdate` defined when defining a component that extends React.PureComponent. | ||
While having `shouldComponentUpdate` will still work, it becomes pointless to extend PureComponent. | ||
|
||
## Rule Details | ||
|
||
The following patterns are considered warnings: | ||
|
||
```jsx | ||
class Foo extends React.PureComponent { | ||
shouldComponentUpdate() { | ||
return true; | ||
} | ||
|
||
render() { | ||
return <div>Radical!</div> | ||
} | ||
} | ||
|
||
function Bar() { | ||
return class Baz extends React.PureComponent { | ||
shouldComponentUpdate() { | ||
return true; | ||
} | ||
|
||
render() { | ||
return <div>Groovy!</div> | ||
} | ||
} | ||
} | ||
``` | ||
|
||
The following patterns are not considered warnings: | ||
|
||
```jsx | ||
class Foo extends React.Component { | ||
shouldComponentUpdate() { | ||
return true; | ||
} | ||
|
||
render() { | ||
return <div>Radical!</div> | ||
} | ||
} | ||
|
||
function Bar() { | ||
return class Baz extends React.Component { | ||
shouldComponentUpdate() { | ||
return true; | ||
} | ||
|
||
render() { | ||
return <div>Groovy!</div> | ||
} | ||
} | ||
} | ||
|
||
class Qux extends React.PureComponent { | ||
render() { | ||
return <div>Tubular!</div> | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* @fileoverview Flag shouldComponentUpdate when extending PureComponent | ||
*/ | ||
'use strict'; | ||
|
||
var Components = require('../util/Components'); | ||
|
||
function errorMessage(node) { | ||
return node + ' does not need shouldComponentUpdate when extending React.PureComponent.'; | ||
} | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Flag shouldComponentUpdate when extending PureComponent', | ||
category: 'Possible Errors', | ||
recommended: false | ||
}, | ||
schema: [] | ||
}, | ||
|
||
create: Components.detect(function(context, components, utils) { | ||
|
||
/** | ||
* Get properties name | ||
* @param {Object} node - Property. | ||
* @returns {String} Property name. | ||
*/ | ||
function getPropertyName(node) { | ||
// Special case for class properties | ||
// (babel-eslint does not expose property name so we have to rely on tokens) | ||
if (node.type === 'ClassProperty') { | ||
var tokens = context.getFirstTokens(node, 2); | ||
return tokens[1] && tokens[1].type === 'Identifier' ? tokens[1].value : tokens[0].value; | ||
} | ||
|
||
return node.key.name; | ||
} | ||
|
||
/** | ||
* Get properties for a given AST node | ||
* @param {ASTNode} node The AST node being checked. | ||
* @returns {Array} Properties array. | ||
*/ | ||
function getComponentProperties(node) { | ||
switch (node.type) { | ||
case 'ClassExpression': | ||
case 'ClassDeclaration': | ||
return node.body.body; | ||
default: | ||
return []; | ||
} | ||
} | ||
|
||
/** | ||
* Checks for shouldComponentUpdate property | ||
* @param {ASTNode} node The AST node being checked. | ||
* @returns {Boolean} Whether or not the property exists. | ||
*/ | ||
function hasShouldComponentUpdate(node) { | ||
var properties = getComponentProperties(node); | ||
return properties.some(function(property) { | ||
var name = getPropertyName(property); | ||
return name === 'shouldComponentUpdate'; | ||
}); | ||
} | ||
|
||
/** | ||
* Checks for violation of rule | ||
* @param {ASTNode} node The AST node being checked. | ||
*/ | ||
function checkForViolation(node) { | ||
if (utils.isPureComponent(node)) { | ||
var hasScu = hasShouldComponentUpdate(node); | ||
if (hasScu) { | ||
var className = node.id.name; | ||
context.report({ | ||
node: node, | ||
message: errorMessage(className) | ||
}); | ||
} | ||
} | ||
} | ||
|
||
return { | ||
ClassDeclaration: checkForViolation, | ||
ClassExpression: checkForViolation | ||
}; | ||
}) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/** | ||
* @fileoverview Tests for no-useless-scu | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Requirements | ||
// ----------------------------------------------------------------------------- | ||
|
||
var rule = require('../../../lib/rules/no-useless-scu'); | ||
var RuleTester = require('eslint').RuleTester; | ||
|
||
var parserOptions = { | ||
ecmaVersion: 6, | ||
ecmaFeatures: { | ||
experimentalObjectRestSpread: true, | ||
jsx: true | ||
} | ||
}; | ||
|
||
function errorMessage(node) { | ||
return node + ' does not need shouldComponentUpdate when extending React.PureComponent.'; | ||
} | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Tests | ||
// ----------------------------------------------------------------------------- | ||
|
||
var ruleTester = new RuleTester(); | ||
ruleTester.run('no-useless-scu', rule, { | ||
valid: [ | ||
{ | ||
code: [ | ||
'class Foo extends React.Component {', | ||
' shouldComponentUpdate() {', | ||
' return true;', | ||
' }', | ||
'}' | ||
].join('\n'), | ||
parserOptions: parserOptions | ||
}, | ||
{ | ||
code: [ | ||
'class Foo extends React.Component {', | ||
' shouldComponentUpdate() {', | ||
' return true;', | ||
' }', | ||
'}' | ||
].join('\n'), | ||
parserOptions: parserOptions | ||
}, | ||
{ | ||
code: [ | ||
'function Foo() {', | ||
' return class Bar extends React.Component {', | ||
' shouldComponentUpdate() {', | ||
' return true;', | ||
' }', | ||
' };', | ||
'}' | ||
].join('\n'), | ||
parserOptions: parserOptions | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
code: [ | ||
'class Foo extends React.PureComponent {', | ||
' shouldComponentUpdate() {', | ||
' return true;', | ||
' }', | ||
'}' | ||
].join('\n'), | ||
errors: [{message: errorMessage('Foo')}], | ||
parserOptions: parserOptions | ||
}, | ||
{ | ||
code: [ | ||
'class Foo extends PureComponent {', | ||
' shouldComponentUpdate() {', | ||
' return true;', | ||
' }', | ||
'}' | ||
].join('\n'), | ||
errors: [{message: errorMessage('Foo')}], | ||
parserOptions: parserOptions | ||
}, | ||
{ | ||
code: [ | ||
'class Foo extends React.PureComponent {', | ||
' shouldComponentUpdate = () => {', | ||
' return true;', | ||
' }', | ||
'}' | ||
].join('\n'), | ||
errors: [{message: errorMessage('Foo')}], | ||
parser: 'babel-eslint', | ||
parserOptions: parserOptions | ||
}, | ||
{ | ||
code: [ | ||
'function Foo() {', | ||
' return class Bar extends React.PureComponent {', | ||
' shouldComponentUpdate() {', | ||
' return true;', | ||
' }', | ||
' };', | ||
'}' | ||
].join('\n'), | ||
errors: [{message: errorMessage('Bar')}], | ||
parserOptions: parserOptions | ||
}, | ||
{ | ||
code: [ | ||
'function Foo() {', | ||
' return class Bar extends PureComponent {', | ||
' shouldComponentUpdate() {', | ||
' return true;', | ||
' }', | ||
' };', | ||
'}' | ||
].join('\n'), | ||
errors: [{message: errorMessage('Bar')}], | ||
parserOptions: parserOptions | ||
} | ||
] | ||
}); |