question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New rule proposal: no-setter-return

See original GitHub issue

Please describe what the rule should do:

no-setter-return disallows returning values from setter functions.

Reports return statement with an argument inside a setter. Targets setters in object literals, classes and property descriptors (4 well-known functions).

What category of rule is this? (place an “X” next to just one item)

[X] Warns about a potential error (problem)

Provide 2-3 code examples that this rule will warn about:

const foo = {
    set a(val) { 
        this._a = val; 
        return val; 
    }
}

class foo {
    set a(val) { 
        this._a = val * 100; 
        return this._a; 
    }
    static set b(val) {
        doSomething(val);
        return true;
    }
}

Object.defineProperty(obj, "foo", { 
    set(val) { 
        return 5 * val; 
    }
});

// also Object.defineProperties, Object.create, Reflect.defineProperty

Why should this rule be included in ESLint (instead of a plugin)?

Returning a value from a setter is either unnecessary or a possible error in logic (intention to somehow use the returned value when it’s different).

As far as I know, even if there is an intention to use the value returned from a setter, it’s simply impossible in ES. E.g., a.prop = foo, a.prop += foo, ++a.prop etc. do not use values returned from setters to evaluate the whole expression.

I think that just return; without value should be allowed, as it can be used for control flow. Returning any value should be disallowed.

Are you willing to submit a pull request to implement this rule?

Yes.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
ilyavolodincommented, Sep 25, 2019

@mdjermanovic Second example is also using return, it’s just shortcut syntax, but it’s still a return. So I think the name is fine.

1reaction
mdjermanoviccommented, Sep 29, 2019

I’m working on this, should be ready soon.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SEC Proposed Rules
SEC Proposed Rules · Regulation NMS: Minimum Pricing Increments, Access Fees, and Transparency of Better Priced Orders · File No: S7-30-22 · Comments...
Read more >
Proposed Rules - GovInfo
This proposed rule, if finalized, would amend parts 50 and 56 of FDA's regulations.
Read more >
SEC Proposes Equity Market Overhaul and Best Execution Rule
The SEC is proposing a new Rule 615 under Regulation NMS that would seek to enhance competition for the execution of marketable orders...
Read more >
Streamlining the Medicaid, Children's Health Insurance ...
This proposed rule would remove barriers and facilitate enrollment of new applicants, particularly those dually eligible for Medicare and ...
Read more >
proposed revisions to - Supreme Court
first be admitted to practice before this Court as provided in Rule 5, ... This new language clarifies that documents must be transmitted...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found