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

removal of backslashes in string. #2789

Closed
zeroEvidence opened this issue Sep 11, 2017 · 10 comments
Closed

removal of backslashes in string. #2789

zeroEvidence opened this issue Sep 11, 2017 · 10 comments
Labels
locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. type:question Questions and support requests. Please use Stack Overflow for them, not the issue tracker.

Comments

@zeroEvidence
Copy link

Prettier Version: 1.5.2 & 1.6.2

Code
No playground, couldn't figure out how to get unique link for my sample.

const decimalNumber = XRegExp('\d\.\d{3}', 'i');

Expected behavior:

const decimalNumber = XRegExp('\d\.\d{3}', 'i');

Actual behavior:

const decimalNumber = XRegExp('d.d{3}', 'i');
@ikatyang
Copy link
Member

Hi, those backslashes does nothing there:

image

I'm not sure if you're attempting to write regex string (should use double backslashes, e.g. '\\d\\.\\d{3}') or want to preserve those unnecessary backslashes?

@ikatyang
Copy link
Member

For the playground link, all you have to do is to copy its url :)

image

@vjeux vjeux closed this as completed Sep 11, 2017
@ikatyang ikatyang added type:question Questions and support requests. Please use Stack Overflow for them, not the issue tracker. and removed Needs More Information labels Sep 14, 2017
@greypants
Copy link

greypants commented Apr 24, 2018

@ikatyang this is still a legit issue inside of template literals.

I'm using a markdown tagged template literal to output some documentation, which happens to include the syntax for string interpolation (${}). I need to escape those characters so they're rendered directly instead of interpolated.

Before

markdown`
  const cssString = css\`
    background-color: \$\{color('base')\}
  \`;
`

But prettier is removing the escape backslashes, which causes the content to be interpolated.

Prettier Output:

markdown`
  const cssString = css\`background-color: ${color('base')}\`;
`;

Playground link

@ikatyang
Copy link
Member

ikatyang commented Apr 24, 2018

Can you open a new issue for that? It seems unrelated to this issue, thanks!

EDIT: --> #4377

@ndac-todoroki
Copy link

We fell in the similar problem.
When using React Router <Route>, it allows to pass strings which are:

Any valid URL path that path-to-regexp understands.

For example, we pass a string <Route exact path="/path/:id(\d+)" /> (It seems Prettier don't remove backslashes here).

Later in our code, we take out the props.match.path which shows to be the same string as above, and want to perform a switch with other path strings, which are all in an Enum. The deletion happens here.

enum Paths {
  Root = "/",
  Users = "/users",
  UserShow = "/users/:id(\d+)", // <- Prettier deletes the backslash here
}

I wish this could be prevented, because switch matches by the string as-is, not by its regex meanings.

@j-f1
Copy link
Member

j-f1 commented May 7, 2018

@ndac-todoroki although the \d in the JSX string is interpreted as the characters \ and d, the \d in the regular string is just interpreted as d, and Prettier removes the now-unnecessary escape. You’ll find that even without running Prettier, the UserShow route won’t work — it’ll actually match /users/ddddddd. Changing it to /users/:id(\\d+) should fix the issue.

@ndac-todoroki
Copy link

You were right! Some how I didn't notice it wasn't working :(

@stclairdaniel
Copy link

stclairdaniel commented May 14, 2018

I'm having a similar issue - I have a string like this:
"matchRegexp:(^$)|(^[^!#$%&'\"()*+,/:;<=>?@[\]^{|}~\\]+$)"
and after saving with Prettier, it changes to:
"matchRegexp:(^$)|(^[^!#$%&'\"()*+,/:;<=>?@[]^{|}~\\]+$)"

Note the missing backslash before the close brackets inside the character class. The backslash is being used to escape the close bracket. Without it, the close bracket will be interpreted as the end of the character class. I would just //prettier-ignore it, but I also need to //eslint-disable and I'm not sure how to do both for a single line.

@j-f1
Copy link
Member

j-f1 commented May 14, 2018

@stclairdaniel You need two slashes for that to work as you’d expect, and four where you currently have \\.

@stclairdaniel
Copy link

Sure enough! I couldn't tell if it was user error or not - thanks for the help.

@lock lock bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Aug 13, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Aug 13, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. type:question Questions and support requests. Please use Stack Overflow for them, not the issue tracker.
Projects
None yet
Development

No branches or pull requests

7 participants