newline-per-chained-call.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * @fileoverview Rule to ensure newline per method call when chaining calls
  3. * @author Rajendra Patil
  4. * @author Burak Yigit Kaya
  5. */
  6. "use strict";
  7. const astUtils = require("../util/ast-utils");
  8. //------------------------------------------------------------------------------
  9. // Rule Definition
  10. //------------------------------------------------------------------------------
  11. module.exports = {
  12. meta: {
  13. type: "layout",
  14. docs: {
  15. description: "require a newline after each call in a method chain",
  16. category: "Stylistic Issues",
  17. recommended: false,
  18. url: "https://eslint.org/docs/rules/newline-per-chained-call"
  19. },
  20. fixable: "whitespace",
  21. schema: [{
  22. type: "object",
  23. properties: {
  24. ignoreChainWithDepth: {
  25. type: "integer",
  26. minimum: 1,
  27. maximum: 10,
  28. default: 2
  29. }
  30. },
  31. additionalProperties: false
  32. }],
  33. messages: {
  34. expected: "Expected line break before `{{callee}}`."
  35. }
  36. },
  37. create(context) {
  38. const options = context.options[0] || {},
  39. ignoreChainWithDepth = options.ignoreChainWithDepth || 2;
  40. const sourceCode = context.getSourceCode();
  41. /**
  42. * Get the prefix of a given MemberExpression node.
  43. * If the MemberExpression node is a computed value it returns a
  44. * left bracket. If not it returns a period.
  45. *
  46. * @param {ASTNode} node - A MemberExpression node to get
  47. * @returns {string} The prefix of the node.
  48. */
  49. function getPrefix(node) {
  50. return node.computed ? "[" : ".";
  51. }
  52. /**
  53. * Gets the property text of a given MemberExpression node.
  54. * If the text is multiline, this returns only the first line.
  55. *
  56. * @param {ASTNode} node - A MemberExpression node to get.
  57. * @returns {string} The property text of the node.
  58. */
  59. function getPropertyText(node) {
  60. const prefix = getPrefix(node);
  61. const lines = sourceCode.getText(node.property).split(astUtils.LINEBREAK_MATCHER);
  62. const suffix = node.computed && lines.length === 1 ? "]" : "";
  63. return prefix + lines[0] + suffix;
  64. }
  65. return {
  66. "CallExpression:exit"(node) {
  67. if (!node.callee || node.callee.type !== "MemberExpression") {
  68. return;
  69. }
  70. const callee = node.callee;
  71. let parent = callee.object;
  72. let depth = 1;
  73. while (parent && parent.callee) {
  74. depth += 1;
  75. parent = parent.callee.object;
  76. }
  77. if (depth > ignoreChainWithDepth && astUtils.isTokenOnSameLine(callee.object, callee.property)) {
  78. context.report({
  79. node: callee.property,
  80. loc: callee.property.loc.start,
  81. messageId: "expected",
  82. data: {
  83. callee: getPropertyText(callee)
  84. },
  85. fix(fixer) {
  86. const firstTokenAfterObject = sourceCode.getTokenAfter(callee.object, astUtils.isNotClosingParenToken);
  87. return fixer.insertTextBefore(firstTokenAfterObject, "\n");
  88. }
  89. });
  90. }
  91. }
  92. };
  93. }
  94. };