github-pages-deploy-action/node_modules/eslint/lib/rules/yoda.js

353 lines
12 KiB
JavaScript
Raw Permalink Normal View History

2020-03-07 11:45:40 +08:00
/**
* @fileoverview Rule to require or disallow yoda comparisons
* @author Nicholas C. Zakas
*/
"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
//--------------------------------------------------------------------------
/**
* Determines whether an operator is a comparison operator.
* @param {string} operator The operator to check.
* @returns {boolean} Whether or not it is a comparison operator.
*/
function isComparisonOperator(operator) {
2020-05-15 05:33:08 +08:00
return /^(==|===|!=|!==|<|>|<=|>=)$/u.test(operator);
2020-03-07 11:45:40 +08:00
}
/**
* Determines whether an operator is an equality operator.
* @param {string} operator The operator to check.
* @returns {boolean} Whether or not it is an equality operator.
*/
function isEqualityOperator(operator) {
2020-05-15 05:33:08 +08:00
return /^(==|===)$/u.test(operator);
2020-03-07 11:45:40 +08:00
}
/**
* Determines whether an operator is one used in a range test.
* Allowed operators are `<` and `<=`.
* @param {string} operator The operator to check.
* @returns {boolean} Whether the operator is used in range tests.
*/
function isRangeTestOperator(operator) {
return ["<", "<="].indexOf(operator) >= 0;
}
/**
* Determines whether a non-Literal node is a negative number that should be
* treated as if it were a single Literal node.
* @param {ASTNode} node Node to test.
* @returns {boolean} True if the node is a negative number that looks like a
* real literal and should be treated as such.
*/
2020-05-15 05:33:08 +08:00
function isNegativeNumericLiteral(node) {
return (
node.type === "UnaryExpression" &&
2020-03-07 11:45:40 +08:00
node.operator === "-" &&
node.prefix &&
2020-05-15 05:33:08 +08:00
astUtils.isNumericLiteral(node.argument)
);
}
/**
* Determines whether a node is a Template Literal which can be determined statically.
* @param {ASTNode} node Node to test
* @returns {boolean} True if the node is a Template Literal without expression.
*/
function isStaticTemplateLiteral(node) {
return node.type === "TemplateLiteral" && node.expressions.length === 0;
}
/**
* Determines whether a non-Literal node should be treated as a single Literal node.
* @param {ASTNode} node Node to test
* @returns {boolean} True if the node should be treated as a single Literal node.
*/
function looksLikeLiteral(node) {
return isNegativeNumericLiteral(node) || isStaticTemplateLiteral(node);
2020-03-07 11:45:40 +08:00
}
/**
* Attempts to derive a Literal node from nodes that are treated like literals.
* @param {ASTNode} node Node to normalize.
* @returns {ASTNode} One of the following options.
* 1. The original node if the node is already a Literal
* 2. A normalized Literal node with the negative number as the value if the
* node represents a negative number literal.
2020-05-15 05:33:08 +08:00
* 3. A normalized Literal node with the string as the value if the node is
* a Template Literal without expression.
2020-03-07 11:45:40 +08:00
* 4. Otherwise `null`.
*/
2020-05-15 05:33:08 +08:00
function getNormalizedLiteral(node) {
2020-03-07 11:45:40 +08:00
if (node.type === "Literal") {
return node;
}
2020-05-15 05:33:08 +08:00
if (isNegativeNumericLiteral(node)) {
2020-03-07 11:45:40 +08:00
return {
type: "Literal",
value: -node.argument.value,
raw: `-${node.argument.value}`
};
}
2020-05-15 05:33:08 +08:00
if (isStaticTemplateLiteral(node)) {
2020-03-07 11:45:40 +08:00
return {
type: "Literal",
2020-05-15 05:33:08 +08:00
value: node.quasis[0].value.cooked,
raw: node.quasis[0].value.raw
2020-03-07 11:45:40 +08:00
};
}
return null;
}
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
type: "suggestion",
docs: {
2020-05-15 05:33:08 +08:00
description: 'require or disallow "Yoda" conditions',
2020-03-07 11:45:40 +08:00
category: "Best Practices",
recommended: false,
url: "https://eslint.org/docs/rules/yoda"
},
schema: [
{
enum: ["always", "never"]
},
{
type: "object",
properties: {
exceptRange: {
type: "boolean",
default: false
},
onlyEquality: {
type: "boolean",
default: false
}
},
additionalProperties: false
}
],
fixable: "code",
messages: {
2020-05-15 05:33:08 +08:00
expected:
"Expected literal to be on the {{expectedSide}} side of {{operator}}."
2020-03-07 11:45:40 +08:00
}
},
create(context) {
// Default to "never" (!always) if no option
2020-05-15 05:33:08 +08:00
const always = context.options[0] === "always";
const exceptRange =
context.options[1] && context.options[1].exceptRange;
const onlyEquality =
context.options[1] && context.options[1].onlyEquality;
2020-03-07 11:45:40 +08:00
const sourceCode = context.getSourceCode();
/**
* Determines whether node represents a range test.
* A range test is a "between" test like `(0 <= x && x < 1)` or an "outside"
* test like `(x < 0 || 1 <= x)`. It must be wrapped in parentheses, and
* both operators must be `<` or `<=`. Finally, the literal on the left side
* must be less than or equal to the literal on the right side so that the
* test makes any sense.
* @param {ASTNode} node LogicalExpression node to test.
* @returns {boolean} Whether node is a range test.
*/
function isRangeTest(node) {
const left = node.left,
right = node.right;
/**
* Determines whether node is of the form `0 <= x && x < 1`.
* @returns {boolean} Whether node is a "between" range test.
*/
function isBetweenTest() {
2020-09-13 06:19:45 +08:00
if (node.operator === "&&" && astUtils.isSameReference(left.right, right.left)) {
2020-05-15 05:33:08 +08:00
const leftLiteral = getNormalizedLiteral(left.left);
const rightLiteral = getNormalizedLiteral(right.right);
if (leftLiteral === null && rightLiteral === null) {
return false;
}
if (rightLiteral === null || leftLiteral === null) {
return true;
}
2020-03-07 11:45:40 +08:00
2020-05-15 05:33:08 +08:00
if (leftLiteral.value <= rightLiteral.value) {
return true;
}
}
return false;
2020-03-07 11:45:40 +08:00
}
/**
* Determines whether node is of the form `x < 0 || 1 <= x`.
* @returns {boolean} Whether node is an "outside" range test.
*/
function isOutsideTest() {
2020-09-13 06:19:45 +08:00
if (node.operator === "||" && astUtils.isSameReference(left.left, right.right)) {
2020-05-15 05:33:08 +08:00
const leftLiteral = getNormalizedLiteral(left.right);
const rightLiteral = getNormalizedLiteral(right.left);
2020-03-07 11:45:40 +08:00
2020-05-15 05:33:08 +08:00
if (leftLiteral === null && rightLiteral === null) {
return false;
}
if (rightLiteral === null || leftLiteral === null) {
return true;
}
if (leftLiteral.value <= rightLiteral.value) {
return true;
}
}
return false;
2020-03-07 11:45:40 +08:00
}
/**
* Determines whether node is wrapped in parentheses.
* @returns {boolean} Whether node is preceded immediately by an open
* paren token and followed immediately by a close
* paren token.
*/
function isParenWrapped() {
return astUtils.isParenthesised(sourceCode, node);
}
2020-05-15 05:33:08 +08:00
return (
node.type === "LogicalExpression" &&
2020-03-07 11:45:40 +08:00
left.type === "BinaryExpression" &&
right.type === "BinaryExpression" &&
isRangeTestOperator(left.operator) &&
isRangeTestOperator(right.operator) &&
(isBetweenTest() || isOutsideTest()) &&
2020-05-15 05:33:08 +08:00
isParenWrapped()
);
2020-03-07 11:45:40 +08:00
}
const OPERATOR_FLIP_MAP = {
"===": "===",
"!==": "!==",
"==": "==",
"!=": "!=",
"<": ">",
">": "<",
"<=": ">=",
">=": "<="
};
/**
* Returns a string representation of a BinaryExpression node with its sides/operator flipped around.
* @param {ASTNode} node The BinaryExpression node
* @returns {string} A string representation of the node with the sides and operator flipped
*/
function getFlippedString(node) {
2020-03-31 20:40:00 +08:00
const tokenBefore = sourceCode.getTokenBefore(node);
2020-05-15 05:33:08 +08:00
const operatorToken = sourceCode.getFirstTokenBetween(
node.left,
node.right,
token => token.value === node.operator
);
const textBeforeOperator = sourceCode
.getText()
.slice(
sourceCode.getTokenBefore(operatorToken).range[1],
operatorToken.range[0]
);
const textAfterOperator = sourceCode
.getText()
.slice(
operatorToken.range[1],
sourceCode.getTokenAfter(operatorToken).range[0]
);
const leftText = sourceCode
.getText()
.slice(
node.range[0],
sourceCode.getTokenBefore(operatorToken).range[1]
);
2020-03-31 20:40:00 +08:00
const firstRightToken = sourceCode.getTokenAfter(operatorToken);
2020-05-15 05:33:08 +08:00
const rightText = sourceCode
.getText()
.slice(firstRightToken.range[0], node.range[1]);
2020-03-07 11:45:40 +08:00
2020-03-31 20:40:00 +08:00
let prefix = "";
2020-05-15 05:33:08 +08:00
if (
tokenBefore &&
tokenBefore.range[1] === node.range[0] &&
!astUtils.canTokensBeAdjacent(tokenBefore, firstRightToken)
) {
2020-03-31 20:40:00 +08:00
prefix = " ";
}
2020-05-15 05:33:08 +08:00
return (
prefix +
rightText +
textBeforeOperator +
OPERATOR_FLIP_MAP[operatorToken.value] +
textAfterOperator +
leftText
);
2020-03-07 11:45:40 +08:00
}
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
return {
BinaryExpression(node) {
const expectedLiteral = always ? node.left : node.right;
const expectedNonLiteral = always ? node.right : node.left;
// If `expectedLiteral` is not a literal, and `expectedNonLiteral` is a literal, raise an error.
if (
2020-05-15 05:33:08 +08:00
(expectedNonLiteral.type === "Literal" ||
looksLikeLiteral(expectedNonLiteral)) &&
!(
expectedLiteral.type === "Literal" ||
looksLikeLiteral(expectedLiteral)
) &&
2020-03-07 11:45:40 +08:00
!(!isEqualityOperator(node.operator) && onlyEquality) &&
isComparisonOperator(node.operator) &&
!(exceptRange && isRangeTest(context.getAncestors().pop()))
) {
context.report({
node,
messageId: "expected",
data: {
operator: node.operator,
expectedSide: always ? "left" : "right"
},
2020-05-15 05:33:08 +08:00
fix: fixer =>
fixer.replaceText(node, getFlippedString(node))
2020-03-07 11:45:40 +08:00
});
}
}
};
}
};