Skip to content

breaking: decouple parser switch and transformer switch#30

Merged
nicolo-ribaudo merged 2 commits intomathiasbynens:masterfrom
JLHwung:decouple-parser-transform-switch
Sep 20, 2019
Merged

breaking: decouple parser switch and transformer switch#30
nicolo-ribaudo merged 2 commits intomathiasbynens:masterfrom
JLHwung:decouple-parser-transform-switch

Conversation

@JLHwung
Copy link
Copy Markdown
Collaborator

@JLHwung JLHwung commented Sep 13, 2019

In this PR we decouple the regjsparser switch from the regexpu-core transformer switch. It is a breaking change since it does not throw for \\p{Unified_Ideograph} when unicodePropertyEscape is not enabled, instead it assumes our user know exactly what she is doing and only transform the features as instructed.

By doing this, babel-preset-env can have more granular control on ES6+ regex features. For example, when user specifies

{
  "target": "Hypothetical browser supports unicode property escape but not named capturing groups"
}

and let's say the pattern we want to transform is /(?<name>\p{Unified_Ideograph}{3})/u we don't have to transform unicode property escape here and instead should only pass namedGroup: true.

rewritePattern(
  /(?<name>\p{Unified_Ideograph}{3})/u,
  "u",
  { namedGroup: true }
)
// expect to transform `?<name>` only without touching `\p{Unified_Ideograph}`

However, to transform this pattern, we still need regjsparser support for unicode property escape otherwise the parser will throw on \p{. Therefore, we maximize the parser support, allow parser to parse any new syntax, and only transform when user opt-in the transformation. By doing so we encourage to ship native browser features without superfluous transformations.

Edits: the previous Safari 12 example above is practically invalid as of Sep 2019. Every browser supports unicode property escape also support named capturing groups.

Copy link
Copy Markdown
Collaborator

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't classify this PR a breakinc change, since it just prevents regexpu from throwing a bunch of exceptions.

@JLHwung
Copy link
Copy Markdown
Collaborator Author

JLHwung commented Sep 16, 2019

I have reverted lookbehind: true since it is just a parser switch, regexpu does no transform on lookbehind: true.

@nicolo-ribaudo
Copy link
Copy Markdown
Collaborator

So now some parser features are enabled by default and some aren't? I don't think that this inconsistency is good.

@JLHwung
Copy link
Copy Markdown
Collaborator Author

JLHwung commented Sep 16, 2019

If we enable lookbehind: true in regjsparser, it will render lookbehind: false completely useless, which is definitely a breaking change.

In babel/babel#10447 we have enabled lookbehind: true so a babel user can still parse lookbehind assertions by default. But as a regexpu-core user, I don't think we should strip away lookbehind in a minor release.

@nicolo-ribaudo nicolo-ribaudo merged commit e27b203 into mathiasbynens:master Sep 20, 2019
@JLHwung JLHwung deleted the decouple-parser-transform-switch branch February 18, 2021 15:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants