I found that prettier will wrap my jsx attributes which has more than one template literal to new line.
For examples in my jsx file:
// before formatted and expected result
<DemoFrame html={`${demoPath}/frame1/index.html`} css={`${demoPath}/frame1/index.min.css`} />
// formatted
<DemoFrame
html={`${demoPath}/frame1/index.html`}
css={`
${demoPath}/frame1/index.min.css
`}
/>
But these were not changed
<CodeChunk path={`${demoPath}/frame1/index.html`} lang="html" />
<DemoFrame html={`${demoPath}/frame1/index.html`} css={`/frame1/index.min.css`} />
<DemoFrame html={`${demoPath}/frame1/index.html`} css={demoPath + `/frame1/index.min.css`} />
I had tried adding these two lines to settings.json
but not working.
"prettier.printWidth": 2000,
"prettier.singleAttributePerLine": false,
2
Answers
Seems that prettier treat
css
attribute as embed css and try to format the template literals.So rename
css
to other name can avoid the issue.I can also disable formatting of embedded code in this workspace by adding
"prettier.embeddedLanguageFormatting": "off"
to.vscode/settings.json
In your example for the ones that didn’t get formatted, try swapping the usage of template literals in
html
andcss
values, for example:If you rename attribute from
css
to any other name (for examplecssHref
), it doesn’t get broken into multiple lines (doesn’t matter how many of your attribute values use template literals). I couldn’t find why this behaviour is baked into prettier, but hope this helps anyway.