sindresorhus/eslint-plugin-unicorn

Rule proposal: `prefer-regexp-code-point-escape`

Closed

#989 opened on Jan 2, 2021

 (11 comments) (5 reactions) (0 assignees)JavaScript (468 forks)user submission
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.

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}';

Contributor guide