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

feat(transformer): add filename #2941

Merged
merged 1 commit into from
Apr 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
2 changes: 1 addition & 1 deletion crates/oxc_transformer/examples/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn main() {

let program = allocator.alloc(ret.program);
let transform_options = TransformOptions::default();
Transformer::new(&allocator, source_type, semantic, transform_options).build(program).unwrap();
Transformer::new(&allocator, path, semantic, transform_options).build(program).unwrap();

let printed = Codegen::<false>::new("", &source_text, CodegenOptions::default())
.build(program)
Expand Down
27 changes: 17 additions & 10 deletions crates/oxc_transformer/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
use std::{cell::RefCell, mem, rc::Rc};
use std::{cell::RefCell, mem, path::Path, rc::Rc};

use oxc_allocator::Allocator;
use oxc_ast::AstBuilder;
use oxc_diagnostics::Error;
use oxc_semantic::Semantic;
use oxc_span::SourceType;

pub type Ctx<'a> = Rc<TransformCtx<'a>>;

pub struct TransformCtx<'a> {
pub ast: AstBuilder<'a>,
pub source_type: SourceType,

pub semantic: Semantic<'a>,

/// <https://babeljs.io/docs/options#filename>
filename: String,

errors: RefCell<Vec<Error>>,
}

impl<'a> TransformCtx<'a> {
pub fn new(allocator: &'a Allocator, source_type: SourceType, semantic: Semantic<'a>) -> Self {
Self {
ast: AstBuilder::new(allocator),
source_type,
semantic,
errors: RefCell::new(vec![]),
}
pub fn new(allocator: &'a Allocator, source_path: &Path, semantic: Semantic<'a>) -> Self {
let ast = AstBuilder::new(allocator);
let filename = source_path
.file_stem() // omit file extension
.map_or_else(|| String::from("unknown"), |name| name.to_string_lossy().to_string());
let errors = RefCell::new(vec![]);
Self { ast, semantic, filename, errors }
}

pub fn take_errors(&self) -> Vec<Error> {
mem::take(&mut self.errors.borrow_mut())
}

pub fn filename(&self) -> &str {
&self.filename
}

/// Add an Error
#[allow(unused)]
pub fn error<T: Into<Error>>(&self, error: T) {
Expand Down
7 changes: 3 additions & 4 deletions crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod decorators;
mod react;
mod typescript;

use std::rc::Rc;
use std::{path::Path, rc::Rc};

use oxc_allocator::Allocator;
use oxc_ast::{
Expand All @@ -24,7 +24,6 @@ use oxc_ast::{
};
use oxc_diagnostics::Error;
use oxc_semantic::Semantic;
use oxc_span::SourceType;

pub use crate::{
compiler_assumptions::CompilerAssumptions,
Expand Down Expand Up @@ -66,11 +65,11 @@ pub struct Transformer<'a> {
impl<'a> Transformer<'a> {
pub fn new(
allocator: &'a Allocator,
source_type: SourceType,
source_path: &Path,
semantic: Semantic<'a>,
options: TransformOptions,
) -> Self {
let ctx = Rc::new(TransformCtx::new(allocator, source_type, semantic));
let ctx = Rc::new(TransformCtx::new(allocator, source_path, semantic));
Self {
ctx: Rc::clone(&ctx),
x0_typescript: TypeScript::new(options.typescript, &ctx),
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/react/display_name/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl<'a> ReactDisplayName<'a> {
) {
let ExportDefaultDeclarationKind::Expression(expr) = &mut decl.declaration else { return };
let Some(obj_expr) = Self::get_object_from_create_class(expr) else { return };
let name = self.ctx.ast.new_atom("input"); // TODO: use the filename
let name = self.ctx.ast.new_atom(self.ctx.filename());
self.add_display_name(obj_expr, name);
}
}
Expand Down
5 changes: 2 additions & 3 deletions crates/oxc_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ impl Oxc {
// Only lint if there are not syntax errors
if run_options.lint() && self.diagnostics.borrow().is_empty() {
let semantic = Rc::new(semantic_ret.semantic);
let lint_ctx = LintContext::new(path.into_boxed_path(), &semantic);
let lint_ctx = LintContext::new(path.clone().into_boxed_path(), &semantic);
let linter_ret = Linter::default().run(lint_ctx);
let diagnostics = linter_ret.into_iter().map(|e| e.error).collect();
self.save_diagnostics(diagnostics);
Expand Down Expand Up @@ -233,8 +233,7 @@ impl Oxc {
.build(program)
.semantic;
let options = TransformOptions::default();
let result =
Transformer::new(&allocator, source_type, semantic, options).build(program);
let result = Transformer::new(&allocator, &path, semantic, options).build(program);
if let Err(errs) = result {
self.save_diagnostics(errs);
}
Expand Down
13 changes: 10 additions & 3 deletions tasks/benchmark/benches/transformer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::Path;

use oxc_allocator::Allocator;
use oxc_benchmark::{criterion_group, criterion_main, BenchmarkId, Criterion};
use oxc_parser::Parser;
Expand All @@ -23,9 +25,14 @@ fn bench_transformer(criterion: &mut Criterion) {
SemanticBuilder::new(source_text, source_type).build(&program).semantic;
let program = allocator.alloc(program);
let transform_options = TransformOptions::default();
Transformer::new(&allocator, source_type, semantic, transform_options)
.build(program)
.unwrap();
Transformer::new(
&allocator,
Path::new(&file.file_name),
semantic,
transform_options,
)
.build(program)
.unwrap();
allocator
});
});
Expand Down
4 changes: 2 additions & 2 deletions tasks/transform_conformance/src/test_case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub trait TestCase {

let transformed_program = allocator.alloc(ret.program);

let result = Transformer::new(&allocator, source_type, semantic, self.transform_options())
let result = Transformer::new(&allocator, path, semantic, self.transform_options())
.build(transformed_program);

result.map(|()| {
Expand Down Expand Up @@ -221,7 +221,7 @@ impl TestCase for ConformanceTestCase {
.semantic;
let program = allocator.alloc(ret.program);
let transformer =
Transformer::new(&allocator, source_type, semantic, transform_options.clone());
Transformer::new(&allocator, &self.path, semantic, transform_options.clone());

let codegen_options = CodegenOptions::default();
let mut transformed_code = String::new();
Expand Down
2 changes: 1 addition & 1 deletion tasks/transform_conformance/src/ts_fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl TypeScriptFixtures {
let semantic = semantic_ret.semantic;
let transformed_program = allocator.alloc(parser_ret.program);

let result = Transformer::new(&allocator, source_type, semantic, Self::transform_options())
let result = Transformer::new(&allocator, path, semantic, Self::transform_options())
.build(transformed_program);

result
Expand Down
Loading