fimbullinter / wotan

Pluggable TypeScript and JavaScript linter
Apache License 2.0
283 stars 23 forks source link

add rule to detect useless escapes #203

Open ajafff opened 6 years ago

ajafff commented 6 years ago

In the git history of trailing-newline.ts you can find a mistake where I wrote '\UFEFF' instead of \uFEFF. The rule should check every string and template literal for escaped characters that are useless. Some background on escape sequences: https://mathiasbynens.be/notes/javascript-escapes

It doesn't need to check for useless escapes in regular expressions.

It should allow '\${' and `\${` only if followed by { to allow for escaping template literal substitutions (or suppress TSLint's no-invalid-template-strings).

Note that this information is not present in the AST node, so you need to use node.getText() to get the raw source text.

The autofixer could just remove the backslash.

ajafff commented 6 years ago

The rule should probably exclude tagged template literals, because the tag function can actually access the content of the original escape and may use that for some strange magic. https://tc39.github.io/proposal-template-literal-revision/

ajafff commented 6 years ago

Octal escapes in string literal types are not parsed, the escape is therefore useless. But that may also just be a bug: https://github.com/Microsoft/TypeScript/issues/24209