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

Allow backtick strings to avoid escaping single or double quotes #421

Closed
LinusU opened this issue Feb 16, 2016 · 11 comments
Closed

Allow backtick strings to avoid escaping single or double quotes #421

LinusU opened this issue Feb 16, 2016 · 11 comments

Comments

@LinusU
Copy link
Member

LinusU commented Feb 16, 2016

I just encountered an interesting situation in which I have to use both single quotes ' and double-quotes " in the same string. I decided that the easiest would be to wrap them in back-ticks ```.

const sql = `INSERT INTO "user" ("firstName", "lastName") VALUES('Linus', 'Unnebäck')`

This is however not acceptable by standard since "Strings must use singlequote.". But the rules on the front page clearly states that it okay when it is to avoid escaping:

Single quotes for strings – except to avoid escaping

Therefor I would consider this a bug, any thoughts?

@oskarhane
Copy link

I would use back-ticks in the query instead:

const sql = 'INSERT INTO `user` (`firstName`, `lastName`) VALUES("Linus", "Unnebäck")'

Would that be an option?
If not, then you should probably just wrap with single quotes and escape them within the query.

@LinusU
Copy link
Member Author

LinusU commented Feb 16, 2016

Postgres wants me to use double-quotes for identifier escaping. Also, there could be other occurrences when I want to use both single- and double-quotes in the same string.

then you should probably just wrap with single quotes and escape them within the query.

Than I'm not following the rules, and also it looks ugly :(

@dcousens
Copy link
Member

const sql = "INSERT INTO \"user\" (\"firstName\", \"lastName\") VALUES ('Linus', 'Unnebäck')"

@rstacruz
Copy link
Member

I've personally used that convention when both " and ' are needed in the string. backticks for strings are allowed by standard.

@LinusU
Copy link
Member Author

LinusU commented Feb 16, 2016

@dcousens Is that really better than this though?

const sql = 'INSERT INTO "user" ("firstName", "lastName") VALUES (\'Linus\', \'Unnebäck\')'

@feross
Copy link
Member

feross commented Feb 19, 2016

Yeah, I think this rule is being overly restrictive. I opened an eslint issue for this before releasing v6: eslint/eslint#5147

Ideally we could enforce this:

  • default rule – use single quotes
  • if string contains a single quote – use single quotes w/ escaping, or double quotes to avoid escaping
  • if string contains single and double quotes – use single quotes w/ escaping, or backticks to avoid escaping

There's an ongoing discussion about this and related issues here: eslint/eslint#5234 Weigh in there if you have opinions.

@feross feross changed the title Strings must use singlequote. Allow backtick strings to avoid escaping single or double quotes Feb 19, 2016
@feross
Copy link
Member

feross commented Apr 14, 2016

We should be able to work around this now: eslint/eslint#5661

@feross feross removed the blocked label Apr 14, 2016
@feross
Copy link
Member

feross commented Jul 13, 2016

This rule change (always allowing template literal strings) will be included in standard v8.

@yoshuawuyts
Copy link
Contributor

if string contains single and double quotes – use single quotes w/ escaping, or backticks to avoid escaping

Does that rule do this ^ ?

@feross
Copy link
Member

feross commented Jul 13, 2016

Does that rule do this ^ ?

No, the rule just always allows template literal strings. So, it's not perfect.

@yoshuawuyts
Copy link
Contributor

Ah ok, reasonable enough

@lock lock bot locked as resolved and limited conversation to collaborators May 10, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Archived in project
Development

No branches or pull requests

6 participants