forked from jsx-eslint/eslint-plugin-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
isMounted is an anti-pattern [0], is not available when using ES6 classes, and will eventually be officially deprecated. This commit adds a rule that prevents the use of this method. [0]: https://facebook.github.io/react/blog/2015/12/16/ismounted-antipattern.html Finishes jsx-eslint#37
- Loading branch information
Showing
5 changed files
with
187 additions
and
0 deletions.
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,34 @@ | ||
# Prevent usage of isMounted (no-is-mounted) | ||
|
||
[`isMounted` is an anti-pattern](anti-pattern), is not available when using ES6 classes, and it is on its way to being officially deprecated. | ||
|
||
[anti-pattern]: https://facebook.github.io/react/blog/2015/12/16/ismounted-antipattern.html | ||
|
||
## Rule Details | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
var Hello = React.createClass({ | ||
handleClick: function() { | ||
setTimeout(function() { | ||
if (this.isMounted()) { | ||
return; | ||
} | ||
}); | ||
}, | ||
render: function() { | ||
return <div onClick={this.handleClick.bind(this)}>Hello</div>; | ||
} | ||
}); | ||
``` | ||
|
||
The following patterns are not considered warnings: | ||
|
||
```js | ||
var Hello = React.createClass({ | ||
render: function() { | ||
return <div onClick={this.props.handleClick}>Hello</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,39 @@ | ||
/** | ||
* @fileoverview Prevent usage of isMounted | ||
* @author Joe Lencioni | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = function(context) { | ||
|
||
// -------------------------------------------------------------------------- | ||
// Public | ||
// -------------------------------------------------------------------------- | ||
|
||
return { | ||
|
||
CallExpression: function(node) { | ||
var callee = node.callee; | ||
if (callee.type !== 'MemberExpression') { | ||
return; | ||
} | ||
if (callee.object.type !== 'ThisExpression' || callee.property.name !== 'isMounted') { | ||
return; | ||
} | ||
var ancestors = context.getAncestors(callee); | ||
for (var i = 0, j = ancestors.length; i < j; i++) { | ||
if (ancestors[i].type === 'Property' || ancestors[i].type === 'MethodDefinition') { | ||
context.report(callee, 'Do not use isMounted'); | ||
break; | ||
} | ||
} | ||
} | ||
}; | ||
|
||
}; | ||
|
||
module.exports.schema = []; |
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,110 @@ | ||
/** | ||
* @fileoverview Prevent usage of isMounted | ||
* @author Joe Lencioni | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var rule = require('../../../lib/rules/no-is-mounted'); | ||
var RuleTester = require('eslint').RuleTester; | ||
|
||
var parserOptions = { | ||
ecmaVersion: 6, | ||
ecmaFeatures: { | ||
jsx: true | ||
} | ||
}; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
var ruleTester = new RuleTester(); | ||
ruleTester.run('no-is-mounted', rule, { | ||
|
||
valid: [{ | ||
code: [ | ||
'var Hello = function() {', | ||
'};' | ||
].join('\n'), | ||
parserOptions: parserOptions | ||
}, { | ||
code: [ | ||
'var Hello = React.createClass({', | ||
' render: function() {', | ||
' return <div>Hello</div>;', | ||
' }', | ||
'});' | ||
].join('\n'), | ||
parserOptions: parserOptions | ||
}, { | ||
code: [ | ||
'var Hello = React.createClass({', | ||
' componentDidUpdate: function() {', | ||
' someNonMemberFunction(arg);', | ||
' this.someFunc = this.isMounted;', | ||
' },', | ||
' render: function() {', | ||
' return <div>Hello</div>;', | ||
' }', | ||
'});' | ||
].join('\n'), | ||
parserOptions: parserOptions | ||
}], | ||
|
||
invalid: [{ | ||
code: [ | ||
'var Hello = React.createClass({', | ||
' componentDidUpdate: function() {', | ||
' if (!this.isMounted()) {', | ||
' return;', | ||
' }', | ||
' },', | ||
' render: function() {', | ||
' return <div>Hello</div>;', | ||
' }', | ||
'});' | ||
].join('\n'), | ||
parserOptions: parserOptions, | ||
errors: [{ | ||
message: 'Do not use isMounted' | ||
}] | ||
}, { | ||
code: [ | ||
'var Hello = React.createClass({', | ||
' someMethod: function() {', | ||
' if (!this.isMounted()) {', | ||
' return;', | ||
' }', | ||
' },', | ||
' render: function() {', | ||
' return <div onClick={this.someMethod.bind(this)}>Hello</div>;', | ||
' }', | ||
'});' | ||
].join('\n'), | ||
parserOptions: parserOptions, | ||
errors: [{ | ||
message: 'Do not use isMounted' | ||
}] | ||
}, { | ||
code: [ | ||
'class Hello extends React.Component {', | ||
' someMethod() {', | ||
' if (!this.isMounted()) {', | ||
' return;', | ||
' }', | ||
' }', | ||
' render() {', | ||
' return <div onClick={this.someMethod.bind(this)}>Hello</div>;', | ||
' }', | ||
'};' | ||
].join('\n'), | ||
parserOptions: parserOptions, | ||
errors: [{ | ||
message: 'Do not use isMounted' | ||
}] | ||
}] | ||
}); |