.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / strictStringExpressionsRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2019 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 _a, _b;
21 var tsutils_1 = require("tsutils");
22 var ts = require("typescript");
23 var Lint = require("../index");
24 var OPTION_ALLOW_EMPTY_TYPES = "allow-empty-types";
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.applyWithProgram = function (sourceFile, program) {
31         return this.applyWithFunction(sourceFile, walk, this.getRuleOptions(), program.getTypeChecker());
32     };
33     Rule.prototype.getRuleOptions = function () {
34         var _a;
35         if (this.ruleArguments[0] === undefined) {
36             return _a = {},
37                 _a[OPTION_ALLOW_EMPTY_TYPES] = true,
38                 _a;
39         }
40         else {
41             return this.ruleArguments[0];
42         }
43     };
44     Rule.CONVERSION_REQUIRED = "Explicit conversion to string type required";
45     Rule.metadata = {
46         description: "Disable implicit toString() calls",
47         descriptionDetails: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n            Require explicit toString() call for variables used in strings. By default only strings are allowed.\n\n            The following nodes are checked:\n\n            * String literals (\"foo\" + bar)\n            * ES6 templates (`foo ${bar}`)"], ["\n            Require explicit toString() call for variables used in strings. By default only strings are allowed.\n\n            The following nodes are checked:\n\n            * String literals (\"foo\" + bar)\n            * ES6 templates (\\`foo \\${bar}\\`)"]))),
48         hasFix: true,
49         optionExamples: [
50             true,
51             [
52                 true,
53                 (_a = {},
54                     _a[OPTION_ALLOW_EMPTY_TYPES] = true,
55                     _a),
56             ],
57         ],
58         options: {
59             properties: (_b = {},
60                 _b[OPTION_ALLOW_EMPTY_TYPES] = {
61                     type: "boolean",
62                 },
63                 _b),
64             type: "object",
65         },
66         optionsDescription: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n                Following arguments may be optionally provided:\n                * `", "` allows `null`, `undefined` and `never` to be passed into strings without explicit conversion"], ["\n                Following arguments may be optionally provided:\n                * \\`", "\\` allows \\`null\\`, \\`undefined\\` and \\`never\\` to be passed into strings without explicit conversion"])), OPTION_ALLOW_EMPTY_TYPES),
67         requiresTypeInfo: true,
68         ruleName: "strict-string-expressions",
69         type: "functionality",
70         typescriptOnly: true,
71     };
72     return Rule;
73 }(Lint.Rules.TypedRule));
74 exports.Rule = Rule;
75 function walk(ctx, checker) {
76     var sourceFile = ctx.sourceFile, options = ctx.options;
77     ts.forEachChild(sourceFile, function cb(node) {
78         switch (node.kind) {
79             case ts.SyntaxKind.BinaryExpression: {
80                 var binaryExpr = node;
81                 if (binaryExpr.operatorToken.kind === ts.SyntaxKind.PlusToken) {
82                     var leftIsPassedAsIs = typeCanBeStringifiedEasily(checker.getTypeAtLocation(binaryExpr.left), options);
83                     var rightIsPassedAsIs = typeCanBeStringifiedEasily(checker.getTypeAtLocation(binaryExpr.right), options);
84                     var leftIsFailed = !leftIsPassedAsIs && rightIsPassedAsIs;
85                     var rightIsFailed = leftIsPassedAsIs && !rightIsPassedAsIs;
86                     if (leftIsFailed || rightIsFailed) {
87                         var expression = leftIsFailed ? binaryExpr.left : binaryExpr.right;
88                         addFailure(binaryExpr, expression);
89                     }
90                 }
91                 break;
92             }
93             case ts.SyntaxKind.TemplateSpan: {
94                 var templateSpanNode = node;
95                 var type = checker.getTypeAtLocation(templateSpanNode.expression);
96                 var shouldPassAsIs = typeCanBeStringifiedEasily(type, options);
97                 if (!shouldPassAsIs) {
98                     var expression = templateSpanNode.expression;
99                     addFailure(templateSpanNode, expression);
100                 }
101             }
102         }
103         return ts.forEachChild(node, cb);
104     });
105     function addFailure(node, expression) {
106         var fix = Lint.Replacement.replaceFromTo(expression.getStart(), expression.end, "String(" + expression.getText() + ")");
107         ctx.addFailureAtNode(node, Rule.CONVERSION_REQUIRED, fix);
108     }
109 }
110 var typeIsEmpty = function (type) {
111     return tsutils_1.isTypeFlagSet(type, ts.TypeFlags.Null) ||
112         tsutils_1.isTypeFlagSet(type, ts.TypeFlags.VoidLike) ||
113         tsutils_1.isTypeFlagSet(type, ts.TypeFlags.Undefined) ||
114         tsutils_1.isTypeFlagSet(type, ts.TypeFlags.Never);
115 };
116 function typeCanBeStringifiedEasily(type, options) {
117     if (tsutils_1.isUnionType(type)) {
118         return type.types.every(function (unionAtomicType) {
119             return typeCanBeStringifiedEasily(unionAtomicType, options);
120         });
121     }
122     if (options[OPTION_ALLOW_EMPTY_TYPES] && typeIsEmpty(type)) {
123         return true;
124     }
125     return (tsutils_1.isTypeFlagSet(type, ts.TypeFlags.BooleanLike) ||
126         tsutils_1.isTypeFlagSet(type, ts.TypeFlags.StringOrNumberLiteral) ||
127         tsutils_1.isTypeFlagSet(type, ts.TypeFlags.NumberLike) ||
128         tsutils_1.isTypeFlagSet(type, ts.TypeFlags.StringLike) ||
129         tsutils_1.isTypeFlagSet(type, ts.TypeFlags.Any));
130 }
131 var templateObject_1, templateObject_2;