Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breaking: Throw for [^\p{…}] with flag i in environments without native flag v #10

Closed
slevithan opened this issue Jul 29, 2024 · 1 comment

Comments

@slevithan
Copy link
Owner

slevithan commented Jul 29, 2024

regex v2.1.0 introduced src/backcompat.js, which extended support for most usage backward to environments without native flag v. This file is a postprocessor that is conditionally applied when flag v is not supported natively, and it does several things:

  1. Transpiles flag v's escaping rules to u's (by un-escaping some characters).
  2. Throws for character class syntax that is invalid with v but valid with u (unescaped (){}/|, reserved double punctuators, and leading/trailing -, since all of these would throw in environments with v).
  3. Throws for features that require native flag v (character class set operators -- and &&, and a more descriptive error for nested character classes which are already invalid with u).
  4. Throws when using doubly-negated [^\P{…}] if the regex uses flag i, to prevent an unintuitive (likely) behavior difference (likely, since many but not all Unicode properties include letters with case).

Regarding # 4, this is fine, but it's incomplete. At the time, I was referencing this and this, which only show examples with doubly-negated [^\P{…}]. However, as @rauschma points out here, this v/u incompatibility applies equally with \p (lowercase) in /[^\p{…}]/iu.

Updating this in src/backcompat.js to also cover \p would be easy, but would also be a (rarely applicable) breaking change. IMO it wouldn't be a problem to start throwing without a new major version if the behavior was always incompatible (since anyone with such patterns in their code would already have a bug in environments without v), but since not all uses of /[^\p{…}]/iu suffer from this issue, I will hold off on updating it until there are additional breaking changes to include in a v4.0.

@slevithan slevithan changed the title Breaking: Throw for ` regex('i')[^\p{…}] in environments without native flag v` Breaking: Throw for [^\p{…}] with flag i in environments without native flag v Jul 29, 2024
@slevithan
Copy link
Owner Author

Rather than throwing for [^\p{…}] with flag i (when v is not supported natively), I resolved this by:

  • Not throwing for uppercase [^\P{…}] with flag i, either (since it's not actually always problematic).
  • Introducing new option unicodeSetsPlugin, which allows overriding the built-it plugin for backcompat with a more robust transpiler (e.g. based on regexpu-core), if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant