oxc_codegen 0.147.0

A collection of JavaScript tools written in Rust.
Documentation
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
use std::borrow::Cow;

use rustc_hash::{FxHashMap, FxHashSet};
use smallvec::SmallVec;

use oxc_ast::{
    Comment, CommentKind,
    ast::{Expression, Program},
};
use oxc_span::GetSpan;
use oxc_syntax::line_terminator::LineTerminatorSplitter;

use crate::{Codegen, LegalComment, options::CommentOptions};

type CommentList = SmallVec<[Comment; 1]>;

pub type CommentsMap = FxHashMap</* attached_to */ u32, CommentList>;

/// Whether a comment remains meaningful if its original AST anchor is removed.
fn preserve_when_orphaned(comment: Comment) -> bool {
    comment.is_legal() || comment.is_coverage_ignore_file()
}

/// A `pife`-marked arrow or function expression prints its leading comments
/// inside its own `(` wrap, so operand emission sites must not consume them.
fn is_pife_function(expression: &Expression<'_>) -> bool {
    match expression {
        Expression::ArrowFunctionExpression(arrow) => arrow.pife,
        Expression::FunctionExpression(function) => function.pife,
        _ => false,
    }
}

/// Which annotation kind an emission site expects to recover from
/// [`Codegen::annotation_comments`].
///
/// `@__PURE__` / `#__PURE__` on a `CallExpression` or `NewExpression`, and
/// `@__NO_SIDE_EFFECTS__` / `#__NO_SIDE_EFFECTS__` on a function declaration or
/// expression, are not interchangeable: downstream tree-shakers only honor
/// each on its corresponding node kind. The filter prevents
/// [`Codegen::print_annotation_comment`] from emitting one kind where the
/// other was expected when both share an `attached_to`.
#[derive(Clone, Copy)]
pub enum AnnotationKind {
    Pure,
    NoSideEffects,
}

impl AnnotationKind {
    #[inline]
    fn matches(self, comment: &Comment) -> bool {
        match self {
            Self::Pure => comment.is_pure(),
            Self::NoSideEffects => comment.is_no_side_effects(),
        }
    }

    /// Canonical literal to emit when no verbatim source is available.
    /// `newline_after = true` is used at statement-level emission sites
    /// (function declarations, exports), `false` at inline emission sites
    /// (call / new / function expressions).
    #[inline]
    fn canonical(self, newline_after: bool) -> &'static str {
        match (self, newline_after) {
            (Self::Pure, false) => "/* @__PURE__ */ ",
            (Self::Pure, true) => "/* @__PURE__ */\n",
            (Self::NoSideEffects, false) => "/* @__NO_SIDE_EFFECTS__ */ ",
            (Self::NoSideEffects, true) => "/* @__NO_SIDE_EFFECTS__ */\n",
        }
    }
}

