aboutsummaryrefslogtreecommitdiff
path: root/src/import_record.zig
blob: 89c439604b14c0be5fdb81ac411aed8f8883e57f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
const fs = @import("fs.zig");
const logger = @import("logger.zig");
const std = @import("std");
const Ref = @import("ast/base.zig").Ref;
const Index = @import("ast/base.zig").Index;
const Api = @import("./api/schema.zig").Api;

pub const ImportKind = enum(u8) {

    // An entry point provided by the user
    entry_point,

    // An ES6 import or re-export statement
    stmt,

    // A call to "require()"
    require,

    // An "import()" expression with a string argument
    dynamic,

    /// A call to "require.resolve()"
    require_resolve,

    /// A CSS "@import" rule
    at,

    /// A CSS "@import" rule with import conditions
    at_conditional,

    /// A CSS "url(...)" token
    url,

    pub const Label = std.EnumArray(ImportKind, []const u8);
    pub const all_labels: Label = brk: {
        var labels = Label.initFill("");
        labels.set(ImportKind.entry_point, "entry-point");
        labels.set(ImportKind.stmt, "import-statement");
        labels.set(ImportKind.require, "require-call");
        labels.set(ImportKind.dynamic, "dynamic-import");
        labels.set(ImportKind.require_resolve, "require-resolve");
        labels.set(ImportKind.at, "import-rule");
        labels.set(ImportKind.url, "url-token");
        break :brk labels;
    };

    pub inline fn label(this: ImportKind) []const u8 {
        return all_labels.get(this);
    }

    pub inline fn isCommonJS(this: ImportKind) bool {
        return switch (this) {
            .require, .require_resolve => true,
            else => false,
        };
    }

    pub fn jsonStringify(self: @This(), options: anytype, writer: anytype) !void {
        return try std.json.stringify(@tagName(self), options, writer);
    }

    pub fn isFromCSS(k: ImportKind) bool {
        return k == .at_conditional or k == .at or k == .url;
    }

    pub fn toAPI(k: ImportKind) Api.ImportKind {
        return switch (k) {
            ImportKind.entry_point => Api.ImportKind.entry_point,
            ImportKind.stmt => Api.ImportKind.stmt,
            ImportKind.require => Api.ImportKind.require,
            ImportKind.dynamic => Api.ImportKind.dynamic,
            ImportKind.require_resolve => Api.ImportKind.require_resolve,
            ImportKind.at => Api.ImportKind.at,
            ImportKind.url => Api.ImportKind.url,
            else => Api.ImportKind.internal,
        };
    }
};

