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

Newlines stripped from processed files #28

Closed
pieterv opened this issue Jul 31, 2015 · 10 comments
Closed

Newlines stripped from processed files #28

pieterv opened this issue Jul 31, 2015 · 10 comments

Comments

@pieterv
Copy link
Member

pieterv commented Jul 31, 2015

new lines seem to be removed even if i dont change a file, for now i am having to do:

return root.toSource({quote: 'single', trailingComma: true}) + '\n';

Maybe this is an issue with recast?

@pieterv
Copy link
Member Author

pieterv commented Jul 31, 2015

Hmm maybe this fixes it, benjamn/recast#199

@fkling
Copy link
Contributor

fkling commented Jul 31, 2015

Mmh, have to look at the recast thing, but also have a look at #27.

@cpojer
Copy link
Contributor

cpojer commented Jul 31, 2015

@fkling this is exactly what I mentioned yesterday. Everyone gets tripped up by this. Let's just merge #27 with the auto-detection? That should cover this really nicely without a "breaking" change.

@fkling
Copy link
Contributor

fkling commented Jul 31, 2015

@cpojer: I know, I was just wondering what recast did about this since it was mentioned there as well.

@fkling
Copy link
Contributor

fkling commented Jul 31, 2015

So, the issue seems to be with using Babel as parser. If esprima is used, trailing and leading whitespace is preserved. I'd suggest we first look into if we can fix this in recast before making changes to jscodeshift.

edit: nvm, it works also when I use babel-core as parser... now I'm confused...

@fkling
Copy link
Contributor

fkling commented Jul 31, 2015

OK, I know what the issue is. recast.parse(...) returns a File AST node which contains the location information about the leading and trailing white spaces, but since we are throwing that object away and only use program in https://github.com/facebook/jscodeshift/blob/master/src/core.js#L74, we are loosing that information.

Maybe everything will just work if we use the File object instead.

fkling added a commit to fkling/jscodeshift that referenced this issue Jul 31, 2015
There have been multiple reports about the printed source missing
trailing line breaks. The reason for the line break missing is that this
information is contained in the `File` node, which is returned by
`recast.parse`.
However, because we build a node collection from the `Program` node
instead of the `File` node, we are loosing this information.
Using the `File` node fixes the issue and doesn't seem to break any
existing functionality.
@fkling fkling closed this as completed in 04c4a9f Jul 31, 2015
@pieterv
Copy link
Member Author

pieterv commented Jul 31, 2015

Awesome! Thanks @fkling

@fkling
Copy link
Contributor

fkling commented Jul 31, 2015

Release in v0.3.3 btw.

euphocat pushed a commit to euphocat/jscodeshift that referenced this issue Oct 22, 2017
Add support for ES6 classes in sort-comp
@nebirhos
Copy link

nebirhos commented Jan 4, 2018

It looks like this is an issue again in v0.4.0.

Edit: Tested the same script with v0.3.32, newlines are preserved as expected.

@ibrahima
Copy link

Aha, ran into this issue as well. I can confirm that downgrading to v0.3.32 fixes the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants