Skip to content

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
wants to merge 6 commits into from
Closed

Conversation

jesusgn90
Copy link
Contributor

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.

@jesusgn90
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 😄

@jesusgn90 jesusgn90 requested a review from FloEdelmann May 13, 2022 15:35
@FloEdelmann
Copy link
Member

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

@jesusgn90
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

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

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

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

Choose a reason for hiding this comment

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

Suggested change
const returnStatementLocations = []
/** @type {SourceLocation[]} */
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.

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

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

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

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

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

Choose a reason for hiding this comment

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

@jesusgn90
Copy link
Contributor Author

I'll resume this with more time

@jesusgn90 jesusgn90 closed this Jun 13, 2022
@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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rule proposal: Allow only one return inside of the setup
3 participants