Skip to content

Typescript types with tagged template literals (TSX) result in awkward formatting #5697

Closed

Description

@inlightmedia

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.

Activity

  1. j-f1 commented on Dec 30, 2018

    @j-f1
    Member

    When run on the TypeScript parser, there’s a different error:

    Prettier 1.15.3
    Playground link

    --parser typescript
    

    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:

    Error: Couldn't insert all the expressions
        at transformCssDoc (https://prettier.io/lib/standalone.js:21481:11)
        at Object.embed$4 [as embed] (https://prettier.io/lib/standalone.js:21321:18)
        at Object.printSubtree (https://prettier.io/lib/standalone.js:15712:31)
        at callPluginPrintFunction (https://prettier.io/lib/standalone.js:15829:29)
        at https://prettier.io/lib/standalone.js:15794:16
        at Object.printComments (https://prettier.io/lib/standalone.js:15528:17)
        at printGenerically (https://prettier.io/lib/standalone.js:15793:22)
        at FastPath.call (https://prettier.io/lib/standalone.js:15639:16)
        at printPathNoParens (https://prettier.io/lib/standalone.js:24805:91)
        at Object.genericPrint$3 [as print] (https://prettier.io/lib/standalone.js:23169:28)
    
  2. added
    priority:high
    lang:css/scss/less
    area:multiparser
    lang:javascript
    on Dec 30, 2018
  3. j-f1 commented on Dec 30, 2018

    @j-f1
    Member
  4. inlightmedia commented on Dec 30, 2018

    @inlightmedia
    Author

    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:

    {
        "arrowParens": "avoid",
        "bracketSpacing": true,
        "htmlWhitespaceSensitivity": "css",
        "insertPragma": false,
        "jsxBracketSameLine": false,
        "jsxSingleQuote": false,
        "parser": "babylon", // works when I change it to "parser": "typescript"
        "printWidth": 80,
        "proseWrap": "always",
        "requirePragma": false,
        "semi": true,
        "singleQuote": true,
        "tabWidth": 4,
        "trailingComma": "all",
        "useTabs": false
    }
    
  5. lydell commented on Dec 30, 2018

    @lydell
    Member
  6. j-f1 commented on Dec 30, 2018

    @j-f1
    Member

    I still think we should fix this bug.

  7. inlightmedia commented on Dec 31, 2018

    @inlightmedia
    Author

    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.

    ${(props) =>
    	props.center
    	        ? ` display: flex;
                        align-items: center;
                        justify-content: center;
                        text-align: center;`
    		:   ''
    }; <----------- THIS MISSING SEMICOLON WAS CAUSING THE ERROR
    @media (max-width: ${(props) => (props.noBreakPoint ? '0' : constants.layout.breakpoint.break1)}px) {
    	font-size: 2em;
    }
    
  8. duailibe commented on Jan 14, 2019

    @duailibe
    Collaborator

    @inlightmedia There are three separate issues here:

    1. Your .ts and .tsx files weren't being parsed with the TypeScript parser because you had "parser": "babylon" in your .prettierrc file. 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.
    2. Babel doesn't support types in tagged template literals. We should check upstream if that has been fixed and update the parser.
    3. The CSS had a missing semicolon. Except on the playground, Prettier doesn't error when it fails to parse a CSS expression inside a JS/TS file, it just print them as it was originally written.
  9. duailibe commented on May 24, 2019

    @duailibe
    Collaborator
  10. thorn0 commented on May 28, 2019

    @thorn0
    Member

    It does parse it if you use @babel/plugin-syntax-typescript. Not sure why this plugin isn't included to @babel/preset-typescript. See babel/babel#8117 & babel/website#2019

  11. Jessidhia commented on May 29, 2019

    @Jessidhia

    As you found, it is more of a problem with the REPL; @babel/preset-typescript will only insert the syntax plugin if it thinks the extension is .tsx or .ts, but the REPL doesn't have it.

  12. added
    locked-due-to-inactivity
    on Aug 6, 2020
  13. locked as resolved and limited conversation to collaborators on Aug 6, 2020
  14. added theissue type on Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:multiparserlang:css/scss/lesslang:javascriptlocked-due-to-inactivitypriority:high

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions