Skip to content

Commit 4015a50

Browse files
authored
feat(codemod-sandbox): integrate OxcResolver for module resolution (#1627)
1 parent 65d8df9 commit 4015a50

File tree

15 files changed

+497
-117
lines changed

15 files changed

+497
-117
lines changed

Cargo.lock

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cli/src/commands/jssg/run.rs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ use clap::Args;
33
use codemod_sandbox::sandbox::{
44
engine::{ExecutionConfig, ExecutionEngine},
55
filesystem::{RealFileSystem, WalkOptions},
6-
loaders::FileSystemLoader,
7-
resolvers::FileSystemResolver,
6+
resolvers::OxcResolver,
87
};
98
use std::{path::Path, sync::Arc};
109

1110
use crate::dirty_git_check;
11+
use codemod_sandbox::utils::project_discovery::find_tsconfig;
1212

1313
#[derive(Args, Debug)]
1414
pub struct Command {
@@ -51,22 +51,37 @@ pub async fn handler(args: &Command) -> Result<()> {
5151
let js_file_path = Path::new(&args.js_file);
5252
let target_directory = Path::new(&args.target_directory);
5353

54-
// Set up the new modular system
54+
dirty_git_check::dirty_check(args.allow_dirty)?;
55+
56+
// Verify the JavaScript file exists
57+
if !js_file_path.exists() {
58+
anyhow::bail!(
59+
"JavaScript file '{}' does not exist",
60+
js_file_path.display()
61+
);
62+
}
63+
64+
// Set up the new modular system with OxcResolver
5565
let filesystem = Arc::new(RealFileSystem::new());
5666
let script_base_dir = js_file_path
5767
.parent()
5868
.unwrap_or(Path::new("."))
5969
.to_path_buf();
60-
let resolver = Arc::new(FileSystemResolver::new(
61-
filesystem.clone(),
62-
script_base_dir.clone(),
63-
));
64-
let loader = Arc::new(FileSystemLoader::new(filesystem.clone()));
6570

66-
let mut config = ExecutionConfig::new(filesystem, resolver, loader, script_base_dir);
67-
let mut walk_options = WalkOptions::default();
71+
let tsconfig_path = find_tsconfig(&script_base_dir);
6872

69-
dirty_git_check::dirty_check(args.allow_dirty)?;
73+
let resolver = Arc::new(
74+
match tsconfig_path {
75+
Some(tsconfig_path) => {
76+
OxcResolver::with_tsconfig(script_base_dir.clone(), tsconfig_path)
77+
}
78+
None => OxcResolver::new(script_base_dir.clone()),
79+
}
80+
.map_err(|e| anyhow::anyhow!("Failed to create OxcResolver: {e}"))?,
81+
);
82+
83+
let mut config = ExecutionConfig::new(filesystem, resolver, script_base_dir);
84+
let mut walk_options = WalkOptions::default();
7085

7186
// Apply command line options
7287
if args.no_gitignore {
@@ -99,14 +114,6 @@ pub async fn handler(args: &Command) -> Result<()> {
99114

100115
config = config.with_walk_options(walk_options);
101116

102-
// Verify the JavaScript file exists
103-
if !js_file_path.exists() {
104-
anyhow::bail!(
105-
"JavaScript file '{}' does not exist",
106-
js_file_path.display()
107-
);
108-
}
109-
110117
// Create and run the execution engine
111118
let engine = ExecutionEngine::new(config);
112119
let stats = engine

crates/cli/src/commands/jssg/testing/runner.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use std::path::{Path, PathBuf};
55
use std::sync::Arc;
66
use tokio::time::timeout;
77

8+
use codemod_sandbox::utils::project_discovery::find_tsconfig;
9+
810
use crate::commands::jssg::testing::{
911
config::TestOptions,
1012
fixtures::{TestCase, TestError, TestFile},
@@ -14,8 +16,7 @@ use codemod_sandbox::sandbox::{
1416
engine::{ExecutionConfig, ExecutionEngine},
1517
errors::ExecutionError,
1618
filesystem::RealFileSystem,
17-
loaders::FileSystemLoader,
18-
resolvers::FileSystemResolver,
19+
resolvers::OxcResolver,
1920
};
2021

2122
#[derive(Debug, Clone)]
@@ -163,13 +164,20 @@ impl TestRunner {
163164
.parent()
164165
.unwrap_or(Path::new("."))
165166
.to_path_buf();
166-
let resolver = Arc::new(FileSystemResolver::new(
167-
filesystem.clone(),
168-
script_base_dir.clone(),
169-
));
170-
let loader = Arc::new(FileSystemLoader::new(filesystem.clone()));
171167

172-
let config = ExecutionConfig::new(filesystem, resolver, loader, script_base_dir)
168+
let tsconfig_path = find_tsconfig(&script_base_dir);
169+
170+
let resolver = Arc::new(
171+
match tsconfig_path {
172+
Some(tsconfig_path) => {
173+
OxcResolver::with_tsconfig(script_base_dir.clone(), tsconfig_path)
174+
}
175+
None => OxcResolver::new(script_base_dir.clone()),
176+
}
177+
.map_err(|e| anyhow::anyhow!("Failed to create OxcResolver: {e}"))?,
178+
);
179+
180+
let config = ExecutionConfig::new(filesystem, resolver, script_base_dir)
173181
.with_language(language_enum);
174182

175183
let engine = ExecutionEngine::new(config);
@@ -224,11 +232,7 @@ impl TestRunner {
224232
}
225233

226234
async fn execute_test_case(
227-
engine: &ExecutionEngine<
228-
RealFileSystem,
229-
FileSystemResolver<RealFileSystem>,
230-
FileSystemLoader<RealFileSystem>,
231-
>,
235+
engine: &ExecutionEngine<RealFileSystem, OxcResolver>,
232236
test_case: &TestCase,
233237
codemod_path: &Path,
234238
options: &TestOptions,
@@ -314,11 +318,7 @@ impl TestRunner {
314318
}
315319

316320
async fn create_expected_files(
317-
engine: &ExecutionEngine<
318-
RealFileSystem,
319-
FileSystemResolver<RealFileSystem>,
320-
FileSystemLoader<RealFileSystem>,
321-
>,
321+
engine: &ExecutionEngine<RealFileSystem, OxcResolver>,
322322
test_case: &TestCase,
323323
codemod_path: &Path,
324324
) -> Result<()> {

crates/codemod-sandbox/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ tokio = { version = "1.0", features = [
1515
"fs",
1616
], optional = true }
1717
bytes = "1.0"
18+
oxc_resolver = "11.6"
1819
swc_core = { version = "33.0", features = [
1920
"common",
2021
"base",

crates/codemod-sandbox/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub mod capabilities;
44
mod plugins;
55
mod rquickjs_compat;
66
pub mod sandbox;
7-
mod utils;
7+
pub mod utils;
88

99
#[cfg(feature = "native")]
1010
pub use ast_grep::{

crates/codemod-sandbox/src/sandbox/engine/config.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
use ast_grep_language::SupportLang;
22

33
use crate::sandbox::filesystem::{FileSystem, WalkOptions};
4-
use crate::sandbox::loaders::ModuleLoader;
54
use crate::sandbox::resolvers::ModuleResolver;
65
use std::path::PathBuf;
76
use std::sync::Arc;
87

98
/// Configuration for JavaScript execution
10-
pub struct ExecutionConfig<F, R, L>
9+
pub struct ExecutionConfig<F, R>
1110
where
1211
F: FileSystem,
1312
R: ModuleResolver,
14-
L: ModuleLoader,
1513
{
1614
/// Filesystem implementation to use
1715
pub filesystem: Arc<F>,
1816
/// Module resolver implementation to use
1917
pub resolver: Arc<R>,
20-
/// Module loader implementation to use
21-
pub loader: Arc<L>,
2218
/// Base directory for script resolution
2319
pub script_base_dir: PathBuf,
2420
/// Maximum number of concurrent threads for parallel execution
@@ -37,22 +33,15 @@ where
3733
pub dry_run: bool,
3834
}
3935

40-
impl<F, R, L> ExecutionConfig<F, R, L>
36+
impl<F, R> ExecutionConfig<F, R>
4137
where
4238
F: FileSystem,
4339
R: ModuleResolver,
44-
L: ModuleLoader,
4540
{
46-
pub fn new(
47-
filesystem: Arc<F>,
48-
resolver: Arc<R>,
49-
loader: Arc<L>,
50-
script_base_dir: PathBuf,
51-
) -> Self {
41+
pub fn new(filesystem: Arc<F>, resolver: Arc<R>, script_base_dir: PathBuf) -> Self {
5242
Self {
5343
filesystem,
5444
resolver,
55-
loader,
5645
script_base_dir,
5746
max_threads: None,
5847
walk_options: WalkOptions::default(),

crates/codemod-sandbox/src/sandbox/engine/execution_engine.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use crate::ast_grep::AstGrepModule;
55
use crate::rquickjs_compat::{CatchResultExt, Function, Module};
66
use crate::sandbox::errors::ExecutionError;
77
use crate::sandbox::filesystem::FileSystem;
8-
use crate::sandbox::loaders::ModuleLoader;
98
use crate::sandbox::resolvers::ModuleResolver;
109
use ast_grep_language::SupportLang;
1110
use ignore::{overrides::OverrideBuilder, WalkBuilder, WalkState};
@@ -126,17 +125,16 @@ impl ExecutionOutput {
126125
///
127126
/// This engine coordinates filesystem operations, module resolution,
128127
/// and JavaScript execution across multiple files and threads.
129-
pub struct ExecutionEngine<F: FileSystem, R: ModuleResolver, L: ModuleLoader> {
130-
config: Arc<ExecutionConfig<F, R, L>>,
128+
pub struct ExecutionEngine<F: FileSystem, R: ModuleResolver> {
129+
config: Arc<ExecutionConfig<F, R>>,
131130
}
132131

133-
impl<F, R, L> ExecutionEngine<F, R, L>
132+
impl<F, R> ExecutionEngine<F, R>
134133
where
135134
F: FileSystem + 'static,
136135
R: ModuleResolver + 'static,
137-
L: ModuleLoader + 'static,
138136
{
139-
pub fn new(config: ExecutionConfig<F, R, L>) -> Self {
137+
pub fn new(config: ExecutionConfig<F, R>) -> Self {
140138
Self {
141139
config: Arc::new(config),
142140
}
@@ -363,7 +361,7 @@ where
363361

364362
/// Execute JavaScript code on a single file
365363
async fn execute_on_single_file(
366-
config: &Arc<ExecutionConfig<F, R, L>>,
364+
config: &Arc<ExecutionConfig<F, R>>,
367365
script_path: &Path,
368366
target_file_path: &Path,
369367
) -> Result<ExecutionResult, ExecutionError> {
@@ -383,7 +381,7 @@ where
383381

384382
#[cfg(feature = "native")]
385383
async fn execute_with_quickjs(
386-
config: &Arc<ExecutionConfig<F, R, L>>,
384+
config: &Arc<ExecutionConfig<F, R>>,
387385
script_path: &Path,
388386
target_file_path: &Path,
389387
) -> Result<ExecutionResult, ExecutionError> {
@@ -449,7 +447,7 @@ where
449447
/// This is the core execution logic that doesn't touch the filesystem
450448
#[cfg(feature = "native")]
451449
async fn execute_codemod_with_quickjs(
452-
config: &Arc<ExecutionConfig<F, R, L>>,
450+
config: &Arc<ExecutionConfig<F, R>>,
453451
script_path: &Path,
454452
file_path: &Path,
455453
content: &str,
@@ -482,8 +480,7 @@ where
482480
built_in_resolver = built_in_resolver.add_name("codemod:ast-grep");
483481
built_in_loader = built_in_loader.with_module("codemod:ast-grep", AstGrepModule);
484482

485-
// Use our new QuickJS adapters with the script's base directory
486-
let fs_resolver = QuickJSResolver::new(config.script_base_dir.clone());
483+
let fs_resolver = QuickJSResolver::new(Arc::clone(&config.resolver));
487484
let fs_loader = QuickJSLoader;
488485

489486
// Combine resolvers and loaders

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy