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