.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / preferTemplateRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2017 Palantir Technologies, Inc.
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
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);
27     function Rule() {
28         return _super !== null && _super.apply(this, arguments) || this;
29     }
30     Rule.prototype.apply = function (sourceFile) {
31         if (sourceFile.isDeclarationFile) {
32             return []; // Not possible in a declaration file
33         }
34         var allowSingleConcat = this.ruleArguments.indexOf(OPTION_SINGLE_CONCAT) !== -1;
35         return this.applyWithFunction(sourceFile, walk, { allowSingleConcat: allowSingleConcat });
36     };
37     /* tslint:disable:object-literal-sort-keys */
38     Rule.metadata = {
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),
42         options: {
43             type: "string",
44             enum: [OPTION_SINGLE_CONCAT],
45         },
46         optionExamples: [true, [true, OPTION_SINGLE_CONCAT]],
47         type: "style",
48         typescriptOnly: false,
49         codeExamples: preferTemplate_examples_1.codeExamples,
50     };
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.";
54     return Rule;
55 }(Lint.Rules.AbstractRule));
56 exports.Rule = Rule;
57 function walk(ctx) {
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);
63         }
64         else {
65             return ts.forEachChild(node, cb);
66         }
67     });
68 }
69 function getError(node, allowSingleConcat) {
70     if (!isPlusExpression(node)) {
71         return undefined;
72     }
73     var left = node.left, right = node.right;
74     var l = isStringLike(left);
75     var r = isStringLike(right);
76     if (l && r) {
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
82             : undefined;
83     }
84     else if (!l && !r) {
85         // Watch out for `"a" + b + c`. Parsed as `("a" + b) + c`.
86         return containsAnyStringLiterals(left) ? Rule.FAILURE_STRING : undefined;
87     }
88     else if (l) {
89         // `"x" + y`
90         return !allowSingleConcat ? Rule.FAILURE_STRING : undefined;
91     }
92     else {
93         // `? + "b"`
94         // If LHS consists of only string literals (as in `"a" + "b" + "c"`, allow it.)
95         return !containsOnlyStringLiterals(left) && (!allowSingleConcat || isPlusExpression(left))
96             ? Rule.FAILURE_STRING
97             : undefined;
98     }
99 }
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");
105         });
106     }
107     else {
108         return node.text.includes("\n");
109     }
110 }
111 function containsOnlyStringLiterals(node) {
112     return (isPlusExpression(node) &&
113         isStringLike(node.right) &&
114         (isStringLike(node.left) || containsAnyStringLiterals(node.left)));
115 }
116 function containsAnyStringLiterals(node) {
117     return (isPlusExpression(node) &&
118         (isStringLike(node.right) ||
119             isStringLike(node.left) ||
120             containsAnyStringLiterals(node.left)));
121 }
122 function isPlusExpression(node) {
123     return tsutils_1.isBinaryExpression(node) && node.operatorToken.kind === ts.SyntaxKind.PlusToken;
124 }
125 function isStringLike(node) {
126     switch (node.kind) {
127         case ts.SyntaxKind.StringLiteral:
128         case ts.SyntaxKind.NoSubstitutionTemplateLiteral:
129         case ts.SyntaxKind.TemplateExpression:
130             return true;
131         default:
132             return false;
133     }
134 }
135 var templateObject_1;