Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 6 additions & 22 deletions crates/oxc_codegen/src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,16 @@ use std::{borrow::Cow, iter::FusedIterator};
use rustc_hash::{FxHashMap, FxHashSet};

use oxc_ast::{Comment, CommentKind, ast::Program};
use oxc_syntax::identifier::{LS, PS, is_line_terminator};
use oxc_syntax::identifier::is_line_terminator;

use crate::{Codegen, LegalComment, options::CommentOptions};
use crate::{
Codegen, LegalComment,
options::CommentOptions,
str::{LS_LAST_2_BYTES, LS_OR_PS_FIRST_BYTE, PS_LAST_2_BYTES},
};

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

/// Convert `char` to UTF-8 bytes array.
const fn to_bytes<const N: usize>(ch: char) -> [u8; N] {
assert!(ch.len_utf8() == N);
let mut bytes = [0u8; N];
ch.encode_utf8(&mut bytes);
bytes
}

/// `LS` character as UTF-8 bytes.
const LS_BYTES: [u8; 3] = to_bytes(LS);
/// `PS` character as UTF-8 bytes.
const PS_BYTES: [u8; 3] = to_bytes(PS);

const LS_OR_PS_FIRST_BYTE: u8 = 0xE2;

const _: () = assert!(LS_BYTES[0] == LS_OR_PS_FIRST_BYTE);
const _: () = assert!(PS_BYTES[0] == LS_OR_PS_FIRST_BYTE);
const LS_LAST_2_BYTES: [u8; 2] = [LS_BYTES[1], LS_BYTES[2]];
const PS_LAST_2_BYTES: [u8; 2] = [PS_BYTES[1], PS_BYTES[2]];

/// Custom iterator that splits text on line terminators while handling CRLF as a single unit.
/// This avoids creating empty strings between CR and LF characters.
///
Expand Down
12 changes: 3 additions & 9 deletions crates/oxc_codegen/src/sourcemap_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ use oxc_index::{Idx, IndexVec};
use oxc_span::Span;
use oxc_syntax::identifier::{LS, PS};

// Irregular line breaks - '\u{2028}' (LS) and '\u{2029}' (PS)
const LS_OR_PS_FIRST: u8 = 0xE2;
const LS_OR_PS_SECOND: u8 = 0x80;
const LS_THIRD: u8 = 0xA8;
const PS_THIRD: u8 = 0xA9;
use crate::str::{LS_LAST_2_BYTES, LS_OR_PS_FIRST_BYTE, PS_LAST_2_BYTES};

/// Number of lines to check with linear search when translating byte position to line index
const LINE_SEARCH_LINEAR_ITERATIONS: usize = 16;
Expand Down Expand Up @@ -268,12 +264,10 @@ impl<'a> SourcemapBuilder<'a> {
_ if b.is_ascii() => {
continue;
}
LS_OR_PS_FIRST => {
LS_OR_PS_FIRST_BYTE => {
let next_byte = *iter.next().unwrap();
let next_next_byte = *iter.next().unwrap();
if next_byte != LS_OR_PS_SECOND
|| !matches!(next_next_byte, LS_THIRD | PS_THIRD)
{
if !matches!([next_byte, next_next_byte], LS_LAST_2_BYTES | PS_LAST_2_BYTES) {
last_line_is_ascii = false;
continue;
}
Expand Down
15 changes: 11 additions & 4 deletions crates/oxc_codegen/src/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ impl PrintStringState<'_> {

/// Convert `char` to UTF-8 bytes array.
const fn to_bytes<const N: usize>(ch: char) -> [u8; N] {
assert!(ch.len_utf8() == N);
let mut bytes = [0u8; N];
ch.encode_utf8(&mut bytes);
bytes
Expand All @@ -291,10 +292,16 @@ const LS_BYTES: [u8; 3] = to_bytes(LS);
/// `PS` character as UTF-8 bytes.
const PS_BYTES: [u8; 3] = to_bytes(PS);

const _: () = assert!(LS_BYTES[0] == 0xE2);
const _: () = assert!(PS_BYTES[0] == 0xE2);
const LS_LAST_2_BYTES: [u8; 2] = [LS_BYTES[1], LS_BYTES[2]];
const PS_LAST_2_BYTES: [u8; 2] = [PS_BYTES[1], PS_BYTES[2]];
/// First byte of either `LS` or `PS`
pub const LS_OR_PS_FIRST_BYTE: u8 = 0xE2;

const _: () = assert!(LS_BYTES[0] == LS_OR_PS_FIRST_BYTE);
const _: () = assert!(PS_BYTES[0] == LS_OR_PS_FIRST_BYTE);

/// Last 2 bytes of `LS` character.
pub const LS_LAST_2_BYTES: [u8; 2] = [LS_BYTES[1], LS_BYTES[2]];
/// Last 2 bytes of `PS` character.
pub const PS_LAST_2_BYTES: [u8; 2] = [PS_BYTES[1], PS_BYTES[2]];

/// `NBSP` character as UTF-8 bytes.
const NBSP_BYTES: [u8; 2] = to_bytes(NBSP);
Expand Down
Loading