Skip to content

Commit 3db7b13

Browse files
armano2michalsnik
authored andcommitted
Fix issue return in computed properties & render to match only corret nodes (#131)
1 parent a3d64c7 commit 3db7b13

File tree

5 files changed

+63
-19
lines changed

5 files changed

+63
-19
lines changed

lib/rules/require-render-return.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,12 @@ function create (context) {
2020
utils.executeOnVue(context, obj => {
2121
const node = obj.properties.find(item => item.type === 'Property' &&
2222
utils.getStaticPropertyName(item) === 'render' &&
23-
(item.value.type === 'ArrowFunctionExpression' || item.value.type === 'FunctionExpression') &&
24-
!item.value.expression // render: () => test
23+
(item.value.type === 'ArrowFunctionExpression' || item.value.type === 'FunctionExpression')
2524
)
2625
if (!node) return
2726

2827
forbiddenNodes.forEach(el => {
29-
if (
30-
el.loc.start.line >= node.value.loc.start.line &&
31-
el.loc.end.line <= node.value.loc.end.line
32-
) {
28+
if (node.value === el) {
3329
context.report({
3430
node: node.key,
3531
message: 'Expected to return a value in render function.'

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

+1-5
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ function create (context) {
2525

2626
computedProperties.forEach(cp => {
2727
forbiddenNodes.forEach(el => {
28-
if (
29-
cp.value &&
30-
el.loc.start.line >= cp.value.loc.start.line &&
31-
el.loc.end.line <= cp.value.loc.end.line
32-
) {
28+
if (cp.value && cp.value.parent === el) {
3329
context.report({
3430
node: el,
3531
message: 'Expected to return a value in "{{name}}" computed property.',

lib/utils/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ module.exports = {
553553
funcInfo.hasReturnValue = Boolean(node.argument)
554554
},
555555
'ArrowFunctionExpression:exit' (node) {
556-
if (!isValidReturn()) {
556+
if (!isValidReturn() && !node.expression) {
557557
cb(funcInfo.node)
558558
}
559559
},

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

+13
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,19 @@ ruleTester.run('require-render-return', rule, {
8888
}
8989
}`,
9090
parserOptions
91+
},
92+
{
93+
filename: 'test.vue',
94+
code: `export default {
95+
render(h) {
96+
const options = []
97+
this.matches.forEach(function (match) {
98+
options.push(match)
99+
})
100+
return h('div', options)
101+
}
102+
}`,
103+
parserOptions
91104
}
92105
],
93106

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

+46-7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ const rule = require('../../../lib/rules/return-in-computed-property')
1212

1313
const RuleTester = require('eslint').RuleTester
1414

15+
const parserOptions = {
16+
ecmaVersion: 6,
17+
sourceType: 'module'
18+
}
19+
1520
// ------------------------------------------------------------------------------
1621
// Tests
1722
// ------------------------------------------------------------------------------
@@ -49,7 +54,41 @@ ruleTester.run('return-in-computed-property', rule, {
4954
}
5055
}
5156
`,
52-
parserOptions: { ecmaVersion: 8, sourceType: 'module' }
57+
parserOptions
58+
},
59+
{
60+
filename: 'test.vue',
61+
code: `
62+
export default {
63+
computed: {
64+
foo () {
65+
const options = []
66+
this.matches.forEach((match) => {
67+
options.push(match)
68+
})
69+
return options
70+
}
71+
}
72+
}
73+
`,
74+
parserOptions
75+
},
76+
{
77+
filename: 'test.vue',
78+
code: `
79+
export default {
80+
computed: {
81+
foo () {
82+
const options = []
83+
this.matches.forEach(function (match) {
84+
options.push(match)
85+
})
86+
return options
87+
}
88+
}
89+
}
90+
`,
91+
parserOptions
5392
},
5493
{
5594
filename: 'test.vue',
@@ -64,7 +103,7 @@ ruleTester.run('return-in-computed-property', rule, {
64103
}
65104
}
66105
`,
67-
parserOptions: { ecmaVersion: 8, sourceType: 'module' },
106+
parserOptions,
68107
options: [{ treatUndefinedAsUnspecified: false }]
69108
}
70109
],
@@ -80,7 +119,7 @@ ruleTester.run('return-in-computed-property', rule, {
80119
}
81120
}
82121
`,
83-
parserOptions: { ecmaVersion: 8, sourceType: 'module' },
122+
parserOptions,
84123
errors: [{
85124
message: 'Expected to return a value in "foo" computed property.',
86125
line: 4
@@ -135,7 +174,7 @@ ruleTester.run('return-in-computed-property', rule, {
135174
}
136175
}
137176
`,
138-
parserOptions: { ecmaVersion: 8, sourceType: 'module' },
177+
parserOptions,
139178
errors: [{
140179
message: 'Expected to return a value in "foo" computed property.',
141180
line: 7
@@ -155,7 +194,7 @@ ruleTester.run('return-in-computed-property', rule, {
155194
}
156195
}
157196
`,
158-
parserOptions: { ecmaVersion: 8, sourceType: 'module' },
197+
parserOptions,
159198
errors: [{
160199
message: 'Expected to return a value in "foo" computed property.',
161200
line: 4
@@ -174,7 +213,7 @@ ruleTester.run('return-in-computed-property', rule, {
174213
}
175214
}
176215
`,
177-
parserOptions: { ecmaVersion: 8, sourceType: 'module' },
216+
parserOptions,
178217
options: [{ treatUndefinedAsUnspecified: false }],
179218
errors: [{
180219
message: 'Expected to return a value in "foo" computed property.',
@@ -192,7 +231,7 @@ ruleTester.run('return-in-computed-property', rule, {
192231
}
193232
}
194233
`,
195-
parserOptions: { ecmaVersion: 8, sourceType: 'module' },
234+
parserOptions,
196235
options: [{ treatUndefinedAsUnspecified: true }],
197236
errors: [{
198237
message: 'Expected to return a value in "foo" computed property.',

0 commit comments

Comments
 (0)