diff --git a/changes/null-check-merge-source-maps.fix.md b/changes/null-check-merge-source-maps.fix.md new file mode 100644 index 0000000000..0b4a6b704a --- /dev/null +++ b/changes/null-check-merge-source-maps.fix.md @@ -0,0 +1 @@ +- Print `e == null` for a null and undefined check with source maps on too, so source maps no longer change the generated JavaScript. diff --git a/compiler/backend/js_dump.ml b/compiler/backend/js_dump.ml index 2c6590ff0d..770451a16b 100644 --- a/compiler/backend/js_dump.ml +++ b/compiler/backend/js_dump.ml @@ -865,9 +865,12 @@ and expression_desc cxt ~(level : int) f x : cxt = ( Or, {expression_desc = Bin (EqEqEq, e1, {expression_desc = Undefined _})}, {expression_desc = Bin (EqEqEq, e2, {expression_desc = Null})} ) - when e1 = e2 && Js_analyzer.no_side_effect_expression e1 -> + when Js_analyzer.eq_expression e1 e2 + && Js_analyzer.no_side_effect_expression e1 -> (* [e == null] evaluates [e] once, where the two checks may evaluate it - twice, so they are merged only for an [e] without side effects *) + twice, so they are merged only for an [e] without side effects. + [eq_expression] ignores source locations, which differ between the two + operands when source maps are on. *) expression_desc cxt ~(level : int) f (Is_null_or_undefined e1) | Bin (op, e1, e2) -> let out, lft, rght = Js_op_util.op_prec op in diff --git a/tests/project_tests/source_map/input.js b/tests/project_tests/source_map/input.js index f1906ca45c..16847f71ce 100644 --- a/tests/project_tests/source_map/input.js +++ b/tests/project_tests/source_map/input.js @@ -461,7 +461,8 @@ async function assertNodeStackTraceUsesSourceMap() { } } -async function assertHiddenOutput() { +/** @param {Map} defaultOutput */ +async function assertHiddenOutput(defaultOutput) { const sourceRoot = "rescript://source-map-test/"; await removeGeneratedMapFiles(); await fs.writeFile( @@ -480,6 +481,11 @@ async function assertHiddenOutput() { /\/\/# sourceMappingURL=/, `${filename} should not include a sourceMappingURL comment in hidden mode`, ); + assert.equal( + js, + defaultOutput.get(filename), + `${filename} should be the same JS as without source maps`, + ); assert.ok(await fileExists(mapPath), `${filename}.map should exist`); assertSourceMap( filename, @@ -510,6 +516,13 @@ async function assertDefaultOutput() { false, `${filename}.map should not exist by default`, ); + if (filename.startsWith("Helper.")) { + assert.match( + js, + /value\.label == null/, + `${filename} should merge the null and undefined checks`, + ); + } output.set(filename, js); } @@ -577,7 +590,7 @@ try { const defaultOutput = await assertDefaultOutput(); await assertLinkedOutput(); await assertDisabledOutput(defaultOutput); - await assertHiddenOutput(); + await assertHiddenOutput(defaultOutput); await assertInlineOutput(); } finally { await fs.writeFile(configPath, originalConfig); diff --git a/tests/project_tests/source_map/src/Helper.res b/tests/project_tests/source_map/src/Helper.res index 8ef1ab805e..069e72de0e 100644 --- a/tests/project_tests/source_map/src/Helper.res +++ b/tests/project_tests/source_map/src/Helper.res @@ -18,3 +18,10 @@ let unicodeLabel = "helper ν•œκΈ€ 🌏" let fail = () => JsError.throwWithMessage("helper source map") let value = pipeThrough(21, multiply) + +type maybeLabel = {label: Nullable.t} + +// The two reads of `value.label` have different source locations, which +// must not stop `value.label == null` from being printed with source maps +let isMissing = (value: maybeLabel) => + String.make(value.label === Nullable.null || value.label === Nullable.undefined)