From 4f5c2e20da8be5049d9cbad5ae94510160ec5d23 Mon Sep 17 00:00:00 2001 From: Boshen Date: Thu, 11 Apr 2024 17:05:38 +0800 Subject: [PATCH] feat(transformer): add filename --- .../oxc_transformer/examples/transformer.rs | 2 +- crates/oxc_transformer/src/context.rs | 27 ++++++++++++------- crates/oxc_transformer/src/lib.rs | 7 +++-- .../src/react/display_name/mod.rs | 2 +- crates/oxc_wasm/src/lib.rs | 5 ++-- tasks/benchmark/benches/transformer.rs | 13 ++++++--- tasks/transform_conformance/src/test_case.rs | 4 +-- .../transform_conformance/src/ts_fixtures.rs | 2 +- 8 files changed, 37 insertions(+), 25 deletions(-) diff --git a/crates/oxc_transformer/examples/transformer.rs b/crates/oxc_transformer/examples/transformer.rs index d401f00c2f879..1013949a0191d 100644 --- a/crates/oxc_transformer/examples/transformer.rs +++ b/crates/oxc_transformer/examples/transformer.rs @@ -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::::new("", &source_text, CodegenOptions::default()) .build(program) diff --git a/crates/oxc_transformer/src/context.rs b/crates/oxc_transformer/src/context.rs index d9d5cbe40d172..1dce7a44d32fc 100644 --- a/crates/oxc_transformer/src/context.rs +++ b/crates/oxc_transformer/src/context.rs @@ -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>; pub struct TransformCtx<'a> { pub ast: AstBuilder<'a>, - pub source_type: SourceType, + pub semantic: Semantic<'a>, + + /// + filename: String, + errors: RefCell>, } 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 { mem::take(&mut self.errors.borrow_mut()) } + pub fn filename(&self) -> &str { + &self.filename + } + /// Add an Error #[allow(unused)] pub fn error>(&self, error: T) { diff --git a/crates/oxc_transformer/src/lib.rs b/crates/oxc_transformer/src/lib.rs index d79c785f995c8..eb34b2864b12d 100644 --- a/crates/oxc_transformer/src/lib.rs +++ b/crates/oxc_transformer/src/lib.rs @@ -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::{ @@ -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, @@ -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), diff --git a/crates/oxc_transformer/src/react/display_name/mod.rs b/crates/oxc_transformer/src/react/display_name/mod.rs index a3a029a9377ab..d5ce654c7cf55 100644 --- a/crates/oxc_transformer/src/react/display_name/mod.rs +++ b/crates/oxc_transformer/src/react/display_name/mod.rs @@ -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); } } diff --git a/crates/oxc_wasm/src/lib.rs b/crates/oxc_wasm/src/lib.rs index ffd57b3672ada..66dabe8462d4f 100644 --- a/crates/oxc_wasm/src/lib.rs +++ b/crates/oxc_wasm/src/lib.rs @@ -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); @@ -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); } diff --git a/tasks/benchmark/benches/transformer.rs b/tasks/benchmark/benches/transformer.rs index dc777494e83fb..06aea349cc47e 100644 --- a/tasks/benchmark/benches/transformer.rs +++ b/tasks/benchmark/benches/transformer.rs @@ -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; @@ -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 }); }); diff --git a/tasks/transform_conformance/src/test_case.rs b/tasks/transform_conformance/src/test_case.rs index bdcdb97f688b5..42bc963e130c1 100644 --- a/tasks/transform_conformance/src/test_case.rs +++ b/tasks/transform_conformance/src/test_case.rs @@ -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(|()| { @@ -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(); diff --git a/tasks/transform_conformance/src/ts_fixtures.rs b/tasks/transform_conformance/src/ts_fixtures.rs index 3e2d03b1565d3..9aee8e01b193f 100644 --- a/tasks/transform_conformance/src/ts_fixtures.rs +++ b/tasks/transform_conformance/src/ts_fixtures.rs @@ -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