]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/newline-per-chained-call.js
a0eefd2e790e4e23b676b0336b6554958657f658
[pve-eslint.git] / eslint / tests / lib / rules / newline-per-chained-call.js
1 /**
2 * @fileoverview Tests for newline-per-chained-call rule.
3 * @author Rajendra Patil
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/newline-per-chained-call"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15 const ruleTester = new RuleTester();
16
17 ruleTester.run("newline-per-chained-call", rule, {
18 valid: ["_\n.chain({})\n.map(foo)\n.filter(bar)\n.value();", "a.b.c.d.e.f", "a()\n.b()\n.c\n.e", "var a = m1.m2(); var b = m1.m2();\nvar c = m1.m2()", "var a = m1()\n.m2();", "var a = m1();", "a()\n.b().c.e.d()", "a().b().c.e.d()", "a.b.c.e.d()", "var a = window\n.location\n.href\n.match(/(^[^#]*)/)[0];", "var a = window['location']\n.href\n.match(/(^[^#]*)/)[0];", "var a = window['location'].href.match(/(^[^#]*)/)[0];", {
19 code: "var a = m1().m2.m3();",
20 options: [{
21 ignoreChainWithDepth: 3
22 }]
23 }, {
24 code: "var a = m1().m2.m3().m4.m5().m6.m7().m8;",
25 options: [{
26 ignoreChainWithDepth: 8
27 }]
28 }],
29 invalid: [{
30 code: "_\n.chain({}).map(foo).filter(bar).value();",
31 output: "_\n.chain({}).map(foo)\n.filter(bar)\n.value();",
32 errors: [{
33 messageId: "expected", data: { callee: ".filter" }
34 }, {
35 messageId: "expected", data: { callee: ".value" }
36 }]
37 }, {
38 code: "_\n.chain({})\n.map(foo)\n.filter(bar).value();",
39 output: "_\n.chain({})\n.map(foo)\n.filter(bar)\n.value();",
40 errors: [{
41 messageId: "expected", data: { callee: ".value" }
42 }]
43 }, {
44 code: "a().b().c().e.d()",
45 output: "a().b()\n.c().e.d()",
46 errors: [{
47 messageId: "expected", data: { callee: ".c" }
48 }]
49 }, {
50 code: "a.b.c().e().d()",
51 output: "a.b.c().e()\n.d()",
52 errors: [{
53 messageId: "expected", data: { callee: ".d" }
54 }]
55 }, {
56 code: "_.chain({}).map(a).value(); ",
57 output: "_.chain({}).map(a)\n.value(); ",
58 errors: [{
59 messageId: "expected", data: { callee: ".value" }
60 }]
61 }, {
62 code: "var a = m1.m2();\n var b = m1.m2().m3().m4().m5();",
63 output: "var a = m1.m2();\n var b = m1.m2().m3()\n.m4()\n.m5();",
64 errors: [{
65 messageId: "expected", data: { callee: ".m4" }
66 }, {
67 messageId: "expected", data: { callee: ".m5" }
68 }]
69 }, {
70 code: "var a = m1.m2();\n var b = m1.m2().m3()\n.m4().m5();",
71 output: "var a = m1.m2();\n var b = m1.m2().m3()\n.m4()\n.m5();",
72 errors: [{
73 messageId: "expected", data: { callee: ".m5" }
74 }]
75 }, {
76 code: "var a = m1().m2\n.m3().m4().m5().m6().m7();",
77 output: "var a = m1().m2\n.m3().m4().m5()\n.m6()\n.m7();",
78 options: [{
79 ignoreChainWithDepth: 3
80 }],
81 errors: [{
82 messageId: "expected", data: { callee: ".m6" }
83 }, {
84 messageId: "expected", data: { callee: ".m7" }
85 }]
86 }, {
87 code: [
88 "http.request({",
89 " // Param",
90 " // Param",
91 " // Param",
92 "}).on('response', function(response) {",
93 " // Do something with response.",
94 " // Do something with response.",
95 " // Do something with response.",
96 " // Do something with response.",
97 " // Do something with response.",
98 " // Do something with response.",
99 " // Do something with response.",
100 " // Do something with response.",
101 " // Do something with response.",
102 " // Do something with response.",
103 "}).on('error', function(error) {",
104 " // Do something with error.",
105 " // Do something with error.",
106 " // Do something with error.",
107 " // Do something with error.",
108 " // Do something with error.",
109 " // Do something with error.",
110 " // Do something with error.",
111 " // Do something with error.",
112 " // Do something with error.",
113 " // Do something with error.",
114 "}).end();"
115 ].join("\n"),
116 output: [
117 "http.request({",
118 " // Param",
119 " // Param",
120 " // Param",
121 "}).on('response', function(response) {",
122 " // Do something with response.",
123 " // Do something with response.",
124 " // Do something with response.",
125 " // Do something with response.",
126 " // Do something with response.",
127 " // Do something with response.",
128 " // Do something with response.",
129 " // Do something with response.",
130 " // Do something with response.",
131 " // Do something with response.",
132 "})",
133 ".on('error', function(error) {",
134 " // Do something with error.",
135 " // Do something with error.",
136 " // Do something with error.",
137 " // Do something with error.",
138 " // Do something with error.",
139 " // Do something with error.",
140 " // Do something with error.",
141 " // Do something with error.",
142 " // Do something with error.",
143 " // Do something with error.",
144 "})",
145 ".end();"
146 ].join("\n"),
147 errors: [{
148 messageId: "expected", data: { callee: ".on" }
149 }, {
150 messageId: "expected", data: { callee: ".end" }
151 }]
152 }, {
153 code: [
154 "anObject.method1().method2()['method' + n]()[aCondition ?",
155 " 'method3' :",
156 " 'method4']()"
157 ].join("\n"),
158 output: [
159 "anObject.method1().method2()",
160 "['method' + n]()",
161 "[aCondition ?",
162 " 'method3' :",
163 " 'method4']()"
164 ].join("\n"),
165 errors: [{
166 messageId: "expected", data: { callee: "['method' + n]" }
167 }, {
168 messageId: "expected", data: { callee: "[aCondition ?" }
169 }]
170 }, {
171 code: "foo.bar()['foo' + \u2029 + 'bar']()",
172 output: "foo.bar()\n['foo' + \u2029 + 'bar']()",
173 options: [{ ignoreChainWithDepth: 1 }],
174 errors: [{ messageId: "expected", data: { callee: "['foo' + " } }]
175 }, {
176 code: "foo.bar()[(biz)]()",
177 output: "foo.bar()\n[(biz)]()",
178 options: [{ ignoreChainWithDepth: 1 }],
179 errors: [{ messageId: "expected", data: { callee: "[biz]" } }]
180 }, {
181 code: "(foo).bar().biz()",
182 output: "(foo).bar()\n.biz()",
183 options: [{ ignoreChainWithDepth: 1 }],
184 errors: [{ messageId: "expected", data: { callee: ".biz" } }]
185 }, {
186 code: "foo.bar(). /* comment */ biz()",
187 output: "foo.bar()\n. /* comment */ biz()",
188 options: [{ ignoreChainWithDepth: 1 }],
189 errors: [{ messageId: "expected", data: { callee: ".biz" } }]
190 }, {
191 code: "foo.bar() /* comment */ .biz()",
192 output: "foo.bar() /* comment */ \n.biz()",
193 options: [{ ignoreChainWithDepth: 1 }],
194 errors: [{ messageId: "expected", data: { callee: ".biz" } }]
195 }]
196 });