Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

line-comment-position.js 4.0KB

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