question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

template string bad formatting example

See original GitHub issue

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
jlongstercommented, Feb 20, 2017

Interesting, yeah we can play with that! Note that this is the output with this PR:

const Test = styled.div`
  ${customCss => customCss.map(({ selector, key, value }) => `
      ${selector} {
          ${key}: ${value};
      }`)}
`;

The thing that’s hard about this is that it’s by definition taking the original user’s formatting into account because they are in a string. So the number of spaces to indent the line starting with ${selector} is just going to be blindy copied. Hopefully #749 helps with this because it won’t break things up for you anymore, and users just sometimes have to readjust things. But it makes getting this right automatically almost impossible.

1reaction
clempatcommented, Mar 1, 2017

For me it would be enough. But would that mean that any template string use in other context than styled-component would have the current formatting ?

Looks like the formatting rules applied otherwhere should not be the same as inside template string variable. so for exemple “&&” does not trigger line.

For the indent it is true that it is not the same as the text out of js should keep the indent.

For @gaelduplessix example it is funny to see how it act when you take

const Test = styled.div`
${(customCss) => customCss.map(
  ({ selector, key, value }) => `
    ${selector} {
        ${key}: ${value};
    }`
)}
`

outputs

const Test = styled.div`
${customCss => customCss.map(({ selector, key, value }) => `
    ${selector} {
        ${key}: ${value};
    }`)}
`;

https://jlongster.github.io/prettier/#{"content"%3A"const Test %3D styled.div`\n%24{(customCss) %3D> customCss.map(\n ({ selector%2C key%2C value }) %3D> `\n %24{selector} {\n %24{key}%3A %24{value}%3B\n }`\n)%7D%5Cn%60%22%2C%22options%22%3A%7B%22printWidth%22%3A80%2C%22tabWidth%22%3A2%2C%22singleQuote%22%3Afalse%2C%22trailingComma%22%3Afalse%2C%22bracketSpacing%22%3Atrue%2C%22jsxBracketSameLine%22%3Afalse%2C%22doc%22%3Atrue%7D%7D

and just adding the tab on the string template bloc so vs:

const Test = styled.div`
  ${(customCss) => customCss.map(
    ({ selector, key, value }) => `
      ${selector} {
          ${key}: ${value};
      }`
  )}
`

outputs

const Test = styled.div`
  ${customCss => customCss.map(({ selector, key, value }) => `
      ${selector} {
          ${key}: ${value};
      }`)}
`;

https://jlongster.github.io/prettier/#{"content"%3A"const Test %3D styled.div`\n %24{(customCss) %3D> customCss.map(\n ({ selector%2C key%2C value }) %3D> `\n %24{selector} {\n %24{key}%3A %24{value}%3B\n }`\n )%7D%5Cn%60%22%2C%22options%22%3A%7B%22printWidth%22%3A80%2C%22tabWidth%22%3A2%2C%22singleQuote%22%3Afalse%2C%22trailingComma%22%3Afalse%2C%22bracketSpacing%22%3Atrue%2C%22jsxBracketSameLine%22%3Afalse%2C%22doc%22%3Atrue%7D%7D

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python String Formatting Best Practices
Learn the four main approaches to string formatting in Python, as well as their strengths and weaknesses. You'll also get a simple rule...
Read more >
Template literals (Template strings) - JavaScript | MDN
Template literals are literals delimited with backtick (`) characters, allowing for multi-line strings, string interpolation with embedded ...
Read more >
advanced string formatting vs template strings - Stack Overflow
Normal format strings will raise an error if a placeholder is not passed a value. For example: "Hello, {first} {last}".format(first='Joe').
Read more >
Out-of-context string template is an anti-pattern | Hackle's blog
Format and the string template turns out to be a pretty poor idea. string templates are not magic strings. String templates do carry...
Read more >
Comprehensive Guide to Java String Format in 2021 - DZone
For example, we can write a formatted String to standard error using the System.err.format method. Note that the PrintStream class also provides ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found