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

Unwanted \n at the end of file when using eol #781

Closed
gabrielmaldi opened this issue Sep 22, 2015 · 5 comments
Closed

Unwanted \n at the end of file when using eol #781

gabrielmaldi opened this issue Sep 22, 2015 · 5 comments

Comments

@gabrielmaldi
Copy link
Contributor

  • Beautifying using v1.5.10, with eol = "\n" on OS X, or with eol = "\r\n" on Windows, adds an unwanted \n at the end of the file.
  • On Windows there is a case where it also removes the \r from the last newline, see below.
  • This only happens with the jsbeautifier, HTML files are not affected.

Given a file using \r\n as newline:

hello1
hello2
hello3

Which expressed in hex is:

68 65 6C 6C 6F 31 0D 0A 68 65 6C 6C 6F 32 0D 0A 68 65 6C 6C 6F 33
  • The result of beautifying it on Windows with eol = "\r\n" adds a \n (0A) at the end of the file:
68 65 6C 6C 6F 31 0D 0A 68 65 6C 6C 6F 32 0D 0A 68 65 6C 6C 6F 33 0A
  • If I also specify end_with_newline = true it does add the new line as it should but it also adds the same \n after it:
68 65 6C 6C 6F 31 0D 0A 68 65 6C 6C 6F 32 0D 0A 68 65 6C 6C 6F 33 0D 0A 0A

If the original file already ends with newline:

hello1
hello2
hello3

68 65 6C 6C 6F 31 0D 0A 68 65 6C 6C 6F 32 0D 0A 68 65 6C 6C 6F 33 0D 0A
  • Beautifying with eol = "\r\n" yields:
68 65 6C 6C 6F 31 0D 0A 68 65 6C 6C 6F 32 0D 0A 68 65 6C 6C 6F 33 0A

So it's removing the \r (0D) in the last newline.

  • And specifying end_with_newline = true adds the \n at the end:
68 65 6C 6C 6F 31 0D 0A 68 65 6C 6C 6F 32 0D 0A 68 65 6C 6C 6F 33 0D 0A 0A

Update: all of the above (except the case where \r is removed) also happens on OS X with a file using \n as newline.


To sum up:

end_with_newline = false end_with_newline = true
File doesn't end with \r\n Adds \n at the end Adds \n at the end
File ends with \r\n Removes \r from last newline Adds \n at the end
@gabrielmaldi gabrielmaldi changed the title Unwanted \n at the end of file on Windows when using eol = "\r\n" Unwanted \n at the end of file when using eol Sep 23, 2015
@Deklin
Copy link

Deklin commented Oct 1, 2015

I can confirm some of this behavior as well with the 1.5.10 version.

If I set eol to \r\n
the end_with_newline doesn't honor eol value and always just appends \n. This may be by design, I was assuming eol would be the end of line character to use including by ends_with_newline.

@bitwiseman bitwiseman added this to the v1.6.0 milestone Nov 17, 2015
@bitwiseman bitwiseman modified the milestones: 2.0.0, v1.6.0 Jan 27, 2016
@bitwiseman bitwiseman removed this from the v2.0.0 milestone Aug 24, 2018
@bitwiseman
Copy link
Member

@gabrielmaldi @Deklin
I believe this was fixed a while ago. I have tests that cover this area. Please comment if I should reopen.

@gabrielmaldi
Copy link
Contributor Author

@bitwiseman just tried this in v1.8.0 and it doesn't seem to be fixed.

Input (no newline at eof):

var a = 1;

Hex: 7661 7220 6120 3d20 313b

Running the beautifier with end_with_newline: true produces:

Output:

var a = 1;

Hex: 7661 7220 6120 3d20 313b 0a0a

So it's adding two new lines (in macOS).

If the input already ends with a new line, it adds another new line anyway.

If I use end_with_newline: false it still adds a newline. So maybe this is the problem (it's always adding a newline regardless of the parameter, and setting it to true produces an extra newline)?

Haven't tried in Windows but I assume it's the same behavior.

Thanks!

@bitwiseman
Copy link
Member

@gabrielmaldi What is the command you are using to run this?

@gabrielmaldi
Copy link
Contributor Author

I was running the beautifier through grunt-jsbeautifier and that caused the extra newline; there's an issue reported: vkadam/grunt-jsbeautifier#60

Just running the beautifier by itself works fine!

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