Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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
18 changes: 16 additions & 2 deletions src/js_parser/p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2194,9 +2194,23 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O

if self.options.features.inlining {
if let Some(replacement) = self.const_values.get(&ref_) {
let replacement = *replacement;
// Use-site loc, as esbuild does. The stored value has the declaration's.
let data = replacement.data;
self.ignore_usage(ref_);
return replacement;
return match data {
// The printer maps the inner value, not the wrapper.
js_ast::ExprData::EInlinedEnum(inlined) => self.new_expr(
E::InlinedEnum {
value: Expr {
loc,
data: inlined.value.data,
},
comment: inlined.comment,
},
loc,
),
_ => Expr { loc, data },
Comment thread
robobun marked this conversation as resolved.
Resolved
};
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/jsc/RuntimeTranspilerCache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ bun_core::declare_scope!(cache, visible);
/// the cache-HIT path reinstates #33904 for them.
/// Version 34: An import that a plugin `onResolve` answers is printed as it is written.
/// Version 35: Long hexadecimal, octal and binary literals are rounded to the nearest double.
const EXPECTED_VERSION: u32 = 35;
/// Version 36: An inlined `const` literal maps to its use site, not to the declaration.
const EXPECTED_VERSION: u32 = 36;

/// Source files smaller than this are not written to / read from the on-disk
/// transpiler cache. Originally 50 KiB, which excluded almost every file in a
Expand Down
47 changes: 47 additions & 0 deletions test/cli/test/coverage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,53 @@ test("calls second", () => {
expect(exitCode).toBe(0);
});

// https://github.com/oven-sh/bun/issues/45218
// The transpiler inlines `B` and `C` into `unused`. The inlined literals must
// map to the use site, or the uncalled function's line range starts at the
// declaration and zeroes every executed line in between.
test("an uncalled function that uses an inlined const keeps the lines before it covered", async () => {
using dir = tempDir("cov-inlined-const", {
"subject.ts": `enum Color { Red = "red" }
const A = 1;
const B = 2;
const C = Color.Red;

export function unused(x: number): string {
return x + B + C;
}
`,
"subject.test.ts": `
import { expect, test } from "bun:test";
import * as m from "./subject.ts";

test("only imports the function", () => {
expect(typeof m.unused).toBe("function");
});
`,
});
await using proc = Bun.spawn({
cmd: [bunExe(), "test", "--coverage", "--coverage-reporter=lcov"],
env: bunEnv,
cwd: String(dir),
stdout: "pipe",
stderr: "pipe",
});
const [, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);

const lcov = readFileSync(path.join(String(dir), "coverage", "lcov.info"), "utf-8");
const record = lcov.split("end_of_record").find(r => r.includes("SF:subject.ts"))!;
const executed = Object.fromEntries([...record.matchAll(/^DA:(\d+),(\d+)$/gm)].map(m => [m[1], Number(m[2]) > 0]));
// Lines 2 to 4 ran at module load. Line 6 is the uncalled function.
expect({ 2: executed[2], 3: executed[3], 4: executed[4], 6: executed[6] }).toEqual({
2: true,
3: true,
4: true,
6: false,
});
expect(stderr).toContain("1 pass");
expect(exitCode).toBe(0);
});

// JSC records coverage per SourceProvider, and a file has one for each time
// it is loaded. Each case has a subject file of its own. Where a case loads
// its subject twice, one load runs the `if` branch and the other runs the
Expand Down
37 changes: 37 additions & 0 deletions test/js/bun/sourcemap/internal-sourcemap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,43 @@ describe("InternalSourceMap", () => {
expect(exited).toBe(1);
});

// https://github.com/oven-sh/bun/issues/45218
// The runtime transpiler inlines a `const` literal (and an enum-valued
// const) at each use. The printed literal must map to the use site, not to
// the declaration.
test("an inlined const maps to its use site, not its declaration", async () => {
const fixture = [
`enum Kind { Callee = "callee" }`,
`const NOT_A_FUNCTION = null;`,
`const ENUM_VALUE = Kind.Callee;`,
`type Unused = { a: number };`,
``,
`function callLiteral(): void {`,
` NOT_A_FUNCTION();`,
`}`,
`function callEnum(): void {`,
` ENUM_VALUE();`,
`}`,
`for (const fn of [callLiteral, callEnum]) {`,
` try {`,
` fn();`,
` } catch (e) {`,
` console.log((e as Error).stack);`,
` }`,
`}`,
``,
].join("\n");

const { stdout, stderr, exited } = await run({ "index.ts": fixture });

expect(stderr).toBe("");
const frames = [...stdout.matchAll(/at (callLiteral|callEnum) \(.*index\.ts:(\d+):(\d+)\)/g)].map(
m => `${m[1]} ${m[2]}:${m[3]}`,
);
expect(frames).toEqual(["callLiteral 7:3", "callEnum 10:3"]);
expect(exited).toBe(0);
});

// 200 single-expression lines so the map has well over `sync_interval` (64)
// mappings and lookup must bsearch past sync_points[0].
test("remaps frames past multiple sync points", async () => {
Expand Down
Loading