Skip to content

Add rule require-render-return. #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions docs/rules/require-render-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Enforces render function to always return value (require-render-return)

This rule aims to enforce render function to allways return value

## :book: Rule Details

:-1: Examples of **incorrect** code for this rule:

```js
export default {
render () {
}
}
```
```js
export default {
render (h) {
if (foo) {
return
}
}
}
```

:+1: Examples of **correct** code for this rule:

```js
export default {
render (h) {
return
}
}
```

## :wrench: Options

Nothing.
59 changes: 59 additions & 0 deletions lib/rules/require-render-return.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* @fileoverview Enforces render function to always return value.
* @author Armano
*/
'use strict'

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

function create (context) {
const forbiddenNodes = []

// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------

return Object.assign({},
utils.executeOnFunctionsWithoutReturn(true, node => {
forbiddenNodes.push(node)
}),
utils.executeOnVue(context, obj => {
const node = obj.properties.find(item => item.type === 'Property' &&
utils.getStaticPropertyName(item) === 'render' &&
(item.value.type === 'ArrowFunctionExpression' || item.value.type === 'FunctionExpression') &&
!item.value.expression // render: () => test
)
if (!node) return

forbiddenNodes.forEach(el => {
if (
el.loc.start.line >= node.value.loc.start.line &&
el.loc.end.line <= node.value.loc.end.line
) {
context.report({
node: node.key,
message: 'Expected to return a value in render function.'
})
}
})
})
)
}

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

module.exports = {
meta: {
docs: {
description: 'Enforces render function to always return value.',
category: 'Possible Errors',
recommended: false
},
fixable: null, // or "code" or "whitespace"
schema: []
},

create
}
53 changes: 6 additions & 47 deletions lib/rules/return-in-computed-property.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,69 +10,28 @@ function create (context) {
const options = context.options[0] || {}
const treatUndefinedAsUnspecified = !(options.treatUndefinedAsUnspecified === false)

let funcInfo = {
funcInfo: null,
codePath: null,
hasReturn: false,
hasReturnValue: false,
node: null
}
const forbiddenNodes = []

// ----------------------------------------------------------------------
// Helpers
// ----------------------------------------------------------------------
function isValidReturn () {
if (!funcInfo.hasReturn) {
return false
}
return !treatUndefinedAsUnspecified || funcInfo.hasReturnValue
}

// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------

return Object.assign({},
{
onCodePathStart (codePath, node) {
funcInfo = {
codePath,
funcInfo: funcInfo,
hasReturn: false,
hasReturnValue: false,
node
}
},
onCodePathEnd () {
funcInfo = funcInfo.funcInfo
},
ReturnStatement (node) {
funcInfo.hasReturn = true
funcInfo.hasReturnValue = Boolean(node.argument)
},
'FunctionExpression:exit' (node) {
if (!isValidReturn()) {
forbiddenNodes.push({
hasReturn: funcInfo.hasReturn,
node: funcInfo.node,
type: 'return'
})
}
}
},
utils.executeOnFunctionsWithoutReturn(treatUndefinedAsUnspecified, node => {
forbiddenNodes.push(node)
}),
utils.executeOnVue(context, properties => {
const computedProperties = utils.getComputedProperties(properties)

computedProperties.forEach(cp => {
forbiddenNodes.forEach(el => {
if (
cp.value &&
el.node.loc.start.line >= cp.value.loc.start.line &&
el.node.loc.end.line <= cp.value.loc.end.line
el.loc.start.line >= cp.value.loc.start.line &&
el.loc.end.line <= cp.value.loc.end.line
) {
context.report({
node: el.node,
node: el,
message: 'Expected to return a value in "{{name}}" computed property.',
data: {
name: cp.key
Expand Down
51 changes: 51 additions & 0 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,5 +520,56 @@ module.exports = {
}
}
}
},

/**
* Find all functions which do not always return values
* @param {boolean} treatUndefinedAsUnspecified
* @param {Function} cb Callback function
*/
executeOnFunctionsWithoutReturn (treatUndefinedAsUnspecified, cb) {
let funcInfo = {
funcInfo: null,
codePath: null,
hasReturn: false,
hasReturnValue: false,
node: null
}

function isValidReturn () {
if (!funcInfo.hasReturn) {
return false
}
return !treatUndefinedAsUnspecified || funcInfo.hasReturnValue
}

return {
onCodePathStart (codePath, node) {
funcInfo = {
codePath,
funcInfo: funcInfo,
hasReturn: false,
hasReturnValue: false,
node
}
},
onCodePathEnd () {
funcInfo = funcInfo.funcInfo
},
ReturnStatement (node) {
funcInfo.hasReturn = true
funcInfo.hasReturnValue = Boolean(node.argument)
},
'ArrowFunctionExpression:exit' (node) {
if (!isValidReturn()) {
cb(funcInfo.node)
}
},
'FunctionExpression:exit' (node) {
if (!isValidReturn()) {
cb(funcInfo.node)
}
}
}
}
}
124 changes: 124 additions & 0 deletions tests/lib/rules/require-render-return.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/**
* @fileoverview Enforces render function to always return value.
* @author Armano
*/
'use strict'

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

const rule = require('../../../lib/rules/require-render-return')
const RuleTester = require('eslint').RuleTester

const parserOptions = {
ecmaVersion: 6,
sourceType: 'module',
ecmaFeatures: { experimentalObjectRestSpread: true, jsx: true }
}

// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------

const ruleTester = new RuleTester()
ruleTester.run('require-render-return', rule, {
valid: [
{
code: `Vue.component('test', {
render() {
return {}
}
})`,
parserOptions
},
{
code: `Vue.component('test', {
foo() {
return {}
}
})`,
parserOptions
},
{
code: `Vue.component('test', {
foo: {}
})`,
parserOptions
},
{
code: `Vue.component('test', {
render: foo
})`,
parserOptions
},
{
code: `Vue.component('test', {
render() {
return <div></div>
}
})`,
parserOptions
},
{
filename: 'test.vue',
code: `export default {
render() {
return {}
}
}`,
parserOptions
},
{
filename: 'test.vue',
code: `export default {
render: () => null
}`,
parserOptions
},
{
filename: 'test.vue',
code: `export default {
render() {
if (a) {
return \`<div>a</div>\`
} else {
return \`<span>a</span>\`
}
}
}`,
parserOptions
}
],

invalid: [
{
filename: 'test.vue',
code: `export default {
render() {
}
}`,
parserOptions,
errors: [{
message: 'Expected to return a value in render function.',
type: 'Identifier',
line: 2
}]
},
{
code: `Vue.component('test', {
render: function () {
if (a) {
return
Copy link
Member

@michalsnik michalsnik Aug 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and what about:

render() {
  if (a) {
    return `<div>a</div>`
  } else {
    return `<span>a</span>`
  }
}

?

Copy link
Contributor Author

@armano2 armano2 Aug 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@michalsnik i forgot about this case, thank you 🍡

}
}
})`,
parserOptions,
errors: [{
message: 'Expected to return a value in render function.',
type: 'Identifier',
line: 2
}]
}
]
})
7 changes: 7 additions & 0 deletions tests/lib/rules/return-in-computed-property.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ ruleTester.run('return-in-computed-property', rule, {
get () {
return true
}
},
bar4 () {
if (foo) {
return true
} else {
return false
}
}
}
}
Expand Down