ota-meshi / eslint-plugin-regexp

ESLint plugin for finding regex mistakes and style guide violations.

Home Page:https://ota-meshi.github.io/eslint-plugin-regexp/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

A new option for `prefer-lookaround`

bhsd-harry opened this issue · comments

commented

Motivation
Lookbehind assertions are not yet implemented by Safari, and they cannot be transpiled by commonly used transpilers either. Therefore, it will be great to have an option such as lookaheadOnly.

Description
When setting "lookaheadOnly" (or {lookaheadOnly: true}), the rule will neither report nor fix regular expressions that can be converted to lookbehind assertions.

Examples

I apologize if the examples below are invalid, but I wish my proposal is clear to you.

// eslint-enable regexp/prefer-lookaround: ["error", "lookaheadOnly"]
/* ✓ GOOD */
"ab".replace(/(a)b/, "$1c");

/* ✗ BAD */
"ab".replace(/a(b)/, "c$1");

WebKit recently (2 weeks ago) merged a lookbehind PR WebKit/WebKit#7109 and fixed their 5-year-old lookbehind issue. With a bit of luck, Safari will support lookbehinds in a few months. The release note for 16.3 beta don't mention this feature, so it might get released in 16.4.

That being said, it's still worth it to add a lookaheadOnly: boolean or lookbehind: boolean option to support current/older versions of Safari.

commented

WebKit recently (2 weeks ago) merged a lookbehind PR WebKit/WebKit#7109 and fixed their 5-year-old lookbehind issue. With a bit of luck, Safari will support lookbehinds in a few months.

Glad to know that, and thanks a lot for your PR!