4 * Copyright 2017 Palantir Technologies, Inc.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 Object.defineProperty(exports, "__esModule", { value: true });
19 var tslib_1 = require("tslib");
20 var tsutils_1 = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 var preferTemplate_examples_1 = require("./code-examples/preferTemplate.examples");
24 var OPTION_SINGLE_CONCAT = "allow-single-concat";
25 var Rule = /** @class */ (function (_super) {
26 tslib_1.__extends(Rule, _super);
28 return _super !== null && _super.apply(this, arguments) || this;
30 Rule.prototype.apply = function (sourceFile) {
31 if (sourceFile.isDeclarationFile) {
32 return []; // Not possible in a declaration file
34 var allowSingleConcat = this.ruleArguments.indexOf(OPTION_SINGLE_CONCAT) !== -1;
35 return this.applyWithFunction(sourceFile, walk, { allowSingleConcat: allowSingleConcat });
37 /* tslint:disable:object-literal-sort-keys */
39 ruleName: "prefer-template",
40 description: "Prefer a template expression over string literal concatenation.",
41 optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n If `", "` is specified, then a single concatenation (`x + y`) is allowed, but not more (`x + y + z`)."], ["\n If \\`", "\\` is specified, then a single concatenation (\\`x + y\\`) is allowed, but not more (\\`x + y + z\\`)."])), OPTION_SINGLE_CONCAT),
44 enum: [OPTION_SINGLE_CONCAT],
46 optionExamples: [true, [true, OPTION_SINGLE_CONCAT]],
48 typescriptOnly: false,
49 codeExamples: preferTemplate_examples_1.codeExamples,
51 /* tslint:enable:object-literal-sort-keys */
52 Rule.FAILURE_STRING = "Use a template literal instead of concatenating with a string literal.";
53 Rule.FAILURE_STRING_MULTILINE = "Use a multiline template literal instead of concatenating string literals with newlines.";
55 }(Lint.Rules.AbstractRule));
58 var allowSingleConcat = ctx.options.allowSingleConcat;
59 return ts.forEachChild(ctx.sourceFile, function cb(node) {
60 var failure = getError(node, allowSingleConcat);
61 if (failure !== undefined) {
62 ctx.addFailureAtNode(node, failure);
65 return ts.forEachChild(node, cb);
69 function getError(node, allowSingleConcat) {
70 if (!isPlusExpression(node)) {
73 var left = node.left, right = node.right;
74 var l = isStringLike(left);
75 var r = isStringLike(right);
77 // They're both strings.
78 // If they're joined by a newline, recommend a template expression instead.
79 // Otherwise ignore. ("a" + "b", probably writing a long newline-less string on many lines.)
80 return containsNewline(left) || containsNewline(right)
81 ? Rule.FAILURE_STRING_MULTILINE
85 // Watch out for `"a" + b + c`. Parsed as `("a" + b) + c`.
86 return containsAnyStringLiterals(left) ? Rule.FAILURE_STRING : undefined;
90 return !allowSingleConcat ? Rule.FAILURE_STRING : undefined;
94 // If LHS consists of only string literals (as in `"a" + "b" + "c"`, allow it.)
95 return !containsOnlyStringLiterals(left) && (!allowSingleConcat || isPlusExpression(left))
100 function containsNewline(node) {
101 if (node.kind === ts.SyntaxKind.TemplateExpression) {
102 return node.templateSpans.some(function (_a) {
103 var text = _a.literal.text;
104 return text.includes("\n");
108 return node.text.includes("\n");
111 function containsOnlyStringLiterals(node) {
112 return (isPlusExpression(node) &&
113 isStringLike(node.right) &&
114 (isStringLike(node.left) || containsAnyStringLiterals(node.left)));
116 function containsAnyStringLiterals(node) {
117 return (isPlusExpression(node) &&
118 (isStringLike(node.right) ||
119 isStringLike(node.left) ||
120 containsAnyStringLiterals(node.left)));
122 function isPlusExpression(node) {
123 return tsutils_1.isBinaryExpression(node) && node.operatorToken.kind === ts.SyntaxKind.PlusToken;
125 function isStringLike(node) {
127 case ts.SyntaxKind.StringLiteral:
128 case ts.SyntaxKind.NoSubstitutionTemplateLiteral:
129 case ts.SyntaxKind.TemplateExpression:
135 var templateObject_1;