]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/getter-return.js
082e584aeb5c94369c19e616ab004112d807a0a5
2 * @fileoverview Enforces that a return statement is present in property getters.
3 * @author Aladdin-ADD(hh_2013@foxmail.com)
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const rule
= require("../../../lib/rules/getter-return");
13 const { RuleTester
} = require("../../../lib/rule-tester");
15 //------------------------------------------------------------------------------
17 //------------------------------------------------------------------------------
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 }];
24 ruleTester
.run("getter-return", rule
, {
30 * option: {allowImplicit: false}
32 "var foo = { get bar(){return true;} };",
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
},
41 * option: {allowImplicit: false}
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;} }",
47 // option: {allowImplicit: true}
48 { code
: "class foo { get bar(){return true;} }", options
},
49 { code
: "class foo { get bar(){return;} }", options
},
52 * test object.defineProperty(s)
53 * option: {allowImplicit: false}
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;}} });",
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
},
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: () => {}};"
82 * option: {allowImplicit: false}
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
] },
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
] },
94 * option: {allowImplicit: false}
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
] },
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
] },
105 * test object.defineProperty(s)
106 * option: {allowImplicit: false}
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" }] },
116 // option: {allowImplicit: true}
117 { code
: "Object.defineProperty(foo, \"bar\", { get: function (){}});", options
, errors
: [{ messageId
: "expected" }] }