no-inline-comments.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * @fileoverview Enforces or disallows inline comments.
  3. * @author Greg Cochard
  4. */
  5. "use strict";
  6. const astUtils = require("./utils/ast-utils");
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. /** @type {import('../shared/types').Rule} */
  11. module.exports = {
  12. meta: {
  13. type: "suggestion",
  14. docs: {
  15. description: "Disallow inline comments after code",
  16. recommended: false,
  17. url: "https://eslint.org/docs/rules/no-inline-comments"
  18. },
  19. schema: [
  20. {
  21. type: "object",
  22. properties: {
  23. ignorePattern: {
  24. type: "string"
  25. }
  26. },
  27. additionalProperties: false
  28. }
  29. ],
  30. messages: {
  31. unexpectedInlineComment: "Unexpected comment inline with code."
  32. }
  33. },
  34. create(context) {
  35. const sourceCode = context.getSourceCode();
  36. const options = context.options[0];
  37. let customIgnoreRegExp;
  38. if (options && options.ignorePattern) {
  39. customIgnoreRegExp = new RegExp(options.ignorePattern, "u");
  40. }
  41. /**
  42. * Will check that comments are not on lines starting with or ending with code
  43. * @param {ASTNode} node The comment node to check
  44. * @private
  45. * @returns {void}
  46. */
  47. function testCodeAroundComment(node) {
  48. const startLine = String(sourceCode.lines[node.loc.start.line - 1]),
  49. endLine = String(sourceCode.lines[node.loc.end.line - 1]),
  50. preamble = startLine.slice(0, node.loc.start.column).trim(),
  51. postamble = endLine.slice(node.loc.end.column).trim(),
  52. isPreambleEmpty = !preamble,
  53. isPostambleEmpty = !postamble;
  54. // Nothing on both sides
  55. if (isPreambleEmpty && isPostambleEmpty) {
  56. return;
  57. }
  58. // Matches the ignore pattern
  59. if (customIgnoreRegExp && customIgnoreRegExp.test(node.value)) {
  60. return;
  61. }
  62. // JSX Exception
  63. if (
  64. (isPreambleEmpty || preamble === "{") &&
  65. (isPostambleEmpty || postamble === "}")
  66. ) {
  67. const enclosingNode = sourceCode.getNodeByRangeIndex(node.range[0]);
  68. if (enclosingNode && enclosingNode.type === "JSXEmptyExpression") {
  69. return;
  70. }
  71. }
  72. // Don't report ESLint directive comments
  73. if (astUtils.isDirectiveComment(node)) {
  74. return;
  75. }
  76. context.report({
  77. node,
  78. messageId: "unexpectedInlineComment"
  79. });
  80. }
  81. //--------------------------------------------------------------------------
  82. // Public
  83. //--------------------------------------------------------------------------
  84. return {
  85. Program() {
  86. sourceCode.getAllComments()
  87. .filter(token => token.type !== "Shebang")
  88. .forEach(testCodeAroundComment);
  89. }
  90. };
  91. }
  92. };