sindresorhus/eslint-plugin-unicorn
Rule proposal: `prefer-regexp-code-point-escape`
Closed
#989 opened on Jan 2, 2021
help wantednew rule
Repository metrics
- Stars
- (5,022 stars)
- PR merge metrics
- (Avg merge 1d 16h) (399 merged PRs in 30d)
Description
Unicode code point escapes are new in ES6. They support more bits than older escapes and it's better to always use them for consistency, even when they're not required.
- https://exploringjs.com/es6/ch_unicode.html#sec_escape-sequences
- https://mathiasbynens.be/notes/javascript-escapes#unicode-code-point
This overlaps with the no-hex-escapes rule. Not sure whether we should deprecate that one. It's plausible that someone wants to prevent Hex escapes, but not prefer code point escapes. Opinions welcome.
Inspired by https://github.com/eslint/eslint/issues/12488.
Fail
const foo = '\123'; // Octal
const foo = '\cA'; // Control escape sequence
const foo = '\x7A'; // Hex
const foo = '\u2661'; // Unicode escape sequence
const foo = '\uD83D\uDCA9'; // Unicode surrogate pair
Pass
const foo = '\u{7A}';
const foo = '\u{1F4A9}';