line-comment-position.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * @fileoverview Rule to enforce the position of line comments
  3. * @author Alberto Rodríguez
  4. * @deprecated in ESLint v9.3.0
  5. */
  6. "use strict";
  7. const astUtils = require("./utils/ast-utils");
  8. //------------------------------------------------------------------------------
  9. // Rule Definition
  10. //------------------------------------------------------------------------------
  11. /** @type {import('../shared/types').Rule} */
  12. module.exports = {
  13. meta: {
  14. deprecated: true,
  15. replacedBy: [],
  16. type: "layout",
  17. docs: {
  18. description: "Enforce position of line comments",
  19. recommended: false,
  20. url: "https://eslint.org/docs/latest/rules/line-comment-position"
  21. },
  22. schema: [
  23. {
  24. oneOf: [
  25. {
  26. enum: ["above", "beside"]
  27. },
  28. {
  29. type: "object",
  30. properties: {
  31. position: {
  32. enum: ["above", "beside"]
  33. },
  34. ignorePattern: {
  35. type: "string"
  36. },
  37. applyDefaultPatterns: {
  38. type: "boolean"
  39. },
  40. applyDefaultIgnorePatterns: {
  41. type: "boolean"
  42. }
  43. },
  44. additionalProperties: false
  45. }
  46. ]
  47. }
  48. ],
  49. messages: {
  50. above: "Expected comment to be above code.",
  51. beside: "Expected comment to be beside code."
  52. }
  53. },
  54. create(context) {
  55. const options = context.options[0];
  56. let above,
  57. ignorePattern,
  58. applyDefaultIgnorePatterns = true;
  59. if (!options || typeof options === "string") {
  60. above = !options || options === "above";
  61. } else {
  62. above = !options.position || options.position === "above";
  63. ignorePattern = options.ignorePattern;
  64. if (Object.hasOwn(options, "applyDefaultIgnorePatterns")) {
  65. applyDefaultIgnorePatterns = options.applyDefaultIgnorePatterns;
  66. } else {
  67. applyDefaultIgnorePatterns = options.applyDefaultPatterns !== false;
  68. }
  69. }
  70. const defaultIgnoreRegExp = astUtils.COMMENTS_IGNORE_PATTERN;
  71. const fallThroughRegExp = /^\s*falls?\s?through/u;
  72. const customIgnoreRegExp = new RegExp(ignorePattern, "u");
  73. const sourceCode = context.sourceCode;
  74. //--------------------------------------------------------------------------
  75. // Public
  76. //--------------------------------------------------------------------------
  77. return {
  78. Program() {
  79. const comments = sourceCode.getAllComments();
  80. comments.filter(token => token.type === "Line").forEach(node => {
  81. if (applyDefaultIgnorePatterns && (defaultIgnoreRegExp.test(node.value) || fallThroughRegExp.test(node.value))) {
  82. return;
  83. }
  84. if (ignorePattern && customIgnoreRegExp.test(node.value)) {
  85. return;
  86. }
  87. const previous = sourceCode.getTokenBefore(node, { includeComments: true });
  88. const isOnSameLine = previous && previous.loc.end.line === node.loc.start.line;
  89. if (above) {
  90. if (isOnSameLine) {
  91. context.report({
  92. node,
  93. messageId: "above"
  94. });
  95. }
  96. } else {
  97. if (!isOnSameLine) {
  98. context.report({
  99. node,
  100. messageId: "beside"
  101. });
  102. }
  103. }
  104. });
  105. }
  106. };
  107. }
  108. };