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

missing rule: require line break after variable assignment #1622

Closed
remitbri opened this issue Jan 4, 2015 · 22 comments

Comments

Projects
None yet
10 participants
@remitbri
Copy link
Contributor

commented Jan 4, 2015

Hi. I've tried to review each and every rule of JSHint, JSCS and ESLint to see if I could entirely "get rid of" the first two in favour of the latter one. As suggested by the owner of the @geteslint twitter account, I'm reporting the JSCS rules I've found actually not covered by ESLint. My apologies if it's a duplicate.

  • "requireLineBreakAfterVariableAssignment"

E.g this is invalid

var abc = 8; var foo = 5;

var a, b, c,
    foo = 7, bar = 8;
@doberkofler

This comment has been minimized.

Copy link
Contributor

commented Jan 23, 2015

+1

@lo1tuma

This comment has been minimized.

Copy link
Member

commented May 13, 2015

Should this only apply to assignments within a variable declaration?

So this would be valid:

var a; var b; var c;

var a; var b = 42;

var a, b = 42,
    c = 42;

But this would be invalid:

var a; var b = 42; var c;

var a, b = 42, c;

Shouldn’t there be an option to enfore linebreaks after each delaration no matter if they have a value assigned or not?

BTW: I think the existing rule newline-after-var could be easily confused with this rule.

@nzakas

This comment has been minimized.

Copy link
Member

commented May 14, 2015

Maybe just make this an option for newline-after-var?

@gyandeeps

This comment has been minimized.

Copy link
Member

commented May 23, 2015

My 2 cents here:
I think this should be an option in newline-after-var rule.
I think this rule should apply to variable declarations no matter if they have a value assigned or not.

@mathieumg

This comment has been minimized.

Copy link
Contributor

commented May 23, 2015

I think this should follow the JSCS rule (as per the original post), i.e. if there is an assignation it must be alone on the line, otherwise multiple declarations on the same line are fine. I'm speaking regarding a single var statement, I think there should never be more than one per line in any case.

Valid:

var a, b, c,
    d = 42;
var a = 42,
    b, c, d;

Invalid:

var a, b, c = 41,
    d = 42;
var a = 42, b = 40, c,
    d = 41;
@gyandeeps

This comment has been minimized.

Copy link
Member

commented May 23, 2015

But we need to some how support functionality to disallow multiple var on same line to. Someone (+ me) is going to ask for it in the future.

invalid

var a = 1; var b = 2;

valid

var a = 1; 
var b = 2;
@robcolburn

This comment has been minimized.

@gyandeeps

This comment has been minimized.

Copy link
Member

commented Sep 22, 2015

In my plugin I have created a rule which checks for vars on new line without any exceptions to it.
https://github.com/gyandeeps/eslint-plugin-ideal

@alberto

This comment has been minimized.

Copy link
Member

commented Jan 9, 2016

I think this should be a different rule, since it deals with linebreaks between declarators (or assignments), while newline-after-var deals with padding after declaration. Ideally newline-after-var should be renamed to padding-after-var

@nzakas

This comment has been minimized.

Copy link
Member

commented Jan 9, 2016

You're probably right about a separate rule. Do you have a name suggestion?

@alberto

This comment has been minimized.

Copy link
Member

commented Jan 9, 2016

If we just want to enforce it for assignments, I would say linebreak-after-assignment or newline-after-assignment

If we want to optionally also enforce it for declarators, like

var a,
    b;

I'm thinking of linebreak-after-declaration or newline-after-declaration, with an options all and assignments (which would behave like the one above).

@nzakas

This comment has been minimized.

Copy link
Member

commented Jan 11, 2016

I think we've hit our max of breaking changes for 2.0.0, so we need to keep the current rule.

I think newline-after-assignment doesn't indicate this is just for variable declaration initialization, so maybe newline-after-var-init?

@alberto

This comment has been minimized.

Copy link
Member

commented Jan 11, 2016

Wouldn't this rule then include declarations without initialization, like in my last comment?

@nzakas

This comment has been minimized.

Copy link
Member

commented Jan 11, 2016

If there were not initializations, then the rule would do nothing (unless I'm misunderstanding the request).

@alberto

This comment has been minimized.

Copy link
Member

commented Jan 12, 2016

What I was thinking of this two options (names open for bikeshedding):

"all"

This would be valid:

var a, 
    b,
    c,
    d = 42;

var a = 42,
    b;

This would be invalid:

var a, b;
var a, b = 40;
var a, b,
    d = 42;

"assignments"

This would be valid:

var a, b, c, 
    d = 42;

var a, 
   b = 40,
   c = 41;

This would be invalid:

var a, b, c = 41;

var a, b, 
    c = 41, d = 42;

@alberto alberto self-assigned this Jan 13, 2016

@nzakas

This comment has been minimized.

Copy link
Member

commented Jan 13, 2016

I like it! Id probably call it "initializations" instead of "assignments", but otherwise, 👍

@alberto

This comment has been minimized.

Copy link
Member

commented Jan 13, 2016

Just to be sure:

Should the part regarding the validness of multiple var statements be left to newline-after-var? (Or even a new rule about multiple statements per line)

var a; var b; var c;

@alberto alberto added feature and removed enhancement labels Jan 13, 2016

@nzakas

This comment has been minimized.

Copy link
Member

commented Jan 13, 2016

Yes, leave that to newline-after-var.

@alberto

This comment has been minimized.

Copy link
Member

commented Jan 14, 2016

Since newlines must come not only after "initializations" but also before, I think we need a better name.
Some suggestions:

newline-per-var-declaration
no-multi-var-declaration-per-line
one-var-declaration-per-line
var-declaration-style

Depending on the name, maybe the option names all and initializations will need adjustment too.

I'm open for suggestions. :)

@ilyavolodin

This comment has been minimized.

Copy link
Member

commented Jan 15, 2016

var-declaration-style sounds OK to me.

@nzakas

This comment has been minimized.

Copy link
Member

commented Jan 15, 2016

I'd go for one- var-declaration-per-line. The word "style" seems too ambiguous.

alberto added a commit that referenced this issue Jan 20, 2016

@nzakas nzakas closed this in 79e8a0b Jan 24, 2016

nzakas added a commit that referenced this issue Jan 24, 2016

Merge pull request #5016 from eslint/issue1622
New: `one-var-declaration-per-line` rule (fixes #1622)

@eslint eslint bot locked and limited conversation to collaborators Feb 7, 2018

@eslint eslint bot added the archived due to age label Feb 7, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.