From b7fdb179e3a06f7e08fbd31ad6d91598924f0f1a Mon Sep 17 00:00:00 2001 From: Justin Magaram Date: Sat, 10 May 2025 15:14:45 -0700 Subject: [PATCH 1/4] Add RegExp.flags external and corresponding tests --- runtime/Stdlib_RegExp.res | 1 + runtime/Stdlib_RegExp.resi | 15 +++++++++++++++ tests/tests/src/core/Core_RegExpTest.res | 18 ++++++++++++++++++ tests/tests/src/core/Core_TestSuite.res | 1 + 4 files changed, 35 insertions(+) create mode 100644 tests/tests/src/core/Core_RegExpTest.res diff --git a/runtime/Stdlib_RegExp.res b/runtime/Stdlib_RegExp.res index 3ef106afad..c371806a83 100644 --- a/runtime/Stdlib_RegExp.res +++ b/runtime/Stdlib_RegExp.res @@ -23,5 +23,6 @@ module Result = { @get external source: t => string = "source" @get external sticky: t => bool = "sticky" @get external unicode: t => bool = "unicode" +@get external flags: t => string = "flags" external ignore: t => unit = "%ignore" diff --git a/runtime/Stdlib_RegExp.resi b/runtime/Stdlib_RegExp.resi index 688cf65678..9dcd11cb12 100644 --- a/runtime/Stdlib_RegExp.resi +++ b/runtime/Stdlib_RegExp.resi @@ -299,6 +299,20 @@ Console.log(regexp2->RegExp.unicode) // Logs `true`, since `u` is set @get external unicode: t => bool = "unicode" +/** +`flags(regexp)` returns a string consisting of all the flags set on this `RegExp`. + +See [`RegExp.flags`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/flags) on MDN. + +## Examples +```rescript +let regexp = RegExp.fromString("\\w+", ~flags="gi") +Console.log(regexp->RegExp.flags) // Logs "gi", all the flags set on the RegExp +``` +*/ +@get +external flags: t => string = "flags" + /** `ignore(regExp)` ignores the provided regExp and returns unit. @@ -306,3 +320,4 @@ external unicode: t => bool = "unicode" without having to store or process it further. */ external ignore: t => unit = "%ignore" +// diff --git a/tests/tests/src/core/Core_RegExpTest.res b/tests/tests/src/core/Core_RegExpTest.res new file mode 100644 index 0000000000..dd1c844f7c --- /dev/null +++ b/tests/tests/src/core/Core_RegExpTest.res @@ -0,0 +1,18 @@ +// Test for RegExp.flags +Test.run( + __POS_OF__("RegExp.flags basic"), + RegExp.fromStringWithFlags("\\w+", ~flags="gi")->RegExp.flags, + eq, + "gi", +) + +// Test for alphabetical sorting of flags +Test.run( + __POS_OF__("RegExp.flags sorting"), + RegExp.fromStringWithFlags("\\w+", ~flags="igd")->RegExp.flags, + eq, + "dgi", +) + +// Test with no flags +Test.run(__POS_OF__("RegExp.flags empty"), RegExp.fromString("\\w+")->RegExp.flags, eq, "") diff --git a/tests/tests/src/core/Core_TestSuite.res b/tests/tests/src/core/Core_TestSuite.res index e048a3bd56..9b9c997f7e 100644 --- a/tests/tests/src/core/Core_TestSuite.res +++ b/tests/tests/src/core/Core_TestSuite.res @@ -11,3 +11,4 @@ include Core_JsonTests include Core_NullableTests include Core_DictTests include Core_IteratorTests +include Core_RegExpTest From 9a5d236f8934713bb7b3886208149aa5fcdf6938 Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Wed, 21 May 2025 18:19:56 +0200 Subject: [PATCH 2/4] fix tests --- tests/tests/src/core/Core_RegExpTest.mjs | 41 ++++++++++++++++++++++++ tests/tests/src/core/Core_RegExpTest.res | 2 ++ tests/tests/src/core/Core_TestSuite.mjs | 7 ++-- 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 tests/tests/src/core/Core_RegExpTest.mjs diff --git a/tests/tests/src/core/Core_RegExpTest.mjs b/tests/tests/src/core/Core_RegExpTest.mjs new file mode 100644 index 0000000000..56a4a9d98c --- /dev/null +++ b/tests/tests/src/core/Core_RegExpTest.mjs @@ -0,0 +1,41 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as Test from "./Test.mjs"; +import * as Primitive_object from "rescript/lib/es6/Primitive_object.js"; + +let eq = Primitive_object.equal; + +Test.run([ + [ + "Core_RegExpTest.res", + 5, + 13, + 33 + ], + "RegExp.flags basic" +], new RegExp("\\w+", "gi").flags, eq, "gi"); + +Test.run([ + [ + "Core_RegExpTest.res", + 13, + 13, + 35 + ], + "RegExp.flags sorting" +], new RegExp("\\w+", "igd").flags, eq, "dgi"); + +Test.run([ + [ + "Core_RegExpTest.res", + 20, + 20, + 40 + ], + "RegExp.flags empty" +], new RegExp("\\w+").flags, eq, ""); + +export { + eq, +} +/* Not a pure module */ diff --git a/tests/tests/src/core/Core_RegExpTest.res b/tests/tests/src/core/Core_RegExpTest.res index dd1c844f7c..cafdd8e7d8 100644 --- a/tests/tests/src/core/Core_RegExpTest.res +++ b/tests/tests/src/core/Core_RegExpTest.res @@ -1,3 +1,5 @@ +let eq = (a, b) => a == b + // Test for RegExp.flags Test.run( __POS_OF__("RegExp.flags basic"), diff --git a/tests/tests/src/core/Core_TestSuite.mjs b/tests/tests/src/core/Core_TestSuite.mjs index a378afbfc5..c17b1abccd 100644 --- a/tests/tests/src/core/Core_TestSuite.mjs +++ b/tests/tests/src/core/Core_TestSuite.mjs @@ -7,6 +7,7 @@ import * as Core_TestTests from "./Core_TestTests.mjs"; import * as Core_ArrayTests from "./Core_ArrayTests.mjs"; import * as Core_ErrorTests from "./Core_ErrorTests.mjs"; import * as Core_FloatTests from "./Core_FloatTests.mjs"; +import * as Core_RegExpTest from "./Core_RegExpTest.mjs"; import * as Core_ObjectTests from "./Core_ObjectTests.mjs"; import * as Core_PromiseTest from "./Core_PromiseTest.mjs"; import * as Core_ResultTests from "./Core_ResultTests.mjs"; @@ -84,8 +85,6 @@ let PatternMatching = Core_DictTests.PatternMatching; let Has = Core_DictTests.Has; -let eq = Core_IteratorTests.eq; - let iterator = Core_IteratorTests.iterator; let syncResult = Core_IteratorTests.syncResult; @@ -94,6 +93,8 @@ let asyncResult = Core_IteratorTests.asyncResult; let asyncIterator = Core_IteratorTests.asyncIterator; +let eq = Core_RegExpTest.eq; + export { bign, TestError, @@ -130,10 +131,10 @@ export { intDict, PatternMatching, Has, - eq, iterator, syncResult, asyncResult, asyncIterator, + eq, } /* Core_IntTests Not a pure module */ From fe21a0e86b445e8f79e669eae0e8ce0cccd7dbd7 Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Wed, 21 May 2025 18:47:38 +0200 Subject: [PATCH 3/4] changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50e01c9a9b..c63669fd56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ # 12.0.0-alpha.14 (Unreleased) +#### :rocket: New Feature + +- Add `RegExp.flags`. https://github.com/rescript-lang/rescript/pull/7461 + #### :bug: Bug fix - `rescript-tools doc` no longer includes shadowed bindings in its output. https://github.com/rescript-lang/rescript/pull/7497 From a74cb23761c29903bec955e0226849f433db9772 Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Wed, 21 May 2025 18:48:40 +0200 Subject: [PATCH 4/4] remove stray comment --- runtime/Stdlib_RegExp.resi | 1 - 1 file changed, 1 deletion(-) diff --git a/runtime/Stdlib_RegExp.resi b/runtime/Stdlib_RegExp.resi index 9dcd11cb12..3b327a1eb4 100644 --- a/runtime/Stdlib_RegExp.resi +++ b/runtime/Stdlib_RegExp.resi @@ -320,4 +320,3 @@ external flags: t => string = "flags" without having to store or process it further. */ external ignore: t => unit = "%ignore" -//