Skip to content
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

New Linter Rule - set() must return a value #247

Closed
caseywatts opened this issue Mar 26, 2018 · 3 comments
Closed

New Linter Rule - set() must return a value #247

caseywatts opened this issue Mar 26, 2018 · 3 comments
Labels
enhancement New Rule Idea for a new lint rule

Comments

@caseywatts
Copy link
Contributor

caseywatts commented Mar 26, 2018

having a linter rule for this would help prevent some bugs
(at least until the behavior changes to be what people expect)
emberjs/rfcs#79

Bad:

height: 100,
goldenRatioWidth: computed('height', {
  get() {
    return this.get('height') * 1.618;
  },
  set(_, value) {
    this.set('height', value / 1.618);
  }
})

Good

height: 100,
goldenRatioWidth: computed('height', {
  get() {
    return this.get('height') * 1.618;
  },
  set(_, value) {
    const newHeight = value / 1.618;
    this.set('height', newHeight);
    return newHeight;
  }
})

set() must have a return.

(I mistakenly created this over here first)

@caseywatts
Copy link
Contributor Author

I just glanced at some of the other rules, and I'm not sure how to get started implementing this lol

@clcuevas I believe you've done some things around return statements in the past (based on my codebase spelunking) - would you have any ideas or suggestions?

@Turbo87
Copy link
Member

Turbo87 commented Mar 26, 2018

@caseywatts my naive idea would be this:

In the AST look for a CallExpression node with an Identifier of computed. If the last argument is a ObjectExpression look for a Property with an Identifier of set and check if the value is a FunctionExpression. Now iterate over the body contents and check for any ReturnStatement nodes and if you can't find any you can report a lint failure.

You can use https://astexplorer.net/ to view the AST corresponding to any JS code you put in.

@clcuevas
Copy link
Contributor

@caseywatts Pretty much what @Turbo87 summarized. Wish I knew of astexplorer in my previous PRs. It would have helped a lot!

Good luck!

gmurphey pushed a commit to gmurphey/eslint-plugin-ember that referenced this issue Jun 11, 2018
gmurphey pushed a commit to gmurphey/eslint-plugin-ember that referenced this issue Jun 12, 2018
gmurphey pushed a commit to gmurphey/eslint-plugin-ember that referenced this issue Jun 12, 2018
gmurphey pushed a commit to gmurphey/eslint-plugin-ember that referenced this issue Jun 12, 2018
gmurphey pushed a commit to gmurphey/eslint-plugin-ember that referenced this issue Jul 9, 2018
gmurphey pushed a commit to gmurphey/eslint-plugin-ember that referenced this issue Jul 9, 2018
gmurphey pushed a commit to gmurphey/eslint-plugin-ember that referenced this issue Nov 12, 2018
gmurphey pushed a commit to gmurphey/eslint-plugin-ember that referenced this issue Nov 12, 2018
gmurphey pushed a commit to gmurphey/eslint-plugin-ember that referenced this issue Nov 12, 2018
gmurphey pushed a commit to gmurphey/eslint-plugin-ember that referenced this issue Nov 13, 2018
gmurphey pushed a commit to gmurphey/eslint-plugin-ember that referenced this issue Nov 13, 2018
gmurphey pushed a commit to gmurphey/eslint-plugin-ember that referenced this issue Mar 16, 2019
bmish pushed a commit that referenced this issue Mar 16, 2019
* Add new 'require-return-from-computed' rule. Fixes #247.
@bmish bmish added the New Rule Idea for a new lint rule label May 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New Rule Idea for a new lint rule
Projects
None yet
Development

No branches or pull requests

4 participants