prettier: Consider removing default parser / Skip processing unsupported file types
Currently when
- Prettier is invoked from the CLI, and
- The file being formatted doesn’t match any of the extensions supported, and
- No
--parser
option is passed, and - No
parser
is in a prettier config file,
then it will fall back to the babylon parser and treat the file as JavaScript. This made sense when prettier only supported JS (and then TypeScript), but now that it supports vastly different languages perhaps it is time to challenge that default.
In #2882, we discovered this can have negative repercussions when used with --write **/*
, modifying files inside of a .git
directory.
If we were to change the default from JavaScript to a no-op, these kind of issues would go away, and we’d also have less issues reported along the lines of “My CSS doesn’t parse”, when they’re accidentally using the babylon parser.
It is already possible to associate other extensions with a parser in .prettierrc
.
{
"overrides": [{
"files": "*.mjs",
"options": { "parser": "babylon" }
}]
}
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 9
- Comments: 35 (29 by maintainers)
I consider this behavior a bug, not a feature, and by that logic, I think we should make this change without waiting for 2.0.
Let’s just ask the most popular ones.
The question is: If
prettier file/with/unknown.extension
printed a warning to stderr instead of trying to formatfile/with/unknown.extension
as JS, would that cause problems for editor integrations?I think it’s really important that we figure out a non-breaking way that fixes 80% of the cases as soon as possible, because lots of users are confused when Prettier tries to format HTML files, which sometimes succeeds and sometimes doesn’t, depending on if the HTML is valid JSX or not.
Could this work out, maybe?
If Prettier is told to format a file with an extension it does not recognize and the
parser
option has not been set (either in the CLI or in a config file), print a warning for that file (Skipping file with unknown extension .html: /home/lydell/foo/src/index.html
).We have to be really careful so we don’t break editor integrations that rely on the CLI such as vim-prettier (ping @mitermayer) though.
I also think we can consider this a bug and not wait for 2.0.
Hi! I’m the developer who wrote most of the code for the WebStorm integration. It would not be a problem for WebStorm, we use getSupportInfo to determine if the file is acceptable. Thank you so much for giving the heads up! In the future, feel free to @ - mention me also.
It wouldn’t be a problem for prettier-emacs. The users could conditionally add specific
--parser
options for custom or unsupported extensions.@robwise Do you specify the parser for unsaved draft documents also? You wouldn’t be able to pass a filepath for those even after #4341, so I’m wondering how you do it now. Do you use the editor scope? Maybe prettier needs to support editor scope for parser inference in addition to filepath?
In the same vein, @mitermayer @rocedo @undeadcat @madskristensen we are thinking about changing the JavaScript API to require passing either
parser
orfilepath
, otherwise the text will be passed through as-is.This will affect
prettier.format
,prettier.formatWithCursor
, andprettier.check
.Would this adversely affect you? Are you always specifying either parser or filepath?
Context: https://github.com/prettier/prettier/pull/4426#discussion_r186271892
I opened a PR that implements this change, I need some help implementing the error message that @lydell mentioned in https://github.com/prettier/prettier/issues/2884#issuecomment-385642962, but otherwise it is functional
Hi @lydell,
This is not a problem for
vim-prettier
as we always include the--parser
option. I am currently on the process of rewritting most of it for 1.0 release so changes like that should be ok since we will do a major release with other breaking changes.cc @docwhat
@undeadcat thanks so much for building it, it’s been a highly requested from people using prettier!
@lydell could this be implemented as built-in an HTML plugin, which simply does not do anything? I.e. it’ll parse the entire text into a syntax tree with a single node and simply print it back as is.