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

--fix removes newline at EOF #100

Open
jcardali opened this issue Aug 22, 2020 · 15 comments
Open

--fix removes newline at EOF #100

jcardali opened this issue Aug 22, 2020 · 15 comments

Comments

@jcardali
Copy link

jcardali commented Aug 22, 2020

"ember-template-lint": "^2.10.0"
"ember-template-lint-plugin-prettier": "^2.0.0"
"prettier": "^2.0.5"

Behavior:

ember-template-lint --fix [FILE] removes the newline at EOF

Expected behavior:

EOF newlines should at minimum be respected, or even added, since that is Prettier's behavior

I do not see this behavior if I remove prettier: true from .template-lintrc.js, which is what leads me to believe this is an issue with the plugin.

Screen Shot 2020-08-22 at 12 04 45 PM

Line 29 is the last line with text, and Line 30 is a newline

@dcyriller
Copy link
Member

Hey @jcardali,

This is actually an expected behavior of Prettier. Indeed, Prettier for handlebars does remove the newline at EOF. I made one attempt at changing that: see prettier/prettier#6243 but it would require more work to refine the approach.

@dcyriller
Copy link
Member

@jcardali
Copy link
Author

Thanks for the quick response @dcyriller! Will definitely be keeping my eye on the PR you opened.

For the sake of completeness, I also investigated whether this issue was surmountable with ember-template-lint's eol-last rule, and sadly it was not :(

@dcyriller
Copy link
Member

When you use Prettier to format your templates, it's recommended to turn ember-template-lint's stylistic preset off (eol-last rule belongs to that set). To do so, you would remove it from your .template-lintrc.js. :)

@jcardali
Copy link
Author

When you use Prettier to format your templates, it's recommended to turn ember-template-lint's stylistic preset off (eol-last rule belongs to that set). To do so, you would remove it from your .template-lintrc.js. :)

Yep, just did it out of curiousity.

@dcyriller
Copy link
Member

Okay, then it doesn't work when you specify eol-last: "never" ? Or eol-last: "editorconfig"?

@jcardali
Copy link
Author

Okay, then it doesn't work when you specify eol-last: "never" ? Or eol-last: "editorconfig"?

"never" only seemingly works because that is the default behavior of Pretter in this case. We have insert_final_newline = true at the top level of our repo, and "editorconfig" does not work.

@jcardali
Copy link
Author

Hi @dcyriller, any update on this?

@dcyriller
Copy link
Member

editorconfig-to-prettier now supports the insert_final_newline option.

Next step would be to use the option in Prettier's handlebars printer.

@jcardali
Copy link
Author

Seems like there is support for that approach prettier/prettier#9067.

I looked at this a bit today, I am not seeing how exactly to achieve it without having minimist throw a warning...

@dcyriller
Copy link
Member

It will have to be probably implemented on this line: https://github.com/prettier/prettier/blob/2.1.2/src/language-handlebars/printer-glimmer.js#L51

@rinoldsimon
Copy link

the --fix still removes the newline at EOF. Any updates on this? We have configured our IDEs to auto insert new line at EOF. Running npm run lint:hbs throws error error Delete ⏎

Is there any way to not remove the newline at EOF while running npm run lint:hbs:fix
OR is there any way to ignore this error? error Delete ⏎

My .template-lintrc.js

'use strict';

module.exports = {
  plugins: ['ember-template-lint-plugin-prettier'],
  extends: ['octane', 'ember-template-lint-plugin-prettier:recommended'],
  rules: {
    "eol-last": "always"
  }
};

I have explicitly mentioned "eol-last": "always". So now,

  • when there is no new line it throws error template must end with newline eol-last
  • when there is new line it throws error Delete prettier

@dcyriller
Copy link
Member

Hi @rinoldsimon,

the --fix still removes the newline at EOF.

This is expected. Indeed, Prettier will always remove the last eol for now.

I have opened prettier/prettier#10759 to read insert_final_newline from editorconfig and insert a final newline if insert_final_newline is true. You can follow there to see if the PR is accepted.

We have configured our IDEs to auto insert new line at EOF. Running npm run lint:hbs throws error error Delete ⏎

As a workaround, you could change your editor config to insert final newlines for all files but hbs.

@rinoldsimon
Copy link

rinoldsimon commented Apr 26, 2021

I'm using VScode and below is the config I wrote to omit the final newline in hbs

settings.json

"[handlebars]": {
    "files.insertFinalNewline": false
},

@jelhan
Copy link
Contributor

jelhan commented Feb 23, 2022

I would like to understand the use case better. Mainly to decide if this should be considered a blocker for emberjs/rfcs#777.

Why do you want to have a final new line in Ember / Glimmer templates?

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

4 participants