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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * @fileoverview Rule to check multiple var declarations per line
  3. * @author Alberto Rodríguez
  4. * @deprecated in ESLint v8.53.0
  5. */
  6. "use strict";
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. /** @type {import('../types').Rule.RuleModule} */
  11. module.exports = {
  12. meta: {
  13. deprecated: {
  14. message: "Formatting rules are being moved out of ESLint core.",
  15. url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/",
  16. deprecatedSince: "8.53.0",
  17. availableUntil: "11.0.0",
  18. replacedBy: [
  19. {
  20. message:
  21. "ESLint Stylistic now maintains deprecated stylistic core rules.",
  22. url: "https://eslint.style/guide/migration",
  23. plugin: {
  24. name: "@stylistic/eslint-plugin",
  25. url: "https://eslint.style",
  26. },
  27. rule: {
  28. name: "one-var-declaration-per-line",
  29. url: "https://eslint.style/rules/one-var-declaration-per-line",
  30. },
  31. },
  32. ],
  33. },
  34. type: "suggestion",
  35. docs: {
  36. description:
  37. "Require or disallow newlines around variable declarations",
  38. recommended: false,
  39. url: "https://eslint.org/docs/latest/rules/one-var-declaration-per-line",
  40. },
  41. schema: [
  42. {
  43. enum: ["always", "initializations"],
  44. },
  45. ],
  46. fixable: "whitespace",
  47. messages: {
  48. expectVarOnNewline:
  49. "Expected variable declaration to be on a new line.",
  50. },
  51. },
  52. create(context) {
  53. const always = context.options[0] === "always";
  54. //--------------------------------------------------------------------------
  55. // Helpers
  56. //--------------------------------------------------------------------------
  57. /**
  58. * Determine if provided keyword is a variant of for specifiers
  59. * @private
  60. * @param {string} keyword keyword to test
  61. * @returns {boolean} True if `keyword` is a variant of for specifier
  62. */
  63. function isForTypeSpecifier(keyword) {
  64. return (
  65. keyword === "ForStatement" ||
  66. keyword === "ForInStatement" ||
  67. keyword === "ForOfStatement"
  68. );
  69. }
  70. /**
  71. * Checks newlines around variable declarations.
  72. * @private
  73. * @param {ASTNode} node `VariableDeclaration` node to test
  74. * @returns {void}
  75. */
  76. function checkForNewLine(node) {
  77. if (isForTypeSpecifier(node.parent.type)) {
  78. return;
  79. }
  80. const declarations = node.declarations;
  81. let prev;
  82. declarations.forEach(current => {
  83. if (prev && prev.loc.end.line === current.loc.start.line) {
  84. if (always || prev.init || current.init) {
  85. context.report({
  86. node,
  87. messageId: "expectVarOnNewline",
  88. loc: current.loc,
  89. fix: fixer => fixer.insertTextBefore(current, "\n"),
  90. });
  91. }
  92. }
  93. prev = current;
  94. });
  95. }
  96. //--------------------------------------------------------------------------
  97. // Public
  98. //--------------------------------------------------------------------------
  99. return {
  100. VariableDeclaration: checkForNewLine,
  101. };
  102. },
  103. };