Skip to content

Add return-in-computed-property rule. #78

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 3 commits into from
Jul 21, 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
44 changes: 44 additions & 0 deletions docs/rules/return-in-computed-property.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Enforces that a return statement is present in computed property (return-in-computed-property)

## :book: Rule Details

This rule enforces that a `return` statement is present in `computed` properties.

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

```js
export default {
computed: {
foo () {
},
bar: function () {
}
}
}
```

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

```js
export default {
computed: {
foo () {
return true
},
bar: function () {
return false
}
}
}
```

## :wrench: Options

This rule has an object option:
- `"treatUndefinedAsUnspecified"`: `true` (default) disallows implicitly returning undefined with a `return;` statement.

```
vue/return-in-computed-property: [2, {
treatUndefinedAsUnspecified: true
}]
```
114 changes: 114 additions & 0 deletions lib/rules/return-in-computed-property.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* @fileoverview Enforces that a return statement is present in computed property (return-in-computed-property)
* @author Armano
*/
'use strict'

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

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.executeOnVueComponent(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
) {
context.report({
node: el.node,
message: 'Expected to return a value in "{{name}}" computed property.',
data: {
name: cp.key
}
})
}
})
})
})
)
}

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

module.exports = {
meta: {
docs: {
description: 'Enforces that a return statement is present in computed property.',
category: 'Possible Errors',
recommended: false
},
fixable: null, // or "code" or "whitespace"
schema: [
{
type: 'object',
properties: {
treatUndefinedAsUnspecified: {
type: 'boolean'
}
},
additionalProperties: false
}
]
},

create
}
196 changes: 196 additions & 0 deletions tests/lib/rules/return-in-computed-property.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
/**
* @fileoverview Enforces that a return statement is present in computed property (return-in-computed-property)
* @author Armano
*/
'use strict'

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

const rule = require('../../../lib/rules/return-in-computed-property')

const RuleTester = require('eslint').RuleTester

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

const ruleTester = new RuleTester()
ruleTester.run('return-in-computed-property', rule, {

valid: [
{
filename: 'test.vue',
code: `
export default {
computed: {
foo () {
return true
},
bar: function () {
return false
},
bar3: {
set () {
return true
},
get () {
return true
}
}
}
}
`,
parserOptions: { ecmaVersion: 8, sourceType: 'module' }
},
{
filename: 'test.vue',
code: `
export default {
computed: {
foo: {
get () {
return
}
}
}
}
`,
parserOptions: { ecmaVersion: 8, sourceType: 'module' },
options: [{ treatUndefinedAsUnspecified: false }]
}
],

invalid: [
{
filename: 'test.vue',
code: `
export default {
computed: {
foo () {
}
}
}
`,
parserOptions: { ecmaVersion: 8, sourceType: 'module' },
errors: [{
message: 'Expected to return a value in "foo" computed property.',
line: 4
}]
},
{
filename: 'test.vue',
code: `
export default {
computed: {
foo: function () {
}
}
}
`,
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
errors: [{
message: 'Expected to return a value in "foo" computed property.',
line: 4
}]
},
{
filename: 'test.vue',
code: `
export default {
computed: {
foo: function () {
if (a) {
return
}
}
}
}
`,
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
errors: [{
message: 'Expected to return a value in "foo" computed property.',
line: 4
}]
},
{
filename: 'test.vue',
code: `
export default {
computed: {
foo: {
set () {
},
get () {
}
}
}
}
`,
parserOptions: { ecmaVersion: 8, sourceType: 'module' },
errors: [{
message: 'Expected to return a value in "foo" computed property.',
line: 7
}]
},
{
filename: 'test.vue',
code: `
export default {
computed: {
foo: function () {
function bar () {
return this.baz * 2
}
bar()
}
}
}
`,
parserOptions: { ecmaVersion: 8, sourceType: 'module' },
errors: [{
message: 'Expected to return a value in "foo" computed property.',
line: 4
}]
},
{
filename: 'test.vue',
code: `
export default {
computed: {
foo () {
},
bar () {
return
}
}
}
`,
parserOptions: { ecmaVersion: 8, sourceType: 'module' },
options: [{ treatUndefinedAsUnspecified: false }],
errors: [{
message: 'Expected to return a value in "foo" computed property.',
line: 4
}]
},
{
filename: 'test.vue',
code: `
export default {
computed: {
foo () {
return
}
}
}
`,
parserOptions: { ecmaVersion: 8, sourceType: 'module' },
options: [{ treatUndefinedAsUnspecified: true }],
errors: [{
message: 'Expected to return a value in "foo" computed property.',
line: 4
}]
}
Copy link
Member

Choose a reason for hiding this comment

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

Could you add invalid tests for treatUndefinedAsUnspecified option?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mysticatea added

]
})