Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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 4.0KB

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