| 1 | import type { types as t } from "@babel/core"; |
| 2 | import { FEATURES, hasFeature } from "./features"; |
| 3 | |
| 4 | import type { RegexpuOptions } from "regexpu-core"; |
| 5 | |
| 6 | export function generateRegexpuOptions( |
| 7 | pattern: string, |
| 8 | toTransform: number, |
| 9 | ): RegexpuOptions { |
| 10 | type Experimental = 1; |
| 11 | |
| 12 | const feat = <Stability extends 0 | 1 = 0>( |
| 13 | name: keyof typeof FEATURES, |
| 14 | ok: "transform" | (Stability extends 0 ? never : "parse") = "transform", |
| 15 | ) => { |
| 16 | return hasFeature(toTransform, FEATURES[name]) ? ok : false; |
| 17 | }; |
| 18 | |
| 19 | const featDuplicateNamedGroups = (): "transform" | false => { |
| 20 | if (!feat("duplicateNamedCaptureGroups")) return false; |
| 21 | |
| 22 | // This can return false positive, for example for /\(?<a>\)/. |
| 23 | // However, it's such a rare occurrence that it's ok to compile |
| 24 | // the regexp even if we only need to compile regexps with |
| 25 | // duplicate named capturing groups. |
| 26 | const regex = /\(\?<([^>]+)>/g; |
| 27 | const seen = new Set(); |
| 28 | for (let match; (match = regex.exec(pattern)); seen.add(match[1])) { |
| 29 | if (seen.has(match[1])) return "transform"; |
| 30 | } |
| 31 | return false; |
| 32 | }; |
| 33 | |
| 34 | return { |
| 35 | unicodeFlag: feat("unicodeFlag"), |
| 36 | unicodeSetsFlag: |
| 37 | feat<Experimental>("unicodeSetsFlag") || |
| 38 | feat<Experimental>("unicodeSetsFlag_syntax", "parse"), |
| 39 | dotAllFlag: feat("dotAllFlag"), |
| 40 | unicodePropertyEscapes: feat("unicodePropertyEscape"), |
| 41 | namedGroups: feat("namedCaptureGroups") || featDuplicateNamedGroups(), |
| 42 | onNamedGroup: () => {}, |
| 43 | }; |
| 44 | } |
| 45 | |
| 46 | export function canSkipRegexpu( |
| 47 | node: t.RegExpLiteral, |
| 48 | options: RegexpuOptions, |
| 49 | ): boolean { |
| 50 | const { flags, pattern } = node; |
| 51 | |
| 52 | if (flags.includes("v")) { |
| 53 | if (options.unicodeSetsFlag === "transform") return false; |
| 54 | } |
| 55 | |
| 56 | if (flags.includes("u")) { |
| 57 | if (options.unicodeFlag === "transform") return false; |
| 58 | if ( |
| 59 | options.unicodePropertyEscapes === "transform" && |
| 60 | /\\[pP]{/.test(pattern) |
| 61 | ) { |
| 62 | return false; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | if (flags.includes("s")) { |
| 67 | if (options.dotAllFlag === "transform") return false; |
| 68 | } |
| 69 | |
| 70 | if (options.namedGroups === "transform" && /\(\?<(?![=!])/.test(pattern)) { |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | export function transformFlags(regexpuOptions: RegexpuOptions, flags: string) { |
| 78 | if (regexpuOptions.unicodeSetsFlag === "transform") { |
| 79 | flags = flags.replace("v", "u"); |
| 80 | } |
| 81 | if (regexpuOptions.unicodeFlag === "transform") { |
| 82 | flags = flags.replace("u", ""); |
| 83 | } |
| 84 | if (regexpuOptions.dotAllFlag === "transform") { |
| 85 | flags = flags.replace("s", ""); |
| 86 | } |
| 87 | return flags; |
| 88 | } |