object-property-newline.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * @fileoverview Rule to enforce placing object properties on separate lines.
  3. * @author Vitor Balocco
  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: "object-property-newline",
  29. url: "https://eslint.style/rules/object-property-newline",
  30. },
  31. },
  32. ],
  33. },
  34. type: "layout",
  35. docs: {
  36. description: "Enforce placing object properties on separate lines",
  37. recommended: false,
  38. url: "https://eslint.org/docs/latest/rules/object-property-newline",
  39. },
  40. schema: [
  41. {
  42. type: "object",
  43. properties: {
  44. allowAllPropertiesOnSameLine: {
  45. type: "boolean",
  46. default: false,
  47. },
  48. allowMultiplePropertiesPerLine: {
  49. // Deprecated
  50. type: "boolean",
  51. default: false,
  52. },
  53. },
  54. additionalProperties: false,
  55. },
  56. ],
  57. fixable: "whitespace",
  58. messages: {
  59. propertiesOnNewlineAll:
  60. "Object properties must go on a new line if they aren't all on the same line.",
  61. propertiesOnNewline: "Object properties must go on a new line.",
  62. },
  63. },
  64. create(context) {
  65. const allowSameLine =
  66. context.options[0] &&
  67. (context.options[0].allowAllPropertiesOnSameLine ||
  68. context.options[0]
  69. .allowMultiplePropertiesPerLine); /* Deprecated */
  70. const messageId = allowSameLine
  71. ? "propertiesOnNewlineAll"
  72. : "propertiesOnNewline";
  73. const sourceCode = context.sourceCode;
  74. return {
  75. ObjectExpression(node) {
  76. if (allowSameLine) {
  77. if (node.properties.length > 1) {
  78. const firstTokenOfFirstProperty =
  79. sourceCode.getFirstToken(node.properties[0]);
  80. const lastTokenOfLastProperty = sourceCode.getLastToken(
  81. node.properties.at(-1),
  82. );
  83. if (
  84. firstTokenOfFirstProperty.loc.end.line ===
  85. lastTokenOfLastProperty.loc.start.line
  86. ) {
  87. // All keys and values are on the same line
  88. return;
  89. }
  90. }
  91. }
  92. for (let i = 1; i < node.properties.length; i++) {
  93. const lastTokenOfPreviousProperty = sourceCode.getLastToken(
  94. node.properties[i - 1],
  95. );
  96. const firstTokenOfCurrentProperty =
  97. sourceCode.getFirstToken(node.properties[i]);
  98. if (
  99. lastTokenOfPreviousProperty.loc.end.line ===
  100. firstTokenOfCurrentProperty.loc.start.line
  101. ) {
  102. context.report({
  103. node,
  104. loc: firstTokenOfCurrentProperty.loc,
  105. messageId,
  106. fix(fixer) {
  107. const comma = sourceCode.getTokenBefore(
  108. firstTokenOfCurrentProperty,
  109. );
  110. const rangeAfterComma = [
  111. comma.range[1],
  112. firstTokenOfCurrentProperty.range[0],
  113. ];
  114. // Don't perform a fix if there are any comments between the comma and the next property.
  115. if (
  116. sourceCode.text
  117. .slice(
  118. rangeAfterComma[0],
  119. rangeAfterComma[1],
  120. )
  121. .trim()
  122. ) {
  123. return null;
  124. }
  125. return fixer.replaceTextRange(
  126. rangeAfterComma,
  127. "\n",
  128. );
  129. },
  130. });
  131. }
  132. }
  133. },
  134. };
  135. },
  136. };