]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/getter-return.js
082e584aeb5c94369c19e616ab004112d807a0a5
[pve-eslint.git] / eslint / tests / lib / rules / getter-return.js
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
12 const rule = require("../../../lib/rules/getter-return");
13 const { RuleTester } = require("../../../lib/rule-tester");
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } });
20 const expectedError = { messageId: "expected", data: { name: "getter 'bar'" } };
21 const expectedAlwaysError = { messageId: "expectedAlways", data: { name: "getter 'bar'" } };
22 const options = [{ allowImplicit: true }];
23
24 ruleTester.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 () {} }",
75 "var foo = { get: () => {}};"
76 ],
77
78 invalid: [
79
80 /*
81 * test obj: get
82 * option: {allowImplicit: false}
83 */
84 { code: "var foo = { get bar() {} };", errors: [expectedError] },
85 { code: "var foo = { get bar(){if(baz) {return true;}} };", errors: [expectedAlwaysError] },
86 { code: "var foo = { get bar() { ~function () {return true;}} };", errors: [expectedError] },
87
88 // option: {allowImplicit: true}
89 { code: "var foo = { get bar() {} };", options, errors: [expectedError] },
90 { code: "var foo = { get bar() {if (baz) {return;}} };", options, errors: [expectedAlwaysError] },
91
92 /*
93 * test class: get
94 * option: {allowImplicit: false}
95 */
96 { code: "class foo { get bar(){} }", errors: [expectedError] },
97 { code: "class foo { get bar(){ if (baz) { return true; }}}", errors: [expectedAlwaysError] },
98 { code: "class foo { get bar(){ ~function () { return true; }()}}", errors: [expectedError] },
99
100 // option: {allowImplicit: true}
101 { code: "class foo { get bar(){} }", options, errors: [expectedError] },
102 { code: "class foo { get bar(){if (baz) {return true;} } }", options, errors: [expectedAlwaysError] },
103
104 /*
105 * test object.defineProperty(s)
106 * option: {allowImplicit: false}
107 */
108 { code: "Object.defineProperty(foo, \"bar\", { get: function (){}});", errors: [{ messageId: "expected", data: { name: "method 'get'" } }] },
109 { code: "Object.defineProperty(foo, \"bar\", { get: () => {}});", errors: [{ messageId: "expected", data: { name: "arrow function 'get'" } }] },
110 { code: "Object.defineProperty(foo, \"bar\", { get: function (){if(bar) {return true;}}});", errors: [{ messageId: "expectedAlways" }] },
111 { code: "Object.defineProperty(foo, \"bar\", { get: function (){ ~function () { return true; }()}});", errors: [{ messageId: "expected" }] },
112 { code: "Object.defineProperties(foo, { bar: { get: function () {}} });", options, errors: [{ messageId: "expected" }] },
113 { code: "Object.defineProperties(foo, { bar: { get: function (){if(bar) {return true;}}}});", options, errors: [{ messageId: "expectedAlways" }] },
114 { code: "Object.defineProperties(foo, { bar: { get: function () {~function () { return true; }()}} });", options, errors: [{ messageId: "expected" }] },
115
116 // option: {allowImplicit: true}
117 { code: "Object.defineProperty(foo, \"bar\", { get: function (){}});", options, errors: [{ messageId: "expected" }] }
118 ]
119 });