newline-per-chained-call.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * @fileoverview Rule to ensure newline per method call when chaining calls
  3. * @author Rajendra Patil
  4. * @author Burak Yigit Kaya
  5. * @deprecated in ESLint v8.53.0
  6. */
  7. "use strict";
  8. const astUtils = require("./utils/ast-utils");
  9. //------------------------------------------------------------------------------
  10. // Rule Definition
  11. //------------------------------------------------------------------------------
  12. /** @type {import('../types').Rule.RuleModule} */
  13. module.exports = {
  14. meta: {
  15. deprecated: {
  16. message: "Formatting rules are being moved out of ESLint core.",
  17. url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/",
  18. deprecatedSince: "8.53.0",
  19. availableUntil: "11.0.0",
  20. replacedBy: [
  21. {
  22. message:
  23. "ESLint Stylistic now maintains deprecated stylistic core rules.",
  24. url: "https://eslint.style/guide/migration",
  25. plugin: {
  26. name: "@stylistic/eslint-plugin",
  27. url: "https://eslint.style",
  28. },
  29. rule: {
  30. name: "newline-per-chained-call",
  31. url: "https://eslint.style/rules/newline-per-chained-call",
  32. },
  33. },
  34. ],
  35. },
  36. type: "layout",
  37. docs: {
  38. description: "Require a newline after each call in a method chain",
  39. recommended: false,
  40. url: "https://eslint.org/docs/latest/rules/newline-per-chained-call",
  41. },
  42. fixable: "whitespace",
  43. schema: [
  44. {
  45. type: "object",
  46. properties: {
  47. ignoreChainWithDepth: {
  48. type: "integer",
  49. minimum: 1,
  50. maximum: 10,
  51. default: 2,
  52. },
  53. },
  54. additionalProperties: false,
  55. },
  56. ],
  57. messages: {
  58. expected: "Expected line break before `{{callee}}`.",
  59. },
  60. },
  61. create(context) {
  62. const options = context.options[0] || {},
  63. ignoreChainWithDepth = options.ignoreChainWithDepth || 2;
  64. const sourceCode = context.sourceCode;
  65. /**
  66. * Get the prefix of a given MemberExpression node.
  67. * If the MemberExpression node is a computed value it returns a
  68. * left bracket. If not it returns a period.
  69. * @param {ASTNode} node A MemberExpression node to get
  70. * @returns {string} The prefix of the node.
  71. */
  72. function getPrefix(node) {
  73. if (node.computed) {
  74. if (node.optional) {
  75. return "?.[";
  76. }
  77. return "[";
  78. }
  79. if (node.optional) {
  80. return "?.";
  81. }
  82. return ".";
  83. }
  84. /**
  85. * Gets the property text of a given MemberExpression node.
  86. * If the text is multiline, this returns only the first line.
  87. * @param {ASTNode} node A MemberExpression node to get.
  88. * @returns {string} The property text of the node.
  89. */
  90. function getPropertyText(node) {
  91. const prefix = getPrefix(node);
  92. const lines = sourceCode
  93. .getText(node.property)
  94. .split(astUtils.LINEBREAK_MATCHER);
  95. const suffix = node.computed && lines.length === 1 ? "]" : "";
  96. return prefix + lines[0] + suffix;
  97. }
  98. return {
  99. "CallExpression:exit"(node) {
  100. const callee = astUtils.skipChainExpression(node.callee);
  101. if (callee.type !== "MemberExpression") {
  102. return;
  103. }
  104. let parent = astUtils.skipChainExpression(callee.object);
  105. let depth = 1;
  106. while (parent && parent.callee) {
  107. depth += 1;
  108. parent = astUtils.skipChainExpression(
  109. astUtils.skipChainExpression(parent.callee).object,
  110. );
  111. }
  112. if (
  113. depth > ignoreChainWithDepth &&
  114. astUtils.isTokenOnSameLine(callee.object, callee.property)
  115. ) {
  116. const firstTokenAfterObject = sourceCode.getTokenAfter(
  117. callee.object,
  118. astUtils.isNotClosingParenToken,
  119. );
  120. context.report({
  121. node: callee.property,
  122. loc: {
  123. start: firstTokenAfterObject.loc.start,
  124. end: callee.loc.end,
  125. },
  126. messageId: "expected",
  127. data: {
  128. callee: getPropertyText(callee),
  129. },
  130. fix(fixer) {
  131. return fixer.insertTextBefore(
  132. firstTokenAfterObject,
  133. "\n",
  134. );
  135. },
  136. });
  137. }
  138. },
  139. };
  140. },
  141. };