/
newline-per-chained-call.js
150 lines (144 loc) · 4.63 KB
/
newline-per-chained-call.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* @fileoverview Tests for newline-per-chained-call rule.
* @author Rajendra Patil
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
var rule = require("../../../lib/rules/newline-per-chained-call"),
RuleTester = require("../../../lib/testers/rule-tester");
var ruleTester = new RuleTester();
ruleTester.run("newline-per-chained-call", rule, {
valid: [{
code: "_\n.chain({})\n.map(foo)\n.filter(bar)\n.value();"
}, {
code: "a.b.c.d.e.f"
}, {
code: "a()\n.b()\n.c\n.e"
}, {
code: "var a = m1.m2(); var b = m1.m2();\nvar c = m1.m2()"
}, {
code: "var a = m1()\n.m2();"
}, {
code: "var a = m1();"
}, {
code: "a()\n.b().c.e.d()"
}, {
code: "a().b().c.e.d()"
}, {
code: "a.b.c.e.d()"
}, {
code: "var a = window\n.location\n.href\n.match(/(^[^#]*)/)[0];"
}, {
code: "var a = window['location']\n.href\n.match(/(^[^#]*)/)[0];"
}, {
code: "var a = window['location'].href.match(/(^[^#]*)/)[0];"
}, {
code: "var a = m1().m2.m3();",
options: [{
ignoreChainWithDepth: 3
}]
}, {
code: "var a = m1().m2.m3().m4.m5().m6.m7().m8;",
options: [{
ignoreChainWithDepth: 8
}]
}],
invalid: [{
code: "_\n.chain({}).map(foo).filter(bar).value();",
errors: [{
message: "Expected line break before `.filter`."
}, {
message: "Expected line break before `.value`."
}]
}, {
code: "_\n.chain({})\n.map(foo)\n.filter(bar).value();",
errors: [{
message: "Expected line break before `.value`."
}]
}, {
code: "a().b().c().e.d()",
errors: [{
message: "Expected line break before `.c`."
}]
}, {
code: "a.b.c().e().d()",
errors: [{
message: "Expected line break before `.d`."
}]
}, {
code: "_.chain({}).map(a).value(); ",
errors: [{
message: "Expected line break before `.value`."
}]
}, {
code: "var a = m1.m2();\n var b = m1.m2().m3().m4().m5();",
errors: [{
message: "Expected line break before `.m4`."
}, {
message: "Expected line break before `.m5`."
}]
}, {
code: "var a = m1.m2();\n var b = m1.m2().m3()\n.m4().m5();",
errors: [{
message: "Expected line break before `.m5`."
}]
}, {
code: "var a = m1().m2\n.m3().m4().m5().m6().m7();",
options: [{
ignoreChainWithDepth: 3
}],
errors: [{
message: "Expected line break before `.m6`."
}, {
message: "Expected line break before `.m7`."
}]
}, {
code: [
"http.request({",
" // Param",
" // Param",
" // Param",
"}).on('response', function(response) {",
" // Do something with response.",
" // Do something with response.",
" // Do something with response.",
" // Do something with response.",
" // Do something with response.",
" // Do something with response.",
" // Do something with response.",
" // Do something with response.",
" // Do something with response.",
" // Do something with response.",
"}).on('error', function(error) {",
" // Do something with error.",
" // Do something with error.",
" // Do something with error.",
" // Do something with error.",
" // Do something with error.",
" // Do something with error.",
" // Do something with error.",
" // Do something with error.",
" // Do something with error.",
" // Do something with error.",
"}).end();"
].join("\n"),
errors: [{
message: "Expected line break before `.on`."
}, {
message: "Expected line break before `.end`."
}]
}, {
code: [
"anObject.method1().method2()['method' + n]()[aCondition ?",
" 'method3' :",
" 'method4']()"
].join("\n"),
errors: [{
message: "Expected line break before `['method' + n]`."
}, {
message: "Expected line break before `[aCondition ?`."
}]
}]
});