Bug 315507 - Add JavaScript formatting option for anonymous functions
Summary: Add JavaScript formatting option for anonymous functions
Status: RESOLVED FIXED
Alias: None
Product: JSDT
Classification: WebTools
Component: General (show other bugs)
Version: unspecified   Edit
Hardware: All All
: P3 enhancement with 2 votes (vote)
Target Milestone: 3.8.1   Edit
Assignee: Project Inbox CLA
QA Contact: Victor Rubezhny CLA
URL:
Whiteboard: RHT
Keywords: needinfo
Depends on:
Blocks:
 
Reported: 2010-06-02 17:52 EDT by ron.schouten CLA
Modified: 2016-09-17 04:17 EDT (History)
9 users (show)

See Also:
cmjaun: review-


Attachments
Fixes an appropriate test (529 bytes, patch)
2012-10-14 07:44 EDT, John Yani CLA
no flags Details | Diff
Actual fix patch (6.97 KB, application/octet-stream)
2012-10-14 07:48 EDT, John Yani CLA
no flags Details
Actual fix patch (767 bytes, patch)
2012-10-14 07:51 EDT, John Yani CLA
no flags Details | Diff
Actual fix patch (767 bytes, patch)
2012-10-14 07:53 EDT, John Yani CLA
no flags Details | Diff
Core patch (20.40 KB, patch)
2013-06-27 16:47 EDT, John Yani CLA
no flags Details | Diff
Tests patch (2.70 KB, patch)
2013-06-27 16:47 EDT, John Yani CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description ron.schouten CLA 2010-06-02 17:52:22 EDT
Build Identifier: Build id: 20100218-1602

It should be possible to set white-space formatting rules differently for anonymous functions and named functions.

Use-case: It should be possible to stipulate that white-space must precede the opening parenthesis in an anonymous function declaration while named function declaration should not.

Example:

var foo = function () {
  // some code
};

function foo() {
  // some code
}


Reproducible: Always

Steps to Reproduce:
Not really a bug, more of a feature request. Go to Window > Preferences > Web > JavaScript > Code Style > Formatter. Under the White Space tab open Declarations and see that there is only a setting for Functions (not Named Functions and Anonymous Functions)
Comment 1 Nitin Dahyabhai CLA 2010-06-02 19:59:55 EDT
I don't expect we'll be working on this.  High-quality contributed patches would be entertained.
Comment 2 John Yani CLA 2012-10-13 14:38:14 EDT
duplicate: https://bugs.eclipse.org/bugs/show_bug.cgi?id=293243
Comment 3 John Yani CLA 2012-10-13 14:40:04 EDT
I think it's a show stopper. This is what stopping me from using eclipse javascript formatter.
Comment 4 John Yani CLA 2012-10-13 15:43:37 EDT
Any chance to port Aptana's solution? http://jira.appcelerator.org/browse/APSTUD-3792
Comment 6 John Yani CLA 2012-10-14 07:44:08 EDT
Created attachment 222271 [details]
Fixes an appropriate test

This is a patch that changes the formatter test.
Comment 7 John Yani CLA 2012-10-14 07:48:14 EDT
Created attachment 222272 [details]
Actual fix patch

This is a patch that changes behaviour of formatter to add a space after function keyword.

I.e. this:

function() {
}
function namedFunction() {
}

would be formatted as this:

function () {
}
function namedFunction() {
}

Still, it doesn't make this behaviour optional, just provides better defaults.
Comment 8 John Yani CLA 2012-10-14 07:51:49 EDT
Created attachment 222273 [details]
Actual fix patch

Oops accedently submitted wrong patch. Update.
Comment 9 John Yani CLA 2012-10-14 07:53:01 EDT
Created attachment 222274 [details]
Actual fix patch

Oh, no, again. Sorry.
Comment 11 John Yani CLA 2012-10-15 18:59:26 EDT
You can review my patches here: https://github.com/eclipse/webtools.jsdt.core/pull/1

