From 9cdb9db637180229369dbabf30903214c3aa62be Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Sat, 1 Feb 2025 14:08:19 +0100 Subject: [PATCH] Fix `%external` extension. Fixes https://github.com/rescript-lang/rescript/issues/7270 --- CHANGELOG.md | 1 + compiler/frontend/ast_exp_handle_external.ml | 2 +- tests/tests/src/ExternalExtension.mjs | 12 ++++++++++++ tests/tests/src/ExternalExtension.res | 4 ++++ 4 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 tests/tests/src/ExternalExtension.mjs create mode 100644 tests/tests/src/ExternalExtension.res diff --git a/CHANGELOG.md b/CHANGELOG.md index 71f1d5a570..77ebdd91a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ #### :bug: Bug fix - Fix async context checking for module await. https://github.com/rescript-lang/rescript/pull/7271 +- Fix `%external` extension. https://github.com/rescript-lang/rescript/pull/7272 # 12.0.0-alpha.8 diff --git a/compiler/frontend/ast_exp_handle_external.ml b/compiler/frontend/ast_exp_handle_external.ml index 437e12dbe2..f4a2ca5395 100644 --- a/compiler/frontend/ast_exp_handle_external.ml +++ b/compiler/frontend/ast_exp_handle_external.ml @@ -60,7 +60,7 @@ let handle_external loc (x : string) : Parsetree.expression = Ast_compatible.app1 ~loc undefined_typeof (Exp.ifthenelse ~loc (Ast_compatible.app2 ~loc - (Exp.ident ~loc {loc; txt = Ldot (Lident "Pervasives", "=")}) + (Exp.ident ~loc {loc; txt = Lident "=="}) (Ast_compatible.app1 ~loc typeof raw_exp) (Ast_compatible.const_exp_string ~loc "undefined")) empty (Some raw_exp)) diff --git a/tests/tests/src/ExternalExtension.mjs b/tests/tests/src/ExternalExtension.mjs new file mode 100644 index 0000000000..18a51ced84 --- /dev/null +++ b/tests/tests/src/ExternalExtension.mjs @@ -0,0 +1,12 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + + +let match = typeof __DEV__ === "undefined" ? undefined : __DEV__; + +if (match !== undefined) { + console.log("dev mode"); +} else { + console.log("production mode"); +} + +/* match Not a pure module */ diff --git a/tests/tests/src/ExternalExtension.res b/tests/tests/src/ExternalExtension.res new file mode 100644 index 0000000000..dcc6c9b019 --- /dev/null +++ b/tests/tests/src/ExternalExtension.res @@ -0,0 +1,4 @@ +switch %external(__DEV__) { +| Some(_) => Console.log("dev mode") +| None => Console.log("production mode") +}