pub const ImportRecord = struct {
    range: logger.Range,
    path: fs.Path,

    /// 0 is invalid
    module_id: u32 = 0,

    source_index: Index = Index.invalid,

    print_mode: PrintMode = .normal,

    kind: ImportKind,

    tag: Tag = Tag.none,

    flags: Flags.Set = Flags.None,

    pub inline fn set(this: *ImportRecord, flag: Flags, value: bool) void {
        this.flags.setPresent(flag, value);
    }

    pub inline fn enable(this: *ImportRecord, flag: Flags) void {
        this.set(flag, true);
    }

    /// True for the following cases:
    ///
    ///   `try { require('x') } catch { handle }`
    ///   `try { await import('x') } catch { handle }`
    ///   `try { require.resolve('x') } catch { handle }`
    ///   `import('x').catch(handle)`
    ///   `import('x').then(_, handle)`
    ///
    /// In these cases we shouldn't generate an error if the path could not be
    /// resolved.
    pub inline fn handles_import_errors(this: *const ImportRecord) bool {
        return this.flags.contains(.handles_import_errors);
    }

    /// Sometimes the parser creates an import record and decides it isn't needed.
    /// For example, TypeScript code may have import statements that later turn
    /// out to be type-only imports after analyzing the whole file.
    pub inline fn is_unused(this: *const ImportRecord) bool {
        return this.flags.contains(.is_unused);
    }

    /// If this is true, the import contains syntax like "* as ns". This is used
    /// to determine whether modules that have no exports need to be wrapped in a
    /// CommonJS wrapper or not.
    pub inline fn contains_import_star(this: *const ImportRecord) bool {
        return this.flags.contains(.contains_import_star);
    }

    /// If this is true, the import contains an import for the alias "default",
    /// either via the "import x from" or "import {default as x} from" syntax.
    pub inline fn contains_default_alias(this: *const ImportRecord) bool {
        return this.flags.contains(.contains_default_alias);
    }

    /// If true, this "export * from 'path'" statement is evaluated at run-time by
    /// calling the "__reExport()" helper function
    pub inline fn calls_runtime_re_export_fn(this: *const ImportRecord) bool {
        return this.flags.contains(.calls_runtime_re_export_fn);
    }
    /// If true, this calls require() at runtime
    pub inline fn calls_runtime_require(this: *const ImportRecord) bool {
        return this.flags.contains(.calls_runtime_require);
    }

    /// Tell the printer to wrap this call to "require()" in "__toModule(...)"
    pub inline fn wrap_with_to_module(this: *const ImportRecord) bool {
        return this.flags.contains(.wrap_with_to_module);
    }

    /// Tell the printer to wrap this call to "toESM()" in "__toESM(...)"
    pub inline fn wrap_with_to_esm(this: *const ImportRecord) bool {
        return this.flags.contains(.wrap_with_to_esm);
    }

    // If this is true, the import contains an import for the alias "__esModule",
    // via the "import {__esModule} from" syntax.
    pub inline fn contains_es_module_alias(this: *const ImportRecord) bool {
        return this.flags.contains(.contains_es_module_alias);
    }

    /// If true, this was originally written as a bare "import 'file'" statement
    pub inline fn was_originally_bare_import(this: *const ImportRecord) bool {
        return this.flags.contains(.was_originally_bare_import);
    }
    pub inline fn was_originally_require(this: *const ImportRecord) bool {
        return this.flags.contains(.was_originally_require);
    }

    pub inline fn is_external_without_side_effects(this: *const ImportRecord) bool {
        return @enumToInt(this.tag) >= @enumToInt(Tag.bun) or this.flags.contains(.is_external_without_side_effects);
    }

    pub const Flags = enum {
        /// True for the following cases:
        ///
        ///   try { require('x') } catch { handle }
        ///   try { await import('x') } catch { handle }
        ///   try { require.resolve('x') } catch { handle }
        ///   import('x').catch(handle)
        ///   import('x').then(_, handle)
        ///
        /// In these cases we shouldn't generate an error if the path could not be
        /// resolved.
        handles_import_errors,

        /// Sometimes the parser creates an import record and decides it isn't needed.
        /// For example, TypeScript code may have import statements that later turn
        /// out to be type-only imports after analyzing the whole file.
        is_unused,

        /// If this is true, the import contains syntax like "* as ns". This is used
        /// to determine whether modules that have no exports need to be wrapped in a
        /// CommonJS wrapper or not.
        contains_import_star,

        /// If this is true, the import contains an import for the alias "default",
        /// either via the "import x from" or "import {default as x} from" syntax.
        contains_default_alias,

        // If this is true, the import contains an import for the alias "__esModule",
        // via the "import {__esModule} from" syntax.
        contains_es_module_alias,

        /// If true, this "export * from 'path'" statement is evaluated at run-time by
        /// calling the "__reExport()" helper function
        calls_runtime_re_export_fn,

        /// If true, this calls require() at runtime
        calls_runtime_require,

        /// Tell the printer to wrap this call to "require()" in "__toModule(...)"
        wrap_with_to_module,

        /// Tell the printer to wrap this call to "toESM()" in "__toESM(...)"
        wrap_with_to_esm,

        /// If true, this was originally written as a bare "import 'file'" statement
        was_originally_bare_import,

        was_originally_require,

        is_external_without_side_effects,

        pub const None = Set{};
        pub const Fields = std.enums.EnumFieldStruct(Flags, bool, false);
        pub const Set = std.enums.EnumSet(Flags);
    };

    pub inline fn isRuntime(this: *const ImportRecord) bool {
        return this.tag.isRuntime();
    }

    pub inline fn isInternal(this: *const ImportRecord) bool {
        return this.tag.isInternal();
    }

    pub inline fn isBundled(this: *const ImportRecord) bool {
        return this.module_id > 0;
    }

    pub const List = @import("./baby_list.zig").BabyList(ImportRecord);

    pub const Tag = enum(u3) {
        none,
        /// JSX auto-import for React Fast Refresh
        react_refresh,
        /// JSX auto-import for jsxDEV or jsx
        jsx_import,
        /// JSX auto-import for Fragment or createElement
        jsx_classic,
        /// Uses the `bun` import specifier
        ///     import {foo} from "bun";
        bun,
        /// Uses the `bun:test` import specifier
        ///     import {expect} from "bun:test";
        bun_test,
        runtime,
        /// A macro: import specifier OR a macro import
        macro,

        pub inline fn isRuntime(this: Tag) bool {
            return this == .runtime;
        }

        pub inline fn isInternal(this: Tag) bool {
            return @enumToInt(this) >= @enumToInt(Tag.runtime);
        }
    };

    pub const PrintMode = enum {
        normal,
        import_path,
        css,
    };
};