github-pages-deploy-action/node_modules/eslint/lib/rules/no-mixed-operators.js

244 lines
8.2 KiB
JavaScript
Raw Normal View History

2020-03-07 11:45:40 +08:00
/**
* @fileoverview Rule to disallow mixed binary operators.
* @author Toru Nagashima
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
2020-03-31 20:40:00 +08:00
const astUtils = require("./utils/ast-utils.js");
2020-03-07 11:45:40 +08:00
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
const ARITHMETIC_OPERATORS = ["+", "-", "*", "/", "%", "**"];
const BITWISE_OPERATORS = ["&", "|", "^", "~", "<<", ">>", ">>>"];
const COMPARISON_OPERATORS = ["==", "!=", "===", "!==", ">", ">=", "<", "<="];
const LOGICAL_OPERATORS = ["&&", "||"];
const RELATIONAL_OPERATORS = ["in", "instanceof"];
2020-03-31 20:40:00 +08:00
const TERNARY_OPERATOR = ["?:"];
2020-06-27 02:01:06 +08:00
const COALESCE_OPERATOR = ["??"];
2020-03-07 11:45:40 +08:00
const ALL_OPERATORS = [].concat(
ARITHMETIC_OPERATORS,
BITWISE_OPERATORS,
COMPARISON_OPERATORS,
LOGICAL_OPERATORS,
2020-03-31 20:40:00 +08:00
RELATIONAL_OPERATORS,
2020-06-27 02:01:06 +08:00
TERNARY_OPERATOR,
COALESCE_OPERATOR
2020-03-07 11:45:40 +08:00
);
const DEFAULT_GROUPS = [
ARITHMETIC_OPERATORS,
BITWISE_OPERATORS,
COMPARISON_OPERATORS,
LOGICAL_OPERATORS,
RELATIONAL_OPERATORS
];
2020-03-31 20:40:00 +08:00
const TARGET_NODE_TYPE = /^(?:Binary|Logical|Conditional)Expression$/u;
2020-03-07 11:45:40 +08:00
/**
* Normalizes options.
2020-03-31 20:40:00 +08:00
* @param {Object|undefined} options A options object to normalize.
2020-03-07 11:45:40 +08:00
* @returns {Object} Normalized option object.
*/
function normalizeOptions(options = {}) {
const hasGroups = options.groups && options.groups.length > 0;
const groups = hasGroups ? options.groups : DEFAULT_GROUPS;
const allowSamePrecedence = options.allowSamePrecedence !== false;
return {
groups,
allowSamePrecedence
};
}
/**
* Checks whether any group which includes both given operator exists or not.
2020-03-31 20:40:00 +08:00
* @param {Array.<string[]>} groups A list of groups to check.
* @param {string} left An operator.
* @param {string} right Another operator.
2020-03-07 11:45:40 +08:00
* @returns {boolean} `true` if such group existed.
*/
function includesBothInAGroup(groups, left, right) {
return groups.some(group => group.indexOf(left) !== -1 && group.indexOf(right) !== -1);
}
2020-03-31 20:40:00 +08:00
/**
* Checks whether the given node is a conditional expression and returns the test node else the left node.
* @param {ASTNode} node A node which can be a BinaryExpression or a LogicalExpression node.
* This parent node can be BinaryExpression, LogicalExpression
* , or a ConditionalExpression node
* @returns {ASTNode} node the appropriate node(left or test).
*/
function getChildNode(node) {
return node.type === "ConditionalExpression" ? node.test : node.left;
}
2020-03-07 11:45:40 +08:00
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
type: "suggestion",
docs: {
description: "disallow mixed binary operators",
category: "Stylistic Issues",
recommended: false,
url: "https://eslint.org/docs/rules/no-mixed-operators"
},
schema: [
{
type: "object",
properties: {
groups: {
type: "array",
items: {
type: "array",
items: { enum: ALL_OPERATORS },
minItems: 2,
uniqueItems: true
},
uniqueItems: true
},
allowSamePrecedence: {
type: "boolean",
default: true
}
},
additionalProperties: false
}
2020-05-15 05:33:08 +08:00
],
messages: {
unexpectedMixedOperator: "Unexpected mix of '{{leftOperator}}' and '{{rightOperator}}'."
}
2020-03-07 11:45:40 +08:00
},
create(context) {
const sourceCode = context.getSourceCode();
const options = normalizeOptions(context.options[0]);
/**
* Checks whether a given node should be ignored by options or not.
2020-03-31 20:40:00 +08:00
* @param {ASTNode} node A node to check. This is a BinaryExpression
2020-03-07 11:45:40 +08:00
* node or a LogicalExpression node. This parent node is one of
* them, too.
* @returns {boolean} `true` if the node should be ignored.
*/
function shouldIgnore(node) {
const a = node;
const b = node.parent;
return (
2020-03-31 20:40:00 +08:00
!includesBothInAGroup(options.groups, a.operator, b.type === "ConditionalExpression" ? "?:" : b.operator) ||
2020-03-07 11:45:40 +08:00
(
options.allowSamePrecedence &&
astUtils.getPrecedence(a) === astUtils.getPrecedence(b)
)
);
}
/**
* Checks whether the operator of a given node is mixed with parent
* node's operator or not.
2020-03-31 20:40:00 +08:00
* @param {ASTNode} node A node to check. This is a BinaryExpression
2020-03-07 11:45:40 +08:00
* node or a LogicalExpression node. This parent node is one of
* them, too.
* @returns {boolean} `true` if the node was mixed.
*/
function isMixedWithParent(node) {
2020-03-31 20:40:00 +08:00
2020-03-07 11:45:40 +08:00
return (
node.operator !== node.parent.operator &&
!astUtils.isParenthesised(sourceCode, node)
);
}
2020-03-31 20:40:00 +08:00
/**
* Checks whether the operator of a given node is mixed with a
* conditional expression.
* @param {ASTNode} node A node to check. This is a conditional
* expression node
* @returns {boolean} `true` if the node was mixed.
*/
function isMixedWithConditionalParent(node) {
return !astUtils.isParenthesised(sourceCode, node) && !astUtils.isParenthesised(sourceCode, node.test);
}
2020-03-07 11:45:40 +08:00
/**
* Gets the operator token of a given node.
2020-03-31 20:40:00 +08:00
* @param {ASTNode} node A node to check. This is a BinaryExpression
2020-03-07 11:45:40 +08:00
* node or a LogicalExpression node.
* @returns {Token} The operator token of the node.
*/
function getOperatorToken(node) {
2020-03-31 20:40:00 +08:00
return sourceCode.getTokenAfter(getChildNode(node), astUtils.isNotClosingParenToken);
2020-03-07 11:45:40 +08:00
}
/**
* Reports both the operator of a given node and the operator of the
* parent node.
2020-03-31 20:40:00 +08:00
* @param {ASTNode} node A node to check. This is a BinaryExpression
2020-03-07 11:45:40 +08:00
* node or a LogicalExpression node. This parent node is one of
* them, too.
* @returns {void}
*/
function reportBothOperators(node) {
const parent = node.parent;
2020-03-31 20:40:00 +08:00
const left = (getChildNode(parent) === node) ? node : parent;
const right = (getChildNode(parent) !== node) ? node : parent;
2020-03-07 11:45:40 +08:00
const data = {
2020-03-31 20:40:00 +08:00
leftOperator: left.operator || "?:",
rightOperator: right.operator || "?:"
2020-03-07 11:45:40 +08:00
};
context.report({
node: left,
2020-03-31 20:40:00 +08:00
loc: getOperatorToken(left).loc,
2020-05-15 05:33:08 +08:00
messageId: "unexpectedMixedOperator",
2020-03-07 11:45:40 +08:00
data
});
context.report({
node: right,
2020-03-31 20:40:00 +08:00
loc: getOperatorToken(right).loc,
2020-05-15 05:33:08 +08:00
messageId: "unexpectedMixedOperator",
2020-03-07 11:45:40 +08:00
data
});
}
/**
* Checks between the operator of this node and the operator of the
* parent node.
2020-03-31 20:40:00 +08:00
* @param {ASTNode} node A node to check.
2020-03-07 11:45:40 +08:00
* @returns {void}
*/
function check(node) {
2020-03-31 20:40:00 +08:00
if (TARGET_NODE_TYPE.test(node.parent.type)) {
if (node.parent.type === "ConditionalExpression" && !shouldIgnore(node) && isMixedWithConditionalParent(node.parent)) {
reportBothOperators(node);
} else {
if (TARGET_NODE_TYPE.test(node.parent.type) &&
isMixedWithParent(node) &&
!shouldIgnore(node)
) {
reportBothOperators(node);
}
}
2020-03-07 11:45:40 +08:00
}
2020-03-31 20:40:00 +08:00
2020-03-07 11:45:40 +08:00
}
return {
BinaryExpression: check,
LogicalExpression: check
};
}
};