preferTemplateRule.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 OPTION_SINGLE_CONCAT = "allow-single-concat";
  24. var Rule = /** @class */ (function (_super) {
  25. tslib_1.__extends(Rule, _super);
  26. function Rule() {
  27. return _super !== null && _super.apply(this, arguments) || this;
  28. }
  29. Rule.prototype.apply = function (sourceFile) {
  30. if (sourceFile.isDeclarationFile) {
  31. return []; // Not possible in a declaration file
  32. }
  33. var allowSingleConcat = this.ruleArguments.indexOf(OPTION_SINGLE_CONCAT) !== -1;
  34. return this.applyWithFunction(sourceFile, walk, { allowSingleConcat: allowSingleConcat });
  35. };
  36. /* tslint:disable:object-literal-sort-keys */
  37. Rule.metadata = {
  38. ruleName: "prefer-template",
  39. description: "Prefer a template expression over string literal concatenation.",
  40. 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),
  41. options: {
  42. type: "string",
  43. enum: [OPTION_SINGLE_CONCAT],
  44. },
  45. optionExamples: [true, [true, OPTION_SINGLE_CONCAT]],
  46. type: "style",
  47. typescriptOnly: false,
  48. };
  49. /* tslint:enable:object-literal-sort-keys */
  50. Rule.FAILURE_STRING = "Use a template literal instead of concatenating with a string literal.";
  51. Rule.FAILURE_STRING_MULTILINE = "Use a multiline template literal instead of concatenating string literals with newlines.";
  52. return Rule;
  53. }(Lint.Rules.AbstractRule));
  54. exports.Rule = Rule;
  55. function walk(ctx) {
  56. var allowSingleConcat = ctx.options.allowSingleConcat;
  57. return ts.forEachChild(ctx.sourceFile, function cb(node) {
  58. var failure = getError(node, allowSingleConcat);
  59. if (failure !== undefined) {
  60. ctx.addFailureAtNode(node, failure);
  61. }
  62. else {
  63. return ts.forEachChild(node, cb);
  64. }
  65. });
  66. }
  67. function getError(node, allowSingleConcat) {
  68. if (!isPlusExpression(node)) {
  69. return undefined;
  70. }
  71. var left = node.left, right = node.right;
  72. var l = isStringLike(left);
  73. var r = isStringLike(right);
  74. if (l && r) {
  75. // They're both strings.
  76. // If they're joined by a newline, recommend a template expression instead.
  77. // Otherwise ignore. ("a" + "b", probably writing a long newline-less string on many lines.)
  78. return containsNewline(left) || containsNewline(right) ? Rule.FAILURE_STRING_MULTILINE : undefined;
  79. }
  80. else if (!l && !r) {
  81. // Watch out for `"a" + b + c`. Parsed as `("a" + b) + c`.
  82. return containsAnyStringLiterals(left) ? Rule.FAILURE_STRING : undefined;
  83. }
  84. else if (l) {
  85. // `"x" + y`
  86. return !allowSingleConcat ? Rule.FAILURE_STRING : undefined;
  87. }
  88. else {
  89. // `? + "b"`
  90. // If LHS consists of only string literals (as in `"a" + "b" + "c"`, allow it.)
  91. return !containsOnlyStringLiterals(left) && (!allowSingleConcat || isPlusExpression(left)) ? Rule.FAILURE_STRING : undefined;
  92. }
  93. }
  94. function containsNewline(node) {
  95. if (node.kind === ts.SyntaxKind.TemplateExpression) {
  96. return node.templateSpans.some(function (_a) {
  97. var text = _a.literal.text;
  98. return text.includes("\n");
  99. });
  100. }
  101. else {
  102. return node.text.includes("\n");
  103. }
  104. }
  105. function containsOnlyStringLiterals(node) {
  106. return isPlusExpression(node) && isStringLike(node.right) && (isStringLike(node.left) || containsAnyStringLiterals(node.left));
  107. }
  108. function containsAnyStringLiterals(node) {
  109. return isPlusExpression(node) && (isStringLike(node.right) || isStringLike(node.left) || containsAnyStringLiterals(node.left));
  110. }
  111. function isPlusExpression(node) {
  112. return tsutils_1.isBinaryExpression(node) && node.operatorToken.kind === ts.SyntaxKind.PlusToken;
  113. }
  114. function isStringLike(node) {
  115. switch (node.kind) {
  116. case ts.SyntaxKind.StringLiteral:
  117. case ts.SyntaxKind.NoSubstitutionTemplateLiteral:
  118. case ts.SyntaxKind.TemplateExpression:
  119. return true;
  120. default:
  121. return false;
  122. }
  123. }
  124. var templateObject_1;