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
Add the 'jsCustomTags' option #6626
base: main
Are you sure you want to change the base?
Add the 'jsCustomTags' option #6626
Conversation
Thanks. Just notes:
What about? {
"jsCustomTags": ["styled.div", "styled.keyframes", "from.mobile"]
}
Bonus: Also it can solve problems when we break code in tagged template expressions, after this PR it should be easy to fix, just setup In fact, we should consider whether we should support formatting template literal or not. Here two solutions:
/cc @prettier/core will be great to get some feedback |
Alternatively, why not try to format all tagged template literals nested to |
@evilebottnawi, but how would you distinguish between
@thorn0, that's an interesting idea indeed, but it would prevent me from doing: const responsiveStyles = media.mobile`
max-width: 90%;
`;
const MyComponent = styled.div`
max-width: 700px;
${responsiveStyles};
`; Personally I think that's confusing so I don't do it, but it's possible so I'm guessing it should be supported too. |
Maybe support also this:
@thorn0 yep, but here more fundamental problem, in future style-components can implement more helpers and for us it is not easy maintenance all supported tagged template literals |
@evilebottnawi sorry I didn't explain myself very well. My point is that in this example, we want to parse the content const MyComponent = styled.div`
max-width: 90%;
${from.mobile`
max-width: 700px;
`};
`; But let's assume that I create a DSL that allows to specify a TCP origin (weird example I know, but let's assume that we need a DSL for that and I write a prettier plugin so it can be properly parsed). We'd have: import { from } from 'my-custom-dsl';
const remote = from`
protocol ssh origin example.com port 22
`; And we'd want: import { from } from 'my-custom-dsl';
const remote = from`
protocol ssh
origin example.com
port 22
`; So, how will prettier know if it needs to format {
"jsCustomTags": {
"styled-components": ["from"]
}
} or {
"jsCustomTags": {
"my-custom-dsl": ["from"]
}
} |
hm, maybe we should allow to execute multiple plugins for extension (maybe already implemented, need tests) and you can implement an own sub plugin for DSL, also it is allow to us move all library specific code to sub own plugins |
I think it might be worth thinking about #5588 when designing this. |
We should merge some options to be a nested object IMO. I do have some issues on formatting HTML in |
I would like to see yaml tagged template support. Prettier already supports both languages standalone, so ideally this PR would be made more generic. |
Would this functionality be covered by #5588? |
This should already be covered by https://github.com/Sec-ant/prettier-plugin-embed? cc @fisker |
docs/
directory)CHANGELOG.unreleased.md
file following the template.✨Try the playground for this PR✨
==========================
Hello!
First of all I want to say that I realize that this PR will be controversial. I have read the opinion philosophy and #40.
This PR adds a new option
jsCustomTags
with the following form:I believe that this option is necesary and must make it into prettier (or any similar option that cover this use case). I understand that we want to keep prettier as opinionated as possible, but in this case this is not a matter of opinion, this is a matter of supporting an use case.
In our code, we use
styled-components
, and we have a custom helper to do media queries:However, in another one of our projects, we do:
We use
from
instead ofmedia
because our queries are mobile-first, and they are more descriptive that way. We are not the only ones to use this pattern: take a look at this article or this package.The point here is that we can't just let prettier decide that the only valid tag names for
styled-components
arestyled
andcss
. There's an open issue about supportingkeyframes
too (#5936), but I believe that opening a PR just forkeyframes
is missing the point. What we need to move towards is a customizable list of allowed tags, in whichstyled
andcss
are the default, but can also be extended.There is no standard way to define media queries in styled-components (neither in
styled-components
itself nor there is a well established standard in the community - like fsa for redux), and while there isn't, we can't have prettier decide which names are acceptable and which aren't.This not only applies to
styled-components
, but to any potential library that relies on tagged template strings likegraphql
; hence why I named the optionjsCustomTags
and notstyledComponentCustomTags
.Some final notes:
Thanks a lot for your time.