Skip to content

Partial rewrite of the destructuring rule #1740

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

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/rules/destructuring-assignment.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ Rule can be set to either of `always` or `never`;
"react/destructuring-assignment": [<enabled>, 'always']
```

It also accepts an object as the first option for more granular control for different component types.

```js
"react/destructuring-assignment": [<enabled>, {
SFC: 'always',
class: 'always',
createClass: 'always',
}]
```

## Rule Details

By default rule is set to `always` enforce destructuring assignment. The following patterns are considered warnings:
Expand Down
212 changes: 148 additions & 64 deletions lib/rules/destructuring-assignment.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,41 @@ const Components = require('../util/Components');
const docsUrl = require('../util/docsUrl');

const DEFAULT_OPTION = 'always';
const SCHEMA = {
type: 'string',
enum: [
'always',
'never'
]
};

function isAssignmentToProp(node) {
return (
node.parent &&
node.parent.type === 'AssignmentExpression' &&
node.parent.left === node
);
}

/**
* Check if the node is in a render method of the component.
*
* @param {Object} node - The node.
* @param {Object} component - The component.
* @returns {Boolean} - Returns whether or not the node is in a render method.
*/
function isInRenderMethod(node, component) {
if (node === component) {
return false;
}

if (node.type === 'MethodDefinition' || node.type === 'Property') {
// Check if the method is named render and that it's not a static method
return !node.static && node.key.name === 'render';
}

return isInRenderMethod(node.parent, component);
}

module.exports = {
meta: {
Expand All @@ -17,64 +52,88 @@ module.exports = {
url: docsUrl('destructuring-assignment')
},
schema: [{
type: 'string',
enum: [
'always',
'never'
]
oneOf: [SCHEMA, {
type: 'object',
properties: {
SFC: SCHEMA,
class: SCHEMA,
createClass: SCHEMA
}
}]
}]
},

create: Components.detect((context, components, utils) => {
const configuration = context.options[0] || DEFAULT_OPTION;

function getConfiguration(componentType) {
if (typeof context.options[0] === 'object') {
return context.options[0][componentType] || 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
);
return context.options[0] || DEFAULT_OPTION;
}

/**
* @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';
const hasProps = node.params && node.params[0];
const isDestructuringProps = hasProps && node.params[0].type === 'ObjectPattern';
const hasContext = node.params && node.params[1];
const isDestructuringContext = hasContext && 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'
});
if (!components.get(node) || components.get(node).confidence === 0) {
return;
}

if (getConfiguration('SFC') === 'never') {
if (hasProps && isDestructuringProps) {
context.report({
node: node,
message: 'Must never use destructuring props assignment in SFC argument'
});
}

if (hasContext && isDestructuringContext) {
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 handleStatelessUsage(node, component) {
if (getConfiguration('SFC') === 'always') {
const propsName = component.params[0] ? component.params[0].name : null;
const contextName = component.params[1] ? component.params[1].name : null;

if (!node.object.type === 'Identifier') {
return;
}

const objName = node.object.name;

if (objName === propsName) {
context.report({
node: node,
message: 'Must use destructuring props assignment in SFC argument'
});
} else if (objName === contextName) {
context.report({
node: node,
message: 'Must use destructuring context assignment in SFC argument'
});
}
}
}

function handleClassUsage(node) {
function handleClassUsage(node, component, configuration) {
// Check if we are in a render method
if (!isInRenderMethod(node, component)) {
return;
}

// this.props.Aprop || this.context.aProp || this.state.aState
const isPropUsed = (
node.object.type === 'MemberExpression' && node.object.object.type === 'ThisExpression' &&
Expand All @@ -91,47 +150,72 @@ module.exports = {
}

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);
const component = utils.getParentComponent();

if (!component) {
return;
}
if (classComponent) {
handleClassUsage(node, classComponent);

if (utils.isES6Component(component)) {
handleClassUsage(node, component, getConfiguration('class'));
} else if (utils.isES5Component(component)) {
handleClassUsage(node, component, getConfiguration('createClass'));
} else {
handleStatelessUsage(node, component);
}
},

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') {
const component = utils.getParentComponent(node);
const isDestructuring = (node.init && node.id && node.id.type === 'ObjectPattern');

if (!isDestructuring || !component) {
return;
}

// Checks for class components
if (utils.isES5Component(component) || utils.isES6Component(component)) {
const isDestructuringClassProperties = node.init.object && node.init.object.type === 'ThisExpression' && (
node.init.property.name === 'props' || node.init.property.name === 'context' || node.init.property.name === 'state'
);

if (!isDestructuringClassProperties || !isInRenderMethod(node, component)) {
return;
}

if (utils.isES5Component(component) && getConfiguration('createClass') === 'never') {
context.report({
node: node,
message: `Must never use destructuring ${node.init.property.name} assignment`
});
} else if (utils.isES6Component(component) && getConfiguration('class') === 'never') {
context.report({
node: node,
message: `Must never use destructuring ${node.init.property.name} assignment`
});
}

return;
}

if (component.params[0] && component.params[0].name === node.init.name) {
context.report({
node: node,
message: `Must never use destructuring ${node.init.name} assignment`
message: 'Must never use destructuring props assignment'
});

return;
}

if (classComponent && destructuringClass && configuration === 'never') {
if (component.params[1] && component.params[1].name === node.init.name) {
context.report({
node: node,
message: `Must never use destructuring ${node.init.property.name} assignment`
message: 'Must never use destructuring context assignment'
});
}
}
Expand Down
Loading