Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(oxc)!: remove passing Trivias around #6446

Merged
merged 1 commit into from
Oct 11, 2024
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
1 change: 0 additions & 1 deletion crates/oxc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ let SemanticBuilderReturn {
errors: semantic_errors,
} = SemanticBuilder::new()
.with_check_syntax_error(true) // Enable extra syntax error checking
.with_trivias(trivias) // Pass comments for JSDoc parsing
.with_build_jsdoc(true) // Enable JSDoc parsing
.with_cfg(true) // Build a Control Flow Graph
.build(&program); // Produce the `Semantic`
Expand Down
29 changes: 8 additions & 21 deletions crates/oxc/src/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{mem, ops::ControlFlow, path::Path};

use oxc_allocator::Allocator;
use oxc_ast::{ast::Program, Trivias};
use oxc_ast::ast::Program;
use oxc_codegen::{CodeGenerator, CodegenOptions, CodegenReturn, CommentOptions};
use oxc_diagnostics::OxcDiagnostic;
use oxc_isolated_declarations::{IsolatedDeclarations, IsolatedDeclarationsOptions};
Expand Down Expand Up @@ -135,11 +135,10 @@ pub trait CompilerInterface {
}

let mut program = parser_return.program;
let trivias = parser_return.trivias;

/* Isolated Declarations */
if let Some(options) = self.isolated_declaration_options() {
self.isolated_declaration(options, &allocator, &program, source_path, &trivias);
self.isolated_declaration(options, &allocator, &program, source_path);
}

