123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- "use strict";
- module.exports = {
- meta: {
- type: "suggestion",
- docs: {
- description: "require destructuring from arrays and/or objects",
- category: "ECMAScript 6",
- recommended: false,
- url: "https://eslint.org/docs/rules/prefer-destructuring"
- },
- fixable: "code",
- schema: [
- {
-
- oneOf: [
- {
- type: "object",
- properties: {
- VariableDeclarator: {
- type: "object",
- properties: {
- array: {
- type: "boolean"
- },
- object: {
- type: "boolean"
- }
- },
- additionalProperties: false
- },
- AssignmentExpression: {
- type: "object",
- properties: {
- array: {
- type: "boolean"
- },
- object: {
- type: "boolean"
- }
- },
- additionalProperties: false
- }
- },
- additionalProperties: false
- },
- {
- type: "object",
- properties: {
- array: {
- type: "boolean"
- },
- object: {
- type: "boolean"
- }
- },
- additionalProperties: false
- }
- ]
- },
- {
- type: "object",
- properties: {
- enforceForRenamedProperties: {
- type: "boolean"
- }
- },
- additionalProperties: false
- }
- ]
- },
- create(context) {
- const enabledTypes = context.options[0];
- const enforceForRenamedProperties = context.options[1] && context.options[1].enforceForRenamedProperties;
- let normalizedOptions = {
- VariableDeclarator: { array: true, object: true },
- AssignmentExpression: { array: true, object: true }
- };
- if (enabledTypes) {
- normalizedOptions = typeof enabledTypes.array !== "undefined" || typeof enabledTypes.object !== "undefined"
- ? { VariableDeclarator: enabledTypes, AssignmentExpression: enabledTypes }
- : enabledTypes;
- }
-
-
-
-
-
- function shouldCheck(nodeType, destructuringType) {
- return normalizedOptions &&
- normalizedOptions[nodeType] &&
- normalizedOptions[nodeType][destructuringType];
- }
-
- function isArrayIndexAccess(node) {
- return Number.isInteger(node.property.value);
- }
-
- function report(reportNode, type, fix) {
- context.report({
- node: reportNode,
- message: "Use {{type}} destructuring.",
- data: { type },
- fix
- });
- }
-
- function shouldFix(node) {
- return node.type === "VariableDeclarator" &&
- node.id.type === "Identifier" &&
- node.init.type === "MemberExpression" &&
- node.id.name === node.init.property.name;
- }
-
- function fixIntoObjectDestructuring(fixer, node) {
- const rightNode = node.init;
- const sourceCode = context.getSourceCode();
- return fixer.replaceText(
- node,
- `{${rightNode.property.name}} = ${sourceCode.getText(rightNode.object)}`
- );
- }
-
- function performCheck(leftNode, rightNode, reportNode) {
- if (rightNode.type !== "MemberExpression" || rightNode.object.type === "Super") {
- return;
- }
- if (isArrayIndexAccess(rightNode)) {
- if (shouldCheck(reportNode.type, "array")) {
- report(reportNode, "array", null);
- }
- return;
- }
- const fix = shouldFix(reportNode)
- ? fixer => fixIntoObjectDestructuring(fixer, reportNode)
- : null;
- if (shouldCheck(reportNode.type, "object") && enforceForRenamedProperties) {
- report(reportNode, "object", fix);
- return;
- }
- if (shouldCheck(reportNode.type, "object")) {
- const property = rightNode.property;
- if (
- (property.type === "Literal" && leftNode.name === property.value) ||
- (property.type === "Identifier" && leftNode.name === property.name && !rightNode.computed)
- ) {
- report(reportNode, "object", fix);
- }
- }
- }
-
- function checkVariableDeclarator(node) {
-
- if (!node.init) {
- return;
- }
-
- if (node.init.type !== "MemberExpression") {
- return;
- }
- performCheck(node.id, node.init, node);
- }
-
- function checkAssigmentExpression(node) {
- if (node.operator === "=") {
- performCheck(node.left, node.right, node);
- }
- }
-
-
-
- return {
- VariableDeclarator: checkVariableDeclarator,
- AssignmentExpression: checkAssigmentExpression
- };
- }
- };
|