no-inline-comments.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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('../types').Rule.RuleModule} */
  11. module.exports = {
  12. meta: {
  13. type: "suggestion",
  14. defaultOptions: [{}],
  15. docs: {
  16. description: "Disallow inline comments after code",
  17. recommended: false,
  18. frozen: true,
  19. url: "https://eslint.org/docs/latest/rules/no-inline-comments",
  20. },
  21. schema: [
  22. {
  23. type: "object",
  24. properties: {
  25. ignorePattern: {
  26. type: "string",
  27. },
  28. },
  29. additionalProperties: false,
  30. },
  31. ],
  32. messages: {
  33. unexpectedInlineComment: "Unexpected comment inline with code.",
  34. },
  35. },
  36. create(context) {
  37. const sourceCode = context.sourceCode;
  38. const [{ ignorePattern }] = context.options;
  39. const customIgnoreRegExp =
  40. ignorePattern && new RegExp(ignorePattern, "u");
  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(
  68. node.range[0],
  69. );
  70. if (
  71. enclosingNode &&
  72. enclosingNode.type === "JSXEmptyExpression"
  73. ) {
  74. return;
  75. }
  76. }
  77. // Don't report ESLint directive comments
  78. if (astUtils.isDirectiveComment(node)) {
  79. return;
  80. }
  81. context.report({
  82. node,
  83. messageId: "unexpectedInlineComment",
  84. });
  85. }
  86. //--------------------------------------------------------------------------
  87. // Public
  88. //--------------------------------------------------------------------------
  89. return {
  90. Program() {
  91. sourceCode
  92. .getAllComments()
  93. .filter(token => token.type !== "Shebang")
  94. .forEach(testCodeAroundComment);
  95. },
  96. };
  97. },
  98. };