preferConditionalExpressionRule.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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_CHECK_ELSE_IF = "check-else-if";
  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. /* tslint:enable:object-literal-sort-keys */
  30. Rule.FAILURE_STRING = function (assigned) {
  31. return "Use a conditional expression instead of assigning to '" + assigned + "' in multiple places.";
  32. };
  33. Rule.prototype.apply = function (sourceFile) {
  34. return this.applyWithFunction(sourceFile, walk, {
  35. checkElseIf: this.ruleArguments.indexOf(OPTION_CHECK_ELSE_IF) !== -1,
  36. });
  37. };
  38. /* tslint:disable:object-literal-sort-keys */
  39. Rule.metadata = {
  40. ruleName: "prefer-conditional-expression",
  41. description: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Recommends to use a conditional expression instead of assigning to the same thing in each branch of an if statement."], ["\n Recommends to use a conditional expression instead of assigning to the same thing in each branch of an if statement."]))),
  42. rationale: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n This reduces duplication and can eliminate an unnecessary variable declaration."], ["\n This reduces duplication and can eliminate an unnecessary variable declaration."]))),
  43. optionsDescription: "If `" + OPTION_CHECK_ELSE_IF + "` is specified, the rule also checks nested if-else-if statements.",
  44. options: {
  45. type: "string",
  46. enum: [OPTION_CHECK_ELSE_IF],
  47. },
  48. optionExamples: [true, [true, OPTION_CHECK_ELSE_IF]],
  49. type: "functionality",
  50. typescriptOnly: false,
  51. };
  52. return Rule;
  53. }(Lint.Rules.AbstractRule));
  54. exports.Rule = Rule;
  55. function walk(ctx) {
  56. var sourceFile = ctx.sourceFile, checkElseIf = ctx.options.checkElseIf;
  57. return ts.forEachChild(sourceFile, function cb(node) {
  58. if (tsutils_1.isIfStatement(node)) {
  59. var assigned = detectAssignment(node, sourceFile, checkElseIf);
  60. if (assigned !== undefined) {
  61. ctx.addFailureAtNode(node.getChildAt(0, sourceFile), Rule.FAILURE_STRING(assigned.getText(sourceFile)));
  62. }
  63. if (assigned !== undefined || !checkElseIf) {
  64. // Be careful not to fail again for the "else if"
  65. do {
  66. ts.forEachChild(node.expression, cb);
  67. ts.forEachChild(node.thenStatement, cb);
  68. if (node.elseStatement === undefined) {
  69. return;
  70. }
  71. node = node.elseStatement;
  72. while (tsutils_1.isBlock(node) && node.statements.length === 1) {
  73. node = node.statements[0];
  74. }
  75. } while (tsutils_1.isIfStatement(node));
  76. }
  77. }
  78. return ts.forEachChild(node, cb);
  79. });
  80. }
  81. /**
  82. * @param inElse `undefined` when this is the top level if statement, `false` when inside the then branch, `true` when inside else
  83. */
  84. function detectAssignment(statement, sourceFile, checkElseIf, inElse) {
  85. if (tsutils_1.isIfStatement(statement)) {
  86. if (inElse === false || !checkElseIf && inElse || statement.elseStatement === undefined) {
  87. return undefined;
  88. }
  89. var then = detectAssignment(statement.thenStatement, sourceFile, checkElseIf, false);
  90. if (then === undefined) {
  91. return undefined;
  92. }
  93. var elze = detectAssignment(statement.elseStatement, sourceFile, checkElseIf, true);
  94. return elze !== undefined && nodeEquals(then, elze, sourceFile) ? then : undefined;
  95. }
  96. else if (tsutils_1.isBlock(statement)) {
  97. return statement.statements.length === 1
  98. ? detectAssignment(statement.statements[0], sourceFile, checkElseIf, inElse)
  99. : undefined;
  100. }
  101. else if (tsutils_1.isExpressionStatement(statement) && tsutils_1.isBinaryExpression(statement.expression)) {
  102. var _a = statement.expression, kind = _a.operatorToken.kind, left = _a.left, right = _a.right;
  103. return kind === ts.SyntaxKind.EqualsToken && tsutils_1.isSameLine(sourceFile, right.getStart(sourceFile), right.end) ? left : undefined;
  104. }
  105. else {
  106. return undefined;
  107. }
  108. }
  109. function nodeEquals(a, b, sourceFile) {
  110. return a.getText(sourceFile) === b.getText(sourceFile);
  111. }
  112. var templateObject_1, templateObject_2;