newline-per-chained-call.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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("./utils/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. * @param {ASTNode} node A MemberExpression node to get
  46. * @returns {string} The prefix of the node.
  47. */
  48. function getPrefix(node) {
  49. return node.computed ? "[" : ".";
  50. }
  51. /**
  52. * Gets the property text of a given MemberExpression node.
  53. * If the text is multiline, this returns only the first line.
  54. * @param {ASTNode} node A MemberExpression node to get.
  55. * @returns {string} The property text of the node.
  56. */
  57. function getPropertyText(node) {
  58. const prefix = getPrefix(node);
  59. const lines = sourceCode.getText(node.property).split(astUtils.LINEBREAK_MATCHER);
  60. const suffix = node.computed && lines.length === 1 ? "]" : "";
  61. return prefix + lines[0] + suffix;
  62. }
  63. return {
  64. "CallExpression:exit"(node) {
  65. if (!node.callee || node.callee.type !== "MemberExpression") {
  66. return;
  67. }
  68. const callee = node.callee;
  69. let parent = callee.object;
  70. let depth = 1;
  71. while (parent && parent.callee) {
  72. depth += 1;
  73. parent = parent.callee.object;
  74. }
  75. if (depth > ignoreChainWithDepth && astUtils.isTokenOnSameLine(callee.object, callee.property)) {
  76. context.report({
  77. node: callee.property,
  78. loc: callee.property.loc.start,
  79. messageId: "expected",
  80. data: {
  81. callee: getPropertyText(callee)
  82. },
  83. fix(fixer) {
  84. const firstTokenAfterObject = sourceCode.getTokenAfter(callee.object, astUtils.isNotClosingParenToken);
  85. return fixer.insertTextBefore(firstTokenAfterObject, "\n");
  86. }
  87. });
  88. }
  89. }
  90. };
  91. }
  92. };