11import rewritePattern from "regexpu-core" ;
2- import { featuresKey , FEATURES , enableFeature , runtimeKey } from "./features" ;
2+ import {
3+ featuresKey ,
4+ FEATURES ,
5+ enableFeature ,
6+ runtimeKey ,
7+ hasFeature ,
8+ } from "./features" ;
39import { generateRegexpuOptions , canSkipRegexpu , transformFlags } from "./util" ;
410import type { NodePath } from "@babel/traverse" ;
511
@@ -44,16 +50,37 @@ export function createRegExpFeaturePlugin({
4450 const features = file . get ( featuresKey ) ?? 0 ;
4551 let newFeatures = enableFeature ( features , FEATURES [ feature ] ) ;
4652
47- const { useUnicodeFlag, runtime = true } = options ;
53+ const { useUnicodeFlag, runtime } = options ;
4854 if ( useUnicodeFlag === false ) {
4955 newFeatures = enableFeature ( newFeatures , FEATURES . unicodeFlag ) ;
5056 }
5157 if ( newFeatures !== features ) {
5258 file . set ( featuresKey , newFeatures ) ;
5359 }
5460
55- if ( ! runtime ) {
56- file . set ( runtimeKey , false ) ;
61+ if ( runtime !== undefined ) {
62+ if (
63+ file . has ( runtimeKey ) &&
64+ file . get ( runtimeKey ) !== runtime &&
65+ // TODO(Babel 8): Remove this check. It's necessary because in Babel 7
66+ // we allow multiple copies of transform-named-capturing-groups-regex
67+ // with conflicting 'runtime' options.
68+ hasFeature ( newFeatures , FEATURES . duplicateNamedCaptureGroups )
69+ ) {
70+ throw new Error (
71+ `The 'runtime' option must be the same for ` +
72+ `'@babel/plugin-transform-named-capturing-groups-regex' and ` +
73+ `'@babel/plugin-proposal-duplicate-named-capturing-groups-regex'.` ,
74+ ) ;
75+ }
76+ // TODO(Babel 8): Remove this check and always set it.
77+ // It's necessary because in Babel 7 we allow multiple copies of
78+ // transform-named-capturing-groups-regex with conflicting 'runtime' options.
79+ if ( feature === "namedCaptureGroups" ) {
80+ if ( ! runtime || ! file . has ( runtimeKey ) ) file . set ( runtimeKey , runtime ) ;
81+ } else {
82+ file . set ( runtimeKey , runtime ) ;
83+ }
5784 }
5885
5986 if ( ! file . has ( versionKey ) || file . get ( versionKey ) < version ) {
@@ -68,13 +95,24 @@ export function createRegExpFeaturePlugin({
6895 const features = file . get ( featuresKey ) ;
6996 const runtime = file . get ( runtimeKey ) ?? true ;
7097
71- const regexpuOptions = generateRegexpuOptions ( features ) ;
72- if ( canSkipRegexpu ( node , regexpuOptions ) ) return ;
98+ const regexpuOptions = generateRegexpuOptions ( node . pattern , features ) ;
99+ if ( canSkipRegexpu ( node , regexpuOptions ) ) {
100+ return ;
101+ }
73102
74- const namedCaptureGroups : Record < string , number > = { } ;
103+ const namedCaptureGroups : Record < string , number | number [ ] > = {
104+ __proto__ : null ,
105+ } ;
75106 if ( regexpuOptions . namedGroups === "transform" ) {
76107 regexpuOptions . onNamedGroup = ( name , index ) => {
77- namedCaptureGroups [ name ] = index ;
108+ const prev = namedCaptureGroups [ name ] ;
109+ if ( typeof prev === "number" ) {
110+ namedCaptureGroups [ name ] = [ prev , index ] ;
111+ } else if ( Array . isArray ( prev ) ) {
112+ prev . push ( index ) ;
113+ } else {
114+ namedCaptureGroups [ name ] = index ;
115+ }
78116 } ;
79117 }
80118
0 commit comments