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

Support newline before logical or ternary operator #605

Closed
olsonpm opened this issue Jan 13, 2015 · 20 comments
Closed

Support newline before logical or ternary operator #605

olsonpm opened this issue Jan 13, 2015 · 20 comments

Comments

@olsonpm
Copy link
Contributor

olsonpm commented Jan 13, 2015

So I use ternary operators every once in a while, and I format them like the following:

var result = (user === myUser)
  ? myUser
  : defaultUser;

however running this through js-beautify I get

var result = (user === myUser) ? myUser : defaultUser;

I haven't used the tool much, and didn't see any relevant options besides the preserve lines but that didn't help. I understand this is a very narrow use-case and so if the functionality doesn't exist I'm fine with that.

Thanks for the work put into this library

@olsonpm
Copy link
Contributor Author

olsonpm commented Jan 13, 2015

Oo, I just realized this made my big equality function a crazy one-liner

function equals(other) {
  return this.prop1 === other.prop1
    && this.prop2 === other.prop2
    && this.prop3 === other.prop3;
}

turns into

function equals(other) {
  return this.prop1 === other.prop1 && this.prop2 === other.prop2 && this.prop3 === other.prop3;
}

So this is more far reaching than I first thought. I'll take a look at the source.

@olsonpm
Copy link
Contributor Author

olsonpm commented Jan 13, 2015

Congratulations on an easy-to read library.

In the small chance that future readers use the same formatting I do, the following code snippet should solve your problems. I haven't tested much besides the few files I wanted js-beautify for, but I will update it if I run into any issues.

Inserted at line 1079 of js-beautify version 1.5.4

// Allow this kind of newline preservation:
// a = b
//   && (c || d);
//
// and also this kind of newline preservation:
// a = (b === c)
//   ? d
//   : e;
if (current_token.text === '&&' || current_token.text === '||' || current_token.text === '?' || current_token.text === ':') {
    if (last_type === 'TK_WORD' || flags.last_text === ')') {
        if (!start_of_object_property()) {
            allow_wrap_or_preserved_newline();
        }
    }
}

due to my styling be different from most others, I won't bother with a PR. Go ahead and close this.

@bitwiseman
Copy link
Member

No, your suggestion seems very reasonable.
It would be great if you'd be willing to add this to the javascript and python, and a few tests for this.
If not, at least we have the example to work from later.
Thanks!

@bitwiseman bitwiseman added this to the v1.6.0 milestone Jan 29, 2015
@olsonpm
Copy link
Contributor Author

olsonpm commented Jan 29, 2015

I appreciate the feedback. I just looked into running the /js/tests/run-tests and found it requires /usr/bin/spidermonkey-1.7 to exist. Any documentation on that? I searched the repo and found nothing.

@bitwiseman
Copy link
Member

try running make all. I don't think I have spidermonkey on my system and I build and test just fine.

@olsonpm
Copy link
Contributor Author

olsonpm commented Jan 30, 2015

I'll have a fix finished tonight, but man would it be helpful for all these tests to be associated with some id for quick reference haha

@olsonpm
Copy link
Contributor Author

olsonpm commented Jan 30, 2015

And actually I do need your or another maintainer's input on whether this should be optional behavior. The only time I can see where it matters is the following case:

var a = (b
||
c);

Without an option, the above case is ambiguous. As I see it, we can either

  1. Make operator-first style an option
  2. Decide on a default when an ambiguous case arises.

I lean towards # 2 because I don't feel any ambiguous cases will be important enough to style correctly. If someone writes code like the above, they likely don't care much about how it comes out.

Please let me know your thoughts.

@bitwiseman
Copy link
Member

Ah, I see.

It makes sense to do #1 in order to preserve existing behavior - no one is going to complain, that your change broke them. It also keeps the combinations for testing somewhat simpler...

But I can see the attraction of #2. As a default, you should be able to check if the operator is also followed by a new line, then force to one behavior or the other. They may not care, but we should shoot for some consistency.

@olsonpm
Copy link
Contributor Author

olsonpm commented Jan 30, 2015

I don't mean to be argumentative - but I don't see a case where anyone will complain about their code breaking as a result of this new operator-first change. I agree that it would be safer to make this optional, but mainly because I understand that I can't foresee all use-cases.

I will certainly make it an option, but for sake of playing devil's advocate, can you think of a case where someone would be surprised?

Edit: I will have a PR tomorrow with it being an option :)

@bitwiseman
Copy link
Member

You'd be surprised. 😄 Let's say someone is expecting the beautifier to force operator-before-newline and then it doesn't anymore, as in the case of team-wide style enforcer.

@bitwiseman bitwiseman changed the title how to keep ternary operator spacing? Support for newline before logical or ternary operator Jan 30, 2015
@bitwiseman bitwiseman changed the title Support for newline before logical or ternary operator Support newline before logical or ternary operator Jan 30, 2015
@olsonpm
Copy link
Contributor Author

olsonpm commented Jan 30, 2015

Ah good point - Thanks much

@olsonpm
Copy link
Contributor Author

olsonpm commented Jan 30, 2015

I find it funny the beautify.js isn't beautified haha.

relevant pull request

The code I wrote could easily be expanded to allow for other operator-first notation (such as string concatenation). Just food for thought.

@alexjamesbrown
Copy link

did this ever get solved?

@olsonpm
Copy link
Contributor Author

olsonpm commented Jun 25, 2015

Nope. I let it be because interest doesn't seem to be there. Taking a look at your referenced issue however, I'd love to have a maintainer give me feedback on my PR. I'm willing to add tests/modify the code as needed.

@olsonpm
Copy link
Contributor Author

olsonpm commented Jun 26, 2015

@alexjamesbrown - I'll for sure have the PR updated by mid-next week, though I'm aiming for this weekend. Go ahead and watch the relevent PR for further updates. When the PR is merged, I'll mark your referenced issue as closed.

@alexjamesbrown
Copy link

cool, sounds good! thanks!

@ghost
Copy link

ghost commented Dec 5, 2015

I would love newline option for && || ? : operators

@olsonpm
Copy link
Contributor Author

olsonpm commented Dec 5, 2015

Ha - I know, bitwiseman and I were working on a draft and life caught up with us. These hollidays are killing me along with work. I will get to this when I can. I appreciate the interest though - because it's nice to know people will use this feature.

@ghost
Copy link

ghost commented Dec 5, 2015

Also looking for an option to beautify our busy schedules 😃

@bitwiseman bitwiseman modified the milestones: 2.0.0, v1.6.0, v1.6.3, v2.0.0 Jan 29, 2016
@bitwiseman
Copy link
Member

#735 added option to control this.

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

3 participants