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

Preserve new line at end of file #492

Closed
hezjing opened this issue Jul 19, 2014 · 12 comments
Closed

Preserve new line at end of file #492

hezjing opened this issue Jul 19, 2014 · 12 comments
Milestone

Comments

@hezjing
Copy link

hezjing commented Jul 19, 2014

Hi,

The js-beautify will remove the last new line before inside the HTML script tag. For example the new line in the following HTML script,

<head>
  <script>
    mocha.setup('bdd');

  </script>
</head>

will be removed after js-beautify reformatted the file,

<head>
  <script>
    mocha.setup('bdd');
  </script>
</head>

However, the problem is ESLint will complaint that a newline required at end of file but not found (eol-last).

May I know if we can configure js-beautify not to remove the last newline?

@PeterDaveHello
Copy link
Contributor

+1

@meinaart
Copy link

+2

@agarrharr
Copy link

It does the same thing with javascript files. I would like an option to preserve the trailing new line at the end of the file that would also add a new line at the end of the file if it doesn't already have one.

@bitwiseman bitwiseman added this to the v1.5.2 milestone Sep 11, 2014
bitwiseman added a commit to bitwiseman/js-beautify that referenced this issue Sep 28, 2014
Progress on beautifier#492

Do not feel comfortable merging yet, is it is a breaking change for node cli output to file (will strip ending newline by default now).
Will ponder how important this is.
bitwiseman added a commit to bitwiseman/js-beautify that referenced this issue Sep 29, 2014
This is a slight change to the way the command-line produces output.
Piped output and file output is now the same, and by default will not add a newline at the end.
Ending with newline is respected when passed html beautifier to css or js beautifier.

-n, --end_with_newline added to all beautifiers

Fixes beautifier#492
@agarrharr
Copy link

Thanks!

@jialinhuang00
Copy link

it helps if remove the extension Beautify that's confused and bothering when using ESLint

@tkalfigo
Copy link

For those wondering how to enable this on the command line, it is provided by the -n flag.

@jonkri
Copy link

jonkri commented Dec 2, 2018

I may be missing something obvious here, but is there a way to have -n add a newline at the end of the file, but at the same time not add newline at the end of script or style elements, like so:

<script>
    console.log()

</script>
<style>
    body {}

</style>

I would like the resulting markup to be...

<script>
    console.log()
</script>
<style>
    body {}
</style>

... with a newline after `</style>.

Thanks!

@bitwiseman
Copy link
Member

bitwiseman commented Dec 3, 2018

@jonkri
Settings overrides should work for this. Set the html (or root) setting to true and set the value for js and css children to false. It would look something like this.

{
  "end_with_newline": true,        
  "html": {
    "js": {
      "end_with_newline": false        
    },
    "css": {
      "end_with_newline": false        
    }
  }
}

@jonkri
Copy link

jonkri commented Dec 8, 2018

@bitwiseman: Brilliant! Thanks a lot! 🙂

@nelson6e65
Copy link

@jonkri
Settings overrides should work for this. Set the html (or root) setting to true and set the value for js and css children to false. It would look something like this.

{
  "end_with_newline": true,        
  "html": {
    "js": {
      "end_with_newline": false        
    },
    "css": {
      "end_with_newline": false        
    }
  }
}

It triggers this "autofix".

"atom-beautify":
    d:
      with_newline: true

But my scss file still not ending with new line.

I had to disable "Beautify on save" 😞

@bitwiseman
Copy link
Member

@nelson6e65
This is not atom-beautify, sorry. You'll need to address the issue in that project.

@lechner
Copy link

lechner commented Mar 28, 2021

Hi, -n does not work for JS files with the version in Debian bullseye. Trailing newlines disappear, which git then flags as an error.

I don't think that's a formatting question. Thanks!

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

10 participants