|
| 1 | +import {isStringLiteral} from './ast/index.js'; |
| 2 | +import {fixSpaceAroundKeyword} from './fix/index.js'; |
| 3 | +import { |
| 4 | + escapeString, |
| 5 | + getParenthesizedRange, |
| 6 | + isParenthesized, |
| 7 | + needsSemicolon, |
| 8 | +} from './utils/index.js'; |
| 9 | +import escapeTemplateElementRaw from './utils/escape-template-element-raw.js'; |
| 10 | + |
| 11 | +const MESSAGE_ID = 'no-useless-concat'; |
| 12 | +const messages = { |
| 13 | + [MESSAGE_ID]: 'Do not concatenate two literals, combine them into one.', |
| 14 | +}; |
| 15 | + |
| 16 | +// A `+` operand that is a template literal can never be tagged, since tagging produces a `TaggedTemplateExpression` node instead. |
| 17 | +const isStringish = node => isStringLiteral(node) || node.type === 'TemplateLiteral'; |
| 18 | + |
| 19 | +// The string value of a literal, or `undefined` if it's a template literal with expressions. |
| 20 | +function getStringValue(node) { |
| 21 | + if (isStringLiteral(node)) { |
| 22 | + return node.value; |
| 23 | + } |
| 24 | + |
| 25 | + if (node.expressions.length === 0) { |
| 26 | + return node.quasis[0].value.cooked; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +// Legacy octal (`\1`, `\012`) and `\8`/`\9` escapes are valid in sloppy-mode string literals but are syntax errors inside template literals. |
| 31 | +const hasTemplateIncompatibleEscape = raw => /(?<=(?:^|[^\\])(?:\\\\)*)\\(?:[1-9]|0\d)/v.test(raw); |
| 32 | + |
| 33 | +// The raw inner content of a literal as it would appear inside a template literal. |
| 34 | +function toTemplateElementRaw(node, sourceCode) { |
| 35 | + if (node.type === 'TemplateLiteral') { |
| 36 | + return sourceCode.getText(node).slice(1, -1); |
| 37 | + } |
| 38 | + |
| 39 | + return escapeTemplateElementRaw(node.raw.slice(1, -1)); |
| 40 | +} |
| 41 | + |
| 42 | +/** @param {import('eslint').Rule.RuleContext} context */ |
| 43 | +const create = context => { |
| 44 | + const {sourceCode} = context; |
| 45 | + |
| 46 | + context.on('BinaryExpression', node => { |
| 47 | + const {right} = node; |
| 48 | + if (node.operator !== '+' || !isStringish(right)) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + // The literal directly to the left of the `+`. For `'a' + 'b'` it's the left operand; for `foo + 'a' + 'b'` it's the right operand of the left `+`. |
| 53 | + let left; |
| 54 | + if (isStringish(node.left)) { |
| 55 | + left = node.left; |
| 56 | + } else if (node.left.type === 'BinaryExpression' && node.left.operator === '+' && isStringish(node.left.right)) { |
| 57 | + left = node.left.right; |
| 58 | + } else { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + // Allow concatenation spanning multiple lines, it's often used intentionally for readability. |
| 63 | + if (sourceCode.getLoc(left).end.line !== sourceCode.getLoc(right).start.line) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + // Whether `left` is the right operand of a preceding `+`, as in `foo + 'a' + 'b'`. |
| 68 | + const isChain = left !== node.left; |
| 69 | + |
| 70 | + const leftValue = getStringValue(left); |
| 71 | + const rightValue = getStringValue(right); |
| 72 | + const replacement = leftValue === undefined || rightValue === undefined |
| 73 | + ? `\`${toTemplateElementRaw(left, sourceCode)}${toTemplateElementRaw(right, sourceCode)}\`` |
| 74 | + : escapeString(leftValue + rightValue); |
| 75 | + |
| 76 | + const operatorToken = sourceCode.getTokenBefore(right, token => token.type === 'Punctuator' && token.value === '+'); |
| 77 | + |
| 78 | + return { |
| 79 | + node, |
| 80 | + loc: sourceCode.getLoc(operatorToken), |
| 81 | + messageId: MESSAGE_ID, |
| 82 | + * fix(fixer, {abort}) { |
| 83 | + const range = [getParenthesizedRange(left, context)[0], getParenthesizedRange(right, context)[1]]; |
| 84 | + |
| 85 | + // Don't drop comments inside the replaced range. |
| 86 | + if (sourceCode.getCommentsInside(node).some(comment => { |
| 87 | + const [start, end] = sourceCode.getRange(comment); |
| 88 | + return start >= range[0] && end <= range[1]; |
| 89 | + })) { |
| 90 | + return abort(); |
| 91 | + } |
| 92 | + |
| 93 | + // In a chain like `(foo + 'a') + 'b'`, the parentheses around the left side sit inside the replaced range, so folding would drop the closing parenthesis. |
| 94 | + if (isChain && isParenthesized(node.left, context)) { |
| 95 | + return abort(); |
| 96 | + } |
| 97 | + |
| 98 | + // In a chain like `foo + 'a' + `${bar()}``, folding moves the right template's expressions before the left side is coerced, changing the order of side effects. |
| 99 | + if (isChain && rightValue === undefined) { |
| 100 | + return abort(); |
| 101 | + } |
| 102 | + |
| 103 | + let text = replacement; |
| 104 | + |
| 105 | + // A template literal replacement starts with a backtick, which needs care around what precedes it. |
| 106 | + if (text.startsWith('`')) { |
| 107 | + // Don't move a string literal's template-incompatible escape into a template literal, it would be a syntax error. |
| 108 | + if (hasTemplateIncompatibleEscape(text)) { |
| 109 | + return abort(); |
| 110 | + } |
| 111 | + |
| 112 | + // A backtick after a keyword like `return` would form a tagged template, so keep them apart. |
| 113 | + yield fixSpaceAroundKeyword(fixer, node, context); |
| 114 | + |
| 115 | + // A backtick after an expression on a previous line would attach as a tagged template, so add the semicolon that automatic semicolon insertion no longer provides. |
| 116 | + if (needsSemicolon(sourceCode.getTokenBefore({range}), context, text)) { |
| 117 | + text = `;${text}`; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + yield fixer.replaceTextRange(range, text); |
| 122 | + }, |
| 123 | + }; |
| 124 | + }); |
| 125 | +}; |
| 126 | + |
| 127 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 128 | +const config = { |
| 129 | + create, |
| 130 | + meta: { |
| 131 | + type: 'suggestion', |
| 132 | + docs: { |
| 133 | + description: 'Disallow useless concatenation of literals.', |
| 134 | + recommended: 'unopinionated', |
| 135 | + }, |
| 136 | + fixable: 'code', |
| 137 | + messages, |
| 138 | + languages: [ |
| 139 | + 'js/js', |
| 140 | + ], |
| 141 | + }, |
| 142 | +}; |
| 143 | + |
| 144 | +export default config; |
0 commit comments