-
-
Notifications
You must be signed in to change notification settings - Fork 679
Add rule vue/no-early-return #1889
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
Closed
Closed
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6afb16b
feat: add no-early-return base rule
jesusgn90 7929a2c
lint: no-early-return files
jesusgn90 85b54cb
chore: changes after running update for new rule
jesusgn90 7c05299
fix: minor corrections from code review
jesusgn90 156b817
feat: extend no-early-return rule to cover asyncData
jesusgn90 d63afa8
tests: found test that makes the rule to fail :(
jesusgn90 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/no-early-return | ||
description: disallow early returns in setup and data functions | ||
--- | ||
# vue/no-early-return | ||
|
||
> disallow early returns in setup and data functions | ||
|
||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge> | ||
|
||
## :book: Rule Details | ||
|
||
This rule helps to identify accidental `return` statements in the `setup` block of a Vue component, these are `return` statements that would exit the `setup` block skipping part of the block itself (not allowing to each the end of the block). | ||
|
||
On the other hand, the rule checks the `data` block too, applying the same logic. | ||
|
||
<eslint-code-block :rules="{'vue/no-early-return': ['error']}"> | ||
|
||
```vue | ||
<!-- ✓ GOOD --> | ||
<script> | ||
export default { | ||
setup () { | ||
const foo = ref() | ||
return { foo } | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
<eslint-code-block :rules="{'vue/no-early-return': ['error']}"> | ||
|
||
```vue | ||
<!-- ✗ BAD --> | ||
<script> | ||
export default { | ||
setup () { | ||
const foo = ref() | ||
|
||
if (maybe) { | ||
return | ||
} | ||
|
||
for (const t of foo.value) { | ||
if (t) { | ||
return | ||
} | ||
} | ||
|
||
return { foo } | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
<eslint-code-block :rules="{'vue/no-early-return': ['error']}"> | ||
|
||
```vue | ||
<!-- ✓ GOOD --> | ||
<script> | ||
export default { | ||
data () { | ||
return { foo: true } | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
<eslint-code-block :rules="{'vue/no-early-return': ['error']}"> | ||
|
||
```vue | ||
<!-- ✗ BAD --> | ||
<script> | ||
export default { | ||
data () { | ||
if (maybe) { | ||
return { foo: false } | ||
} | ||
|
||
return { foo: true } | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-early-return.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-early-return.js) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* @author *****your name***** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please fill in your name or username here :) |
||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const utils = require('../utils') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'disallow early returns in setup and data functions', | ||
jesusgn90 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
categories: undefined, | ||
url: 'https://eslint.vuejs.org/rules/no-early-return.html' | ||
}, | ||
fixable: null, | ||
schema: [] | ||
}, | ||
|
||
/** @param {RuleContext} context */ | ||
create(context) { | ||
const rStatementLocs = [] | ||
jesusgn90 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return utils.compositingVisitors( | ||
jesusgn90 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
utils.defineVueVisitor(context, { | ||
ReturnStatement(node) { | ||
if (context.getScope().type === 'function') { | ||
return | ||
} | ||
rStatementLocs.push(node.loc) | ||
}, | ||
|
||
onSetupFunctionExit() { | ||
if (rStatementLocs.length === 0) { | ||
return | ||
} | ||
|
||
for (const loc of rStatementLocs) { | ||
context.report({ | ||
loc, | ||
message: 'Extra return statement in setup function.' | ||
jesusgn90 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
} | ||
}, | ||
onVueObjectEnter(node) { | ||
const dataProperty = utils.findProperty(node, 'data') | ||
jesusgn90 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ( | ||
!dataProperty || | ||
(dataProperty.value.type !== 'FunctionExpression' && | ||
dataProperty.value.type !== 'ArrowFunctionExpression') | ||
) { | ||
return | ||
} | ||
if ( | ||
context | ||
.getSourceCode() | ||
.ast.tokens.filter((t) => t.value === 'return').length > 1 | ||
) { | ||
context.report({ | ||
node, | ||
message: 'Extra return statement in data function.' | ||
jesusgn90 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
} | ||
} | ||
}) | ||
) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.