Skip to content
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
1 change: 1 addition & 0 deletions changes/null-check-merge-source-maps.fix.md
Original file line number Diff line number Diff line change
@@ -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.
7 changes: 5 additions & 2 deletions compiler/backend/js_dump.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 15 additions & 2 deletions tests/project_tests/source_map/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,8 @@ async function assertNodeStackTraceUsesSourceMap() {
}
}

async function assertHiddenOutput() {
/** @param {Map<string, string>} defaultOutput */
async function assertHiddenOutput(defaultOutput) {
const sourceRoot = "rescript://source-map-test/";
await removeGeneratedMapFiles();
await fs.writeFile(
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions tests/project_tests/source_map/src/Helper.res
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,10 @@ let unicodeLabel = "helper 한글 🌏"
let fail = () => JsError.throwWithMessage("helper source map")

let value = pipeThrough(21, multiply)

type maybeLabel = {label: Nullable.t<string>}

// 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)
Loading