Skip to content

Commit 99ac405

Browse files
committed
Option: treatUndefinedAsUnspecified - allows implicitly returning undefined with a return; statement.
1 parent 4aeccdd commit 99ac405

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

lib/rules/computed-property-return.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,24 @@
77
const utils = require('../utils')
88

99
function create (context) {
10+
const options = context.options[0] || {}
11+
const treatUndefinedAsUnspecified = !(options.treatUndefinedAsUnspecified === false)
12+
1013
let funcInfo = {
1114
funcInfo: null,
1215
codePath: null,
1316
hasReturn: false,
17+
hasReturnValue: false,
1418
node: null
1519
}
1620
const forbiddenNodes = []
1721

1822
// ----------------------------------------------------------------------
1923
// Helpers
2024
// ----------------------------------------------------------------------
25+
function isValidReturn () {
26+
return (!treatUndefinedAsUnspecified && funcInfo.hasReturn) || (treatUndefinedAsUnspecified && funcInfo.hasReturnValue)
27+
}
2128

2229
// ----------------------------------------------------------------------
2330
// Public
@@ -30,6 +37,7 @@ function create (context) {
3037
codePath,
3138
funcInfo: funcInfo,
3239
hasReturn: false,
40+
hasReturnValue: false,
3341
node
3442
}
3543
},
@@ -38,16 +46,10 @@ function create (context) {
3846
},
3947
ReturnStatement (node) {
4048
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)
4850
},
4951
'FunctionExpression:exit' (node) {
50-
if (funcInfo.codePath.currentSegments.some((segment) => segment.reachable) && !funcInfo.hasReturn) {
52+
if (!isValidReturn() && funcInfo.codePath.currentSegments.some((segment) => segment.reachable)) {
5153
forbiddenNodes.push({
5254
hasReturn: funcInfo.hasReturn,
5355
node: funcInfo.node,
@@ -86,13 +88,21 @@ function create (context) {
8688
module.exports = {
8789
meta: {
8890
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',
9193
recommended: false
9294
},
9395
fixable: null, // or "code" or "whitespace"
9496
schema: [
95-
// fill in your schema
97+
{
98+
type: 'object',
99+
properties: {
100+
treatUndefinedAsUnspecified: {
101+
type: 'boolean'
102+
}
103+
},
104+
additionalProperties: false
105+
}
96106
]
97107
},
98108

tests/lib/rules/computed-property-return.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ ruleTester.run('computed-property-return', rule, {
4343
}
4444
`,
4545
parserOptions: { ecmaVersion: 8, sourceType: 'module' }
46+
},
47+
{
48+
filename: 'test.vue',
49+
code: `
50+
export default {
51+
computed: {
52+
foo: {
53+
get () {
54+
return
55+
}
56+
}
57+
}
58+
}
59+
`,
60+
parserOptions: { ecmaVersion: 8, sourceType: 'module' },
61+
options: [{ treatUndefinedAsUnspecified: false }]
4662
}
4763
],
4864

0 commit comments

Comments
 (0)