ember-template-lint-plugin-prettier icon indicating copy to clipboard operation
ember-template-lint-plugin-prettier copied to clipboard

--fix removes newline at EOF

Open jcardali opened this issue 5 years ago • 15 comments

"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

jcardali avatar Aug 22 '20 16:08 jcardali

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 https://github.com/prettier/prettier/pull/6243 but it would require more work to refine the approach.

dcyriller avatar Aug 24 '20 10:08 dcyriller

Opened https://github.com/josephfrazier/editorconfig-to-prettier/pull/8

dcyriller avatar Aug 24 '20 12:08 dcyriller

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 :(

jcardali avatar Aug 24 '20 14:08 jcardali

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. :)

dcyriller avatar Aug 24 '20 15:08 dcyriller

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.

jcardali avatar Aug 24 '20 15:08 jcardali

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

dcyriller avatar Aug 24 '20 15:08 dcyriller

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 avatar Aug 24 '20 15:08 jcardali

Hi @dcyriller, any update on this?

jcardali avatar Sep 16 '20 21:09 jcardali

editorconfig-to-prettier now supports the insert_final_newline option.

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

dcyriller avatar Sep 17 '20 07:09 dcyriller

Seems like there is support for that approach https://github.com/prettier/prettier/pull/9067.

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

jcardali avatar Sep 18 '20 13:09 jcardali

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

dcyriller avatar Sep 18 '20 20:09 dcyriller

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

rinoldsimon avatar Apr 21 '21 07:04 rinoldsimon

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 https://github.com/prettier/prettier/pull/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.

dcyriller avatar Apr 22 '21 14:04 dcyriller

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

settings.json

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

rinoldsimon avatar Apr 26 '21 05:04 rinoldsimon

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

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

jelhan avatar Feb 23 '22 12:02 jelhan