Bug 357829 - Javascript formatter seems not to do anything
Summary: Javascript formatter seems not to do anything
Status: NEW
Alias: None
Product: JSDT
Classification: WebTools
Component: General (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows XP
: P3 normal with 4 votes (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact: Chris Jaun CLA
URL:
Whiteboard:
Keywords: needinfo
Depends on:
Blocks:
 
Reported: 2011-09-15 11:39 EDT by Ronald Portier CLA
Modified: 2023-01-18 01:35 EST (History)
13 users (show)

See Also:


Attachments
Source that will not format, because (I think) it is to large. (42.38 KB, application/x-javascript)
2011-12-08 05:02 EST, Ronald Portier CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Ronald Portier CLA 2011-09-15 11:39:37 EDT
Build Identifier: 20110218-0911

Both when formatting (CTRL+SHIFT+F, of right mouse click action) a whole source file, or when selecting lines and trying to format, nothing seems to happen, certainly the format specifications ar not honored.

Example. I have in my source a number of places where there is no space in an assigment before or the assignment operator. 

Like this: 

	needEscape=true;

Formatting preview (detail) shows this:

var index = 0;

In my preferences (named MijnFormatterVoorkeuren, dutch for My Formatting Preferences) I have specified to have a space before and after assignment operators.

Preview on tab whitespace, then expressions node, then assignment node:

var arr = new Array();
var a = -4 + -9;
var b = a++ / --a;
a += 4;
var value = true && false;


Reproducible: Always

Steps to Reproduce:
1. Check formatting options.
2. Press CTRL+SHIFT+F
3. Nothing happens....
Comment 1 Nitin Dahyabhai CLA 2011-09-15 13:20:20 EDT
Are you working with a file in a source folder of a JavaScript Project?
Comment 2 Ronald Portier CLA 2011-09-18 08:45:51 EDT
I do not exactly know I I can answer your question correctly.

The project is a web-project. The contents of the .project files are as follows:

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
	<name>fakt400-html</name>
	<comment></comment>
	<projects>
	</projects>
	<buildSpec>
		<buildCommand>
			<name>org.eclipse.wst.common.project.facet.core.builder</name>
			<arguments>
			</arguments>
		</buildCommand>
		<buildCommand>
			<name>org.eclipse.wst.validation.validationbuilder</name>
			<arguments>
			</arguments>
		</buildCommand>
	</buildSpec>
	<natures>
		<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
		<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
		<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
		<nature>org.python.pydev.pythonNature</nature>
	</natures>
</projectDescription>

(The pydev nature is there for some build scripts)

Further more the javascript files are contained in a subdirectory of the WebContent directory.
Comment 3 Nitin Dahyabhai CLA 2011-09-18 22:41:42 EDT
Can you also post the contents of the .settings/.jsdtscope file?
Comment 4 Ronald Portier CLA 2011-09-27 08:14:33 EDT
Sorry for the delay, I was away for a few days.

Here are the contents of the requested file:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
	<classpathentry kind="src" path="WebContent/javascript"/>
	<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.JRE_CONTAINER"/>
	<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.WebProject">
		<attributes>
			<attribute name="hide" value="true"/>
		</attributes>
	</classpathentry>
	<classpathentry exported="true" kind="con" path="org.eclipse.wst.jsdt.launching.baseBrowserLibrary"/>
	<classpathentry exported="true" kind="con" path="org.eclipse.wst.jsdt.USER_LIBRARY/jquery"/>
	<classpathentry kind="con" path="org.eclipse.wst.jsdt.USER_LIBRARY/fakt400-html"/>
	<classpathentry kind="output" path=""/>
</classpath>
Comment 5 Nitin Dahyabhai CLA 2011-12-07 14:52:40 EST
And where was the file in this project?
Comment 6 Ronald Portier CLA 2011-12-08 05:00:10 EST
Hello,

My file was actually in a subfolder WebContent/fakt400gui.

However the same problem occurs in WebContent/javascript.

After some further testing I suspect that the problem has to do with either the size of the file, or the number of changes. 

I took a large javascript file (lispanel.js - I will attach it to this bug). Formatting would NOT work. When I truncated the file to a bit more than 200 lines, formatting would work.

To test wether the size of the file was the problem, or the number of changes, I first formatted the first 200 lines. Formatted them. Then another 114 lines (to prevent cutting a function in the middle). These formatted OK. Then another 150 or so. Formatting did not work. Cut the source to 405 lines. Formatting worked. Added the lines just cut back in. Formatting still did not work. 

Now copied already formatted source in place of lines just re-added. So all source formatted. Then added one unformatted line. Formatting worked. Now started adding functions one by one and formatting. At a certain moment formatting stopped. Removed the last unformatted function. Added some dummy lines and one unformatted line. At about 400 lines, formatting no longer worked.

My conclusion: formatting stops working at a certain size of the source.
Comment 7 Ronald Portier CLA 2011-12-08 05:02:20 EST
Created attachment 208086 [details]
Source that will not format, because (I think) it is to large.
Comment 8 Tsogtgerel Tsogtsaikhan CLA 2017-01-31 06:07:21 EST
character '/' make problem.
Comment 9 Patrik Suzzi CLA 2017-01-31 13:33:51 EST
(In reply to Tsogtgerel Tsogtsaikhan from comment #8)
> character '/' make problem.

Hi, Tsogtgerel!

Could you please tell us which version of JSDT are you using?
Comment 10 Tsogtgerel Tsogtsaikhan CLA 2017-01-31 21:42:55 EST
I use just Neon.2 Release of the Eclipse Java EE Developers package. I think it's bug for eclipse generally not only for JSDT.
Comment 11 Bogdan Butnaru CLA 2017-02-15 06:24:56 EST
I’m having similar issues with Ronald. In my case (and, I suspect, Ronald’s) it’s not caused by file size. I believe the formater cannot correctly parse some kinds JS syntax, and if it encounters them it refuses to format the file. Since there’s no indication which syntax it doesn’t like, it’s tricky to notice.

I started with one of the file it doesn’t work, and by removing stuff I reduced it to this nearly-minimal test case:

-------------------
function test(){
    var y = (1, 2);
}
-------------------

I can add spaces anywhere in the example above, and Format does nothing to fix it. In this case, it seems to be the comma operator that it doesn’t like, but I think I’ve encountered other cases in the past.

(Strangely enough, my version of Eclipse *does* format the example attached by Ronald. I think it’s because there are different parsing bugs between our versions of Eclipse. It also routinely format much larger files, as long as they don’t contain syntax it doesn’t like.)

Eclipse version info: 

Eclipse IDE for Java Developers
Version: Neon.2 Release (4.6.2)
Build id: 20161208-0600

JavaScript Development Tools
Version: 2.0.0.v201608301904
Build id: @build@ (<-this is probably a bug)

(I’m actually using an Eclipse Platform release binary with manually installed JDT and WTP.)
Comment 12 Richard Kulp CLA 2017-02-17 12:45:52 EST
Yes it is do to new node syntax. This neon.1

I had a very small file. 

Will format:
x(function(z) { ...some stuff ...});

Will not format:
x((z) => { ...some stuff...});


It also doesn't seem to matter whether you are selecting just a portion of the file or the whole file. If anywhere in the file there is syntax like above, even though not in the section being formatted it will not format.

This is very annoying.
Comment 13 Vlad Dinulescu CLA 2017-03-25 14:12:17 EDT
Hi,

This also happens for the following snippet (Version: Neon.2 Release (4.6.2)):

for (let listItem of listItems) {
}
Comment 14 David Krizanic CLA 2017-04-05 07:22:31 EDT
I hit the same issue and found this bug, I also assumed it was down to size but confused since other files would parse fine. As suggested by others I removed pieces of code until I narrowed the parsing error down to a single line:

function( char ) { return map[ char ]; }

It baffled the hell out of me but turns out the variable name "char" crashes the formatter, renaming it to "character" and the whole file formatting now works fine. This may not be the only "type" variable name that breaks the formatter, didn't bother to check all.

Version: Neon.3 Release (4.6.3)
Build id: 20170314-1500
Comment 15 Deses 12 CLA 2017-06-08 06:10:16 EDT
I'm also having this issue with Neon.2, latest (as of this comment) JSDT from the release repository installed.

When I try to format code that contains a "class" key name, the formatter does nothing.
Comment 16 Deses 12 CLA 2017-06-08 06:10:36 EDT
I'm also having this issue with Neon.2, latest (as of this comment) JSDT from the release repository installed.

When I try to format code that contains a "class" key name, the formatter does nothing.
Comment 17 Floyd Elliot CLA 2017-07-08 17:33:56 EDT
I've found that any JS file that includes a <Promise>.then().finally() will fail to format. If you comment out the .finally() block it formats; put it back, and it fails.
Comment 18 Floyd Elliot CLA 2017-07-08 17:35:26 EDT
(In reply to Floyd Elliot from comment #17)
> I've found that any JS file that includes a <Promise>.then().finally() will
> fail to format. If you comment out the .finally() block it formats; put it
> back, and it fails.

Actually, to be more precise: you don't need the .then(). It will fail with just <Promise>.finally() as well.
Comment 19 Floyd Elliot CLA 2017-10-03 15:45:56 EDT
Well, I think I've figured out the use-case when this problem occurs: it's whenever you use a method that's also a JavaScript keyword, so, as noted above "finally" in <Promise>.finally() kills the formatting, as does "continue" in cursor.continue() when using the IndexedDB API.
Comment 20 Van Lepthien CLA 2017-10-06 18:11:10 EDT
Oxygen.1:

It appears that the problem occurs when a reserved word appears as a key:

   var func = shape.function

breaks alt-ctrl-f,

   var func = shape["function"]

does not.

Reading the other comments, this also appears to be the case when keywords are encountered in unexpected circumstances, e.g. <Promise>.finally().

Perhaps the solution is to do keyword validation after formatting. 

The change above worked when I copied the existing code to a new file (ctrl-c / ctrl-v on the file contents) and edited it before saving it. The original file and a file copied using ctrl-c / ctrl_v on the file itself in eclipse still did not format. F5, project clean, and restart did not help. Neither did restarting eclipse with -clean specified.
Comment 21 Mike Bretherick CLA 2017-10-17 12:44:48 EDT
Version: Neon.3 Release (4.6.3)
Build id: 20170314-1500

JavaScript Development Tools
Version: 2.0.200.v201612211424

This also appears to be a problem when dividing inside parenthesis then again outside. For example the following will not format:

function test() {
	var y=(1 / 1) / 1;
}
Comment 22 Lukas Zboron CLA 2017-11-29 08:20:05 EST
It seems that various ES2015 features break formatter.

When I add this code, formatter no longer works.

	const f = {
			...obj,
				a : 1,
		b : 4
	};


JSDT 2.1.0.v201706011953
Comment 23 Bruno Harbulot CLA 2018-05-10 09:52:22 EDT
Using JSDT 2.1.1.v201803202007 (in Oxygen), statements like these also prevent the formatter to work at all, even on small files:


import { Something, SomethingElse } from 'test.js';


or:


for (let x of y) {
  doSomething();
}
Comment 24 Sharan Kumar CLA 2023-01-18 01:35:25 EST
Version: Neon.3 Release (4.6.3)
Build id: 20170314-1500