github-pages-deploy-action/node_modules/eslint/lib/rules/no-self-assign.js

234 lines
6.9 KiB
JavaScript
Raw Normal View History

2020-03-07 11:45:40 +08:00
/**
* @fileoverview Rule to disallow assignments where both sides are exactly the same
* @author Toru Nagashima
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
2020-03-31 20:40:00 +08:00
const astUtils = require("./utils/ast-utils");
2020-03-07 11:45:40 +08:00
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
const SPACES = /\s+/gu;
/**
* Checks whether the property of 2 given member expression nodes are the same
* property or not.
2020-03-31 20:40:00 +08:00
* @param {ASTNode} left A member expression node to check.
* @param {ASTNode} right Another member expression node to check.
2020-03-07 11:45:40 +08:00
* @returns {boolean} `true` if the member expressions have the same property.
*/
function isSameProperty(left, right) {
if (left.property.type === "Identifier" &&
left.property.type === right.property.type &&
left.property.name === right.property.name &&
left.computed === right.computed
) {
return true;
}
const lname = astUtils.getStaticPropertyName(left);
const rname = astUtils.getStaticPropertyName(right);
return lname !== null && lname === rname;
}
/**
* Checks whether 2 given member expression nodes are the reference to the same
* property or not.
2020-03-31 20:40:00 +08:00
* @param {ASTNode} left A member expression node to check.
* @param {ASTNode} right Another member expression node to check.
2020-03-07 11:45:40 +08:00
* @returns {boolean} `true` if the member expressions are the reference to the
* same property or not.
*/
function isSameMember(left, right) {
if (!isSameProperty(left, right)) {
return false;
}
const lobj = left.object;
const robj = right.object;
if (lobj.type !== robj.type) {
return false;
}
if (lobj.type === "MemberExpression") {
return isSameMember(lobj, robj);
}
2020-03-31 20:40:00 +08:00
if (lobj.type === "ThisExpression") {
return true;
}
2020-03-07 11:45:40 +08:00
return lobj.type === "Identifier" && lobj.name === robj.name;
}
/**
* Traverses 2 Pattern nodes in parallel, then reports self-assignments.
2020-03-31 20:40:00 +08:00
* @param {ASTNode|null} left A left node to traverse. This is a Pattern or
2020-03-07 11:45:40 +08:00
* a Property.
2020-03-31 20:40:00 +08:00
* @param {ASTNode|null} right A right node to traverse. This is a Pattern or
2020-03-07 11:45:40 +08:00
* a Property.
2020-03-31 20:40:00 +08:00
* @param {boolean} props The flag to check member expressions as well.
* @param {Function} report A callback function to report.
2020-03-07 11:45:40 +08:00
* @returns {void}
*/
function eachSelfAssignment(left, right, props, report) {
if (!left || !right) {
// do nothing
} else if (
left.type === "Identifier" &&
right.type === "Identifier" &&
left.name === right.name
) {
report(right);
} else if (
left.type === "ArrayPattern" &&
right.type === "ArrayExpression"
) {
const end = Math.min(left.elements.length, right.elements.length);
for (let i = 0; i < end; ++i) {
2020-03-31 20:40:00 +08:00
const leftElement = left.elements[i];
2020-03-07 11:45:40 +08:00
const rightElement = right.elements[i];
2020-03-31 20:40:00 +08:00
// Avoid cases such as [...a] = [...a, 1]
if (
leftElement &&
leftElement.type === "RestElement" &&
i < right.elements.length - 1
) {
break;
}
eachSelfAssignment(leftElement, rightElement, props, report);
2020-03-07 11:45:40 +08:00
// After a spread element, those indices are unknown.
if (rightElement && rightElement.type === "SpreadElement") {
break;
}
}
} else if (
left.type === "RestElement" &&
right.type === "SpreadElement"
) {
eachSelfAssignment(left.argument, right.argument, props, report);
} else if (
left.type === "ObjectPattern" &&
right.type === "ObjectExpression" &&
right.properties.length >= 1
) {
/*
* Gets the index of the last spread property.
* It's possible to overwrite properties followed by it.
*/
let startJ = 0;
for (let i = right.properties.length - 1; i >= 0; --i) {
const propType = right.properties[i].type;
if (propType === "SpreadElement" || propType === "ExperimentalSpreadProperty") {
startJ = i + 1;
break;
}
}
for (let i = 0; i < left.properties.length; ++i) {
for (let j = startJ; j < right.properties.length; ++j) {
eachSelfAssignment(
left.properties[i],
right.properties[j],
props,
report
);
}
}
} else if (
left.type === "Property" &&
right.type === "Property" &&
right.kind === "init" &&
2020-03-31 20:40:00 +08:00
!right.method
2020-03-07 11:45:40 +08:00
) {
2020-03-31 20:40:00 +08:00
const leftName = astUtils.getStaticPropertyName(left);
if (leftName !== null && leftName === astUtils.getStaticPropertyName(right)) {
eachSelfAssignment(left.value, right.value, props, report);
}
2020-03-07 11:45:40 +08:00
} else if (
props &&
left.type === "MemberExpression" &&
right.type === "MemberExpression" &&
isSameMember(left, right)
) {
report(right);
}
}
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
type: "problem",
docs: {
description: "disallow assignments where both sides are exactly the same",
category: "Best Practices",
recommended: true,
url: "https://eslint.org/docs/rules/no-self-assign"
},
schema: [
{
type: "object",
properties: {
props: {
type: "boolean",
default: true
}
},
additionalProperties: false
}
2020-05-15 05:33:08 +08:00
],
messages: {
selfAssignment: "'{{name}}' is assigned to itself."
}
2020-03-07 11:45:40 +08:00
},
create(context) {
const sourceCode = context.getSourceCode();
const [{ props = true } = {}] = context.options;
/**
* Reports a given node as self assignments.
2020-03-31 20:40:00 +08:00
* @param {ASTNode} node A node to report. This is an Identifier node.
2020-03-07 11:45:40 +08:00
* @returns {void}
*/
function report(node) {
context.report({
node,
2020-05-15 05:33:08 +08:00
messageId: "selfAssignment",
2020-03-07 11:45:40 +08:00
data: {
name: sourceCode.getText(node).replace(SPACES, "")
}
});
}
return {
AssignmentExpression(node) {
if (node.operator === "=") {
eachSelfAssignment(node.left, node.right, props, report);
}
}
};
}
};