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

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

Projects

None yet

6 participants

@LinusU
Collaborator
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

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
Collaborator
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
Collaborator
const sql = "INSERT INTO \"user\" (\"firstName\", \"lastName\") VALUES ('Linus', 'Unnebäck')"
@dcousens dcousens added the question label Feb 16, 2016
@rstacruz
Collaborator

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

@LinusU
Collaborator
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
Owner
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 from Strings must use singlequote. to Allow backtick strings to avoid escaping single or double quotes Feb 19, 2016
@Chun-Yang Chun-Yang referenced this issue in poetic/reacterminator Mar 6, 2016
Closed

/test/intergration ignored for linting #1

@feross
Owner
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
Owner
feross commented Jul 13, 2016

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

@yoshuawuyts
Collaborator

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

Does that rule do this ^ ?

@feross
Owner
feross commented Jul 13, 2016

Does that rule do this ^ ?

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

@feross feross added a commit to feross/eslint-config-standard that closed this issue Jul 13, 2016
@feross Allow backtick strings to avoid escaping single or double quotes fcdb4dd
@feross feross referenced this issue Jul 13, 2016
Closed

Release proposal: standard v8 #564

16 of 16 tasks complete
@yoshuawuyts
Collaborator

Ah ok, reasonable enough

@saadq saadq referenced this issue in nodejs/nodejs.org Aug 25, 2016
Merged

Update dependencies #873

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment