Skip to content

Fix type of RegExp.Result.matches #7393

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

Merged
merged 2 commits into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

- 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

Expand Down
4 changes: 2 additions & 2 deletions runtime/Stdlib_RegExp.res
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ type t
module Result = {
type t = array<option<string>>
@get_index external fullMatch: (t, @as(0) _) => string = ""
@send external matches: (t, @as(1) _) => array<string> = "slice"
@send external matches: (t, @as(1) _) => array<option<string>> = "slice"
@get external index: t => int = "index"
@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"
Expand Down
15 changes: 12 additions & 3 deletions runtime/Stdlib_RegExp.resi
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ 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...")
}
}
```
*/
@send
external matches: (t, @as(1) _) => array<string> = "slice"
external matches: (t, @as(1) _) => array<option<string>> = "slice"
@get external index: t => int = "index"

/**
Expand Down Expand Up @@ -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`.
Expand All @@ -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"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down