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.

one-var-declaration-per-line.js 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * @fileoverview Rule to check multiple var declarations per line
  3. * @author Alberto Rodríguez
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. type: "suggestion",
  12. docs: {
  13. description: "require or disallow newlines around variable declarations",
  14. category: "Stylistic Issues",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/one-var-declaration-per-line"
  17. },
  18. schema: [
  19. {
  20. enum: ["always", "initializations"]
  21. }
  22. ],
  23. fixable: "whitespace"
  24. },
  25. create(context) {
  26. const ERROR_MESSAGE = "Expected variable declaration to be on a new line.";
  27. const always = context.options[0] === "always";
  28. //--------------------------------------------------------------------------
  29. // Helpers
  30. //--------------------------------------------------------------------------
  31. /**
  32. * Determine if provided keyword is a variant of for specifiers
  33. * @private
  34. * @param {string} keyword - keyword to test
  35. * @returns {boolean} True if `keyword` is a variant of for specifier
  36. */
  37. function isForTypeSpecifier(keyword) {
  38. return keyword === "ForStatement" || keyword === "ForInStatement" || keyword === "ForOfStatement";
  39. }
  40. /**
  41. * Checks newlines around variable declarations.
  42. * @private
  43. * @param {ASTNode} node - `VariableDeclaration` node to test
  44. * @returns {void}
  45. */
  46. function checkForNewLine(node) {
  47. if (isForTypeSpecifier(node.parent.type)) {
  48. return;
  49. }
  50. const declarations = node.declarations;
  51. let prev;
  52. declarations.forEach(current => {
  53. if (prev && prev.loc.end.line === current.loc.start.line) {
  54. if (always || prev.init || current.init) {
  55. context.report({
  56. node,
  57. message: ERROR_MESSAGE,
  58. loc: current.loc.start,
  59. fix: fixer => fixer.insertTextBefore(current, "\n")
  60. });
  61. }
  62. }
  63. prev = current;
  64. });
  65. }
  66. //--------------------------------------------------------------------------
  67. // Public
  68. //--------------------------------------------------------------------------
  69. return {
  70. VariableDeclaration: checkForNewLine
  71. };
  72. }
  73. };