4 * Copyright 2019 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");
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);
28 return _super !== null && _super.apply(this, arguments) || this;
30 Rule.prototype.applyWithProgram = function (sourceFile, program) {
31 return this.applyWithFunction(sourceFile, walk, this.getRuleOptions(), program.getTypeChecker());
33 Rule.prototype.getRuleOptions = function () {
35 if (this.ruleArguments[0] === undefined) {
37 _a[OPTION_ALLOW_EMPTY_TYPES] = true,
41 return this.ruleArguments[0];
44 Rule.CONVERSION_REQUIRED = "Explicit conversion to string type required";
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}\\`)"]))),
54 _a[OPTION_ALLOW_EMPTY_TYPES] = true,
60 _b[OPTION_ALLOW_EMPTY_TYPES] = {
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",
73 }(Lint.Rules.TypedRule));
75 function walk(ctx, checker) {
76 var sourceFile = ctx.sourceFile, options = ctx.options;
77 ts.forEachChild(sourceFile, function cb(node) {
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);
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);
103 return ts.forEachChild(node, cb);
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);
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);
116 function typeCanBeStringifiedEasily(type, options) {
117 if (tsutils_1.isUnionType(type)) {
118 return type.types.every(function (unionAtomicType) {
119 return typeCanBeStringifiedEasily(unionAtomicType, options);
122 if (options[OPTION_ALLOW_EMPTY_TYPES] && typeIsEmpty(type)) {
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));
131 var templateObject_1, templateObject_2;