7
7
const utils = require ( '../utils' )
8
8
9
9
function create ( context ) {
10
+ const options = context . options [ 0 ] || { }
11
+ const treatUndefinedAsUnspecified = ! ( options . treatUndefinedAsUnspecified === false )
12
+
10
13
let funcInfo = {
11
14
funcInfo : null ,
12
15
codePath : null ,
13
16
hasReturn : false ,
17
+ hasReturnValue : false ,
14
18
node : null
15
19
}
16
20
const forbiddenNodes = [ ]
17
21
18
22
// ----------------------------------------------------------------------
19
23
// Helpers
20
24
// ----------------------------------------------------------------------
25
+ function isValidReturn ( ) {
26
+ return ( ! treatUndefinedAsUnspecified && funcInfo . hasReturn ) || ( treatUndefinedAsUnspecified && funcInfo . hasReturnValue )
27
+ }
21
28
22
29
// ----------------------------------------------------------------------
23
30
// Public
@@ -30,6 +37,7 @@ function create (context) {
30
37
codePath,
31
38
funcInfo : funcInfo ,
32
39
hasReturn : false ,
40
+ hasReturnValue : false ,
33
41
node
34
42
}
35
43
} ,
@@ -38,16 +46,10 @@ function create (context) {
38
46
} ,
39
47
ReturnStatement ( node ) {
40
48
funcInfo . hasReturn = true
41
- if ( ! node . argument ) {
42
- forbiddenNodes . push ( {
43
- hasReturn : false ,
44
- node,
45
- type : 'return'
46
- } )
47
- }
49
+ funcInfo . hasReturnValue = Boolean ( node . argument )
48
50
} ,
49
51
'FunctionExpression:exit' ( node ) {
50
- if ( funcInfo . codePath . currentSegments . some ( ( segment ) => segment . reachable ) && ! funcInfo . hasReturn ) {
52
+ if ( ! isValidReturn ( ) && funcInfo . codePath . currentSegments . some ( ( segment ) => segment . reachable ) ) {
51
53
forbiddenNodes . push ( {
52
54
hasReturn : funcInfo . hasReturn ,
53
55
node : funcInfo . node ,
@@ -86,13 +88,21 @@ function create (context) {
86
88
module . exports = {
87
89
meta : {
88
90
docs : {
89
- description : 'Enforces that a return statement is present in computed property (computed-property-return) ' ,
90
- category : 'Fill me in ' ,
91
+ description : 'Enforces that a return statement is present in computed property. ' ,
92
+ category : 'Possible Errors ' ,
91
93
recommended : false
92
94
} ,
93
95
fixable : null , // or "code" or "whitespace"
94
96
schema : [
95
- // fill in your schema
97
+ {
98
+ type : 'object' ,
99
+ properties : {
100
+ treatUndefinedAsUnspecified : {
101
+ type : 'boolean'
102
+ }
103
+ } ,
104
+ additionalProperties : false
105
+ }
96
106
]
97
107
} ,
98
108
0 commit comments