-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Added no-unsafe
rule
#1831
Merged
Merged
Added no-unsafe
rule
#1831
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b5e86bf
Added `no-unsafe` rule
sergei-startsev 5b08a1e
Adjusted `no-unsafe` rule
sergei-startsev 5e17159
Added missed position details for `no-unsafe`
sergei-startsev e41500a
Added early termination for no-unsafe, adjusted tests to support ESLint3
sergei-startsev 0285eef
Excluded `no-unsafe` from the recommended for now to avoid breaking c…
sergei-startsev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,47 @@ | ||
# Prevent usage of `UNSAFE_` methods (react/no-unsafe) | ||
|
||
Certain legacy lifecycle methods are [unsafe for use in async React applications][async_rendering] and cause warnings in [_strict mode_][strict_mode]. These also happen to be the lifecycles that cause the most [confusion within the React community][component_lifecycle_changes]. | ||
|
||
[async_rendering]: https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html | ||
[strict_mode]: https://reactjs.org/docs/strict-mode.html#identifying-unsafe-lifecycles | ||
[component_lifecycle_changes]: https://reactjs.org/blog/2018/03/29/react-v-16-3.html#component-lifecycle-changes | ||
|
||
The rule checks the following methods: `UNSAFE_componentWillMount`, `UNSAFE_componentWillReceiveProps`, `UNSAFE_componentWillUpdate`. | ||
|
||
## Rule Details | ||
|
||
The following patterns are considered warnings: | ||
|
||
```jsx | ||
class Foo extends React.Component { | ||
UNSAFE_componentWillMount() {} | ||
UNSAFE_componentWillReceiveProps() {} | ||
UNSAFE_componentWillUpdate() {} | ||
} | ||
``` | ||
|
||
```jsx | ||
const Foo = createReactClass({ | ||
UNSAFE_componentWillMount: function() {}, | ||
UNSAFE_componentWillReceiveProps: function() {}, | ||
UNSAFE_componentWillUpdate: function() {} | ||
}); | ||
``` | ||
|
||
The following patterns are **not** considered warnings: | ||
|
||
```jsx | ||
class Foo extends Bar { | ||
UNSAFE_componentWillMount() {} | ||
UNSAFE_componentWillReceiveProps() {} | ||
UNSAFE_componentWillUpdate() {} | ||
} | ||
``` | ||
|
||
```jsx | ||
const Foo = bar({ | ||
UNSAFE_componentWillMount: function() {}, | ||
UNSAFE_componentWillReceiveProps: function() {}, | ||
UNSAFE_componentWillUpdate: function() {} | ||
}); | ||
``` |
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,99 @@ | ||
/** | ||
* @fileoverview Prevent usage of UNSAFE_ methods | ||
* @author Sergei Startsev | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const Components = require('../util/Components'); | ||
const astUtil = require('../util/ast'); | ||
const docsUrl = require('../util/docsUrl'); | ||
const versionUtil = require('../util/version'); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Prevent usage of UNSAFE_ methods', | ||
category: 'Best Practices', | ||
recommended: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
url: docsUrl('no-unsafe') | ||
}, | ||
schema: [] | ||
}, | ||
|
||
create: Components.detect((context, components, utils) => { | ||
const isApplicable = versionUtil.testReactVersion(context, '16.3.0'); | ||
if (!isApplicable) { | ||
return {}; | ||
} | ||
|
||
/** | ||
* Returns a list of unsafe methods | ||
* @returns {Array} A list of unsafe methods | ||
*/ | ||
function getUnsafeMethods() { | ||
return [ | ||
'UNSAFE_componentWillMount', | ||
'UNSAFE_componentWillReceiveProps', | ||
'UNSAFE_componentWillUpdate' | ||
]; | ||
} | ||
|
||
/** | ||
* Checks if a passed method is unsafe | ||
* @param {string} method Life cycle method | ||
* @returns {boolean} Returns true for unsafe methods, otherwise returns false | ||
*/ | ||
function isUnsafe(method) { | ||
const unsafeMethods = getUnsafeMethods(); | ||
return unsafeMethods.indexOf(method) !== -1; | ||
} | ||
|
||
/** | ||
* Reports the error for an unsafe method | ||
* @param {ASTNode} node The AST node being checked | ||
* @param {string} method Life cycle method | ||
*/ | ||
function checkUnsafe(node, method) { | ||
if (!isUnsafe(method)) { | ||
return; | ||
} | ||
|
||
context.report({ | ||
node: node, | ||
message: `${method} is unsafe for use in async rendering, see https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html` | ||
}); | ||
} | ||
|
||
/** | ||
* Returns life cycle methods if available | ||
* @param {ASTNode} node The AST node being checked. | ||
* @returns {Array} The array of methods. | ||
*/ | ||
function getLifeCycleMethods(node) { | ||
const properties = astUtil.getComponentProperties(node); | ||
return properties.map(property => astUtil.getPropertyName(property)); | ||
} | ||
|
||
/** | ||
* Checks life cycle methods | ||
* @param {ASTNode} node The AST node being checked. | ||
*/ | ||
function checkLifeCycleMethods(node) { | ||
if (utils.isES5Component(node) || utils.isES6Component(node)) { | ||
const methods = getLifeCycleMethods(node); | ||
methods.forEach(method => checkUnsafe(node, method)); | ||
} | ||
} | ||
|
||
return { | ||
ClassDeclaration: checkLifeCycleMethods, | ||
ClassExpression: checkLifeCycleMethods, | ||
ObjectExpression: checkLifeCycleMethods | ||
}; | ||
}) | ||
}; |
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,155 @@ | ||
/** | ||
* @fileoverview Prevent usage of UNSAFE_ methods | ||
* @author Sergei Startsev | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const rule = require('../../../lib/rules/no-unsafe'); | ||
const RuleTester = require('eslint').RuleTester; | ||
|
||
const parserOptions = { | ||
ecmaVersion: 2018, | ||
sourceType: 'module', | ||
ecmaFeatures: { | ||
jsx: true | ||
} | ||
}; | ||
|
||
function errorMessage(method) { | ||
return `${method} is unsafe for use in async rendering, see https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html`; | ||
} | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester({parserOptions}); | ||
ruleTester.run('no-unsafe', rule, { | ||
valid: [ | ||
{ | ||
code: ` | ||
class Foo extends React.Component { | ||
componentDidUpdate() {} | ||
render() {} | ||
} | ||
`, | ||
settings: {react: {version: '16.4.0'}} | ||
}, | ||
{ | ||
code: ` | ||
const Foo = createReactClass({ | ||
componentDidUpdate: function() {}, | ||
render: function() {} | ||
}); | ||
`, | ||
settings: {react: {version: '16.4.0'}} | ||
}, | ||
{ | ||
code: ` | ||
class Foo extends Bar { | ||
UNSAFE_componentWillMount() {} | ||
UNSAFE_componentWillReceiveProps() {} | ||
UNSAFE_componentWillUpdate() {} | ||
} | ||
`, | ||
settings: {react: {version: '16.4.0'}} | ||
}, | ||
{ | ||
code: ` | ||
const Foo = bar({ | ||
UNSAFE_componentWillMount: function() {}, | ||
UNSAFE_componentWillReceiveProps: function() {}, | ||
UNSAFE_componentWillUpdate: function() {}, | ||
}); | ||
`, | ||
settings: {react: {version: '16.4.0'}} | ||
}, | ||
{ | ||
code: ` | ||
class Foo extends React.Component { | ||
UNSAFE_componentWillMount() {} | ||
UNSAFE_componentWillReceiveProps() {} | ||
UNSAFE_componentWillUpdate() {} | ||
} | ||
`, | ||
settings: {react: {version: '16.2.0'}} | ||
}, | ||
{ | ||
code: ` | ||
const Foo = createReactClass({ | ||
UNSAFE_componentWillMount: function() {}, | ||
UNSAFE_componentWillReceiveProps: function() {}, | ||
UNSAFE_componentWillUpdate: function() {}, | ||
}); | ||
`, | ||
settings: {react: {version: '16.2.0'}} | ||
} | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: ` | ||
class Foo extends React.Component { | ||
UNSAFE_componentWillMount() {} | ||
UNSAFE_componentWillReceiveProps() {} | ||
UNSAFE_componentWillUpdate() {} | ||
} | ||
`, | ||
settings: {react: {version: '16.3.0'}}, | ||
errors: [ | ||
{ | ||
message: errorMessage('UNSAFE_componentWillMount'), | ||
line: 2, | ||
column: 7, | ||
type: 'ClassDeclaration' | ||
}, | ||
{ | ||
message: errorMessage('UNSAFE_componentWillReceiveProps'), | ||
line: 2, | ||
column: 7, | ||
type: 'ClassDeclaration' | ||
}, | ||
{ | ||
message: errorMessage('UNSAFE_componentWillUpdate'), | ||
line: 2, | ||
column: 7, | ||
type: 'ClassDeclaration' | ||
} | ||
] | ||
}, | ||
{ | ||
code: ` | ||
const Foo = createReactClass({ | ||
UNSAFE_componentWillMount: function() {}, | ||
UNSAFE_componentWillReceiveProps: function() {}, | ||
UNSAFE_componentWillUpdate: function() {}, | ||
}); | ||
`, | ||
settings: {react: {version: '16.3.0'}}, | ||
errors: [ | ||
{ | ||
message: errorMessage('UNSAFE_componentWillMount'), | ||
line: 2, | ||
column: 38, | ||
type: 'ObjectExpression' | ||
}, | ||
{ | ||
message: errorMessage('UNSAFE_componentWillReceiveProps'), | ||
line: 2, | ||
column: 38, | ||
type: 'ObjectExpression' | ||
}, | ||
{ | ||
message: errorMessage('UNSAFE_componentWillUpdate'), | ||
line: 2, | ||
column: 38, | ||
type: 'ObjectExpression' | ||
} | ||
] | ||
} | ||
] | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
enabling new rules in an exported config is a semver-major change; please set this to 0 for now.