|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
-
-
- "use strict";
-
-
-
-
-
- const astUtils = require("../util/ast-utils");
-
-
-
-
-
-
- function isConcatenation(node) {
- return node.type === "BinaryExpression" && node.operator === "+";
- }
-
-
- function getTopConcatBinaryExpression(node) {
- let currentNode = node;
-
- while (isConcatenation(currentNode.parent)) {
- currentNode = currentNode.parent;
- }
- return currentNode;
- }
-
-
- function isOctalEscapeSequence(node) {
-
-
- const isStringLiteral = node.type === "Literal" && typeof node.value === "string";
-
- if (!isStringLiteral) {
- return false;
- }
-
- const match = node.raw.match(/^([^\\]|\\[^0-7])*\\([0-7]{1,3})/);
-
- if (match) {
-
-
- if (match[2] !== "0" || typeof match[3] !== "undefined") {
- return true;
- }
- }
- return false;
- }
-
-
- function hasOctalEscapeSequence(node) {
- if (isConcatenation(node)) {
- return hasOctalEscapeSequence(node.left) || hasOctalEscapeSequence(node.right);
- }
-
- return isOctalEscapeSequence(node);
- }
-
-
- function hasStringLiteral(node) {
- if (isConcatenation(node)) {
-
-
- return hasStringLiteral(node.right) || hasStringLiteral(node.left);
- }
- return astUtils.isStringLiteral(node);
- }
-
-
- function hasNonStringLiteral(node) {
- if (isConcatenation(node)) {
-
-
- return hasNonStringLiteral(node.right) || hasNonStringLiteral(node.left);
- }
- return !astUtils.isStringLiteral(node);
- }
-
-
- function startsWithTemplateCurly(node) {
- if (node.type === "BinaryExpression") {
- return startsWithTemplateCurly(node.left);
- }
- if (node.type === "TemplateLiteral") {
- return node.expressions.length && node.quasis.length && node.quasis[0].range[0] === node.quasis[0].range[1];
- }
- return node.type !== "Literal" || typeof node.value !== "string";
- }
-
-
- function endsWithTemplateCurly(node) {
- if (node.type === "BinaryExpression") {
- return startsWithTemplateCurly(node.right);
- }
- if (node.type === "TemplateLiteral") {
- return node.expressions.length && node.quasis.length && node.quasis[node.quasis.length - 1].range[0] === node.quasis[node.quasis.length - 1].range[1];
- }
- return node.type !== "Literal" || typeof node.value !== "string";
- }
-
-
-
-
-
- module.exports = {
- meta: {
- type: "suggestion",
-
- docs: {
- description: "require template literals instead of string concatenation",
- category: "ECMAScript 6",
- recommended: false,
- url: "https://eslint.org/docs/rules/prefer-template"
- },
-
- schema: [],
- fixable: "code"
- },
-
- create(context) {
- const sourceCode = context.getSourceCode();
- let done = Object.create(null);
-
-
-
- function getTextBetween(node1, node2) {
- const allTokens = [node1].concat(sourceCode.getTokensBetween(node1, node2)).concat(node2);
- const sourceText = sourceCode.getText();
-
- return allTokens.slice(0, -1).reduce((accumulator, token, index) => accumulator + sourceText.slice(token.range[1], allTokens[index + 1].range[0]), "");
- }
-
-
-
- function getTemplateLiteral(currentNode, textBeforeNode, textAfterNode) {
- if (currentNode.type === "Literal" && typeof currentNode.value === "string") {
-
-
-
- return `\`${currentNode.raw.slice(1, -1).replace(/\\*(\${|`)/g, matched => {
- if (matched.lastIndexOf("\\") % 2) {
- return `\\${matched}`;
- }
- return matched;
-
- // Unescape any quotes that appear in the original Literal that no longer need to be escaped.
- }).replace(new RegExp(`\\\\${currentNode.raw[0]}`, "g"), currentNode.raw[0])}\``;
- }
-
- if (currentNode.type === "TemplateLiteral") {
- return sourceCode.getText(currentNode);
- }
-
- if (isConcatenation(currentNode) && hasStringLiteral(currentNode) && hasNonStringLiteral(currentNode)) {
- const plusSign = sourceCode.getFirstTokenBetween(currentNode.left, currentNode.right, token => token.value === "+");
- const textBeforePlus = getTextBetween(currentNode.left, plusSign);
- const textAfterPlus = getTextBetween(plusSign, currentNode.right);
- const leftEndsWithCurly = endsWithTemplateCurly(currentNode.left);
- const rightStartsWithCurly = startsWithTemplateCurly(currentNode.right);
-
- if (leftEndsWithCurly) {
-
-
-
- return getTemplateLiteral(currentNode.left, textBeforeNode, textBeforePlus + textAfterPlus).slice(0, -1) +
- getTemplateLiteral(currentNode.right, null, textAfterNode).slice(1);
- }
- if (rightStartsWithCurly) {
-
-
-
- return getTemplateLiteral(currentNode.left, textBeforeNode, null).slice(0, -1) +
- getTemplateLiteral(currentNode.right, textBeforePlus + textAfterPlus, textAfterNode).slice(1);
- }
-
-
-
- return `${getTemplateLiteral(currentNode.left, textBeforeNode, null)}${textBeforePlus}+${textAfterPlus}${getTemplateLiteral(currentNode.right, textAfterNode, null)}`;
- }
-
- return `\`\${${textBeforeNode || ""}${sourceCode.getText(currentNode)}${textAfterNode || ""}}\``;
- }
-
-
-
- function fixNonStringBinaryExpression(fixer, node) {
- const topBinaryExpr = getTopConcatBinaryExpression(node.parent);
-
- if (hasOctalEscapeSequence(topBinaryExpr)) {
- return null;
- }
-
- return fixer.replaceText(topBinaryExpr, getTemplateLiteral(topBinaryExpr, null, null));
- }
-
-
-
- function checkForStringConcat(node) {
- if (!astUtils.isStringLiteral(node) || !isConcatenation(node.parent)) {
- return;
- }
-
- const topBinaryExpr = getTopConcatBinaryExpression(node.parent);
-
-
- if (done[topBinaryExpr.range[0]]) {
- return;
- }
- done[topBinaryExpr.range[0]] = true;
-
- if (hasNonStringLiteral(topBinaryExpr)) {
- context.report({
- node: topBinaryExpr,
- message: "Unexpected string concatenation.",
- fix: fixer => fixNonStringBinaryExpression(fixer, node)
- });
- }
- }
-
- return {
- Program() {
- done = Object.create(null);
- },
-
- Literal: checkForStringConcat,
- TemplateLiteral: checkForStringConcat
- };
- }
- };
|