prettier / prettier-emacs

Minor mode to format JS code on file save
http://jlongster.com/A-Prettier-Formatter
372 stars 53 forks source link

Emacs mode does not seem to check package.json/.prettierrc for config options #19

Closed jbr closed 6 years ago

jbr commented 6 years ago

I assume this is because it's not running prettier in-place. What would the downsides of using prettier --write and revert-buffer as save hook? If a tempfile is necessary, could it be created as a sibling to the actual file so prettier can traverse and find relevant configuration options?

mattdeboard commented 6 years ago

I like this idea. The mode should be driven by the config file. Even when I add "--config" "/abs/path/to/.prettierrc" to prettier-js-args, it still doesn't work out.

mattdeboard commented 6 years ago

Actually it looks like prettier itself does not have any protection in place for my personal issue.

For example, I added "requirePragma": true to my .prettierrc.

When I run prettier -l "src/**/*.js" I get nothing. But when I just do prettier "src/**/*.js" it lists out all the changes it would make. Even if I do prettier "src/**/*.js" --require-pragma it still lists out everything.

So this seems like a problem with prettier, not necessarily with prettier-emacs.

joefiorini commented 6 years ago

UPDATE: Never mind, apparently I was still on an older version of prettier and didn't realize it. Thanks to the --stdin-filepath this does work for the use case below.

I am sad that this doesn't work. We have a big team and I want to make sure everyone is using the same settings. I have a config file with singleQuote: true, but when I run prettier in emacs (even with --config /abs/path/to/.prettierrc it still formats with double quotes. I don't understand why it wouldn't work if we directly specify the config file path. Is there something in prettier stopping it from applying when you use --stdin?

mattdeboard commented 6 years ago

@joefiorini Can you explain more about how you used --stdin-filepath to make this plugin respect your .prettierrc?

mattdeboard commented 6 years ago

Aha, wait, here we go.

Before, I was doing this:

(setq prettier-js-command (concat HOME "path/to/project/node_modules/.bin/prettier"))
(setq prettier-js-args
      `("--config " ,(concat HOME "path/to/project/.prettierrc")
        "--write"))

I changed it to this and it works:

(setq prettier-js-command (concat HOME "path/to/project/node_modules/.bin/prettier"))
(setq prettier-js-args
      `(,(concat "--config " (concat HOME "path/to/project/.prettierrc"))
        "--write"))
mattdeboard commented 6 years ago

@jbr Might work for you as well.

jbr commented 6 years ago

@mattdeboard this type of solution assumes that there's only one project, which means that switching between repos requires editing prettier settings between each save

giodamelio commented 6 years ago

Ya, it would be great if could check if prettier is installed in the current project and use it, otherwise use the globally installed prettier if it exists.

wpcarro commented 6 years ago

I tried using vim-prettier and it seems to be reading my .prettierrc, which is fantastic. I expected similar behavior in Emacs. Perhaps there is some configuration in vim-prettier that needs to be ported to the prettier-emacs?

rcoedo commented 6 years ago

Excuse me for the delay on this. I believe you can achieve this using a tool like this one. It allows emacs to add to path the bin folder in node_modules. With this I think Emacs will use your node_modules/.bin/prettier and it will read your local configuration

https://github.com/codesuki/add-node-modules-path

nickmccurdy commented 6 years ago

IMO this is solved by add-node-modules-path and isn't necessary to fix in this package.

jbr commented 6 years ago

I think there are a few different related issues. I'm not having trouble finding the binary, but have project-specific prettier settings in my package.json, as is supported by the prettier cli tool. This bug also applies to projects that use prettierrc. Prettier traverses up from the file in question until it finds a project configuration file and applies those settings. prettier-emacs moves files to a temp directory, so those project-specific configuration files are not found. It is possible to specify the settings in prettier-emacs customize-group (through args), but if you're working on multiple projects with different settings, prettier emacs will not adapt, but prettier on the cli will.

tldr: there is a distinct issue has nothing to do with the binary path, but has to do with the fact that the file to be prettier-ed is moved out of the project directory

nickmccurdy commented 6 years ago

That's odd, I have changed my prettier config in package.json and prettier-js-mode picks it up automatically.

jbr commented 6 years ago

I think https://github.com/prettier/prettier-emacs/pull/10 actually fixed this. Closing the issue. Thanks, all.

soywod commented 5 years ago

@jbr this didn't fix the issue for me (with TypeScript / TSX). To make it work with tide, I had to install add-node-modules-path (thanks @rcoedo), remove before-save:

(use-package tide
  :ensure t
  :after (typescript-mode company flycheck)
  :hook ((typescript-mode . tide-setup)
         (typescript-mode . tide-hl-identifier-mode)))
         ; (before-save . tide-format-before-save)))

and add:

(add-hook 'before-save-hook 'prettier-js-mode)

(eval-after-load
  'typescript-mode
  '(add-hook 'typescript-mode-hook #'add-node-modules-path))