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

CRLF line endings are converted to LF line endings. #8921

Closed
hanxinwei opened this issue Oct 24, 2018 · 13 comments
Closed

CRLF line endings are converted to LF line endings. #8921

hanxinwei opened this issue Oct 24, 2018 · 13 comments
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@hanxinwei
Copy link

Bug Report

Current Behavior
Use babel & gulp-babel in Windows 10.
The files with CRLF line endings will be transpiled to files with LF line endings, which in my view is not the expected behavior, since in Windows it's usually CRLF setting for git, and after the transpilation the git diff will show a lot of changes in output files caused by CRLF to LF changes, which is unexpcted.

Expected behavior/code
Keep the output CRLF/LF with source.

Environment

  • Babel version(s): [e.g. 6.26.0]
  • gulp-babel: [e.g. 7.0.1]
  • Node/npm version: [e.g. Node v8.11.2/npm 5.6.0]
  • OS: [Windows 10]

Possible Solution
Keep the output CRLF/LF with source.

@babel-bot
Copy link
Collaborator

Hey @hanxinwei! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

@loganfsmyth
Copy link
Member

Babel doesn't keep any whitespace information around after the file is parsed. In general we're reducing the amount of control over output formatting that we expose, because all we really want to guarantee is that the file we output will run properly, and CR/LF values don't affect that.

Assuming you're using autocrlf, I believe you can set a specific eol=lf value for the compiled assets: https://help.github.com/articles/dealing-with-line-endings/#per-repository-settings

@hanxinwei
Copy link
Author

Thank you very much for your reply. @loganfsmyth
eol=lf seems to be an option.
This is kind of ... As a control freak in Windows every line ending should be CRLF. LOL. Thank you very much anyway.

@loganfsmyth
Copy link
Member

loganfsmyth commented Oct 25, 2018

I don't really know the specifics of how Git handles things. If we defaulted to CRLF instead, would people have the opposite problem? I could imagine if Git converts the CRLF to LF on checkout, and then the build outputs CRLF, it'd be a similar issue for people.

Since editors generally just support both anyway, it's not super clear to me how much there is to gain from going to CRLF route.

@hanxinwei
Copy link
Author

I was kind of surprised when I googled and found no info regarding the babel line endings, since there must be the little gnawing problem if there exist collabrations between Mac and Windows. I can only assume most web developers are fans and users of Mac, which is true indeed in my work environment. Sometimes I feel a miracle using Windows with people around all using Mac. LOL.

Regarding CRLF/LF
https://stackoverflow.com/questions/10418975/how-to-change-line-ending-settings
image
We can guess the principle of Git is that let the users keep with the norm of OS ( CRLF in Windows, LF in Mac/Unix ), use one encoding in repo ( apparently the Git designers prefer and choose Unix's LF ), and provider the conversion betwen the disk files and repo.

@loganfsmyth
Copy link
Member

I can only assume most web developers are fans and users of Mac,

There's certainly a large portion of Mac/Linux, but I think really you won't find much because people don't often commit their build output into Git in the first place. Usually I'd expect things to just be built and published to npm, but never tracked in Git.

@hanxinwei
Copy link
Author

I can only assume most web developers are fans and users of Mac,

There's certainly a large portion of Mac/Linux, but I think really you won't find much because people don't often commit their build output into Git in the first place. Usually I'd expect things to just be built and published to npm, but never tracked in Git.

Oh yes, this is very likely the main reason. My project is kind of special that the compiled assets are intermediate products which are used as source by other projects and therefore tracked by Git. Surely this way is for compatibility, since not every project supports ES6/Babel.

@hanxinwei
Copy link
Author

hanxinwei commented Oct 25, 2018

Actually I found something weirder is CRLF and LF both appear in the compiled file.
I didn't observe any rule regarding why some locations CRLF and the others LF.

@loganfsmyth
Copy link
Member

We likely preserve CRLF in the output for literal tokens that contained newlines in the original source, like if you have CRLF in a comment or a template literal and don't transform them somehow, I think they would pass through with CRLF values still.

@hanxinwei
Copy link
Author

We likely preserve CRLF in the output for literal tokens that contained newlines in the original source, like if you have CRLF in a comment or a template literal and don't transform them somehow, I think they would pass through with CRLF values still.

Got it. Thank you!

@catdad
Copy link

catdad commented Jan 28, 2019

Not sure if this is the right place, but it was mentioned that line endings are correctly kept when they are part of the code itself, like template literals. I am finding that is not the case, and even in template literals, those endings are modified, which could result in a logical code change. Here is am example:

> babel.transform('var a = `\r\n`').code
'var a = `\n`;'
> babel.transform('var a = `\r`').code
'var a = `\n`;'
> babel.transform('var a = `\n`').code
'var a = `\n`;'

In this case, the code is trying to assign \r to a variable, but once transformed, it would be assigning \n to that variable. If I were to then use that variable in an if statement, my code would be logically different from the original source.

The code above uses babel-core 6.26.0, but I have also reproduced it with @babel/core 7.1.16.

@nicolo-ribaudo
Copy link
Member

@catdad
It is expected: newlines in template literals are normalized to \n:

// Chrome console

> eval("`\r\n`")
"
"

> eval("`\r\n`").charCodeAt(0)
10

> eval("`\n`").charCodeAt(0)
10

> eval("`\r`").charCodeAt(0)
10

> "\n".charCodeAt(0)
10

If you don't want it, you should use actual \r or \n escape sequences:

`\r\n`;

which, when put inside a string, would become

babel.transform('var a = `\\r\\n`').code

Spec reference: https://tc39.github.io/ecma262/#sec-static-semantics-tv-and-trv (last bullet points)

@nicolo-ribaudo
Copy link
Member

I'm closing this issue since maintaining the original code style is not a goal of Babel.

@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Aug 13, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Aug 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

6 participants
@loganfsmyth @catdad @nicolo-ribaudo @hanxinwei @babel-bot and others