/* Semantic */
Expand All @@ -158,15 +157,8 @@ pub trait CompilerInterface {
/* Transform */

if let Some(options) = self.transform_options() {
let mut transformer_return = self.transform(
options,
&allocator,
&mut program,
source_path,
&trivias,
symbols,
scopes,
);
let mut transformer_return =
self.transform(options, &allocator, &mut program, source_path, symbols, scopes);

if !transformer_return.errors.is_empty() {
self.handle_errors(transformer_return.errors);
Expand Down Expand Up @@ -210,7 +202,7 @@ pub trait CompilerInterface {
/* Codegen */

if let Some(options) = self.codegen_options() {
let ret = self.codegen(&program, source_path, &trivias, mangler, options);
let ret = self.codegen(&program, source_path, mangler, options);
self.after_codegen(ret);
}
}
Expand Down Expand Up @@ -245,15 +237,12 @@ pub trait CompilerInterface {
allocator: &'a Allocator,
program: &Program<'a>,
source_path: &Path,
trivias: &Trivias,
) {
let ret = IsolatedDeclarations::new(allocator, program.source_text, trivias, options)
.build(program);
let ret = IsolatedDeclarations::new(allocator, options).build(program);
self.handle_errors(ret.errors);
let ret = self.codegen(
&ret.program,
source_path,
trivias,
None,
self.codegen_options().unwrap_or_default(),
);
Expand All @@ -267,11 +256,10 @@ pub trait CompilerInterface {
allocator: &'a Allocator,
program: &mut Program<'a>,
source_path: &Path,
trivias: &Trivias,
symbols: SymbolTable,
scopes: ScopeTree,
) -> TransformerReturn {
Transformer::new(allocator, source_path, trivias.clone(), options)
Transformer::new(allocator, source_path, options)
.build_with_symbols_and_scopes(symbols, scopes, program)
}

Expand All @@ -292,15 +280,14 @@ pub trait CompilerInterface {
&self,
program: &Program<'_>,
source_path: &Path,
trivias: &Trivias,
mangler: Option<Mangler>,
options: CodegenOptions,
) -> CodegenReturn {
let comment_options = CommentOptions { preserve_annotate_comments: true };
let mut codegen = CodeGenerator::new()
.with_options(options)
.with_mangler(mangler)
.enable_comment(program.source_text, trivias.clone(), comment_options);
.enable_comment(program, comment_options);
if self.enable_sourcemap() {
codegen = codegen
.enable_source_map(source_path.to_string_lossy().as_ref(), program.source_text);
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub use crate::{
ast_builder::AstBuilder,
ast_builder_impl::NONE,
ast_kind::{AstKind, AstType},
trivia::{SortedComments, Trivias},
trivia::{comments_range, has_comments_between, CommentsRange},
visit::{Visit, VisitMut},
};

Expand Down
68 changes: 13 additions & 55 deletions crates/oxc_ast/src/trivia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,22 @@

use std::{
iter::FusedIterator,
ops::{Bound, Deref, RangeBounds},
sync::Arc,
ops::{Bound, RangeBounds},
};

use oxc_span::Span;

use crate::ast::comment::*;

/// Sorted set of unique trivia comments, in ascending order by starting position.
pub type SortedComments = Box<[Comment]>;

#[derive(Debug, Clone, Default)]
pub struct Trivias(Arc<TriviasImpl>);

#[derive(Debug, Default)]
pub struct TriviasImpl {
/// Unique comments, ordered by increasing span-start.
comments: SortedComments,

irregular_whitespaces: Box<[Span]>,
pub fn comments_range<R>(comments: &[Comment], range: R) -> CommentsRange<'_>
where
R: RangeBounds<u32>,
{
CommentsRange::new(comments, range.start_bound().cloned(), range.end_bound().cloned())
}

impl Deref for Trivias {
type Target = TriviasImpl;

#[inline]
fn deref(&self) -> &Self::Target {
self.0.as_ref()
}
}

impl Trivias {
pub fn new(comments: SortedComments, irregular_whitespaces: Vec<Span>) -> Trivias {
Self(Arc::new(TriviasImpl {
comments,
irregular_whitespaces: irregular_whitespaces.into_boxed_slice(),
}))
}

pub fn comments(&self) -> impl Iterator<Item = &Comment> {
self.comments.iter()
}

pub fn comments_range<R>(&self, range: R) -> CommentsRange<'_>
where
R: RangeBounds<u32>,
{
CommentsRange::new(&self.comments, range.start_bound().cloned(), range.end_bound().cloned())
}

pub fn has_comments_between(&self, span: Span) -> bool {
self.comments_range(span.start..span.end).count() > 0
}

pub fn irregular_whitespaces(&self) -> &[Span] {
&self.irregular_whitespaces
}
pub fn has_comments_between(comments: &[Comment], span: Span) -> bool {
comments_range(comments, span.start..span.end).count() > 0
}

/// Double-ended iterator over a range of comments, by starting position.
Expand Down Expand Up @@ -141,7 +100,7 @@ mod test {

#[test]
fn test_comments_range() {
let comments: SortedComments = vec![
let comments = vec![
Comment::new(0, 4, CommentKind::Line),
Comment::new(5, 9, CommentKind::Line),
Comment::new(10, 13, CommentKind::Line),
Expand All @@ -150,10 +109,9 @@ mod test {
]
.into_boxed_slice();
let full_len = comments.len();
let trivias = Trivias::new(comments, vec![]);
assert_eq!(trivias.comments_range(..).count(), full_len);
assert_eq!(trivias.comments_range(1..).count(), full_len.saturating_sub(1));
assert_eq!(trivias.comments_range(..18).count(), full_len.saturating_sub(1));
assert_eq!(trivias.comments_range(..=18).count(), full_len);
assert_eq!(comments_range(&comments, ..).count(), full_len);
assert_eq!(comments_range(&comments, 1..).count(), full_len.saturating_sub(1));
assert_eq!(comments_range(&comments, ..18).count(), full_len.saturating_sub(1));
assert_eq!(comments_range(&comments, ..=18).count(), full_len);
}
}
12 changes: 4 additions & 8 deletions crates/oxc_codegen/examples/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn main() -> std::io::Result<()> {

let printed = {
let Some(ret) = parse(&allocator, &source_text, source_type) else { return Ok(()) };
codegen(&source_text, &ret, minify)
codegen(&ret, minify)
};
println!("First time:");
println!("{printed}");
Expand All @@ -35,7 +35,7 @@ fn main() -> std::io::Result<()> {

let Some(ret) = parse(&allocator, &printed, source_type) else { return Ok(()) };
println!("Second time:");
let printed = codegen(&printed, &ret, minify);
let printed = codegen(&ret, minify);
println!("{printed}");
// Check syntax error
parse(&allocator, &printed, source_type);
Expand All @@ -59,13 +59,9 @@ fn parse<'a>(
Some(ret)
}

fn codegen(source_text: &str, ret: &ParserReturn<'_>, minify: bool) -> String {
fn codegen(ret: &ParserReturn<'_>, minify: bool) -> String {
CodeGenerator::new()
.enable_comment(
source_text,
ret.trivias.clone(),
CommentOptions { preserve_annotate_comments: true },
)
.enable_comment(&ret.program, CommentOptions { preserve_annotate_comments: true })
.with_options(CodegenOptions { minify, ..CodegenOptions::default() })
.build(&ret.program)
.code
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_codegen/src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use daachorse::DoubleArrayAhoCorasick;
use once_cell::sync::Lazy;
use rustc_hash::FxHashMap;

use oxc_ast::{Comment, CommentKind, Trivias};
use oxc_ast::{Comment, CommentKind};
use oxc_syntax::identifier::is_line_terminator;

use crate::Codegen;
Expand All @@ -20,9 +20,9 @@ impl<'a> Codegen<'a> {
self.comment_options.preserve_annotate_comments && !self.options.minify
}

pub(crate) fn build_comments(&mut self, trivias: &Trivias) {
for comment in trivias.comments().copied() {
self.comments.entry(comment.attached_to).or_default().push(comment);
pub(crate) fn build_comments(&mut self, comments: &[Comment]) {
for comment in comments {
self.comments.entry(comment.attached_to).or_default().push(*comment);
}
}

Expand Down
19 changes: 5 additions & 14 deletions crates/oxc_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ mod sourcemap_builder;

use std::borrow::Cow;

use oxc_ast::{
ast::{BindingIdentifier, BlockStatement, Expression, IdentifierReference, Program, Statement},
Trivias,
use oxc_ast::ast::{
BindingIdentifier, BlockStatement, Expression, IdentifierReference, Program, Statement,
};
use oxc_mangler::Mangler;
use oxc_span::{GetSpan, Span};
Expand Down Expand Up @@ -72,7 +71,6 @@ pub struct Codegen<'a> {
/// Original source code of the AST
source_text: Option<&'a str>,

trivias: Trivias,
comments: CommentsMap,

/// Start of comment that needs to be moved to the before VariableDeclarator
Expand Down Expand Up @@ -146,7 +144,6 @@ impl<'a> Codegen<'a> {
options: CodegenOptions::default(),
comment_options: CommentOptions::default(),
source_text: None,
trivias: Trivias::default(),
comments: CommentsMap::default(),
start_of_annotation_comment: None,
mangler: None,
Expand Down Expand Up @@ -199,16 +196,10 @@ impl<'a> Codegen<'a> {

/// Also sets the [Self::with_source_text]
#[must_use]
pub fn enable_comment(
mut self,
source_text: &'a str,
trivias: Trivias,
options: CommentOptions,
) -> Self {
pub fn enable_comment(mut self, program: &Program<'a>, options: CommentOptions) -> Self {
self.comment_options = options;
self.build_comments(&trivias);
self.trivias = trivias;
self.with_source_text(source_text)
self.build_comments(&program.comments);
self.with_source_text(program.source_text)
}

#[must_use]
Expand Down
6 changes: 1 addition & 5 deletions crates/oxc_codegen/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ pub fn codegen(source_text: &str) -> String {
let ret = Parser::new(&allocator, source_text, source_type).parse();
CodeGenerator::new()
.with_options(CodegenOptions { single_quote: true, ..CodegenOptions::default() })
.enable_comment(
source_text,
ret.trivias,
CommentOptions { preserve_annotate_comments: true },
)
.enable_comment(&ret.program, CommentOptions { preserve_annotate_comments: true })
.build(&ret.program)
.code
}
Expand Down
6 changes: 1 addition & 5 deletions crates/oxc_codegen/tests/integration/tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ pub fn test(source_text: &str, expected: &str) {
let allocator = Allocator::default();
let ret = Parser::new(&allocator, source_text, source_type).parse();
let result = CodeGenerator::new()
.enable_comment(
source_text,
ret.trivias,
CommentOptions { preserve_annotate_comments: true },
)
.enable_comment(&ret.program, CommentOptions { preserve_annotate_comments: true })
.build(&ret.program)
.code;
assert_eq!(result, expected, "\nfor source: {source_text:?}");
Expand Down
16 changes: 4 additions & 12 deletions crates/oxc_isolated_declarations/examples/isolated_declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,11 @@ fn main() {
println!("Original:\n");
println!("{source_text}\n");

let id_ret = IsolatedDeclarations::new(
&allocator,
&source_text,
&ret.trivias,
IsolatedDeclarationsOptions { strip_internal: true },
)
.build(&ret.program);
let id_ret =
IsolatedDeclarations::new(&allocator, IsolatedDeclarationsOptions { strip_internal: true })
.build(&ret.program);
let printed = CodeGenerator::new()
.enable_comment(
&source_text,
ret.trivias,
CommentOptions { preserve_annotate_comments: false },
)
.enable_comment(&ret.program, CommentOptions { preserve_annotate_comments: false })
.build(&id_ret.program)
.code;

Expand Down
Loading