-
-
Notifications
You must be signed in to change notification settings - Fork 174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: introduce option to force format extracted fragements #561
feat: introduce option to force format extracted fragements #561
Conversation
🦋 Changeset detectedLatest commit: 4c7077b The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
📊 Package size report No changes
Unchanged files
🤖 This report was automatically generated by pkg-size-action |
@@ -153,6 +153,22 @@ If you’re fixing large of amounts of previously unformatted code, consider tem | |||
} | |||
``` | |||
|
|||
- `forceFormatExtracted`: Enables formatting of extracted fragements, (default: `false`). | |||
ESLint supports processors that let you extract and lint JS fragments within a non-JS language, for example using `eslint-plugin-markdown` to extract and lint a code block inside a Markdown file. Usually, you don't want to have the fragement itself formatted by Prettier as the whole file | |||
might already be formatted anyway. This is why `eslint-plugin-prettier` ignores such fragements by default. However, for cases you want to have such extracted fragements formatted by Prettier you can use this option to force it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
However, for cases you want to have such extracted fragements formatted by Prettier you can use this option to force it.
So, the case is, you want to format js in markdown via prettier, but not the markdown file itself? cc @BPScott
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, sort of. Actually I'd like to be able to apply a different set of Prettier rules to code blocks in Markdown files resp. be able to individually handle those blocks (for example disabling Prettier for JS code blocks, while formatting other code blocks like JSON). This can be achieved via ESLint rules on Markdown files processed with eslint-plugin-markdown
. This set-up is working fine, but only if I'm able to disable the default behavior of eslint-plugin-prettier
with this newly introduced option to not skip those code blocks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Commented down below. I think there's ways of doing this without introducing a new option.
I was about to ask for an exact example of this case. Fortunately you've provided one in a comment already:
This change feels like a really really big footgun. A big aim of When we diverge we introduce the support burden of having to answer a bunch of support questions asking why formatting a file with prettier does one thing but eslint does another. This gets extra fun inside editors if you've got eslint and prettier plugins installed at the same time because now we have to explain potential for editor misconfiguration too.
If you're trying to do something particularly funky that truly can not be resolved by leveraging existing config, honestly I'd rather you fork I suspect we might be able to get your desired state another way without adding a config option.
You should be able to add the following override to your eslint config to disable prettier in those js blocks within markdown files: This would mean that those blocks would be not overrides: [
{
// 1. Target ```js code blocks in .md files.
files: ["**/*.md/*.js"],
rules: {
"prettier/prettier": "off",
}
}
] Alternatively you can prefix the fenced codeblocks that you don't want to format with prettier with a ignore comment to get prettier to skip it:
This is almost certainly the most idiomatic approach, which would result in the same behaviour when you run the file through both Please try one of the two above approaches. The latter certainly will work, and I believe that you should go with that approach instead of instead of us adding a new option to |
close as above comment |
The default behavior skipping the formatting of extracted fragments (e.g. code blocks in Markdown files, parsed via
eslint-plugin-markdown
) is usually fine.eslint-plugin-prettier/worker.js
Lines 127 to 150 in 00449df
However, there are cases where you want to have such fragments formatted anyways.
This pull request introduces the new option
forceFormatExtracted
(happy to change the naming) which turns this particular check off.