Repository navigation
Description
Editors are the main place where we want people to use prettier, in order for it to be a great experience it should have the following properties:
- Use the version of prettier that the project uses. If not, fall back to an arbitrary version.
- When saving, use the options specified by the project, otherwise fall back to an arbitrary configuration.
- When reading a file that is inside of a project using prettier, and if an option is enabled, reformat the code using the local user preferences.
Proposal
Allow people adding their configuration options inside of prettier in package.json:
{
...
"prettier": {
"bracketSpacing": false,
"trailingComma": "all"
}
}How to find a package version of prettier?
Given a file with a path a/b/c/d/e.js, we want to crawl up the tree up until we find a file package.json.
It should check in this order:
a/b/c/d/package.jsona/b/c/package.jsona/b/package.jsona/package.jsonpackage.json
If it doesn't find one, then the file isn't inside of a prettier repo.
If it finds one in a/b/package.json, then look at the file a/b/node_modules/.bin/prettier. If it finds one, then use this prettier binary.
How to deal with caching?
This is a fair amount of io to be done for each invocation. If you want to add caching, you want to be careful, here are the things to watch out for:
a/b/package.jsonis updateda/b/node_modules/.bin/prettieris updateda/b/c/package.jsonora/b/c/d/package.jsonis created
How to find the configuration associated to a package version of prettier?
Given that a package version of prettier is always in a/b/node_modules/.bin/prettier and the package configuration is always in a/b/package.json, we can inside of prettier check if ../../package.json exists, and if it does read it and use the prettier key.
If prettier is being used as a CLI with require('prettier'), I'm not sure how to find it and if it's even a good idea to.
How do you resolve conflicting rules?
I suggest that we merge rules one by one and the order they are loaded in/override each other is:
- First load
package.jsonrules - Then load CLI arguments which override the one above
For example,
// package.json
{ "prettier": {"trailingComma": "all", "singleQuote": true} }prettier --trailing-comma es5 --parser flowwould result in
{singleQuote: true, trailingComma: 'es5', parser: 'flow'}How to make it fast?
Because all the integrations now have to use the cli and can no longer just use the local require version, we're going to pay js/node startup time on every single invocation.
I recommend adding an option to prettier bin --json-rpc. With this, you can open the prettier process once and send formatting commands through stdin/stdout without having to restart it every time.
A sample connection looks like this:
-->
Content Length: xxx
{"jsonrpc": "2.0", "method": "format", "params": ["abc", {"trailingComma": "es6"}], "id": 1}
<--
Content Length: xxx
{"jsonrpc": "2.0", "result": {"formatted": "abc;", "error": null}, "id": 1}Conclusion
Let me know what you think :)
Activity
vjeux commentedon Mar 6, 2017
cc @jlongster @robwise
robwise commentedon Mar 6, 2017
Wow, great work writing all of this up.
This is something a number of users are requesting on prettier-atom. We haven't implemented yet, but what you're describing sounds about right to me.
I believe @kentcdodds solved this issue in a similar case where he needed to find the ESlint configuration for prettier-eslint. He solved it by optionally passing the filepath of the file as an option. Then the tool knows where to start recursively checking from.
I vote for this also.
This is definitely something we will need to watch out for.
Have we determined that performance is a problem? The
jsonrpcthing sounds like a good idea when doing multiple files at once, but inside of editors, I assume the use cases are just to do the single file or even only part of the file.kentcdodds commentedon Mar 6, 2017
Great way to do that is with find-up
lydell commentedon Mar 6, 2017
There's also read-pkg-up.
SimenB commentedon Mar 6, 2017
JetBrains IDE (IntelliJ, WebStorm etc) issue: https://youtrack.jetbrains.com/issue/WEB-25077
vjeux commentedon Mar 8, 2017
Previous discussions on the prettier repo: #753 #735
millermedeiros commentedon Mar 9, 2017
One thing I added o esformatter was the concept of a
rootconfig to avoid merging other settings and to speed up the lookup a little bit (as soon as it finds a config withroot: truedon't need to keep looking up the file tree).This is basically how I decided to implement it: millermedeiros/esformatter#76 (comment) - cli options always take precedence (assumes user is overriding the defaults on purpose)
The whole user config logic is handled by the
getRcmethod, I expose it so that plugins and other consumers can reuse it (see: source)The package version logic I do inside the
binfile using theresolvemodule - it starts looking for the locally installed module based on the config file (if user provides one) or thecwd.jsonrpcis a good idea (was going to do the same for esformatter but never found the time), I would implement it as a dedicated bin tho.bakkot commentedon Mar 25, 2017
Most JS I write does not live in an npm package, so this would do nothing for me.
I know that's kind of unusual, but I thought it worth mentioning - package.json is an npm thing, not a JS thing.
gustavnikolaj commentedon Mar 28, 2017
Instead of implementing a JSON RPC protocol through stdin/stdout you could also just create a programmatic API like the one eslint has.
Both eslint and standard-engine-based linters implement similar APIs. I wrote a plugin for integrating standard-engine linters into Atom which use this API to implement a worker to avoid the node.js startup cost on each lint.
It allows for the same separation as the stdin/stdout protocol, but is more flexible in my opinion, and probably a bit easier to do as well :-) It also won't rule out the stdin/stdout based solution, but rather be the first step to implement it.
SimenB commentedon Mar 28, 2017
@gustavnikolaj that's only useful for editors written in JS (so atom and code?), not for emacs, vim, intellij, webstorm, sublime etc.
And prettier already has a programatic API, so those integrations can be done today
gustavnikolaj commentedon Mar 28, 2017
@SimenB Oh sorry, I should have checked whether it was already there.
vjeux commentedon Mar 28, 2017
We also need it for atom. It has chrome flags enabled that prevent doing eval(), so you can't just require a js file found in package.json. What the current atom editor plugin does is to use a specific version, which isn't ideal since you may have multiple projects using different versions.
So the workaround is to shell out and use stdio
gustavnikolaj commentedon Mar 28, 2017
@vjeux
child_process.forkcan be used from within atom to sidestep the CSP that prevents use of eval. Had to do the same thing to be able to use the projects linter instead of bundling them in the plugin.kentcdodds commentedon Mar 28, 2017
Yeah, most atom packages that need to do this use
loophole. In fact,prettier-atomdoes this :)22 remaining items
AxelTheGerman commentedon May 11, 2017
@danawoodman would love to see exactly that!
chrisui commentedon May 25, 2017
Been mentioned already but I would push for using something such as cosmiconfig which implements the config formats commonly seen across the ecosystem. Imo it gives the most predictable way for writing and consuming configuration for tools in the js world.
From the README:
chrisui commentedon Jun 13, 2017
Is there anything we can do to help move configuration/editor support along?
We seem to be getting in a bit of a muddle moving around projects with tools using different prettier versions (global vs project vs tool/editor vs ci) and it's causing us some degree of pain.
chrisui commentedon Jun 13, 2017
I reckon we should be able to specify the prettier
"version"in the config (wherever it lives) so that if someone attempts to use prettier with the wrong version we can fail loudly. This is the same as happens with using the incorrect version offlowto what is specified in configuration.Ie.
chrisblossom commentedon Jun 14, 2017
I think it is fairly important to be able to flag subdirectories with a new prettier config/overrides because not all JS features are supported per directory, specifically
--trailing-comma all. The current Node LTS's (4 & 6) do not support this feature. If using it, at least part of your build chain will need an exception of some sort.IMO don't reinvent the wheel...copy how
eslintrc.jsworks. Nested.eslintrc.jsinherit from configs further down the line but overwrite any settings.vjeux commentedon Jun 14, 2017
Fyi, node 8 that was just released supports trailing comma!!
chrisblossom commentedon Jun 14, 2017
@vjeux Yeah it will be nice when it is LTS in October! nodejs/LTS#lts-schedule has Node 4 supported until April 2018 and Node 6 until April 2019.
Because Node's LTS, I don't think Prettier should adopt a configuration style that makes it more difficult to use until April 2019. I think the configuration decisions should follow the Node LTS schedule.
Here is my current configuration...
.prettierignorepackage.jsonBecause of the "no configuration" philosophy, there ends up being more hacks/workarounds getting
Prettierto work with a modern build setup. Not to mention the editor's plugin has no idea what config to use. End up having to runyarn run prettier:toolsafter saving any file in thetoolsdirectory if I don't commit right away otherwise the trailing comma will error node out.If anyone has any suggestion on how to better handle this I'd love to hear how.
josephfrazier commentedon Jun 14, 2017
@chrisblossom, since you're already using eslint, you could use eslint-plugin-prettier and eslint-config-prettier, add your prettier configuration to
.eslintrc.jsfiles, then have lint-staged runeslint --fixinstead ofprettier.rattrayalex commentedon Jul 6, 2017
I'm a pretty big +1 to allowing
.prettierconfig files, if for no other reason than that they function as great marketing on Github. But also because there are many times when there simply isn't a simpler way for people to use it.Sharing bash files or "always run it with args --x --y z" within a company really isn't wonderful, and package.json simply doesn't work in all cases.
Ciantic commentedon Jul 8, 2017
Is this thread mostly about agreeing to filename (e.g. .prettierrc / package.json) and format in it, like opts or JSON?
If so I think this should be decided by one maintainer, just do it.
There is already a cottage industry of these non-standards, one example from VSCode extension here:
This is not good! We need standard for this, such a simple thing should not be outsourced when in the end it's maintainers that decide these simple things.
rattrayalex commentedon Jul 8, 2017
@Ciantic yes, I am planning on doing exactly that this weekend 😄 and it seems @azz has the same idea
rattrayalex commentedon Jul 8, 2017
EDIT: moved a proposal regarding perf to #2434 (comment) as more relevant conversations are happening there.