github-pages-deploy-action/node_modules/eslint/lib/rules/no-useless-concat.js

116 lines
3.2 KiB
JavaScript
Raw Permalink Normal View History

2020-03-07 11:45:40 +08:00
/**
2020-05-15 05:33:08 +08:00
* @fileoverview disallow unnecessary concatenation of template strings
2020-03-07 11:45:40 +08:00
* @author Henry Zhu
*/
"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
//------------------------------------------------------------------------------
/**
* Checks whether or not a given node is a concatenation.
2020-03-31 20:40:00 +08:00
* @param {ASTNode} node A node to check.
2020-03-07 11:45:40 +08:00
* @returns {boolean} `true` if the node is a concatenation.
*/
function isConcatenation(node) {
return node.type === "BinaryExpression" && node.operator === "+";
}
/**
* Checks if the given token is a `+` token or not.
2020-03-31 20:40:00 +08:00
* @param {Token} token The token to check.
2020-03-07 11:45:40 +08:00
* @returns {boolean} `true` if the token is a `+` token.
*/
function isConcatOperatorToken(token) {
return token.value === "+" && token.type === "Punctuator";
}
/**
* Get's the right most node on the left side of a BinaryExpression with + operator.
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 {ASTNode} node
*/
function getLeft(node) {
let left = node.left;
while (isConcatenation(left)) {
left = left.right;
}
return left;
}
/**
* Get's the left most node on the right side of a BinaryExpression with + operator.
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 {ASTNode} node
*/
function getRight(node) {
let right = node.right;
while (isConcatenation(right)) {
right = right.left;
}
return right;
}
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
type: "suggestion",
docs: {
description: "disallow unnecessary concatenation of literals or template literals",
category: "Best Practices",
recommended: false,
url: "https://eslint.org/docs/rules/no-useless-concat"
},
2020-05-15 05:33:08 +08:00
schema: [],
messages: {
unexpectedConcat: "Unexpected string concatenation of literals."
}
2020-03-07 11:45:40 +08:00
},
create(context) {
const sourceCode = context.getSourceCode();
return {
BinaryExpression(node) {
// check if not concatenation
if (node.operator !== "+") {
return;
}
// account for the `foo + "a" + "b"` case
const left = getLeft(node);
const right = getRight(node);
if (astUtils.isStringLiteral(left) &&
astUtils.isStringLiteral(right) &&
astUtils.isTokenOnSameLine(left, right)
) {
const operatorToken = sourceCode.getFirstTokenBetween(left, right, isConcatOperatorToken);
context.report({
node,
2020-05-15 05:33:08 +08:00
loc: operatorToken.loc,
messageId: "unexpectedConcat"
2020-03-07 11:45:40 +08:00
});
}
}
};
}
};