github-pages-deploy-action/node_modules/eslint/lib/rules/no-implicit-coercion.js

306 lines
10 KiB
JavaScript
Raw Normal View History

2020-03-07 11:45:40 +08:00
/**
* @fileoverview A rule to disallow the type conversions with shorter notations.
* @author Toru Nagashima
*/
"use strict";
2020-03-31 20:40:00 +08:00
const astUtils = require("./utils/ast-utils");
2020-03-07 11:45:40 +08:00
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
const INDEX_OF_PATTERN = /^(?:i|lastI)ndexOf$/u;
const ALLOWABLE_OPERATORS = ["~", "!!", "+", "*"];
/**
* Parses and normalizes an option object.
2020-03-31 20:40:00 +08:00
* @param {Object} options An option object to parse.
2020-03-07 11:45:40 +08:00
* @returns {Object} The parsed and normalized option object.
*/
function parseOptions(options) {
return {
boolean: "boolean" in options ? options.boolean : true,
number: "number" in options ? options.number : true,
string: "string" in options ? options.string : true,
allow: options.allow || []
};
}
/**
* Checks whether or not a node is a double logical nigating.
2020-03-31 20:40:00 +08:00
* @param {ASTNode} node An UnaryExpression node to check.
2020-03-07 11:45:40 +08:00
* @returns {boolean} Whether or not the node is a double logical nigating.
*/
function isDoubleLogicalNegating(node) {
return (
node.operator === "!" &&
node.argument.type === "UnaryExpression" &&
node.argument.operator === "!"
);
}
/**
* Checks whether or not a node is a binary negating of `.indexOf()` method calling.
2020-03-31 20:40:00 +08:00
* @param {ASTNode} node An UnaryExpression node to check.
2020-03-07 11:45:40 +08:00
* @returns {boolean} Whether or not the node is a binary negating of `.indexOf()` method calling.
*/
function isBinaryNegatingOfIndexOf(node) {
2020-09-13 06:19:45 +08:00
if (node.operator !== "~") {
return false;
}
const callNode = astUtils.skipChainExpression(node.argument);
2020-03-07 11:45:40 +08:00
return (
2020-09-13 06:19:45 +08:00
callNode.type === "CallExpression" &&
astUtils.isSpecificMemberAccess(callNode.callee, null, INDEX_OF_PATTERN)
2020-03-07 11:45:40 +08:00
);
}
/**
* Checks whether or not a node is a multiplying by one.
2020-03-31 20:40:00 +08:00
* @param {BinaryExpression} node A BinaryExpression node to check.
2020-03-07 11:45:40 +08:00
* @returns {boolean} Whether or not the node is a multiplying by one.
*/
function isMultiplyByOne(node) {
return node.operator === "*" && (
node.left.type === "Literal" && node.left.value === 1 ||
node.right.type === "Literal" && node.right.value === 1
);
}
/**
* Checks whether the result of a node is numeric or not
* @param {ASTNode} node The node to test
* @returns {boolean} true if the node is a number literal or a `Number()`, `parseInt` or `parseFloat` call
*/
function isNumeric(node) {
return (
node.type === "Literal" && typeof node.value === "number" ||
node.type === "CallExpression" && (
node.callee.name === "Number" ||
node.callee.name === "parseInt" ||
node.callee.name === "parseFloat"
)
);
}
/**
* Returns the first non-numeric operand in a BinaryExpression. Designed to be
* used from bottom to up since it walks up the BinaryExpression trees using
* node.parent to find the result.
* @param {BinaryExpression} node The BinaryExpression node to be walked up on
* @returns {ASTNode|null} The first non-numeric item in the BinaryExpression tree or null
*/
function getNonNumericOperand(node) {
const left = node.left,
right = node.right;
if (right.type !== "BinaryExpression" && !isNumeric(right)) {
return right;
}
if (left.type !== "BinaryExpression" && !isNumeric(left)) {
return left;
}
return null;
}
/**
* Checks whether a node is an empty string literal or not.
* @param {ASTNode} node The node to check.
* @returns {boolean} Whether or not the passed in node is an
* empty string literal or not.
*/
function isEmptyString(node) {
return astUtils.isStringLiteral(node) && (node.value === "" || (node.type === "TemplateLiteral" && node.quasis.length === 1 && node.quasis[0].value.cooked === ""));
}
/**
* Checks whether or not a node is a concatenating with an empty string.
2020-03-31 20:40:00 +08:00
* @param {ASTNode} node A BinaryExpression node to check.
2020-03-07 11:45:40 +08:00
* @returns {boolean} Whether or not the node is a concatenating with an empty string.
*/
function isConcatWithEmptyString(node) {
return node.operator === "+" && (
(isEmptyString(node.left) && !astUtils.isStringLiteral(node.right)) ||
(isEmptyString(node.right) && !astUtils.isStringLiteral(node.left))
);
}
/**
* Checks whether or not a node is appended with an empty string.
2020-03-31 20:40:00 +08:00
* @param {ASTNode} node An AssignmentExpression node to check.
2020-03-07 11:45:40 +08:00
* @returns {boolean} Whether or not the node is appended with an empty string.
*/
function isAppendEmptyString(node) {
return node.operator === "+=" && isEmptyString(node.right);
}
/**
* Returns the operand that is not an empty string from a flagged BinaryExpression.
2020-03-31 20:40:00 +08:00
* @param {ASTNode} node The flagged BinaryExpression node to check.
2020-03-07 11:45:40 +08:00
* @returns {ASTNode} The operand that is not an empty string from a flagged BinaryExpression.
*/
function getNonEmptyOperand(node) {
return isEmptyString(node.left) ? node.right : node.left;
}
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
type: "suggestion",
docs: {
description: "disallow shorthand type conversions",
category: "Best Practices",
recommended: false,
url: "https://eslint.org/docs/rules/no-implicit-coercion"
},
fixable: "code",
schema: [{
type: "object",
properties: {
boolean: {
type: "boolean",
default: true
},
number: {
type: "boolean",
default: true
},
string: {
type: "boolean",
default: true
},
allow: {
type: "array",
items: {
enum: ALLOWABLE_OPERATORS
},
uniqueItems: true
}
},
additionalProperties: false
2020-05-15 05:33:08 +08:00
}],
messages: {
useRecommendation: "use `{{recommendation}}` instead."
}
2020-03-07 11:45:40 +08:00
},
create(context) {
const options = parseOptions(context.options[0] || {});
const sourceCode = context.getSourceCode();
/**
* Reports an error and autofixes the node
2020-03-31 20:40:00 +08:00
* @param {ASTNode} node An ast node to report the error on.
* @param {string} recommendation The recommended code for the issue
* @param {bool} shouldFix Whether this report should fix the node
2020-03-07 11:45:40 +08:00
* @returns {void}
*/
function report(node, recommendation, shouldFix) {
context.report({
node,
2020-05-15 05:33:08 +08:00
messageId: "useRecommendation",
2020-03-07 11:45:40 +08:00
data: {
recommendation
},
fix(fixer) {
if (!shouldFix) {
return null;
}
const tokenBefore = sourceCode.getTokenBefore(node);
if (
tokenBefore &&
tokenBefore.range[1] === node.range[0] &&
!astUtils.canTokensBeAdjacent(tokenBefore, recommendation)
) {
return fixer.replaceText(node, ` ${recommendation}`);
}
return fixer.replaceText(node, recommendation);
}
});
}
return {
UnaryExpression(node) {
let operatorAllowed;
// !!foo
operatorAllowed = options.allow.indexOf("!!") >= 0;
if (!operatorAllowed && options.boolean && isDoubleLogicalNegating(node)) {
const recommendation = `Boolean(${sourceCode.getText(node.argument.argument)})`;
report(node, recommendation, true);
}
// ~foo.indexOf(bar)
operatorAllowed = options.allow.indexOf("~") >= 0;
if (!operatorAllowed && options.boolean && isBinaryNegatingOfIndexOf(node)) {
2020-09-13 06:19:45 +08:00
// `foo?.indexOf(bar) !== -1` will be true (== found) if the `foo` is nullish. So use `>= 0` in that case.
const comparison = node.argument.type === "ChainExpression" ? ">= 0" : "!== -1";
const recommendation = `${sourceCode.getText(node.argument)} ${comparison}`;
2020-03-07 11:45:40 +08:00
report(node, recommendation, false);
}
// +foo
operatorAllowed = options.allow.indexOf("+") >= 0;
if (!operatorAllowed && options.number && node.operator === "+" && !isNumeric(node.argument)) {
const recommendation = `Number(${sourceCode.getText(node.argument)})`;
report(node, recommendation, true);
}
},
// Use `:exit` to prevent double reporting
"BinaryExpression:exit"(node) {
let operatorAllowed;
// 1 * foo
operatorAllowed = options.allow.indexOf("*") >= 0;
const nonNumericOperand = !operatorAllowed && options.number && isMultiplyByOne(node) && getNonNumericOperand(node);
if (nonNumericOperand) {
const recommendation = `Number(${sourceCode.getText(nonNumericOperand)})`;
report(node, recommendation, true);
}
// "" + foo
operatorAllowed = options.allow.indexOf("+") >= 0;
if (!operatorAllowed && options.string && isConcatWithEmptyString(node)) {
const recommendation = `String(${sourceCode.getText(getNonEmptyOperand(node))})`;
report(node, recommendation, true);
}
},
AssignmentExpression(node) {
// foo += ""
const operatorAllowed = options.allow.indexOf("+") >= 0;
if (!operatorAllowed && options.string && isAppendEmptyString(node)) {
const code = sourceCode.getText(getNonEmptyOperand(node));
const recommendation = `${code} = String(${code})`;
report(node, recommendation, true);
}
}
};
}
};