.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / restrictPlusOperandsRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2018 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 Rule = /** @class */ (function (_super) {
24     tslib_1.__extends(Rule, _super);
25     function Rule() {
26         return _super !== null && _super.apply(this, arguments) || this;
27     }
28     Rule.prototype.applyWithProgram = function (sourceFile, program) {
29         return this.applyWithFunction(sourceFile, walk, undefined, program.getTypeChecker());
30     };
31     /* tslint:disable:object-literal-sort-keys */
32     Rule.metadata = {
33         ruleName: "restrict-plus-operands",
34         description: "When adding two variables, operands must both be of type number or of type string.",
35         optionsDescription: "Not configurable.",
36         options: null,
37         optionExamples: [true],
38         type: "functionality",
39         typescriptOnly: false,
40         requiresTypeInfo: true,
41     };
42     /* tslint:enable:object-literal-sort-keys */
43     Rule.INVALID_TYPES_ERROR = "Operands of '+' operation must either be both strings or both numbers or both bigints";
44     Rule.SUGGEST_TEMPLATE_LITERALS = ". Consider using template literals.";
45     return Rule;
46 }(Lint.Rules.TypedRule));
47 exports.Rule = Rule;
48 function walk(ctx, tc) {
49     return ts.forEachChild(ctx.sourceFile, function cb(node) {
50         if (tsutils_1.isBinaryExpression(node) && node.operatorToken.kind === ts.SyntaxKind.PlusToken) {
51             var leftType = tc.getTypeAtLocation(node.left);
52             var leftTypeStr = getBaseTypeOfLiteralType(leftType);
53             var rightType = tc.getTypeAtLocation(node.right);
54             var rightTypeStr = getBaseTypeOfLiteralType(rightType);
55             if (leftTypeStr === "invalid" ||
56                 rightTypeStr === "invalid" ||
57                 leftTypeStr !== rightTypeStr) {
58                 var actualTypes = ", but found " + getTypeString(tc, node.left, leftType) + " + " + getTypeString(tc, node.right, rightType);
59                 var message = Rule.INVALID_TYPES_ERROR + actualTypes;
60                 if (leftTypeStr === "string" || rightTypeStr === "string") {
61                     message += Rule.SUGGEST_TEMPLATE_LITERALS;
62                 }
63                 return ctx.addFailureAtNode(node, message);
64             }
65         }
66         return ts.forEachChild(node, cb);
67     });
68 }
69 function getTypeString(tc, node, type) {
70     var typeString = tc.typeToString(type, node);
71     if (typeString === "undefined[]" &&
72         ts.isArrayLiteralExpression(node) &&
73         node.elements.length === 0) {
74         // Special case literal "[]" arrays that would otherwise be emitted as undefined[].
75         return "[]";
76     }
77     return typeString;
78 }
79 function getBaseTypeOfLiteralType(type) {
80     if (tsutils_1.isTypeFlagSet(type, ts.TypeFlags.StringLiteral) ||
81         tsutils_1.isTypeFlagSet(type, ts.TypeFlags.String)) {
82         return "string";
83     }
84     else if (tsutils_1.isTypeFlagSet(type, ts.TypeFlags.NumberLiteral) ||
85         tsutils_1.isTypeFlagSet(type, ts.TypeFlags.Number)) {
86         return "number";
87     }
88     else if (tsutils_1.isTypeFlagSet(type, ts.TypeFlags.BigIntLiteral) ||
89         tsutils_1.isTypeFlagSet(type, ts.TypeFlags.BigInt)) {
90         return "bigint";
91     }
92     else if (tsutils_1.isUnionType(type) && !tsutils_1.isTypeFlagSet(type, ts.TypeFlags.Enum)) {
93         var types = type.types.map(getBaseTypeOfLiteralType);
94         return allSame(types) ? types[0] : "invalid";
95     }
96     else if (tsutils_1.isTypeFlagSet(type, ts.TypeFlags.EnumLiteral)) {
97         // Compatibility for TypeScript pre-2.4, which used EnumLiteralType instead of LiteralType
98         getBaseTypeOfLiteralType(type.baseType);
99     }
100     return "invalid";
101 }
102 function allSame(array) {
103     return array.every(function (value) { return value === array[0]; });
104 }