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
Vue: Support custom blocks #8023
Vue: Support custom blocks #8023
Conversation
I guess we still can't parse this <custom lang="babel">
const foo = "</";
</custom> |
@sosukesuzuki prettier/src/language-html/utils.js Line 364 in 5569e33
maybe also both |
a2a451e
to
5dc4bb7
Compare
src/language-html/printer-html.js
Outdated
for (let i = 0; i < parsers.length && !printed; i++) { | ||
const parser = parsers[i]; | ||
try { | ||
printed = textToDoc(node.value, { parser }); |
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.
I don't like the idea guessing languages, people even complaining about auto format with known language.
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.
I don't like this too, but in the real world custom blocks are often used without attributes.
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.
In that way, why don't we ignore it. Want format? Tell me what it is.
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.
I think formatting for custom blocks is needed. For example, eslint-plugin-gridsome and eslint-plugin-prettier-vue use Prettier to format custom blocks.
Could you tell me what is the reason for ignoring 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.
I'm not saying disable formatting custom blocks.
I'm worried about wrong results.
We should ignore
<docs></docs>
But format
<docs lang="markdown"></docs>
Maybe with some exception like <page-query>
known as graphql
block, just like <script>
in html we assume that's javascript
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.
I understood. What do you think about adding options for custom blocks like eslint-plugin-prettier-vue? (I'm reluctant to add options)
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.
I'll remove guessing languages for now. At least for this PR, it only supports custom blocks using the lang / type attribute.
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.
👍 , I guess we can use --embedded-language-formatting
later?
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.
sounds good.
src/document/doc-utils.js
Outdated
function stripTrailingHardline(doc) { | ||
// HACK remove ending hardline, original PR: #1984 | ||
if (doc.type === "concat" && doc.parts.length !== 0) { | ||
const lastPart = doc.parts[doc.parts.length - 1]; | ||
const parts = getInnerParts(doc); | ||
const lastPart = parts[parts.length - 1]; | ||
if (lastPart.type === "concat") { | ||
if ( | ||
lastPart.parts.length === 2 && | ||
lastPart.parts[0].hard && | ||
lastPart.parts[1].type === "break-parent" | ||
) { | ||
return { type: "concat", parts: doc.parts.slice(0, -1) }; | ||
return { type: "concat", parts: parts.slice(0, -1) }; |
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.
Potential breaking change for plugins
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.
I think we can implement the withInnerParts
argument (and maybe put todo to change it on default for prettier@3)
/cc @thorn0
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.
Are doc-utils
considered part of the public plugin API?
c241784
to
6ca2f96
Compare
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.
Looks good, but want some feedback from @thorn0 about changes in doc-utils
Just want let you know, I tried use |
Prettier pr-8023 --parser vue Input: <custom lang="zzz">
const foo = "foo";
const foo = "foo";</custom> Output: <custom lang="zzz"> const foo = "foo";
const foo = "foo";>
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
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.
- Vue: Support custom blocks #8023 (comment)
lang="babel"
should not be recognized.lang="javascript"
should.babel
is not a language, it's Prettier's implementation detail. Check how the language recognition is implemented for code blocks in Markdown.--vue-indent-script-and-style
shouldn't affect custom block whose language wasn't recognized.
Prettier pr-8023 --parser vue Input: <custom lang="json">{
"a": 1
}</custom>
<custom lang="json">{
"a": 1
}
</custom> Output: <custom lang="json"
{
"a": 1
}
>
<custom lang="json"
{
"a": 1
}
</custom>
|
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.
-
Blocks with unknown languages must be preserved as is.
Prettier pr-8023
Playground link--parser vue
Input:
<custom lang="zzz">123</custom>
Output:
<custom lang="zzz"> 123 </custom>
-
Please merge
master
or rebase to see how this will work with--embedded-language-formatting=off
.
I'm busy this week, so I'll continue working on it over the weekend. |
7d5e818
to
afc96f6
Compare
I worked on this because it wasn't as busy as I thought it would be. |
This comment has been minimized.
This comment has been minimized.
What about this point? Why can that code be parsed in |
@thorn0 Probably, we cannot fix it from Prettier side. angular-html-parser seems to parse |
85d4cec
to
ba61e74
Compare
This reverts commit f65c7cd.
1064458
to
d7be06c
Compare
This improves formatting custom blocks in Vue SFC.(see #5448 and #5458)
If there is
lang
attribute, the value oflang
is passed toparser
option.(If the value oflang
is unknown, no formatting is done)real world custom blocks examples:
How do you think?
changelog_unreleased/*/pr-XXXX.md
file followingchangelog_unreleased/TEMPLATE.md
.✨Try the playground for this PR✨