-
-
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.
[destructuring-assignment] : new rule to enforce consistent usage of …
…destructuring assignment
- Loading branch information
1 parent
1f14fad
commit 753e3fb
Showing
5 changed files
with
333 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,58 @@ | ||
# Enforce consostent usage of destructuring assignment of props, state and context (react/destructuring-assignment) | ||
|
||
Rule can be set to either of `always`, `never`, `ignore` for SFC and to `always` or `ignore` for class components. | ||
|
||
```js | ||
"react/destructuring-assignment": [<enabled>, { "SFC": "always", "class": "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 Foo = class extends React.PureComponent { | ||
render() { | ||
const { foo } = this.props; | ||
return <div>{foo}</div>; | ||
} | ||
}; | ||
``` | ||
|
||
If rule option is set to `never` for SFC, the following pattern is considered warning: | ||
|
||
```js | ||
const MyComponent = ({id}) => { | ||
return (<div id={id} />) | ||
}; | ||
``` | ||
|
||
and below pattern is correct: | ||
|
||
```js | ||
const MyComponent = (props) => { | ||
return (<div id={props.id} />) | ||
}; | ||
``` |
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,134 @@ | ||
/** | ||
* @fileoverview Enforce consostent usage of destructuring assignment of props, state and context. | ||
**/ | ||
'use strict'; | ||
|
||
const Components = require('../util/Components'); | ||
|
||
const DEFAULT_OPTIONS = { | ||
SFC: 'always', | ||
class: 'always' | ||
}; | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Enforce consostent usage of destructuring assignment of props, state and context', | ||
category: 'Stylistic Issues', | ||
recommended: false | ||
}, | ||
schema: [{ | ||
definitions: { | ||
valueSFC: { | ||
enum: [ | ||
'always', | ||
'never', | ||
'ignore' | ||
] | ||
}, | ||
valueClass: { | ||
enum: [ | ||
'always', | ||
'ignore' | ||
] | ||
} | ||
}, | ||
type: 'object', | ||
properties: { | ||
SFC: {$ref: '#/definitions/valueSFC'}, | ||
class: {$ref: '#/definitions/valueClass'} | ||
}, | ||
additionalProperties: false | ||
}] | ||
}, | ||
|
||
create: Components.detect((context, components, utils) => { | ||
const configuration = context.options[0] || {}; | ||
const options = { | ||
SFC: configuration.SFC || DEFAULT_OPTIONS.SFC, | ||
class: configuration.class || DEFAULT_OPTIONS.class | ||
}; | ||
|
||
|
||
/** | ||
* 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) && options.SFC === 'never') { | ||
context.report({ | ||
node: node, | ||
message: 'Must never use destructuring props assignment in SFC argument' | ||
}); | ||
} else if (destructuringContext && components.get(node) && options.SFC === '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 && options.SFC === '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 && options.class === 'always') { | ||
context.report({ | ||
node: node, | ||
message: `Must use destructuring ${node.object.property.name} assignment` | ||
}); | ||
} | ||
} | ||
|
||
return { | ||
|
||
FunctionDeclaration: handleStatelessComponent, | ||
|
||
ArrowFunctionExpression: handleStatelessComponent, | ||
|
||
FunctionExpression: handleStatelessComponent, | ||
|
||
MemberExpression: function(node) { | ||
debugger; | ||
const SFCComponent = components.get(context.getScope(node).block); | ||
const classComponent = utils.getParentComponent(node); | ||
if (SFCComponent) { | ||
handleSFCUsage(node); | ||
} | ||
if (classComponent) { | ||
handleClassUsage(node, classComponent); | ||
} | ||
} | ||
}; | ||
}) | ||
}; |
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,139 @@ | ||
|
||
/** | ||
* @fileoverview Rule to forbid or enforce destructuring assignment consistency. | ||
**/ | ||
'use strict'; | ||
|
||
const rule = require('../../../lib/rules/destructuring-assignment'); | ||
const RuleTester = require('eslint').RuleTester; | ||
|
||
require('babel-eslint'); | ||
|
||
const parserOptions = { | ||
ecmaVersion: 6, | ||
sourceType: 'module', | ||
ecmaFeatures: { | ||
jsx: true | ||
} | ||
}; | ||
|
||
const ruleTester = new RuleTester({parserOptions}); | ||
ruleTester.run('destructuring-assignment', rule, { | ||
valid: [{ | ||
code: `const Foo = class extends React.PureComponent { | ||
render() { | ||
const { foo } = this.props; | ||
return <div>{foo}</div>; | ||
} | ||
};` | ||
}, { | ||
code: `const MyComponent = ({ id, className }) => ( | ||
<div id={id} className={className} /> | ||
);` | ||
}, { | ||
code: `const MyComponent = (props) => { | ||
const { id, className } = props; | ||
return <div id={id} className={className} /> | ||
};` | ||
}, { | ||
code: `const MyComponent = (props) => ( | ||
<div id={id} props={props} /> | ||
);` | ||
}, { | ||
code: `const MyComponent = (props, { color }) => ( | ||
<div id={id} props={props} color={color} /> | ||
);` | ||
}, { | ||
code: `const Foo = class extends React.PureComponent { | ||
render() { | ||
return <div>{this.props.foo}</div>; | ||
} | ||
};`, | ||
options: [{class: 'ignore'}] | ||
}, { | ||
code: `class Foo extends React.Component { | ||
doStuff() {} | ||
render() { | ||
return <div>{this.props.foo}</div>; | ||
} | ||
}`, | ||
options: [{class: 'ignore'}] | ||
}, { | ||
code: `const Foo = class extends React.PureComponent { | ||
render() { | ||
const { foo } = this.props; | ||
return <div>{foo}</div>; | ||
} | ||
};` | ||
}], | ||
invalid: [{ | ||
code: `const MyComponent = (props) => { | ||
return (<div id={props.id} />) | ||
};`, | ||
errors: [ | ||
{message: 'Must use destructuring props assignment'} | ||
] | ||
}, { | ||
code: `const MyComponent = ({ id, className }) => ( | ||
<div id={id} className={className} /> | ||
);`, | ||
options: [{SFC: 'never'}], | ||
errors: [ | ||
{message: 'Must never use destructuring props assignment in SFC argument'} | ||
] | ||
}, { | ||
code: `const MyComponent = (props, { color }) => ( | ||
<div id={props.id} className={props.className} /> | ||
);`, | ||
options: [{SFC: 'never'}], | ||
errors: [ | ||
{message: 'Must never use destructuring context assignment in SFC argument'} | ||
] | ||
}, { | ||
code: `const Foo = class extends React.PureComponent { | ||
render() { | ||
return <div>{this.props.foo}</div>; | ||
} | ||
};`, | ||
errors: [ | ||
{message: 'Must use destructuring props assignment'} | ||
] | ||
}, { | ||
code: `const Foo = class extends React.PureComponent { | ||
render() { | ||
return <div>{this.state.foo}</div>; | ||
} | ||
};`, | ||
errors: [ | ||
{message: 'Must use destructuring state assignment'} | ||
] | ||
}, { | ||
code: `const Foo = class extends React.PureComponent { | ||
render() { | ||
return <div>{this.context.foo}</div>; | ||
} | ||
};`, | ||
errors: [ | ||
{message: 'Must use destructuring context assignment'} | ||
] | ||
}, { | ||
code: `class Foo extends React.Component { | ||
render() { return this.foo(); } | ||
foo() { | ||
return this.props.children; | ||
} | ||
}`, | ||
errors: [ | ||
{message: 'Must use destructuring props assignment'} | ||
] | ||
}, { | ||
code: `var Hello = React.createClass({ | ||
render: function() { | ||
return <Text>{this.props.foo}</Text>; | ||
} | ||
});`, | ||
errors: [ | ||
{message: 'Must use destructuring props assignment'} | ||
] | ||
}] | ||
}); |