impl Codegen<'_> {
    pub(crate) fn build_comments(&mut self, comments: &[Comment]) {
        if self.options.comments == CommentOptions::disabled() {
            return;
        }
        // Each retained comment can create at most one map entry. Reserving
        // this upper bound avoids incremental map growth while preprocessing.
        self.comments.reserve(comments.len());
        for comment in comments {
            // Stash pure / no-side-effects comments by `attached_to` so the
            // emission site can recover the verbatim source text instead of
            // falling back to the canonical literal (rolldown#9408).
            // Best-effort: when several annotation comments share an
            // `attached_to`, only the last survives; the emission site falls
            // back to the canonical literal for the dropped ones.
            if comment.is_pure() || comment.is_no_side_effects() {
                if comment.is_leading() && self.options.print_annotation_comment() {
                    self.annotation_comments.insert(comment.attached_to, *comment);
                }
                continue;
            }

            let mut add = false;
            if comment.is_leading() {
                add = (comment.is_legal() && self.options.print_legal_comment())
                    || (comment.is_jsdoc() && self.options.print_jsdoc_comment())
                    || (comment.is_annotation() && self.options.print_annotation_comment())
                    || (comment.is_normal() && self.options.print_normal_comment());
            }

            if add {
                self.has_property_key_annotations |= comment.is_property_key_annotation();
                if preserve_when_orphaned(*comment)
                    && let Err(idx) = self.orphan_comment_keys.binary_search(&comment.attached_to)
                {
                    self.orphan_comment_keys.insert(idx, comment.attached_to);
                }
                self.comments.entry(comment.attached_to).or_default().push(*comment);
            }
        }
    }

    pub(crate) fn has_comment(&self, start: u32) -> bool {
        self.comments.contains_key(&start)
    }

    /// Emit a pure / no-side-effects annotation comment for the AST node at
    /// `start`, falling back to the canonical literal when no verbatim source
    /// can be recovered.
    ///
    /// The fallback covers four cases:
    /// - no annotation comment is stashed at `start`,
    /// - the stashed comment's kind doesn't match the emission site (e.g. a
    ///   `@__NO_SIDE_EFFECTS__` slot being queried by a `CallExpression`
    ///   site that needs `@__PURE__`),
    /// - the comment is a line comment but the site can't break the line, or
    /// - source text is unavailable (e.g. the [`Codegen::print_expression`]
    ///   path that skips [`Codegen::build_comments`]).
    ///
    /// Export sites pass `self.span.start` and only recover verbatim when the
    /// annotation precedes the `export` keyword. The rarer
    /// `export /* @__NO_SIDE_EFFECTS__ */ function …` form (annotation between
    /// `export` and `function`) attaches to the inner function's span and
    /// falls back to canonical here.
    pub(crate) fn print_annotation_comment(
        &mut self,
        start: u32,
        kind: AnnotationKind,
        newline_after: bool,
    ) {
        if self.source_text.is_some()
            && let Some(comment) = self.annotation_comments.get(&start).copied()
            && kind.matches(&comment)
            // Inline line comments would swallow the rest of the line.
            && (!comment.is_line() || newline_after)
        {
            self.annotation_comments.remove(&start);
            self.print_comment(&comment);
            if newline_after {
                self.print_hard_newline();
            } else {
                self.print_str(" ");
            }
            return;
        }
        self.print_str(kind.canonical(newline_after));
    }

    pub(crate) fn print_leading_comments(&mut self, start: u32) {
        if let Some(comments) = self.comments.remove(&start) {
            self.print_comments(&comments);
        }
    }

    pub(crate) fn get_comments(&mut self, start: u32) -> Option<CommentList> {
        if self.comments.is_empty() {
            return None;
        }
        self.comments.remove(&start)
    }

    #[inline]
    pub(crate) fn print_comments_at(&mut self, start: u32) {
        if let Some(comments) = self.get_comments(start) {
            self.print_comments(&comments);
        }
    }

    /// Print leading comments at `start` and glue the next token to them: after a
    /// group ending in a newline (line comment / `followed_by_newline`), print the
    /// indent — mid-expression callers have no statement machinery to do it, and an
    /// unindented next token renders differently once the parser re-anchors the
    /// comments to it (codegen would no longer be idempotent). Otherwise consume the
    /// pending indent-as-space so the token glues with a single space.
    #[inline]
    pub(crate) fn print_leading_comments_anchored_to_self(&mut self, start: u32) {
        if let Some(comments) = self.get_comments(start) {
            self.print_comments(&comments);
            if self.last_byte() == Some(b'\n') {
                self.print_indent();
            } else {
                self.consume_pending_indent_space();
            }
        }
    }

    /// Print a property-key annotation attached directly to a string or template literal.
    #[inline]
    pub(crate) fn print_property_key_annotation(&mut self, start: u32) {
        if !self.has_property_key_annotations {
            return;
        }
        if self.comments.get(&start).is_some_and(|comments| {
            comments.iter().any(|comment| comment.is_property_key_annotation())
        }) {
            self.print_leading_comments_anchored_to_self(start);
        }
    }

    /// Print comments attached to an expression that survives codegen.
    ///
    /// Probes the parenthesized layers too: `a || /* c */ (x)` anchors the
    /// comment at the `(`, `a || (/* c */ x)` at `x` — an operand printer only
    /// sees one node, so the walk happens here for every emission site.
    pub(crate) fn print_leading_comments_before_expression(&mut self, expression: &Expression<'_>) {
        if self.comments.is_empty() || is_pife_function(expression) {
            return;
        }
        self.print_leading_comments_anchored_to_self(expression.span().start);
        if let Expression::ParenthesizedExpression(paren) = expression {
            self.print_leading_comments_before_expression(&paren.expression);
        }
    }

    /// Print an expression's comment group only when it contains an annotation
    /// comment, probing parenthesized layers like
    /// [`Self::print_leading_comments_before_expression`].
    ///
    /// This is the variant for emission sites that mutating consumers move
    /// statements into (the minifier merges `if(a)x;if(b)x;` into
    /// `if(a||(b,..))x`; rolldown finalizes moved nodes with their original
    /// spans). Comments are anchored by source position, so a dissolved
    /// statement's leading normal-comment group can coincide with the moved
    /// operand's span start — printing it there misplaces statement-level
    /// trivia inside an expression and is not idempotent
    /// (`test_normal_comment_before_logical_rhs_not_printed` documents the
    /// falsifier). Annotations are the one comment kind with expression-level
    /// meaning, so they still pass through.
    pub(crate) fn print_annotation_comments_before_expression(
        &mut self,
        expression: &Expression<'_>,
    ) {
        if self.comments.is_empty() || is_pife_function(expression) {
            return;
        }
        let start = expression.span().start;
        if self
            .comments
            .get(&start)
            .is_some_and(|comments| comments.iter().any(|comment| comment.is_annotation()))
        {
            self.print_leading_comments_anchored_to_self(start);
        }
        if let Expression::ParenthesizedExpression(paren) = expression {
            self.print_annotation_comments_before_expression(&paren.expression);
        }
    }

    /// Whether an orphan comment with `attached_to < end` is still pending.
    /// Used by block emitters to keep an empty body multi-line.
    #[inline]
    pub(crate) fn has_orphan_comments_before(&self, end: u32) -> bool {
        self.orphan_comment_keys
            .iter()
            .take_while(|&&k| k < end)
            .any(|k| self.comments.contains_key(k))
    }

    /// Drain pending orphan comments with `attached_to < end` and emit them in
    /// source order. Called at every statement boundary so legal and file-level
    /// coverage comments survive when their original anchor was removed by an
    /// upstream pass.
    #[inline]
    pub(crate) fn print_orphan_comments_before(&mut self, end: u32) {
        if self.orphan_comment_keys.is_empty() {
            return;
        }
        let idx = self.orphan_comment_keys.partition_point(|&k| k < end);
        if idx == 0 {
            return;
        }
        // Concatenate across keys so `print_comments` sees one sequence;
        // per-key calls would leak `print_next_indent_as_space` and produce
        // stray leading spaces.
        let mut orphans: Vec<Comment> = Vec::new();
        let comments = &mut self.comments;
        for k in self.orphan_comment_keys.drain(..idx) {
            let Some(entry) = comments.get_mut(&k) else { continue };
            debug_assert!(entry.iter().any(|c| preserve_when_orphaned(*c)));
            entry.retain(|comment| {
                if preserve_when_orphaned(*comment) {
                    orphans.push(*comment);
                    false
                } else {
                    true
                }
            });
            if entry.is_empty() {
                comments.remove(&k);
            }
        }
        if let Some(last) = orphans.last_mut() {
            // Orphans aren't in their original position, so the source's
            // `followed_by_newline` hint no longer applies. Force it on so
            // `print_comments` emits a trailing newline instead of setting
            // `print_next_indent_as_space` — otherwise the next indent (often
            // before `}`) collapses to a space and pass 2 stops matching.
            last.set_followed_by_newline(true);
            self.print_comments(&orphans);
        }
    }

    /// Print comments attached to any position in the given range `(start, end)` (exclusive).
    /// Returns `true` if any comments were printed.
    pub(crate) fn print_comments_in_range(&mut self, start: u32, end: u32) -> bool {
        if self.comments.is_empty() {
            return false;
        }
        // Find and remove the first key in the range.
        let key = self.comments.keys().find(|&&k| k > start && k < end).copied();
        if let Some(key) = key {
            let comments = self.comments.remove(&key).unwrap();
            self.print_comments(&comments);
            return true;
        }
        false
    }

    pub(crate) fn print_expr_comments(&mut self, start: u32) -> bool {
        if self.comments.is_empty() {
            return false;
        }
        let Some(comments) = self.comments.remove(&start) else { return false };

        for comment in &comments {
            self.print_hard_newline();
            self.print_indent();
            self.print_comment(comment);
        }

        if comments.is_empty() {
            false
        } else {
            self.print_hard_newline();
            true
        }
    }

    pub(crate) fn print_comments(&mut self, comments: &[Comment]) {
        let Some((first, rest)) = comments.split_first() else {
            return;
        };

        if first.preceded_by_newline() {
            // Skip printing newline if this comment is already on a newline.
            if let Some(b) = self.last_byte() {
                match b {
                    b'\n' => self.print_indent(),
                    b'\t' => { /* noop */ }
                    _ => {
                        self.print_hard_newline();
                        self.print_indent();
                    }
                }
            }
        } else if !self.consume_pending_indent_space()
            && matches!(self.last_byte(), None | Some(b'\n'))
        {
            // Only indent at a line start. Mid-line emission sites (`a ?? /* c */ b`,
            // `key: /* c */ value`, `${/* c */ expr}`) would otherwise get a full
            // indent injected mid-line, growing indentation on every codegen pass.
            self.print_indent();
        }
        self.print_comment(first);

        if let Some((last, middle)) = rest.split_last() {
            for comment in middle {
                if comment.preceded_by_newline() {
                    self.print_hard_newline();
                    self.print_indent();
                } else if comment.is_legal() {
                    self.print_hard_newline();
                } else {
                    self.print_soft_space();
                }
                self.print_comment(comment);
            }

            if last.preceded_by_newline() {
                self.print_hard_newline();
                self.print_indent();
            } else if last.is_legal() {
                self.print_hard_newline();
            } else {
                self.print_soft_space();
            }
            self.print_comment(last);

            if last.is_line() || last.followed_by_newline() {
                self.print_hard_newline();
            } else {
                self.print_next_indent_as_space = true;
            }
        } else if first.is_line() || first.followed_by_newline() {
            self.print_hard_newline();
        } else {
            self.print_next_indent_as_space = true;
        }
    }

    fn print_comment(&mut self, comment: &Comment) {
        let Some(source_text) = self.source_text else {
            return;
        };
        let comment_source = comment.span.source_text(source_text);
        match comment.kind {
            CommentKind::Line | CommentKind::SingleLineBlock => {
                self.print_str_escaping_script_close_tag(comment_source);
            }
            CommentKind::MultiLineBlock => {
                for line in LineTerminatorSplitter::new(comment_source) {
                    if !line.starts_with("/*") {
                        self.print_indent();
                    }
                    self.print_str_escaping_script_close_tag(line.trim_start());
                    if !line.ends_with("*/") {
                        self.print_hard_newline();
                    }
                }
            }
        }
    }

    /// Handle Eof / Linked / External Comments.
    /// Return a list of comments of linked or external.
    pub(crate) fn handle_eof_linked_or_external_comments(
        &mut self,
        program: &Program<'_>,
    ) -> Vec<Comment> {
        let legal_comments = &self.options.comments.legal;
        if matches!(legal_comments, LegalComment::None | LegalComment::Inline) {
            return vec![];
        }

        // Dedupe legal comments for smaller output size.
        let mut set = FxHashSet::default();
        let mut comments = vec![];

        let source_text = program.source_text;
        for comment in program.comments.iter().filter(|c| c.is_legal()) {
            let mut text = Cow::Borrowed(comment.span.source_text(source_text));
            if comment.is_multiline_block() {
                let mut buffer = String::with_capacity(text.len());
                // Print block comments with our own indentation.
                for line in LineTerminatorSplitter::new(&text) {
                    if !line.starts_with("/*") {
                        buffer.push('\t');
                    }
                    buffer.push_str(line.trim_start());
                    if !line.ends_with("*/") {
                        buffer.push('\n');
                    }
                }
                text = Cow::Owned(buffer);
            }
            if set.insert(text) {
                comments.push(*comment);
            }
        }

        if comments.is_empty() {
            return vec![];
        }

        match legal_comments {
            LegalComment::Eof => {
                self.print_hard_newline();
                // Clear the flag to ensure consistent formatting for all EOF comments
                self.print_next_indent_as_space = false;
                for c in comments {
                    self.print_comment(&c);
                    self.print_hard_newline();
                }
                vec![]
            }
            LegalComment::Linked(path) => {
                let path = path.clone();
                self.print_hard_newline();
                self.print_str("/*! For license information please see ");
                self.print_str(&path);
                self.print_str(" */");
                comments
            }
            LegalComment::External => comments,
            LegalComment::None | LegalComment::Inline => unreachable!(),
        }
    }
}