项目原始demo,不改动
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 

105 lines
3.4 KiB

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