As you requested: Provide a configuration setting, add JSLint built-in profile.
Comment 12 John Yani CLA 2012-10-15 20:15:36 EDT
Tests can be found here: https://github.com/eclipse/webtools.jsdt.tests/pull/1
Comment 13 Nitin Dahyabhai CLA 2012-10-16 06:35:18 EDT
Reopening for potential contribution.
Comment 14 Nitin Dahyabhai CLA 2012-10-30 11:18:34 EDT
(In reply to comment #10)
> You can also find my patches here
> https://github.com/Vanuan/webtools.jsdt.core/commit/
> c3b3cfc10ed4109ed05eb0c5810249610751f5a7
> and here:
> https://github.com/Vanuan/webtools.jsdt.tests/commit/
> 47dd3b27caf2e40653ae4ffd2fb0f1269983e98f

Commented on the change in the tests repo (https://github.com/Vanuan/webtools.jsdt.tests/commit/4d53cfe7a5c1c0f3da5747eefef55aac0d617e0b#commitcomment-2074117 ).  Unrelated to that, why remove the ';' from the white space example?  Were the formatted results not coming out right for the preview?
Comment 15 John Yani CLA 2012-10-30 18:37:41 EDT
(In reply to comment #14)
> Commented on the change in the tests repo
> (https://github.com/Vanuan/webtools.jsdt.tests/commit/
> 4d53cfe7a5c1c0f3da5747eefef55aac0d617e0b#commitcomment-2074117 ).

Thanks, pushed a new version.

> Unrelated
> to that, why remove the ';' from the white space example?  Were the
> formatted results not coming out right for the preview?

Do you mean this: https://github.com/Vanuan/webtools.jsdt.core/commit/0adccb0e6e8897fd01e1e6b476db2bb341070315#L5R193

That semicolon is not needed there.

If semicolon was there that snippet would be formatted like this:

function foo() {
}
;
function bar(x, y) {
}
var baz = function () {
};

I.e. semicolon in this context is an empty statement.
Because function declarations are not statements:
http://stackoverflow.com/questions/1834642/best-practice-for-semicolon-after-every-function-in-javascript

While function expressions are. So the semicolon after baz variable assignment is strongly recommended.

I think that examples in formatter should be of proper javascript, not only of formatter options. That's why I edited it. If you disagree, I can revert that, no problem.
Comment 16 John Yani CLA 2013-01-31 14:29:27 EST
Seems like this project is doomed.

Sorry, but 3 months of awaiting for a simple change like this to be accepted is too much for me.

VJET looks much more dynamic.
Comment 17 Jon Houghton CLA 2013-06-19 11:37:49 EDT
I have reviewed this patch and the patches on GitHub for the new JSLint profile and recommend that they all be included in 3.5.1.  Chris, can you officially review it.
Comment 18 Chris Jaun CLA 2013-06-27 15:53:28 EDT
This patch simply takes one arbitrary formatting decision and changes it to another. I support adding a preference to accomplish this.
Comment 19 Nitin Dahyabhai CLA 2013-06-27 15:58:14 EDT
(In reply to comment #18)
> This patch simply takes one arbitrary formatting decision and changes it to
> another. I support adding a preference to accomplish this.

Isn't that basically what it's doing? Just in a manner that gives it slightly higher visibility by establishing its own profile?
Comment 20 John Yani CLA 2013-06-27 16:39:01 EDT
(In reply to comment #18)
> This patch simply takes one arbitrary formatting decision and changes it to
> another. I support adding a preference to accomplish this.

Obviously, you reviewed the wrong patch. If you look closer into this discussion, you'll see that I provided a link to an appropriate pull request on github.

I thought it would've been easier for committer to fetch my branch and cherry pick corresponding commits.

Anyway, to avoid confusion, and since there is no established process to accept patches from github, I'll resubmit these patches here.
Comment 21 John Yani CLA 2013-06-27 16:47:19 EDT
Created attachment 232865 [details]
Core patch
Comment 22 John Yani CLA 2013-06-27 16:47:53 EDT
Created attachment 232866 [details]
Tests patch
Comment 23 John Yani CLA 2013-06-27 16:48:56 EDT
By the way, they are also available under the following links:

https://github.com/eclipse/webtools.jsdt.core/pull/1.patch

https://github.com/eclipse/webtools.jsdt.tests/pull/1.patch
Comment 24 John Yani CLA 2013-06-27 16:50:57 EDT
They are presented as three commits. You can squash them if you like.
Comment 25 John Yani CLA 2013-06-29 21:47:01 EDT
Somebody had assured me that this would be included in the Kepler release. I'm a bit disappointed that nobody had looked into this patch for 7 months.
Anyway, it seems that this project is not maintained anymore. It's been 2 commits since the last release.
Comment 26 Mickael Istria CLA 2013-12-30 05:59:00 EST
@John: that seems to be a good improvement (functionally speaking, I didn't look at the code). Could you please make it in a Gerrit patch to ease the review?
https://wiki.eclipse.org/JSDTDevelopment#Pushing_Gerrit_Reviews
Comment 27 Marcus Krassmann CLA 2015-11-08 12:41:25 EST
2015 is almost over. Even in Mars it is still not possible to format JavaScript code to fulfill JSHint's rules.

I changed from Eclipse to ******** **** some years ago. Tickets like this (a patch exists, but is not merged due to politics or so) remind me why I felt so uncomfortable with Eclipse. Too sad this still seems to be true :-(
Comment 28 Mickael Istria CLA 2015-11-09 04:43:20 EST
@Marcus: Did you have the opportunity to try those patches?
It would be nice if John would submit the patches as Gerrit contributions, making the review and merge easier.
Comment 29 Eclipse Genie CLA 2015-11-09 08:41:13 EST
New Gerrit change created: https://git.eclipse.org/r/59941
Comment 30 Eclipse Genie CLA 2015-11-09 08:43:58 EST
New Gerrit change created: https://git.eclipse.org/r/59943
Comment 31 John Yani CLA 2015-11-09 08:53:03 EST
Added gerrit. Not sure why it's failing though.
Comment 32 Mickael Istria CLA 2015-11-09 08:57:59 EST
Thanks. I'm taking care of avoiding the failures.
@Victor and others: do you think you could have a look at this patch soon?