From 079aa68a945aeb8d476d2ff567cf569bebe9056c Mon Sep 17 00:00:00 2001 From: RiskyMH Date: Sat, 4 Oct 2025 09:44:53 +1000 Subject: [PATCH] slightly better solution --- src/ast/P.zig | 7 ++++++- src/ast/visitExpr.zig | 21 +++++++-------------- src/js_parser.zig | 1 - 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/ast/P.zig b/src/ast/P.zig index adb8cc79a3..0f569ea6b8 100644 --- a/src/ast/P.zig +++ b/src/ast/P.zig @@ -2772,12 +2772,17 @@ pub fn NewParser_( } if (comptime allow_macros) { + // Track {ms} from "bun" as it will 99% of the time be statically known + // so might as well make it a macro automatically if (strings.eqlComptime(path.text, "bun") and strings.eqlComptime(item.alias, "ms")) { try p.macro.refs.put(ref, .{ .import_record_id = stmt.import_record_index, .name = item.alias, - .is_bun_module = true, }); + const import_record = &p.import_records.items[stmt.import_record_index]; + if (import_record.tag == .none) { + import_record.tag = .bun; + } } } diff --git a/src/ast/visitExpr.zig b/src/ast/visitExpr.zig index fed0c0bb0a..f56b455317 100644 --- a/src/ast/visitExpr.zig +++ b/src/ast/visitExpr.zig @@ -1370,20 +1370,6 @@ pub fn VisitExpr( }; const macro_ref_data = p.macro.refs.get(ref).?; - - if (macro_ref_data.is_bun_module) { - if (macro_ref_data.name) |name| { - if (strings.eqlComptime(name, "ms")) { - const res = bun.handleOom(bun.api.ms.astFunction(p, e_, expr.loc)); - if (res) |r| { - p.ignoreUsage(ref); - return r; - } - return expr; - } - } - } - p.ignoreUsage(ref); if (p.is_control_flow_dead) { return p.newExpr(E.Undefined{}, e_.target.loc); @@ -1401,6 +1387,13 @@ pub fn VisitExpr( const name = macro_ref_data.name orelse e_.target.data.e_dot.name; const record = &p.import_records.items[macro_ref_data.import_record_id]; + + // Special case: import { ms } from "bun" - inline instead of executing as macro + if (record.tag == .bun and strings.eqlComptime(name, "ms")) { + const res = bun.handleOom(bun.api.ms.astFunction(p, e_, expr.loc)); + return res orelse expr; + } + const copied = Expr{ .loc = expr.loc, .data = .{ .e_call = e_ } }; const start_error_count = p.log.msgs.items.len; p.macro_call_count += 1; diff --git a/src/js_parser.zig b/src/js_parser.zig index 170c1936ed..9bba1c6028 100644 --- a/src/js_parser.zig +++ b/src/js_parser.zig @@ -125,7 +125,6 @@ const MacroRefData = struct { // if name is null the macro is imported as a namespace import // import * as macros from "./macros.js" with {type: "macro"}; name: ?string = null, - is_bun_module: bool = false, }; const MacroRefs = std.AutoArrayHashMap(Ref, MacroRefData);