Skip to content

New rule proposal: no-setter-return #12285

@mdjermanovic

Description

@mdjermanovic

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.

Metadata

Metadata

Assignees

Labels

acceptedThere is consensus among the team that this change meets the criteria for inclusionarchived due to ageThis issue has been archived; please open a new issue for any further discussionfeatureThis change adds a new feature to ESLintruleRelates to ESLint's core rules

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions