From aae95ea1dea99af9603f99fc40ff10c30501493f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9di-R=C3=A9mi=20Hashim?= Date: Sun, 13 Apr 2025 19:09:31 +0200 Subject: [PATCH 1/2] Fix type of RegExp.Result.matches --- CHANGELOG.md | 1 + runtime/Stdlib_RegExp.res | 2 +- runtime/Stdlib_RegExp.resi | 4 ++-- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fa2de7b05f..76ca029d15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ - Fix node.js ExperimentalWarning. https://github.com/rescript-lang/rescript/pull/7379 - Fix issue with gentype and stdlib json. https://github.com/rescript-lang/rescript/pull/7378 +- Fix type of `RegExp.Result.matches`. https://github.com/rescript-lang/rescript/pull/7393 #### :house: Internal diff --git a/runtime/Stdlib_RegExp.res b/runtime/Stdlib_RegExp.res index 6d2c34f893..68116644a7 100644 --- a/runtime/Stdlib_RegExp.res +++ b/runtime/Stdlib_RegExp.res @@ -3,7 +3,7 @@ type t module Result = { type t = array> @get_index external fullMatch: (t, @as(0) _) => string = "" - @send external matches: (t, @as(1) _) => array = "slice" + @send external matches: (t, @as(1) _) => array> = "slice" @get external index: t => int = "index" @get external input: t => string = "input" } diff --git a/runtime/Stdlib_RegExp.resi b/runtime/Stdlib_RegExp.resi index 62ce2dd19e..75811b4d55 100644 --- a/runtime/Stdlib_RegExp.resi +++ b/runtime/Stdlib_RegExp.resi @@ -43,7 +43,7 @@ module Result: { // This below will log "ReScript" and "is" to the console. switch regexp->RegExp.exec("ReScript is pretty cool, right?") { | None => Console.log("Nope, no match...") - | Some(result) => switch result->RegExp.Result.matches { + | Some(result) => switch result->RegExp.Result.matches->Array.keepSome { | [firstWord, secondWord] => Console.log2(firstWord, secondWord) | _ => Console.log("Didn't find exactly two words...") } @@ -51,7 +51,7 @@ module Result: { ``` */ @send - external matches: (t, @as(1) _) => array = "slice" + external matches: (t, @as(1) _) => array> = "slice" @get external index: t => int = "index" /** From 0025d937962050d648ca2d794dfd739d10064bef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9di-R=C3=A9mi=20Hashim?= Date: Sun, 13 Apr 2025 19:23:10 +0200 Subject: [PATCH 2/2] Add optional flags argument to RegExp.fromString --- CHANGELOG.md | 1 + runtime/Stdlib_RegExp.res | 2 +- runtime/Stdlib_RegExp.resi | 11 ++++++++++- .../tests/src/expected/CompletionExpressions.res.txt | 2 +- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 76ca029d15..0a6cdecaa1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ - Fix node.js ExperimentalWarning. https://github.com/rescript-lang/rescript/pull/7379 - Fix issue with gentype and stdlib json. https://github.com/rescript-lang/rescript/pull/7378 - Fix type of `RegExp.Result.matches`. https://github.com/rescript-lang/rescript/pull/7393 +- Add optional `flags` argument to `RegExp.fromString` and deprecate `RegExp.fromStringWithFlags`. https://github.com/rescript-lang/rescript/pull/7393 #### :house: Internal diff --git a/runtime/Stdlib_RegExp.res b/runtime/Stdlib_RegExp.res index 68116644a7..ff24abab95 100644 --- a/runtime/Stdlib_RegExp.res +++ b/runtime/Stdlib_RegExp.res @@ -8,7 +8,7 @@ module Result = { @get external input: t => string = "input" } -@new external fromString: string => t = "RegExp" +@new external fromString: (string, ~flags: string=?) => t = "RegExp" @new external fromStringWithFlags: (string, ~flags: string) => t = "RegExp" @send external test: (t, string) => bool = "test" diff --git a/runtime/Stdlib_RegExp.resi b/runtime/Stdlib_RegExp.resi index 75811b4d55..5415ab1d3a 100644 --- a/runtime/Stdlib_RegExp.resi +++ b/runtime/Stdlib_RegExp.resi @@ -87,10 +87,18 @@ switch regexp->RegExp.exec("ReScript is pretty cool, right?") { | None => Console.log("Nope, no match...") | Some(result) => Console.log(result->RegExp.Result.fullMatch) // Prints "ReScript" } + +// Match 'foo' with case insensitive flag +let regexp = RegExp.fromString("foo", ~flags="i") + +switch regexp->RegExp.exec("FOO") { +| None => Console.log("Nope, no match...") +| Some(result) => Console.log(result->RegExp.Result.fullMatch) // Prints "FOO" +} ``` */ @new -external fromString: string => t = "RegExp" +external fromString: (string, ~flags: string=?) => t = "RegExp" /** `fromStringWithFlags(string)` creates a `RegExp.t` from the provided string, using the provided `flags`. This can then be used to match on strings using `RegExp.exec`. @@ -108,6 +116,7 @@ switch regexp->RegExp.exec("ReScript is pretty cool, right?") { } ``` */ +@deprecated("Use `fromString` instead") @new external fromStringWithFlags: (string, ~flags: string) => t = "RegExp" diff --git a/tests/analysis_tests/tests/src/expected/CompletionExpressions.res.txt b/tests/analysis_tests/tests/src/expected/CompletionExpressions.res.txt index 057b6130d6..cefc48617b 100644 --- a/tests/analysis_tests/tests/src/expected/CompletionExpressions.res.txt +++ b/tests/analysis_tests/tests/src/expected/CompletionExpressions.res.txt @@ -1384,7 +1384,7 @@ Path mkStuff "label": "RegExp.fromString()", "kind": 12, "tags": [], - "detail": "string => t", + "detail": "(string, ~flags: string=?) => t", "documentation": null, "insertText": "RegExp.fromString($0)", "insertTextFormat": 2