jsdocFormatRule.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 utils = require("tsutils");
  21. var ts = require("typescript");
  22. var Lint = require("../index");
  23. var OPTION_CHECK_MULTILINE_START = "check-multiline-start";
  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. return this.applyWithFunction(sourceFile, walk, {
  31. firstLineOfMultiline: this.ruleArguments.indexOf(OPTION_CHECK_MULTILINE_START) !== -1,
  32. });
  33. };
  34. /* tslint:disable:object-literal-sort-keys */
  35. Rule.metadata = {
  36. ruleName: "jsdoc-format",
  37. description: "Enforces basic format rules for JSDoc comments.",
  38. descriptionDetails: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n The following rules are enforced for JSDoc comments (comments starting with `/**`):\n\n * each line contains an asterisk and asterisks must be aligned\n * each asterisk must be followed by either a space or a newline (except for the first and the last)\n * the only characters before the asterisk on each line must be whitespace characters\n * one line comments must start with `/** ` and end with `*/`\n * multiline comments don't allow text after `/** ` in the first line (with option `\"", "\"`)\n "], ["\n The following rules are enforced for JSDoc comments (comments starting with \\`/**\\`):\n\n * each line contains an asterisk and asterisks must be aligned\n * each asterisk must be followed by either a space or a newline (except for the first and the last)\n * the only characters before the asterisk on each line must be whitespace characters\n * one line comments must start with \\`/** \\` and end with \\`*/\\`\n * multiline comments don't allow text after \\`/** \\` in the first line (with option \\`\"", "\"\\`)\n "])), OPTION_CHECK_MULTILINE_START),
  39. rationale: "Helps maintain a consistent, readable style for JSDoc comments.",
  40. optionsDescription: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n You can optionally specify the option `\"", "\"` to enforce the first line of a\n multiline JSDoc comment to be empty.\n "], ["\n You can optionally specify the option \\`\"", "\"\\` to enforce the first line of a\n multiline JSDoc comment to be empty.\n "])), OPTION_CHECK_MULTILINE_START),
  41. options: {
  42. type: "array",
  43. minItems: 0,
  44. maxItems: 1,
  45. items: {
  46. type: "string",
  47. enum: [OPTION_CHECK_MULTILINE_START],
  48. },
  49. },
  50. optionExamples: [true, [true, OPTION_CHECK_MULTILINE_START]],
  51. type: "style",
  52. typescriptOnly: false,
  53. };
  54. /* tslint:enable:object-literal-sort-keys */
  55. Rule.ALIGNMENT_FAILURE_STRING = "asterisks in jsdoc must be aligned";
  56. Rule.FORMAT_FAILURE_STRING = "jsdoc is not formatted correctly on this line";
  57. return Rule;
  58. }(Lint.Rules.AbstractRule));
  59. exports.Rule = Rule;
  60. function walk(ctx) {
  61. return utils.forEachComment(ctx.sourceFile, function (fullText, _a) {
  62. var kind = _a.kind, pos = _a.pos, end = _a.end;
  63. if (kind !== ts.SyntaxKind.MultiLineCommentTrivia ||
  64. fullText[pos + 2] !== "*" || fullText[pos + 3] === "*" || fullText[pos + 3] === "/") {
  65. return;
  66. }
  67. var lines = fullText.slice(pos + 3, end - 2).split("\n");
  68. var firstLine = lines[0];
  69. if (lines.length === 1) {
  70. if (firstLine[0] !== " " || !firstLine.endsWith(" ")) {
  71. ctx.addFailure(pos, end, Rule.FORMAT_FAILURE_STRING);
  72. }
  73. return;
  74. }
  75. var alignColumn = getAlignColumn(ctx.sourceFile, pos + 1);
  76. if (ctx.options.firstLineOfMultiline && /\S/.test(firstLine)) {
  77. // first line of multiline JSDoc should be empty, i.e. only contain whitespace
  78. ctx.addFailureAt(pos, firstLine.length + 3, Rule.FORMAT_FAILURE_STRING);
  79. }
  80. var lineStart = pos + firstLine.length + 4; // +3 for the comment start "/**" and +1 for the newline
  81. var endIndex = lines.length - 1;
  82. for (var i = 1; i < endIndex; ++i) {
  83. var line = lines[i].endsWith("\r") ? lines[i].slice(0, -1) : lines[i];
  84. // regex is: start of string, followed by any amount of whitespace, followed by *,
  85. // followed by either a space or the end of the string
  86. if (!/^\s*\*(?: |$)/.test(line)) {
  87. ctx.addFailureAt(lineStart, line.length, Rule.FORMAT_FAILURE_STRING);
  88. }
  89. if (line.indexOf("*") !== alignColumn) {
  90. ctx.addFailureAt(lineStart, line.length, Rule.ALIGNMENT_FAILURE_STRING);
  91. }
  92. lineStart += lines[i].length + 1; // + 1 for the splitted-out newline
  93. }
  94. var lastLine = lines[endIndex];
  95. // last line should only consist of whitespace
  96. if (lastLine.search(/\S/) !== -1) {
  97. ctx.addFailure(lineStart, end, Rule.FORMAT_FAILURE_STRING);
  98. }
  99. if (lastLine.length !== alignColumn) {
  100. ctx.addFailure(lineStart, end, Rule.ALIGNMENT_FAILURE_STRING);
  101. }
  102. });
  103. }
  104. function getAlignColumn(sourceFile, pos) {
  105. var result = ts.getLineAndCharacterOfPosition(sourceFile, pos);
  106. // handle files starting with BOM
  107. return result.line === 0 && sourceFile.text[0] === "\uFEFF"
  108. ? result.character - 1
  109. : result.character;
  110. }
  111. var templateObject_1, templateObject_2;