Skip to content

Commit 41455b0

Browse files
Copilottsnobip
andcommitted
Improve template literal detection with better comments and comprehensive tests
Co-authored-by: tsnobip <2479216+tsnobip@users.noreply.github.com>
1 parent ed6cd17 commit 41455b0

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

compiler/core/lam_compile.ml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,11 +1537,13 @@ let compile output_prefix =
15371537
{expression_desc = Array (strings, _); _};
15381538
{expression_desc = Array (values, _); _};
15391539
] ->
1540-
(* This looks like a template literal call: fn(["str1", "str2"], [val1, val2])
1541-
Convert it to use template literal syntax: fn`str1${val1}str2` *)
1540+
(* This matches the template literal pattern: fn(["str1", "str2"], [val1, val2])
1541+
which is generated from: fn`str1${val1}str2`
1542+
Convert it to use JavaScript template literal syntax for consistency
1543+
with external @taggedTemplate functions *)
15421544
E.tagged_template fn_code strings values
15431545
| _ ->
1544-
(* Regular function call *)
1546+
(* Regular function call - keep existing behavior *)
15451547
E.call
15461548
~info:
15471549
(call_info_of_ap_status appinfo.ap_transformed_jsx

tests/tests/src/template_literal_consistency_test.res

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@ let sqlReScript = (strings, values) => {
1818
result.contents ++ strings[valCount]
1919
}
2020

21+
// Regular function with two array args - should NOT be treated as template literal
22+
let regularFunction = (arr1, arr2) => {
23+
"regular function result"
24+
}
25+
2126
// Test data
22-
let table = "users"
27+
let table = "users"
2328
let id = 42
2429

2530
// Both calls should now generate identical JavaScript template literal syntax:
@@ -28,6 +33,20 @@ let id = 42
2833
let externalResult = sqlExternal`SELECT * FROM ${table} WHERE id = ${id}`
2934
let rescriptResult = sqlReScript`SELECT * FROM ${table} WHERE id = ${id}`
3035

31-
// Simple case
36+
// Simple cases
3237
let simple1 = sqlExternal`hello ${123} world`
33-
let simple2 = sqlReScript`hello ${123} world`
38+
let simple2 = sqlReScript`hello ${123} world`
39+
40+
// Edge cases: empty interpolations
41+
let empty1 = sqlExternal`no interpolations`
42+
let empty2 = sqlReScript`no interpolations`
43+
44+
// Regular function call (should remain as function call, not template literal)
45+
let regularCall = regularFunction(["not", "template"], ["literal", "call"])
46+
47+
// Test various data types
48+
let numberTest1 = sqlExternal`number: ${42}`
49+
let numberTest2 = sqlReScript`number: ${42}`
50+
51+
let stringTest1 = sqlExternal`string: ${"test"}`
52+
let stringTest2 = sqlReScript`string: ${"test"}`

0 commit comments

Comments
 (0)