Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

newline-per-chained-call.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. }
  29. },
  30. additionalProperties: false
  31. }]
  32. },
  33. create(context) {
  34. const options = context.options[0] || {},
  35. ignoreChainWithDepth = options.ignoreChainWithDepth || 2;
  36. const sourceCode = context.getSourceCode();
  37. /**
  38. * Get the prefix of a given MemberExpression node.
  39. * If the MemberExpression node is a computed value it returns a
  40. * left bracket. If not it returns a period.
  41. *
  42. * @param {ASTNode} node - A MemberExpression node to get
  43. * @returns {string} The prefix of the node.
  44. */
  45. function getPrefix(node) {
  46. return node.computed ? "[" : ".";
  47. }
  48. /**
  49. * Gets the property text of a given MemberExpression node.
  50. * If the text is multiline, this returns only the first line.
  51. *
  52. * @param {ASTNode} node - A MemberExpression node to get.
  53. * @returns {string} The property text of the node.
  54. */
  55. function getPropertyText(node) {
  56. const prefix = getPrefix(node);
  57. const lines = sourceCode.getText(node.property).split(astUtils.LINEBREAK_MATCHER);
  58. const suffix = node.computed && lines.length === 1 ? "]" : "";
  59. return prefix + lines[0] + suffix;
  60. }
  61. return {
  62. "CallExpression:exit"(node) {
  63. if (!node.callee || node.callee.type !== "MemberExpression") {
  64. return;
  65. }
  66. const callee = node.callee;
  67. let parent = callee.object;
  68. let depth = 1;
  69. while (parent && parent.callee) {
  70. depth += 1;
  71. parent = parent.callee.object;
  72. }
  73. if (depth > ignoreChainWithDepth && astUtils.isTokenOnSameLine(callee.object, callee.property)) {
  74. context.report({
  75. node: callee.property,
  76. loc: callee.property.loc.start,
  77. message: "Expected line break before `{{callee}}`.",
  78. data: {
  79. callee: getPropertyText(callee)
  80. },
  81. fix(fixer) {
  82. const firstTokenAfterObject = sourceCode.getTokenAfter(callee.object, astUtils.isNotClosingParenToken);
  83. return fixer.insertTextBefore(firstTokenAfterObject, "\n");
  84. }
  85. });
  86. }
  87. }
  88. };
  89. }
  90. };