Skip to content
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

escaped $ and { characters are not handled correctly #45

Closed
G-Rath opened this issue Jul 13, 2023 · 3 comments · Fixed by #46
Closed

escaped $ and { characters are not handled correctly #45

G-Rath opened this issue Jul 13, 2023 · 3 comments · Fixed by #46
Labels
status: accepting prs Please, send a pull request to resolve this! type: bug Something isn't working :(

Comments

@G-Rath
Copy link
Contributor

G-Rath commented Jul 13, 2023

Follow on from #2 - currently dedent does not treat $ or { as special when determining what it should do with escaped characters:

dedent`\$` === '\\$'
dedent`\{` === '\\{'
@JoshuaKGoldberg
Copy link
Collaborator

🤔 maybe this is me not understanding template literal strings well, but I don't follow why that should be the case. The dedent function isn't doing anything special with escaping special characters such as $ and {. Can you help me explain why the behavior should be changed?

@JoshuaKGoldberg JoshuaKGoldberg added the status: waiting for author Needs an action taken by the original poster label Jul 14, 2023
@G-Rath
Copy link
Contributor Author

G-Rath commented Jul 14, 2023

Because otherwise you can't dedent strings with ${ i.e. https://github.com/jest-community/eslint-plugin-jest/blob/02ec945f0794949ce38a11addb0ef3ceafa1aed2/src/rules/__tests__/no-identical-title.test.ts#L108-L113

    dedent`
      describe("foo", () => {
        it(\`ignores $\{someVar} with the same title\`, () => {});
        it(\`ignores $\{someVar} with the same title\`, () => {});
      });
    `.replace(/\\\{/u, '{'),

The replace is the workaround for this - note that I don't have to do it for backticks because that was already addressed in #2.

Technically the best form would be detecting that it's ${, but I think it's reasonable to say that special template string characters should be handled like this because you can otherwise just add a second \ i.e. say I was testing a JS parser and that it didn't pick up escaped interpolation:

    dedent`
      describe("foo", () => {
        it(\`ignores $\\{someVar} with the same title\`, () => {});
        it(\`ignores $\\{someVar} with the same title\`, () => {});
      });
    `

@meowtec
Copy link

meowtec commented Jul 22, 2023

My workaround be like:

dedent`
  export function Demo({ active }) {
    return (
      <div
        className={\`demo $${''}{active ? 'active' : ''}\`}
      ></div>
    )
  }
`

It is ugly

@JoshuaKGoldberg JoshuaKGoldberg added type: bug Something isn't working :( status: accepting prs Please, send a pull request to resolve this! and removed status: waiting for author Needs an action taken by the original poster labels Jul 25, 2023
JoshuaKGoldberg added a commit that referenced this issue Jul 27, 2023
These characters are special in template string literals, so it should
be assumed if they're escaped it's to avoid them being interpolated.

If it desired to have the escaping slash for these characters, then an
escaped slash can be used.

Without this it's not possible to dedent strings that reassemble
template string literal interpolations without a follow-up
transformation (i.e. calling `.replace` or similar).

Resolves #45

---------

Co-authored-by: Josh Goldberg <git@joshuakgoldberg.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: accepting prs Please, send a pull request to resolve this! type: bug Something isn't working :(
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants