Repository navigation
Closed
Copy link
Labels
area:multiparserIssues with printing one language inside another, like CSS-in-JSlang:css/scss/lessIssues affecting CSS, Less or SCSSlang:javascriptIssues affecting JSlocked-due-to-inactivityPlease open a new issue and fill out the template instead of commenting.priority:highCode is printed in a way that alters the AST, breaks syntax, or is a significant regression. Urgent!
Description
Prettier 1.15.3
Playground link
--parser babylon
Input:
const StyledH1 = styled.div<StyledComponentProps>`
font-size: 2.5em;
font-weight: ${(props) => (props.strong ? 500 : 100)};
font-family: ${constants.text.displayFont.fontFamily};
letter-spacing: ${(props) => (props.light ? '0.04em' : 0)};
color: ${(props) => props.textColor};
${(props) =>
props.center
? ` display: flex;
align-items: center;
justify-content: center;
text-align: center;`
: ''} @media (max-width: ${(props) => (props.noBreakPoint ? '0' : constants.layout.breakpoint.break1)}px) {
font-size: 2em;
}
`;
Output:
const StyledH1 =
styled.div <
StyledComponentProps >
`
font-size: 2.5em;
font-weight: ${props => (props.strong ? 500 : 100)};
font-family: ${constants.text.displayFont.fontFamily};
letter-spacing: ${props => (props.light ? "0.04em" : 0)};
color: ${props => props.textColor};
${props =>
props.center
? ` display: flex;
align-items: center;
justify-content: center;
text-align: center;`
: ""} @media (max-width: ${props =>
props.noBreakPoint ? "0" : constants.layout.breakpoint.break1}px) {
font-size: 2em;
}
`;
Expected behavior:
My issue is with the very first line where it multi-lines the angled brackets.
const StyledH1 = styled.div<StyledComponentProps>`
font-size: 2.5em;
font-weight: ${(props) => (props.strong ? 500 : 100)};
font-family: ${constants.text.displayFont.fontFamily};
letter-spacing: ${(props) => (props.light ? '0.04em' : 0)};
color: ${(props) => props.textColor};
${(props) =>
props.center
? ` display: flex;
align-items: center;
justify-content: center;
text-align: center;`
: ''} @media (max-width: ${(props) => (props.noBreakPoint ? '0' : constants.layout.breakpoint.break1)}px) {
font-size: 2em;
}
`;
This happens in ts and tsx files.
Metadata
Metadata
Assignees
Labels
area:multiparserIssues with printing one language inside another, like CSS-in-JSlang:css/scss/lessIssues affecting CSS, Less or SCSSlang:javascriptIssues affecting JSlocked-due-to-inactivityPlease open a new issue and fill out the template instead of commenting.priority:highCode is printed in a way that alters the AST, breaks syntax, or is a significant regression. Urgent!
Activity
j-f1 commentedon Dec 30, 2018
When run on the TypeScript parser, there’s a different error:
Prettier 1.15.3
Playground link
Input:
Output:
j-f1 commentedon Dec 30, 2018
Also reproduces with plain JS and no type cast.
inlightmedia commentedon Dec 30, 2018
It looks like my issue was with my .prettierrc. I was not using the typescript parser. I had read that the babylon parser worked with typescript. I guess that was not accurate. Once I switched to the typescript parser and reloaded the VSCode window (type cmd+shift+p then type "Reload Window" into the prompt and hit enter) it worked! Thanks @j-f1!
My .prettierrc file:
lydell commentedon Dec 30, 2018
Actually, it's better not to specify
parserlike that in your .prettierrc at all.j-f1 commentedon Dec 30, 2018
I still think we should fix this bug.
inlightmedia commentedon Dec 31, 2018
There should be a semi-colon before the @media query and after the fat arrow function passing in the prop. This was causing the error. A syntax error. The VSCode parser didn't error out though.
duailibe commentedon Jan 14, 2019
@inlightmedia There are three separate issues here:
"parser": "babylon"in your.prettierrcfile. But instead of changing it to"typescript", you should not set this option at all, like @lydell said. Prettier will infer the parser using the filename extension.duailibe commentedon May 24, 2019
FTR as of today, Babel still doesn't parse this (v7.4.5)
thorn0 commentedon May 28, 2019
It does parse it if you use
@babel/plugin-syntax-typescript.Not sure why this plugin isn't included toSee babel/babel#8117 & babel/website#2019@babel/preset-typescript.Jessidhia commentedon May 29, 2019
As you found, it is more of a problem with the REPL;
@babel/preset-typescriptwill only insert the syntax plugin if it thinks the extension is.tsxor.ts, but the REPL doesn't have it.