]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/getter-return.md
b35f0d5cfd23a55f7b61c2ef69c7a7fff6cd98ec
[pve-eslint.git] / eslint / docs / src / rules / getter-return.md
1 ---
2 title: getter-return
3 layout: doc
4 rule_type: problem
5 further_reading:
6 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get
7 - https://leanpub.com/understandinges6/read/#leanpub-auto-accessor-properties
8 ---
9
10
11
12 The get syntax binds an object property to a function that will be called when that property is looked up. It was first introduced in ECMAScript 5:
13
14 ```js
15 var p = {
16 get name(){
17 return "nicholas";
18 }
19 };
20
21 Object.defineProperty(p, "age", {
22 get: function (){
23 return 17;
24 }
25 });
26 ```
27
28 Note that every `getter` is expected to return a value.
29
30 ## Rule Details
31
32 This rule enforces that a return statement is present in property getters.
33
34 Examples of **incorrect** code for this rule:
35
36 ::: incorrect
37
38 ```js
39 /*eslint getter-return: "error"*/
40
41 p = {
42 get name(){
43 // no returns.
44 }
45 };
46
47 Object.defineProperty(p, "age", {
48 get: function (){
49 // no returns.
50 }
51 });
52
53 class P{
54 get name(){
55 // no returns.
56 }
57 }
58 ```
59
60 :::
61
62 Examples of **correct** code for this rule:
63
64 ::: correct
65
66 ```js
67 /*eslint getter-return: "error"*/
68
69 p = {
70 get name(){
71 return "nicholas";
72 }
73 };
74
75 Object.defineProperty(p, "age", {
76 get: function (){
77 return 18;
78 }
79 });
80
81 class P{
82 get name(){
83 return "nicholas";
84 }
85 }
86 ```
87
88 :::
89
90 ## Options
91
92 This rule has an object option:
93
94 * `"allowImplicit": false` (default) disallows implicitly returning `undefined` with a `return` statement.
95
96 Examples of **correct** code for the `{ "allowImplicit": true }` option:
97
98 ::: correct
99
100 ```js
101 /*eslint getter-return: ["error", { allowImplicit: true }]*/
102 p = {
103 get name(){
104 return; // return undefined implicitly.
105 }
106 };
107 ```
108
109 :::
110
111 ## When Not To Use It
112
113 If your project will not be using ES5 property getters you do not need this rule.