]> git.proxmox.com Git - pve-eslint.git/blame - eslint/tests/lib/rules/getter-return.js
import 8.3.0 source
[pve-eslint.git] / eslint / tests / lib / rules / getter-return.js
CommitLineData
eb39fafa
DC
1/**
2 * @fileoverview Enforces that a return statement is present in property getters.
3 * @author Aladdin-ADD(hh_2013@foxmail.com)
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12const rule = require("../../../lib/rules/getter-return");
13const { RuleTester } = require("../../../lib/rule-tester");
14
15//------------------------------------------------------------------------------
16// Tests
17//------------------------------------------------------------------------------
18
609c276f 19const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2022 } });
eb39fafa
DC
20const expectedError = { messageId: "expected", data: { name: "getter 'bar'" } };
21const expectedAlwaysError = { messageId: "expectedAlways", data: { name: "getter 'bar'" } };
22const options = [{ allowImplicit: true }];
23
24ruleTester.run("getter-return", rule, {
25
26 valid: [
27
28 /*
29 * test obj: get
30 * option: {allowImplicit: false}
31 */
32 "var foo = { get bar(){return true;} };",
33
34 // option: {allowImplicit: true}
35 { code: "var foo = { get bar() {return;} };", options },
36 { code: "var foo = { get bar(){return true;} };", options },
37 { code: "var foo = { get bar(){if(bar) {return;} return true;} };", options },
38
39 /*
40 * test class: get
41 * option: {allowImplicit: false}
42 */
43 "class foo { get bar(){return true;} }",
44 "class foo { get bar(){if(baz){return true;} else {return false;} } }",
45 "class foo { get(){return true;} }",
46
47 // option: {allowImplicit: true}
48 { code: "class foo { get bar(){return true;} }", options },
49 { code: "class foo { get bar(){return;} }", options },
50
51 /*
52 * test object.defineProperty(s)
53 * option: {allowImplicit: false}
54 */
55 "Object.defineProperty(foo, \"bar\", { get: function () {return true;}});",
56 "Object.defineProperty(foo, \"bar\", { get: function () { ~function (){ return true; }();return true;}});",
57 "Object.defineProperties(foo, { bar: { get: function () {return true;}} });",
58 "Object.defineProperties(foo, { bar: { get: function () { ~function (){ return true; }(); return true;}} });",
59
60 // option: {allowImplicit: true}
61 { code: "Object.defineProperty(foo, \"bar\", { get: function () {return true;}});", options },
62 { code: "Object.defineProperty(foo, \"bar\", { get: function (){return;}});", options },
63 { code: "Object.defineProperties(foo, { bar: { get: function () {return true;}} });", options },
64 { code: "Object.defineProperties(foo, { bar: { get: function () {return;}} });", options },
65
66 // not getter.
67 "var get = function(){};",
68 "var get = function(){ return true; };",
69 "var foo = { bar(){} };",
70 "var foo = { bar(){ return true; } };",
71 "var foo = { bar: function(){} };",
72 "var foo = { bar: function(){return;} };",
73 "var foo = { bar: function(){return true;} };",
74 "var foo = { get: function () {} }",
609c276f
TL
75 "var foo = { get: () => {}};",
76 "class C { get; foo() {} }"
eb39fafa
DC
77 ],
78
79 invalid: [
80
81 /*
82 * test obj: get
83 * option: {allowImplicit: false}
84 */
56c4a2cb
DC
85 {
86 code: "var foo = { get bar() {} };",
87 errors: [{
88 ...expectedError,
89 line: 1,
90 column: 13,
91 endLine: 1,
92 endColumn: 20
93 }]
94 },
95 {
96 code: "var foo = { get\n bar () {} };",
97 errors: [{
98 ...expectedError,
99 line: 1,
100 column: 13,
101 endLine: 2,
102 endColumn: 6
103 }]
104 },
105 {
106 code: "var foo = { get bar(){if(baz) {return true;}} };",
107 errors: [{
108 ...expectedAlwaysError,
109 line: 1,
110 column: 13,
111 endLine: 1,
112 endColumn: 20
113 }]
114 },
115 {
116 code: "var foo = { get bar() { ~function () {return true;}} };",
117 errors: [{
118 ...expectedError,
119 line: 1,
120 column: 13,
121 endLine: 1,
122 endColumn: 20
123 }]
124 },
125 {
126 code: "var foo = { get bar() { return; } };",
127 errors: [{
128 ...expectedError,
129 line: 1,
130 column: 25,
131 endLine: 1,
132 endColumn: 32
133 }]
134 },
eb39fafa
DC
135
136 // option: {allowImplicit: true}
137 { code: "var foo = { get bar() {} };", options, errors: [expectedError] },
138 { code: "var foo = { get bar() {if (baz) {return;}} };", options, errors: [expectedAlwaysError] },
139
140 /*
141 * test class: get
142 * option: {allowImplicit: false}
143 */
56c4a2cb
DC
144 {
145 code: "class foo { get bar(){} }",
146 errors: [{
147 ...expectedError,
148 line: 1,
149 column: 13,
150 endLine: 1,
151 endColumn: 20
152 }]
153 },
154 {
155 code: "var foo = class {\n static get\nbar(){} }",
156 errors: [{
157 messageId: "expected",
158 data: { name: "static getter 'bar'" },
159 line: 2,
160 column: 3,
161 endLine: 3,
162 endColumn: 4
163 }]
164 },
eb39fafa
DC
165 { code: "class foo { get bar(){ if (baz) { return true; }}}", errors: [expectedAlwaysError] },
166 { code: "class foo { get bar(){ ~function () { return true; }()}}", errors: [expectedError] },
167
168 // option: {allowImplicit: true}
169 { code: "class foo { get bar(){} }", options, errors: [expectedError] },
170 { code: "class foo { get bar(){if (baz) {return true;} } }", options, errors: [expectedAlwaysError] },
171
172 /*
173 * test object.defineProperty(s)
174 * option: {allowImplicit: false}
175 */
56c4a2cb
DC
176 {
177 code: "Object.defineProperty(foo, 'bar', { get: function (){}});",
178 errors: [{
179 messageId: "expected",
180 data: { name: "method 'get'" },
181 line: 1,
182 column: 37,
183 endLine: 1,
184 endColumn: 51
185 }]
186 },
187 {
188 code: "Object.defineProperty(foo, 'bar', { get: function getfoo (){}});",
189 errors: [{
190 messageId: "expected",
609c276f 191 data: { name: "method 'get'" },
56c4a2cb
DC
192 line: 1,
193 column: 37,
194 endLine: 1,
195 endColumn: 58
196 }]
197 },
198 {
199 code: "Object.defineProperty(foo, 'bar', { get(){} });",
200 errors: [{
201 messageId: "expected",
202 data: { name: "method 'get'" },
203 line: 1,
204 column: 37,
205 endLine: 1,
206 endColumn: 40
207 }]
208 },
209 {
210 code: "Object.defineProperty(foo, 'bar', { get: () => {}});",
211 errors: [{
212 messageId: "expected",
609c276f 213 data: { name: "method 'get'" },
56c4a2cb 214 line: 1,
609c276f 215 column: 37,
56c4a2cb 216 endLine: 1,
609c276f 217 endColumn: 42
56c4a2cb
DC
218 }]
219 },
eb39fafa
DC
220 { code: "Object.defineProperty(foo, \"bar\", { get: function (){if(bar) {return true;}}});", errors: [{ messageId: "expectedAlways" }] },
221 { code: "Object.defineProperty(foo, \"bar\", { get: function (){ ~function () { return true; }()}});", errors: [{ messageId: "expected" }] },
6f036462
TL
222
223 // option: {allowImplicit: true}
eb39fafa
DC
224 { code: "Object.defineProperties(foo, { bar: { get: function () {}} });", options, errors: [{ messageId: "expected" }] },
225 { code: "Object.defineProperties(foo, { bar: { get: function (){if(bar) {return true;}}}});", options, errors: [{ messageId: "expectedAlways" }] },
226 { code: "Object.defineProperties(foo, { bar: { get: function () {~function () { return true; }()}} });", options, errors: [{ messageId: "expected" }] },
6f036462 227 { code: "Object.defineProperty(foo, \"bar\", { get: function (){}});", options, errors: [{ messageId: "expected" }] },
eb39fafa 228
6f036462
TL
229 // Optional chaining
230 {
231 code: "Object?.defineProperty(foo, 'bar', { get: function (){} });",
232 parserOptions: { ecmaVersion: 2020 },
233 errors: [{ messageId: "expected", data: { name: "method 'get'" } }]
234 },
235 {
236 code: "(Object?.defineProperty)(foo, 'bar', { get: function (){} });",
237 parserOptions: { ecmaVersion: 2020 },
238 errors: [{ messageId: "expected", data: { name: "method 'get'" } }]
239 },
240 {
241 code: "Object?.defineProperty(foo, 'bar', { get: function (){} });",
242 options,
243 parserOptions: { ecmaVersion: 2020 },
244 errors: [{ messageId: "expected", data: { name: "method 'get'" } }]
245 },
246 {
247 code: "(Object?.defineProperty)(foo, 'bar', { get: function (){} });",
248 options,
249 parserOptions: { ecmaVersion: 2020 },
250 errors: [{ messageId: "expected", data: { name: "method 'get'" } }]
251 }
eb39fafa
DC
252 ]
253});