-1

Escaping non-special characters in strings, template literals, and regular expressions doesn't have any effect

Source: https://eslint.org/docs/rules/no-useless-escape

But clearly this is wrong:

enter image description here

There's nothing to "fix" here; we all know this is a perfectly valid sequence of characters.

Joking aside what's the rationale behind this?

If even ESLint acknowledges that there's no harm then why bother? It may be useless if you only look at the end result (I know that the \ character won't be printed out) but in a "code is data" context, this may be useful data when doing syntax analysis.

ESLint makes a judgment on what goes into the string, yet it won't budge with this code: (and that's pretty useless to me too)

var x = 10;
var y = x + 0;
var z = y * 1;
customcommander
  • 17,580
  • 5
  • 58
  • 84
  • 1
    Just log the string and you will see the problem – Andreas May 13 '20 at 18:22
  • @Andreas I know what the “problem” is. that’s not the point. – customcommander May 13 '20 at 18:41
  • 1
    The escape character is useless and will most likely change the content of the string in an unwanted way. So why should this not be objected to? – Andreas May 13 '20 at 18:45
  • @Andreas I'm not sure the content is the concern here, `'\a'.length` is 1 not 2. I'd be fine if the error message was saying something like _"Did you mean to escape this character?"_ or _"You may need to escape \ as it won't be printed out"_. – customcommander May 13 '20 at 19:05
  • 2
    @customcommander — Umm. So you can understand the purpose but you just wanted to complain that the error message isn't cuddly enough? – Quentin May 13 '20 at 19:08
  • @customcommander this linting rule is helping programmers, it doesn't avoid any error, as there is no error. Please take a look at my answer. – Robo Robok May 13 '20 at 19:08
  • Of I see code with a string like that, I need to think if it should have been "\\", anything that slows down another programmer reading your code is a problem. – Ian Ringrose Jul 06 '20 at 21:45

2 Answers2

5

If the escape character isn't doing anything useful, what is it doing there?

Perhaps the writer of the code wanted a literal backslash character. The error draws attention to it so they can replace \ with \\.

Perhaps they thought the next character needed escaping. This educates them otherwise.

Perhaps it is just a typo.

Since there is no good reason to have the \ there, there can only be bad reasons. Some are more seriously bad than others, but the linter will draw attention to them.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Fair enough. I guess I just wasn’t satisfied with the error message. Pull requests welcome indeed. – customcommander May 13 '20 at 19:18
  • 1
    The error message is perfect though. I think you need to get mindset of a programming perfectionist to understand the purpose of linting rules like this one. – Robo Robok May 13 '20 at 19:19
0

Some linting rules are meant to make your code perfect.

You can escape characters if you want to, but sometimes it doesn't have any effect. If you still do that, you just put a meaningless character in your code, confusing other programmers and wasting 1 byte. Your code escapes the underscore character with \_ sequence, whereas \ doesn't do anything in that sequence.

This lint rule tells you about that. It's not the end of the world, but using that lint rule, you wanted to know.

Robo Robok
  • 21,132
  • 17
  • 68
  • 126