Skip to content

Commit 76bf945

Browse files
committed
Add new function findMissingReturns
1 parent 807cbdf commit 76bf945

File tree

5 files changed

+108
-65
lines changed

5 files changed

+108
-65
lines changed

lib/rules/require-render-return.js

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,38 @@
66

77
const utils = require('../utils')
88

9+
function create (context) {
10+
const forbiddenNodes = []
11+
12+
// ----------------------------------------------------------------------
13+
// Public
14+
// ----------------------------------------------------------------------
15+
16+
return Object.assign({},
17+
utils.findMissingReturns(forbiddenNodes, true),
18+
utils.executeOnVue(context, obj => {
19+
const node = obj.properties.find(item => item.type === 'Property' &&
20+
utils.getStaticPropertyName(item) === 'render' &&
21+
(item.value.type === 'ArrowFunctionExpression' || item.value.type === 'FunctionExpression') &&
22+
!item.value.expression // render: () => test
23+
)
24+
if (!node) return
25+
26+
forbiddenNodes.forEach(el => {
27+
if (
28+
el.loc.start.line >= node.value.loc.start.line &&
29+
el.loc.end.line <= node.value.loc.end.line
30+
) {
31+
context.report({
32+
node: node.key,
33+
message: 'Expected to return a value in render function.'
34+
})
35+
}
36+
})
37+
})
38+
)
39+
}
40+
941
// ------------------------------------------------------------------------------
1042
// Rule Definition
1143
// ------------------------------------------------------------------------------
@@ -21,22 +53,5 @@ module.exports = {
2153
schema: []
2254
},
2355

24-
create (context) {
25-
return utils.executeOnVue(context, obj => {
26-
const node = obj.properties.find(item => item.type === 'Property' &&
27-
utils.getStaticPropertyName(item) === 'render' &&
28-
(item.value.type === 'ArrowFunctionExpression' || item.value.type === 'FunctionExpression') &&
29-
!item.value.expression // render: () => test
30-
)
31-
if (node) {
32-
const body = node.value.body
33-
if (body.type === 'BlockStatement' && !body.body.find(item => item.type === 'ReturnStatement')) {
34-
context.report({
35-
node: node.key,
36-
message: 'Expected to return a value in render function.'
37-
})
38-
}
39-
}
40-
})
41-
}
56+
create
4257
}

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

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,69 +10,26 @@ function create (context) {
1010
const options = context.options[0] || {}
1111
const treatUndefinedAsUnspecified = !(options.treatUndefinedAsUnspecified === false)
1212

13-
let funcInfo = {
14-
funcInfo: null,
15-
codePath: null,
16-
hasReturn: false,
17-
hasReturnValue: false,
18-
node: null
19-
}
2013
const forbiddenNodes = []
2114

22-
// ----------------------------------------------------------------------
23-
// Helpers
24-
// ----------------------------------------------------------------------
25-
function isValidReturn () {
26-
if (!funcInfo.hasReturn) {
27-
return false
28-
}
29-
return !treatUndefinedAsUnspecified || funcInfo.hasReturnValue
30-
}
31-
3215
// ----------------------------------------------------------------------
3316
// Public
3417
// ----------------------------------------------------------------------
3518

3619
return Object.assign({},
37-
{
38-
onCodePathStart (codePath, node) {
39-
funcInfo = {
40-
codePath,
41-
funcInfo: funcInfo,
42-
hasReturn: false,
43-
hasReturnValue: false,
44-
node
45-
}
46-
},
47-
onCodePathEnd () {
48-
funcInfo = funcInfo.funcInfo
49-
},
50-
ReturnStatement (node) {
51-
funcInfo.hasReturn = true
52-
funcInfo.hasReturnValue = Boolean(node.argument)
53-
},
54-
'FunctionExpression:exit' (node) {
55-
if (!isValidReturn()) {
56-
forbiddenNodes.push({
57-
hasReturn: funcInfo.hasReturn,
58-
node: funcInfo.node,
59-
type: 'return'
60-
})
61-
}
62-
}
63-
},
20+
utils.findMissingReturns(forbiddenNodes, treatUndefinedAsUnspecified),
6421
utils.executeOnVue(context, properties => {
6522
const computedProperties = utils.getComputedProperties(properties)
6623

6724
computedProperties.forEach(cp => {
6825
forbiddenNodes.forEach(el => {
6926
if (
7027
cp.value &&
71-
el.node.loc.start.line >= cp.value.loc.start.line &&
72-
el.node.loc.end.line <= cp.value.loc.end.line
28+
el.loc.start.line >= cp.value.loc.start.line &&
29+
el.loc.end.line <= cp.value.loc.end.line
7330
) {
7431
context.report({
75-
node: el.node,
32+
node: el,
7633
message: 'Expected to return a value in "{{name}}" computed property.',
7734
data: {
7835
name: cp.key

lib/utils/index.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,5 +439,56 @@ module.exports = {
439439
cb(node.arguments.slice(-1)[0])
440440
}
441441
}
442+
},
443+
444+
/**
445+
* Find all function witch do not allways return values
446+
* @param {Array} nodes an array nodes
447+
* @param {boolean} treatUndefinedAsUnspecified
448+
*/
449+
findMissingReturns (nodes, treatUndefinedAsUnspecified) {
450+
let funcInfo = {
451+
funcInfo: null,
452+
codePath: null,
453+
hasReturn: false,
454+
hasReturnValue: false,
455+
node: null
456+
}
457+
458+
function isValidReturn () {
459+
if (!funcInfo.hasReturn) {
460+
return false
461+
}
462+
return !treatUndefinedAsUnspecified || funcInfo.hasReturnValue
463+
}
464+
465+
return {
466+
onCodePathStart (codePath, node) {
467+
funcInfo = {
468+
codePath,
469+
funcInfo: funcInfo,
470+
hasReturn: false,
471+
hasReturnValue: false,
472+
node
473+
}
474+
},
475+
onCodePathEnd () {
476+
funcInfo = funcInfo.funcInfo
477+
},
478+
ReturnStatement (node) {
479+
funcInfo.hasReturn = true
480+
funcInfo.hasReturnValue = Boolean(node.argument)
481+
},
482+
'ArrowFunctionExpression:exit' (node) {
483+
if (!isValidReturn()) {
484+
nodes.push(funcInfo.node)
485+
}
486+
},
487+
'FunctionExpression:exit' (node) {
488+
if (!isValidReturn()) {
489+
nodes.push(funcInfo.node)
490+
}
491+
}
492+
}
442493
}
443494
}

tests/lib/rules/require-render-return.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,19 @@ ruleTester.run('require-render-return', rule, {
7575
render: () => null
7676
}`,
7777
parserOptions
78+
},
79+
{
80+
filename: 'test.vue',
81+
code: `export default {
82+
render() {
83+
if (a) {
84+
return \`<div>a</div>\`
85+
} else {
86+
return \`<span>a</span>\`
87+
}
88+
}
89+
}`,
90+
parserOptions
7891
}
7992
],
8093

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ ruleTester.run('return-in-computed-property', rule, {
3838
get () {
3939
return true
4040
}
41+
},
42+
bar4 () {
43+
if (foo) {
44+
return true
45+
} else {
46+
return false
47+
}
4148
}
4249
}
4350
}

0 commit comments

Comments
 (0)