123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2013 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_BRACE = "check-open-brace";
  24. var OPTION_CATCH = "check-catch";
  25. var OPTION_ELSE = "check-else";
  26. var OPTION_FINALLY = "check-finally";
  27. var OPTION_WHITESPACE = "check-whitespace";
  28. var Rule = /** @class */ (function (_super) {
  29. tslib_1.__extends(Rule, _super);
  30. function Rule() {
  31. return _super !== null && _super.apply(this, arguments) || this;
  32. }
  33. Rule.prototype.apply = function (sourceFile) {
  34. return this.applyWithWalker(new OneLineWalker(sourceFile, this.ruleName, {
  35. brace: this.ruleArguments.indexOf(OPTION_BRACE) !== -1,
  36. catch: this.ruleArguments.indexOf(OPTION_CATCH) !== -1,
  37. else: this.ruleArguments.indexOf(OPTION_ELSE) !== -1,
  38. finally: this.ruleArguments.indexOf(OPTION_FINALLY) !== -1,
  39. whitespace: this.ruleArguments.indexOf(OPTION_WHITESPACE) !== -1,
  40. }));
  41. };
  42. /* tslint:disable:object-literal-sort-keys */
  43. Rule.metadata = {
  44. ruleName: "one-line",
  45. description: "Requires the specified tokens to be on the same line as the expression preceding them.",
  46. optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Five arguments may be optionally provided:\n\n * `\"", "\"` checks that `catch` is on the same line as the closing brace for `try`.\n * `\"", "\"` checks that `finally` is on the same line as the closing brace for `catch`.\n * `\"", "\"` checks that `else` is on the same line as the closing brace for `if`.\n * `\"", "\"` checks that an open brace falls on the same line as its preceding expression.\n * `\"", "\"` checks preceding whitespace for the specified tokens."], ["\n Five arguments may be optionally provided:\n\n * \\`\"", "\"\\` checks that \\`catch\\` is on the same line as the closing brace for \\`try\\`.\n * \\`\"", "\"\\` checks that \\`finally\\` is on the same line as the closing brace for \\`catch\\`.\n * \\`\"", "\"\\` checks that \\`else\\` is on the same line as the closing brace for \\`if\\`.\n * \\`\"", "\"\\` checks that an open brace falls on the same line as its preceding expression.\n * \\`\"", "\"\\` checks preceding whitespace for the specified tokens."])), OPTION_CATCH, OPTION_FINALLY, OPTION_ELSE, OPTION_BRACE, OPTION_WHITESPACE),
  47. options: {
  48. type: "array",
  49. items: {
  50. type: "string",
  51. enum: [OPTION_CATCH, OPTION_FINALLY, OPTION_ELSE, OPTION_BRACE, OPTION_WHITESPACE],
  52. },
  53. minLength: 0,
  54. maxLength: 5,
  55. },
  56. optionExamples: [[true, OPTION_CATCH, OPTION_FINALLY, OPTION_ELSE]],
  57. type: "style",
  58. typescriptOnly: false,
  59. hasFix: true,
  60. };
  61. /* tslint:enable:object-literal-sort-keys */
  62. Rule.WHITESPACE_FAILURE_STRING = "missing whitespace";
  63. return Rule;
  64. }(Lint.Rules.AbstractRule));
  65. exports.Rule = Rule;
  66. var OneLineWalker = /** @class */ (function (_super) {
  67. tslib_1.__extends(OneLineWalker, _super);
  68. function OneLineWalker() {
  69. return _super !== null && _super.apply(this, arguments) || this;
  70. }
  71. OneLineWalker.prototype.walk = function (sourceFile) {
  72. var _this = this;
  73. var cb = function (node) {
  74. switch (node.kind) {
  75. case ts.SyntaxKind.Block:
  76. if (!tsutils_1.isBlockLike(node.parent)) {
  77. _this.check({ pos: node.pos, end: node.statements.pos });
  78. }
  79. break;
  80. case ts.SyntaxKind.CaseBlock:
  81. _this.check({ pos: node.pos, end: node.clauses.pos });
  82. break;
  83. case ts.SyntaxKind.ModuleBlock:
  84. _this.check({ pos: node.pos, end: node.statements.pos });
  85. break;
  86. case ts.SyntaxKind.EnumDeclaration:
  87. _this.check({ pos: node.name.end, end: node.members.pos });
  88. break;
  89. case ts.SyntaxKind.InterfaceDeclaration:
  90. case ts.SyntaxKind.ClassDeclaration:
  91. case ts.SyntaxKind.ClassExpression: {
  92. var openBrace = tsutils_1.getChildOfKind(node, ts.SyntaxKind.OpenBraceToken, sourceFile);
  93. if (openBrace !== undefined) {
  94. _this.check(openBrace);
  95. }
  96. break;
  97. }
  98. case ts.SyntaxKind.IfStatement: {
  99. var _a = node, thenStatement = _a.thenStatement, elseStatement = _a.elseStatement;
  100. if (elseStatement !== undefined && thenStatement.kind === ts.SyntaxKind.Block) {
  101. _this.check({ pos: thenStatement.end, end: elseStatement.pos }, "else");
  102. }
  103. break;
  104. }
  105. case ts.SyntaxKind.TryStatement: {
  106. var _b = node, finallyBlock = _b.finallyBlock, catchClause = _b.catchClause, tryBlock = _b.tryBlock;
  107. if (catchClause !== undefined) {
  108. _this.check(catchClause.getChildAt(0, sourceFile), "catch");
  109. if (finallyBlock !== undefined) {
  110. _this.check({ pos: catchClause.end, end: finallyBlock.pos }, "finally");
  111. }
  112. }
  113. else if (finallyBlock !== undefined) {
  114. _this.check({ pos: tryBlock.end, end: finallyBlock.pos }, "finally");
  115. }
  116. break;
  117. }
  118. case ts.SyntaxKind.BinaryExpression: {
  119. var _c = node, operatorToken = _c.operatorToken, right = _c.right;
  120. if (operatorToken.kind === ts.SyntaxKind.EqualsToken && tsutils_1.isObjectLiteralExpression(right)) {
  121. _this.check({ pos: right.pos, end: right.properties.pos });
  122. }
  123. break;
  124. }
  125. case ts.SyntaxKind.VariableDeclaration: {
  126. var initializer = node.initializer;
  127. if (initializer !== undefined && tsutils_1.isObjectLiteralExpression(initializer)) {
  128. _this.check({ pos: initializer.pos, end: initializer.properties.pos });
  129. }
  130. break;
  131. }
  132. case ts.SyntaxKind.TypeAliasDeclaration: {
  133. var type = node.type;
  134. if (type.kind === ts.SyntaxKind.MappedType || type.kind === ts.SyntaxKind.TypeLiteral) {
  135. _this.check(type.getChildAt(0, sourceFile));
  136. }
  137. }
  138. }
  139. return ts.forEachChild(node, cb);
  140. };
  141. return ts.forEachChild(sourceFile, cb);
  142. };
  143. OneLineWalker.prototype.check = function (range, kind) {
  144. var tokenStart = range.end - (kind === undefined ? 1 : kind.length);
  145. if (this.options[kind === undefined ? "brace" : kind] && !tsutils_1.isSameLine(this.sourceFile, range.pos, tokenStart)) {
  146. this.addFailure(tokenStart, range.end, "misplaced " + (kind === undefined ? "opening brace" : "'" + kind + "'"), Lint.Replacement.replaceFromTo(range.pos, tokenStart, this.options.whitespace ? " " : ""));
  147. }
  148. else if (this.options.whitespace && range.pos === tokenStart) {
  149. this.addFailure(tokenStart, range.end, Rule.WHITESPACE_FAILURE_STRING, Lint.Replacement.appendText(range.pos, " "));
  150. }
  151. };
  152. return OneLineWalker;
  153. }(Lint.AbstractWalker));
  154. var templateObject_1;