prettier / prettier-emacs

Minor mode to format JS code on file save

Home Page:http://jlongster.com/A-Prettier-Formatter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Files in .prettierignore are emptied out on save

hsribei opened this issue · comments

Steps to reproduce:

mkdir test
cd test
npm init -y
echo "package.json" > .prettierignore
emacsclient package.json
# make sure prettier-js-mode is running
# insert space then save

Desired result:

File gets saved without modifications.

Observed result:

File contents get deleted and file is saved as empty file.

This happens because prettier itself prints nothing and returns with exit code 0 (success) when passed a file as a parameter that is matched by the .prettierignore file.

This can be seen by running here:

mkdir test
cd test
npm init -y
echo "package.json" > .prettierignore

prettier package.json
# outputs nothing, goes straight to next prompt

mv .prettierignore .prettierignorebak
prettier package.json
# outputs this:
{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

Since it returns empty without an error code, this condition is not caught by the if here:

https://github.com/prettier/prettier-emacs/blob/master/prettier-js.el#L185

My elisp-fu is non-existent, but checking if prettier's output isn't empty shouldn't be too hard, so I might try fixing it.

My immediate use case for .prettierignore, which was avoiding formatting conflicts between npm and prettier on package.json, has been resolved by prettier just special-casing for package.json and package-lock.json to match npm's use of JSON.stringify(data, null, 2).

https://twitter.com/hsribei/status/1001560322082263040

I'll still see if I can make prettier-js-mode not clobber other .prettierignored files in case that helps someone else.

Ok, nevermind. The whole issue is solved since December last year. It's always a good idea to keep your packages updated!

Now instead of returning an empty output for ignored files, prettier just prints them back as-is, solving the problem.

prettier/prettier#3590