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

Ternary line breaking #2528

Open
prettydiff opened this issue Jul 5, 2015 · 19 comments
Open

Ternary line breaking #2528

prettydiff opened this issue Jul 5, 2015 · 19 comments

Comments

@prettydiff
Copy link

var a = (tern)
    ? b
    : c;

This code produces the warning: Bad line breaking before '?'.

The jshint flow control is:

First catch the "?" token and then pass it to the expression function

  infix("?", function(left, that) {
    increaseComplexityCount();
    that.left = left;
    that.right = expression(10);
    advance(":");
    that["else"] = expression(10);
    return that;
  }, 30);

Function expression contains this logic near it's start

    var isDangerous =
      state.option.asi &&
      state.tokens.prev.line !== startLine(state.tokens.curr) &&
      _.contains(["]", ")"], state.tokens.prev.id) &&
      _.contains(["[", "("], state.tokens.curr.id);

    if (isDangerous)
      warning("W014", state.tokens.curr, state.tokens.curr.id);

It appears the JSHint code is incorrectly catching the line break between the ")" and the "?" in its ASI logic. ASI must not apply in this case. Breaking a ternary onto multiple lines makes the code easier to read. Example:

var a = (tern1 > 0)
        ? b
        : (tern2 > x)
            ? c
            : d,
    e = 1;
@erezfz
Copy link

erezfz commented Jul 5, 2015

why are you still using JSHint, and not only JSLint which is much better?

@caitp caitp added the P1 label Jul 5, 2015
@prettydiff
Copy link
Author

@Kaizer200 JSHint is far more frequently used tool. My goal is to attain conformance to the default settings of both as closely as I can. This way I can accurately represent any user wishes to conform to either tool with the fewest possible adjustments.

@nicolo-ribaudo
Copy link
Contributor

I think this is wanted, as there is this test:

code = [ 'var a = b ', '? c : d;' ];
TestRun(test)
  .addError(2, "Bad line breaking before '?'.")
  .test(code, { es3: true });

(tests/unit/options.js, line 1583)

@caitp
Copy link
Member

caitp commented Jul 5, 2015

The fact that there's a test doesn't mean it's wanted. This is more jscs's territory

@nicolo-ribaudo
Copy link
Contributor

@caitp Are you fixing this?

@lukeapage
Copy link
Member

This isn't disabled by laxbreak? I know its deprecated, but it won't be removed till v3

@caitp
Copy link
Member

caitp commented Jul 5, 2015

There's no real roadmap for v3, it's safe to get rid of this --- it won't break anyone's builds

@nicolo-ribaudo
Copy link
Contributor

@caitp For the same token, JSHint should also allow line breaks before &&, ||, ... by default.

@lukeapage
Copy link
Member

There is a roadmap, me and @jugglinmike have discussing it.its everything on the v3 milestone.

It will not break peoples builds, but it will stop warnings people rely on, when there is no warning and very little documentation that the option has been removed (and no major version bump to make people look at the changelist and notice they should use jscs)

@caitp
Copy link
Member

caitp commented Jul 5, 2015

Nobody is realistically relying on this, and a milestone isn't a roadmap. The v3 milestone has been around for ages, there's no deadline and no pressure. There is a change log for all releases, not just major ones, and other bogus warnings have been removed already. There is no compelling argument to keep this around indefinitely, it is jscs territory

@lukeapage
Copy link
Member

The v3 milestone has been around for ages

We recently cleared it up and made it into a shorter, achievable list.

There are a couple of PR's waiting for it.

There is a change log for all releases, not just major ones, and other bogus warnings have been removed already. There is no compelling argument to keep this around indefinitely, it is jscs territory

I agree, its just when. I just disagree with both classing do not warn about deprecated rules as being a breaking change, so not merging till v3 and at the same time, removing deprecated rules. If jshint wants to be ultra careful, be ultra careful. If it wants to remove rules, bump a major version or warn people.

Its pretty easy to miss that the options have become deprecated if you set jshint up some time ago - do we want the experience that people get is for them to raise a bug and then get told its deprecated? or make it obvious?

@lukeapage
Copy link
Member

Nobody is realistically relying on this

I used to rely on jshint for some kind of style checking until I found out about jscs and switched to it. I bet alot of people still are.

@nicolo-ribaudo
Copy link
Contributor

Nobody is realistically relying on this

Searching on GitHub... https://github.com/search?l=json&q=%22laxbreak%22%3A+false&ref=searchresults&type=Code&utf8=%E2%9C%93

@lukeapage
Copy link
Member

@prettydiff
this passes jshint

/* jshint laxbreak: true */
var a = (tern)
    ? b
    : c;

So, just turn that option on to not get warnings.

laxbreak will be removed in the near future. this is the issue raised for removing it - #1867

@lukeapage
Copy link
Member

any objection to closing this as a duplicate of #1867 ?

@caitp
Copy link
Member

caitp commented Jul 5, 2015

My vote is to make it a fresh bug and fix this properly. No major version bump it's required here

@prettydiff
Copy link
Author

@lukeapage If laxbreak is removed in v3.0 will this code sample generate warnings in v3.0 and what is the approximate timeline on a v3.0 release?

@lukeapage
Copy link
Member

If laxbreak is removed in v3.0 will this code sample generate warnings in v3.0

The plan is to remove the laxbreak option and no longer warn in cases of laxbreak.
The use of the option may generate a warning that the option is deprecated and can be removed.

what is the approximate timeline on a v3.0 release

I'm not sure, there is a que of pr's to review/finalise and get in which fix alot of bugs. I doubt it will happen in the next month.

Cait is a more long term contributor and other contributors/owners might decide to agree with her and remove the warnings in the next release, at which point you would stop seeing warnings and then in v3 you would start getting warned that the laxbreak option is deprecated (should you decide to start using that now).

@prettydiff
Copy link
Author

@lukeapage With that you may now close this bug (from my perspective).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants