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

Conversation

armano2
Copy link
Contributor

@armano2 armano2 commented Jul 15, 2017

This PR implements rules proposed in #75

DONE:

  • Basic check for return
  • Check if all paths return value
  • Option: treatUndefinedAsUnspecified - disallows implicitly returning undefined with a return; statement.

@armano2 armano2 changed the title WIP: Patch 6 computed property return WIP: Computed property return Jul 15, 2017
@armano2 armano2 changed the title WIP: Computed property return WIP: Add computed-property-return rule. Jul 15, 2017
@armano2 armano2 changed the title WIP: Add computed-property-return rule. Add computed-property-return rule. Jul 16, 2017
@armano2 armano2 force-pushed the patch-6-computed-property-return branch from 86d85c6 to dbb36d8 Compare July 16, 2017 11:54
@armano2
Copy link
Contributor Author

armano2 commented Jul 16, 2017

@michalsnik can you review this pull request?

@michalsnik
Copy link
Member

Sure, later today I'll try to push my PR and both of yours forward. Busy time :)

@armano2
Copy link
Contributor Author

armano2 commented Jul 16, 2017

It's my first time working with rules from eslint (developing them) but they seems not to complex to make.

sorry for pushing you to test my code 🗡

Copy link
Member

@michalsnik michalsnik left a comment

Choose a reason for hiding this comment

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

I'll continue checking the rest once my PR that serves as the base for all of yours is merged so the diff is more obvious :)

@@ -0,0 +1,44 @@
# Enforces that a return statement is present in computed property (computed-property-return)
Copy link
Member

Choose a reason for hiding this comment

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

I'd call this rule return-in-computed-property to make it more descriptive. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i'm bad at naming stuff, your name is way better

- `"treatUndefinedAsUnspecified"`: `true` (default) allows implicitly returning undefined with a `return;` statement.

```
vue/order-in-components: [2, {
Copy link
Member

Choose a reason for hiding this comment

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

Whoops

## :wrench: Options

This rule has an object option:
- `"treatUndefinedAsUnspecified"`: `true` (default) allows implicitly returning undefined with a `return;` statement.
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 we should force to return something explicitly, thus in my opinion return; should also trigger an error and this option should not be necessary.
We could consider two types of messages:

  • There is no return statement inside foo computed property
  • There is an incomplete return statement inside foo computed property
    What do you think @armano2 @chrisvfritz ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

damn description is invalid, i forgot to change it, by default its treating undefined as unspecified than return; will thrown an error, but i prefer to leave a choice to disable it because undefined can be useful in some cases.

}
}
},
utils.executeOnVueInstance(context, properties => {
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't it be executeOnVueComponent ? And this one in computed-data rule instead?

Copy link
Contributor Author

@armano2 armano2 Jul 16, 2017

Choose a reason for hiding this comment

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

no right now there is 2 functions, executeOnVueInstance and executeOnVueComponent we have to distinct both of those types,

see: 281f230

Vue Component ~ Vue Instance

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, but I thought this matters for data as function case, as it's ok to have data as object in vue instance, but not in component. And this rule is about return statement in computed property which seems irrelevant.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

executeOnVueInstance supports components & new vue

i just renamed function to simplified merges and improve readability.

@armano2 armano2 changed the title Add computed-property-return rule. Add return-in-computed-property rule. Jul 16, 2017
@armano2 armano2 force-pushed the patch-6-computed-property-return branch from 12eb0f1 to 253a86e Compare July 19, 2017 16:06
@armano2
Copy link
Contributor Author

armano2 commented Jul 19, 2017

@michalsnik cleaned up and merged

@chrisvfritz
Copy link
Contributor

Will this also flag computed properties that technically have a return somewhere in their content, but not directly? For example:

computed: {
  foo: function () {
    function bar () {
      return this.baz * 2 
    }
    bar()
  }
}

@armano2
Copy link
Contributor Author

armano2 commented Jul 19, 2017

@chrisvfritz yes (test for this case added)

i think i should make distinction in messages based on hasReturn this flag is set when there is ReturnStatement in function but not all path return values

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

Copy link
Member

@michalsnik michalsnik left a comment

Choose a reason for hiding this comment

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

Great work @armano2 !

@michalsnik michalsnik merged commit b7e6ff1 into vuejs:master Jul 21, 2017
@armano2 armano2 deleted the patch-6-computed-property-return branch July 22, 2017 12:12
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.

4 participants