mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 10:28:47 +00:00
slightly better solution
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user