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.

no-inline-comments.js 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * @fileoverview Enforces or disallows inline comments.
  3. * @author Greg Cochard
  4. */
  5. "use strict";
  6. const astUtils = require("../util/ast-utils");
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. type: "suggestion",
  13. docs: {
  14. description: "disallow inline comments after code",
  15. category: "Stylistic Issues",
  16. recommended: false,
  17. url: "https://eslint.org/docs/rules/no-inline-comments"
  18. },
  19. schema: []
  20. },
  21. create(context) {
  22. const sourceCode = context.getSourceCode();
  23. /**
  24. * Will check that comments are not on lines starting with or ending with code
  25. * @param {ASTNode} node The comment node to check
  26. * @private
  27. * @returns {void}
  28. */
  29. function testCodeAroundComment(node) {
  30. // Get the whole line and cut it off at the start of the comment
  31. const startLine = String(sourceCode.lines[node.loc.start.line - 1]);
  32. const endLine = String(sourceCode.lines[node.loc.end.line - 1]);
  33. const preamble = startLine.slice(0, node.loc.start.column).trim();
  34. // Also check after the comment
  35. const postamble = endLine.slice(node.loc.end.column).trim();
  36. // Check that this comment isn't an ESLint directive
  37. const isDirective = astUtils.isDirectiveComment(node);
  38. // Should be empty if there was only whitespace around the comment
  39. if (!isDirective && (preamble || postamble)) {
  40. context.report({ node, message: "Unexpected comment inline with code." });
  41. }
  42. }
  43. //--------------------------------------------------------------------------
  44. // Public
  45. //--------------------------------------------------------------------------
  46. return {
  47. Program() {
  48. const comments = sourceCode.getAllComments();
  49. comments.filter(token => token.type !== "Shebang").forEach(testCodeAroundComment);
  50. }
  51. };
  52. }
  53. };