-
-
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
[destructuring-assignment] : new rule #1462
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a25003c
[destructuring-assignment] : new rule to enforce consistent usage of …
DianaSuvorova c967000
feat: [destructuring-assignment] setting up never for sfc and class
DianaSuvorova 1e7dc52
chore(destructuring-assignment) desc typos
DianaSuvorova 77b1383
refactor: [destructuring-assignment] a single config option
DianaSuvorova e80d3b6
chore:[destructuring-assignment] typos
DianaSuvorova File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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,87 @@ | ||
# Enforce consistent usage of destructuring assignment of props, state, and context (react/destructuring-assignment) | ||
|
||
Rule can be set to either of `always` or `never`; | ||
```js | ||
"react/destructuring-assignment": [<enabled>, 'always'] | ||
``` | ||
|
||
## Rule Details | ||
|
||
By default rule is set to `always` enforce destructuring assignment. The following patterns are considered warnings: | ||
|
||
```js | ||
const MyComponent = (props) => { | ||
return (<div id={props.id} />) | ||
}; | ||
``` | ||
|
||
```js | ||
const Foo = class extends React.PureComponent { | ||
render() { | ||
return <div>{this.context.foo}</div>; | ||
} | ||
}; | ||
``` | ||
|
||
Below pattern is correct: | ||
|
||
```js | ||
const MyComponent = ({id}) => { | ||
return (<div id={id} />) | ||
}; | ||
``` | ||
|
||
```js | ||
const MyComponent = (props, context) => { | ||
const { id } = props; | ||
return (<div id={id} />) | ||
}; | ||
``` | ||
|
||
```js | ||
const Foo = class extends React.PureComponent { | ||
render() { | ||
const { title } = this.context; | ||
return <div>{title}</div>; | ||
} | ||
}; | ||
``` | ||
|
||
If rule is set to `never`, the following patterns are considered warning: | ||
|
||
```js | ||
const MyComponent = ({id}) => { | ||
return (<div id={id} />) | ||
}; | ||
``` | ||
|
||
```js | ||
const MyComponent = (props) => { | ||
const { id } = props; | ||
return (<div id={id} />) | ||
}; | ||
``` | ||
|
||
```js | ||
const Foo = class extends React.PureComponent { | ||
render() { | ||
const { title } = this.state; | ||
return <div>{title}</div>; | ||
} | ||
}; | ||
``` | ||
|
||
and below pattern is correct: | ||
|
||
```js | ||
const MyComponent = (props) => { | ||
return (<div id={props.id} />) | ||
}; | ||
``` | ||
|
||
```js | ||
const Foo = class extends React.PureComponent { | ||
render() { | ||
return <div>{this.state.title}</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,137 @@ | ||
/** | ||
* @fileoverview Enforce consistent usage of destructuring assignment of props, state, and context. | ||
**/ | ||
'use strict'; | ||
|
||
const Components = require('../util/Components'); | ||
|
||
const DEFAULT_OPTION = 'always'; | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Enforce consistent usage of destructuring assignment of props, state, and context', | ||
category: 'Stylistic Issues', | ||
recommended: false | ||
}, | ||
schema: [{ | ||
type: 'string', | ||
enum: [ | ||
'always', | ||
'never' | ||
] | ||
}] | ||
}, | ||
|
||
create: Components.detect((context, components, utils) => { | ||
const configuration = context.options[0] || DEFAULT_OPTION; | ||
|
||
|
||
/** | ||
* Checks if a prop is being assigned a value props.bar = 'bar' | ||
* @param {ASTNode} node The AST node being checked. | ||
* @returns {Boolean} | ||
*/ | ||
|
||
function isAssignmentToProp(node) { | ||
return ( | ||
node.parent && | ||
node.parent.type === 'AssignmentExpression' && | ||
node.parent.left === node | ||
); | ||
} | ||
/** | ||
* @param {ASTNode} node We expect either an ArrowFunctionExpression, | ||
* FunctionDeclaration, or FunctionExpression | ||
*/ | ||
function handleStatelessComponent(node) { | ||
const destructuringProps = node.params && node.params[0] && node.params[0].type === 'ObjectPattern'; | ||
const destructuringContext = node.params && node.params[1] && node.params[1].type === 'ObjectPattern'; | ||
|
||
if (destructuringProps && components.get(node) && configuration === 'never') { | ||
context.report({ | ||
node: node, | ||
message: 'Must never use destructuring props assignment in SFC argument' | ||
}); | ||
} else if (destructuringContext && components.get(node) && configuration === 'never') { | ||
context.report({ | ||
node: node, | ||
message: 'Must never use destructuring context assignment in SFC argument' | ||
}); | ||
} | ||
} | ||
|
||
function handleSFCUsage(node) { | ||
// props.aProp || context.aProp | ||
const isPropUsed = (node.object.name === 'props' || node.object.name === 'context') && !isAssignmentToProp(node); | ||
if (isPropUsed && configuration === 'always') { | ||
context.report({ | ||
node: node, | ||
message: `Must use destructuring ${node.object.name} assignment` | ||
}); | ||
} | ||
} | ||
|
||
function handleClassUsage(node) { | ||
// this.props.Aprop || this.context.aProp || this.state.aState | ||
const isPropUsed = (node.object.type === 'MemberExpression' && node.object.object.type === 'ThisExpression' && | ||
(node.object.property.name === 'props' || node.object.property.name === 'context' || node.object.property.name === 'state') && | ||
node.object.type === 'MemberExpression' | ||
); | ||
|
||
if (isPropUsed && configuration === 'always') { | ||
context.report({ | ||
node: node, | ||
message: `Must use destructuring ${node.object.property.name} assignment` | ||
}); | ||
} | ||
} | ||
|
||
return { | ||
|
||
FunctionDeclaration: handleStatelessComponent, | ||
|
||
ArrowFunctionExpression: handleStatelessComponent, | ||
|
||
FunctionExpression: handleStatelessComponent, | ||
|
||
MemberExpression: function(node) { | ||
const SFCComponent = components.get(context.getScope(node).block); | ||
const classComponent = utils.getParentComponent(node); | ||
if (SFCComponent) { | ||
handleSFCUsage(node); | ||
} | ||
if (classComponent) { | ||
handleClassUsage(node, classComponent); | ||
} | ||
}, | ||
|
||
VariableDeclarator: function(node) { | ||
const classComponent = utils.getParentComponent(node); | ||
const SFCComponent = components.get(context.getScope(node).block); | ||
|
||
const destructuring = (node.init && node.id && node.id.type === 'ObjectPattern'); | ||
// let {foo} = props; | ||
const destructuringSFC = destructuring && (node.init.name === 'props' || node.init.name === 'context'); | ||
// let {foo} = this.props; | ||
const destructuringClass = destructuring && node.init.object && node.init.object.type === 'ThisExpression' && ( | ||
node.init.property.name === 'props' || node.init.property.name === 'context' || node.init.property.name === 'state' | ||
); | ||
|
||
if (SFCComponent && destructuringSFC && configuration === 'never') { | ||
context.report({ | ||
node: node, | ||
message: `Must never use destructuring ${node.init.name} assignment` | ||
}); | ||
} | ||
|
||
if (classComponent && destructuringClass && configuration === 'never') { | ||
context.report({ | ||
node: node, | ||
message: `Must never use destructuring ${node.init.property.name} assignment` | ||
}); | ||
} | ||
} | ||
}; | ||
}) | ||
}; |
Oops, something went wrong.
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.
node.object.type === 'MemberExpression'
is asserted twiceThere 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.
whoops, thanks - want to make a PR to clean it up? :-D
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.
Sure: #1502