/
getter-return.js
228 lines (207 loc) · 8.16 KB
/
getter-return.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* @fileoverview Enforces that a return statement is present in property getters.
* @author Aladdin-ADD(hh_2013@foxmail.com)
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require("../../../lib/rules/getter-return");
const { RuleTester } = require("../../../lib/rule-tester");
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } });
const expectedError = { messageId: "expected", data: { name: "getter 'bar'" } };
const expectedAlwaysError = { messageId: "expectedAlways", data: { name: "getter 'bar'" } };
const options = [{ allowImplicit: true }];
ruleTester.run("getter-return", rule, {
valid: [
/*
* test obj: get
* option: {allowImplicit: false}
*/
"var foo = { get bar(){return true;} };",
// option: {allowImplicit: true}
{ code: "var foo = { get bar() {return;} };", options },
{ code: "var foo = { get bar(){return true;} };", options },
{ code: "var foo = { get bar(){if(bar) {return;} return true;} };", options },
/*
* test class: get
* option: {allowImplicit: false}
*/
"class foo { get bar(){return true;} }",
"class foo { get bar(){if(baz){return true;} else {return false;} } }",
"class foo { get(){return true;} }",
// option: {allowImplicit: true}
{ code: "class foo { get bar(){return true;} }", options },
{ code: "class foo { get bar(){return;} }", options },
/*
* test object.defineProperty(s)
* option: {allowImplicit: false}
*/
"Object.defineProperty(foo, \"bar\", { get: function () {return true;}});",
"Object.defineProperty(foo, \"bar\", { get: function () { ~function (){ return true; }();return true;}});",
"Object.defineProperties(foo, { bar: { get: function () {return true;}} });",
"Object.defineProperties(foo, { bar: { get: function () { ~function (){ return true; }(); return true;}} });",
// option: {allowImplicit: true}
{ code: "Object.defineProperty(foo, \"bar\", { get: function () {return true;}});", options },
{ code: "Object.defineProperty(foo, \"bar\", { get: function (){return;}});", options },
{ code: "Object.defineProperties(foo, { bar: { get: function () {return true;}} });", options },
{ code: "Object.defineProperties(foo, { bar: { get: function () {return;}} });", options },
// not getter.
"var get = function(){};",
"var get = function(){ return true; };",
"var foo = { bar(){} };",
"var foo = { bar(){ return true; } };",
"var foo = { bar: function(){} };",
"var foo = { bar: function(){return;} };",
"var foo = { bar: function(){return true;} };",
"var foo = { get: function () {} }",
"var foo = { get: () => {}};"
],
invalid: [
/*
* test obj: get
* option: {allowImplicit: false}
*/
{
code: "var foo = { get bar() {} };",
errors: [{
...expectedError,
line: 1,
column: 13,
endLine: 1,
endColumn: 20
}]
},
{
code: "var foo = { get\n bar () {} };",
errors: [{
...expectedError,
line: 1,
column: 13,
endLine: 2,
endColumn: 6
}]
},
{
code: "var foo = { get bar(){if(baz) {return true;}} };",
errors: [{
...expectedAlwaysError,
line: 1,
column: 13,
endLine: 1,
endColumn: 20
}]
},
{
code: "var foo = { get bar() { ~function () {return true;}} };",
errors: [{
...expectedError,
line: 1,
column: 13,
endLine: 1,
endColumn: 20
}]
},
{
code: "var foo = { get bar() { return; } };",
errors: [{
...expectedError,
line: 1,
column: 25,
endLine: 1,
endColumn: 32
}]
},
// option: {allowImplicit: true}
{ code: "var foo = { get bar() {} };", options, errors: [expectedError] },
{ code: "var foo = { get bar() {if (baz) {return;}} };", options, errors: [expectedAlwaysError] },
/*
* test class: get
* option: {allowImplicit: false}
*/
{
code: "class foo { get bar(){} }",
errors: [{
...expectedError,
line: 1,
column: 13,
endLine: 1,
endColumn: 20
}]
},
{
code: "var foo = class {\n static get\nbar(){} }",
errors: [{
messageId: "expected",
data: { name: "static getter 'bar'" },
line: 2,
column: 3,
endLine: 3,
endColumn: 4
}]
},
{ code: "class foo { get bar(){ if (baz) { return true; }}}", errors: [expectedAlwaysError] },
{ code: "class foo { get bar(){ ~function () { return true; }()}}", errors: [expectedError] },
// option: {allowImplicit: true}
{ code: "class foo { get bar(){} }", options, errors: [expectedError] },
{ code: "class foo { get bar(){if (baz) {return true;} } }", options, errors: [expectedAlwaysError] },
/*
* test object.defineProperty(s)
* option: {allowImplicit: false}
*/
{
code: "Object.defineProperty(foo, 'bar', { get: function (){}});",
errors: [{
messageId: "expected",
data: { name: "method 'get'" },
line: 1,
column: 37,
endLine: 1,
endColumn: 51
}]
},
{
code: "Object.defineProperty(foo, 'bar', { get: function getfoo (){}});",
errors: [{
messageId: "expected",
data: { name: "method 'getfoo'" },
line: 1,
column: 37,
endLine: 1,
endColumn: 58
}]
},
{
code: "Object.defineProperty(foo, 'bar', { get(){} });",
errors: [{
messageId: "expected",
data: { name: "method 'get'" },
line: 1,
column: 37,
endLine: 1,
endColumn: 40
}]
},
{
code: "Object.defineProperty(foo, 'bar', { get: () => {}});",
errors: [{
messageId: "expected",
data: { name: "arrow function 'get'" },
line: 1,
column: 45,
endLine: 1,
endColumn: 47
}]
},
{ code: "Object.defineProperty(foo, \"bar\", { get: function (){if(bar) {return true;}}});", errors: [{ messageId: "expectedAlways" }] },
{ code: "Object.defineProperty(foo, \"bar\", { get: function (){ ~function () { return true; }()}});", errors: [{ messageId: "expected" }] },
{ code: "Object.defineProperties(foo, { bar: { get: function () {}} });", options, errors: [{ messageId: "expected" }] },
{ code: "Object.defineProperties(foo, { bar: { get: function (){if(bar) {return true;}}}});", options, errors: [{ messageId: "expectedAlways" }] },
{ code: "Object.defineProperties(foo, { bar: { get: function () {~function () { return true; }()}} });", options, errors: [{ messageId: "expected" }] },
// option: {allowImplicit: true}
{ code: "Object.defineProperty(foo, \"bar\", { get: function (){}});", options, errors: [{ messageId: "expected" }] }
]
});