Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
jesusgn90 wants to merge 6 commits into vuejs:master from jesusgn90:no-early-return
Closed

Conversation

Copy link
Contributor

@jesusgn90 jesusgn90 commented May 13, 2022

fixes #1884

Copy link
Member

@FloEdelmann FloEdelmann left a comment

Choose a reason for hiding this comment

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

Wow, that was fast, thanks! I have a few remarks.

Copy link
Contributor Author

@FloEdelmann thanks for your review, I think I've addressed your comments. There is one point however, the one related to Nuxt that I'm not super sure so I'd prefer to leave the rule as it is for now. Perhaps, it's worth opening a new issue specific for that so others can comment on making this rule cover that for Nuxt or not. For me, even covering the data part was an extra, the original purpose was to cover just extra returns on setup blocks 😄

Copy link
Member

The logic for asyncData would be exactly the same as for data.

Copy link
Contributor Author

The logic for asyncData would be exactly the same as for data.

@FloEdelmann ok, if they work the same, I just pushed a small change to cover it (and in a way that if we need to add more properties in the future, it's easily extendable)

meta: {
type: 'problem',
docs: {
description: 'disallow early `returns` in `setup` and `data` functions',
Copy link
Member

@FloEdelmann FloEdelmann May 13, 2022

Choose a reason for hiding this comment

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

Suggested change
description: 'disallow early `returns` in `setup` and `data` functions',
description: 'disallow early `return` in `setup` and `data` functions',

@@ -0,0 +1,79 @@
/**
* @author *****your name*****
Copy link
Member

@FloEdelmann FloEdelmann May 13, 2022

Choose a reason for hiding this comment

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

Please fill in your name or username here :)

context.report({
node,
messageId: 'extraReturnStatement',
data: { functionType: 'data' }
Copy link
Member

@FloEdelmann FloEdelmann May 13, 2022

Choose a reason for hiding this comment

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

It should mention asyncData or data respectively.

Suggested change
data: { functionType: 'data' }
data: { functionType: property }


/** @param {RuleContext} context */
create(context) {
const returnStatementLocations = []
Copy link
Member

@FloEdelmann FloEdelmann May 13, 2022

Choose a reason for hiding this comment

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

Suggested change
constreturnStatementLocations=[]
/** @type {SourceLocation[]} */
const returnStatementLocations = []

}
]
},
{
Copy link
Member

@FloEdelmann FloEdelmann May 13, 2022
edited
Loading

Choose a reason for hiding this comment

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

Please add this test case. I'm afraid it will produce too many errors 🙁

Suggested change
{
{
filename: 'test.vue',
code: `
<script>
export default {
setup: () => {
if (maybe) {
return { foo: false }
}
return { foo: true }
},
data() {
return {
foo: true
}
},
computed: {
foo() {
return true
}
}
}
</script>
`,
errors: [
{
messageId: 'extraReturnStatement',
data: { functionType: 'setup' },
line: 3
}
]
},
{

Comment on lines +132 to +147
{
filename: 'test.vue',
code: `
<script>
export default {
setup () {
const foo = computed(() => {
if (s) {
return
}
})
}
}
</script>
`
}
Copy link
Member

@FloEdelmann FloEdelmann May 13, 2022

Choose a reason for hiding this comment

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

Unfortunately, this test case fails although it should pass:

https://github.com/vuejs/eslint-plugin-vue/runs/6426396807?check_suite_focus=true#step:5:187

returnStatementLocations.push(node.loc)
},

onSetupFunctionExit() {
Copy link
Member

@FloEdelmann FloEdelmann May 13, 2022

Choose a reason for hiding this comment

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

Can't the logic for data/asyncData and for setup be shared? I think they should work/behave exactly the same.

{
messageId: 'extraReturnStatement',
data: { functionType: 'data' },
line: 3
Copy link
Member

@ota-meshi ota-meshi May 14, 2022

Choose a reason for hiding this comment

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

I think the report should point to line 6.

</script>
`
}
],
Copy link
Member

@ota-meshi ota-meshi May 14, 2022

Choose a reason for hiding this comment

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

I found a false positive in the next test source.

<script>
export default {
 methods: {
 fn () {
 if (a) {
 return {}; // <- false positive
 }
 }
 },
 setup () {
 const foo = ref()
 return { foo }
 }
}
</script>

I think we need to check the scope of ReturnStatement correctly.
The implementation of the return-in-emits-validator rule may be helpful.
https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/return-in-emits-validator.js

Copy link
Member

@ota-meshi ota-meshi May 14, 2022

Choose a reason for hiding this comment

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

Copy link
Contributor Author

I'll resume this with more time

@jesusgn90 jesusgn90 deleted the no-early-return branch June 13, 2022 14:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers

@ota-meshi ota-meshi ota-meshi requested changes

@FloEdelmann FloEdelmann FloEdelmann requested changes

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Rule proposal: Allow only one return inside of the setup

AltStyle によって変換されたページ (->オリジナル) /