Skip to content

Commit

Permalink
🦄 refactor: Revise error handling 1
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Jul 31, 2024
1 parent f16487e commit b60221c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 32 deletions.
56 changes: 43 additions & 13 deletions rust/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@

use anyhow::{Error, Result};
use base64::Engine;
use deno_ast::swc::ast::Program;
use deno_ast::swc::ast::{Module, Program, Script};
use deno_ast::swc::codegen::{text_writer::JsWriter, Config, Emitter};
use deno_ast::swc::common::{sync::Lrc, FileName, FilePathMapping, SourceMap};
use deno_ast::*;
use swc::common::util::take::Take;

use crate::{enums, options, outputs, plugin_utils};

Expand All @@ -33,14 +34,40 @@ fn parse_by_mode(
) -> Result<ParsedSource> {
let result = if let Some(plugin_host) = plugin_host {
let code: &str = &parse_params.text.to_owned();
match parse_mode {
enums::ParseMode::Module => {
parse_module_with_post_process(parse_params, |module, _| plugin_host.process_module(code, module))
}
enums::ParseMode::Script => {
parse_script_with_post_process(parse_params, |script, _| plugin_host.process_script(code, script))
}
_ => parse_program_with_post_process(parse_params, |program, _| plugin_host.process_program(code, program)),
let mut error: Option<Error> = None;
let result = match parse_mode {
enums::ParseMode::Module => parse_module_with_post_process(parse_params, |module, _| {
match plugin_host.process_module(code, module) {
Ok(module) => module,
Err(err) => {
error = Some(err);
Module::dummy()
}
}
}),
enums::ParseMode::Script => parse_script_with_post_process(parse_params, |script, _| {
match plugin_host.process_script(code, script) {
Ok(script) => script,
Err(err) => {
error = Some(err);
Script::dummy()
}
}
}),
_ => parse_program_with_post_process(parse_params, |program, _| {
match plugin_host.process_program(code, program) {
Ok(program) => program,
Err(err) => {
error = Some(err);
Program::Script(Script::dummy())
}
}
}),
};
if let Some(error) = error {
return Err(error);
} else {
result
}
} else {
match parse_mode {
Expand All @@ -53,8 +80,9 @@ fn parse_by_mode(
}

pub fn parse<'local>(code: String, options: options::ParseOptions) -> Result<outputs::ParseOutput> {
let specifier = options.get_specifier()?;
let parse_params = ParseParams {
specifier: options.get_specifier(),
specifier,
text: code.into(),
media_type: options.media_type,
capture_tokens: options.capture_tokens,
Expand All @@ -67,8 +95,9 @@ pub fn parse<'local>(code: String, options: options::ParseOptions) -> Result<out
}

pub fn transform<'local>(code: String, options: options::TransformOptions) -> Result<outputs::TransformOutput> {
let specifier = options.get_specifier()?;
let parse_params = ParseParams {
specifier: options.get_specifier(),
specifier: specifier.clone(),
text: code.into(),
media_type: options.media_type,
capture_tokens: false,
Expand All @@ -79,7 +108,7 @@ pub fn transform<'local>(code: String, options: options::TransformOptions) -> Re
let code: String = parse_params.text.clone().to_string();
let parsed_source = parse_by_mode(parse_params, options.parse_mode, &mut plugin_host)?;
let source_map = Lrc::new(SourceMap::new(FilePathMapping::empty()));
let filename = Lrc::new(FileName::Url(options.get_specifier()));
let filename = Lrc::new(FileName::Url(specifier));
source_map.new_source_file(filename, code);
let mut buffer = vec![];
let mut source_map_buffer = vec![];
Expand Down Expand Up @@ -141,8 +170,9 @@ pub fn transform<'local>(code: String, options: options::TransformOptions) -> Re
}

pub fn transpile<'local>(code: String, options: options::TranspileOptions) -> Result<outputs::TranspileOutput> {
let specifier = options.get_specifier()?;
let parse_params = ParseParams {
specifier: options.get_specifier(),
specifier,
text: code.into(),
media_type: options.media_type,
capture_tokens: options.capture_tokens,
Expand Down
19 changes: 8 additions & 11 deletions rust/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
* limitations under the License.
*/

use anyhow::{Error, Result};
use deno_ast::ModuleSpecifier;
use jni::objects::{GlobalRef, JMethodID, JObject, JString};
use jni::signature::{Primitive, ReturnType};
use jni::JNIEnv;

use deno_ast::ModuleSpecifier;

use crate::enums::*;
use crate::jni_utils::*;
use crate::plugin_utils::PluginHost;
Expand Down Expand Up @@ -1325,9 +1325,8 @@ pub struct ParseOptions<'a> {
}

impl<'a> ParseOptions<'a> {
pub fn get_specifier(&self) -> ModuleSpecifier {
ModuleSpecifier::parse(self.specifier.as_str())
.unwrap_or_else(|_| ModuleSpecifier::parse(&"file:///main.js").unwrap())
pub fn get_specifier(&self) -> Result<ModuleSpecifier> {
ModuleSpecifier::parse(self.specifier.as_str()).map_err(Error::msg)
}
}

Expand Down Expand Up @@ -1422,9 +1421,8 @@ pub struct TransformOptions<'a> {
}

impl<'a> TransformOptions<'a> {
pub fn get_specifier(&self) -> ModuleSpecifier {
ModuleSpecifier::parse(self.specifier.as_str())
.unwrap_or_else(|_| ModuleSpecifier::parse(&"file:///main.js").unwrap())
pub fn get_specifier(&self) -> Result<ModuleSpecifier> {
ModuleSpecifier::parse(self.specifier.as_str()).map_err(Error::msg)
}
}

Expand Down Expand Up @@ -1565,9 +1563,8 @@ pub struct TranspileOptions<'a> {
}

impl<'a> TranspileOptions<'a> {
pub fn get_specifier(&self) -> ModuleSpecifier {
ModuleSpecifier::parse(self.specifier.as_str())
.unwrap_or_else(|_| ModuleSpecifier::parse(&"file:///main.js").unwrap())
pub fn get_specifier(&self) -> Result<ModuleSpecifier> {
ModuleSpecifier::parse(self.specifier.as_str()).map_err(Error::msg)
}
}

Expand Down
16 changes: 8 additions & 8 deletions rust/src/plugin_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
* limitations under the License.
*/

use anyhow::Result;
use deno_ast::swc::ast::{Module, Program, Script};
use jni::objects::{GlobalRef, JMethodID, JObject};
use jni::signature::{Primitive, ReturnType};
use jni::sys::jvalue;
use jni::JNIEnv;

use deno_ast::swc::ast::{Module, Program, Script};

use crate::jni_utils::*;
use crate::span_utils::{ByteToIndexMap, RegisterWithMap, ToJavaWithMap};

Expand All @@ -39,7 +39,7 @@ impl<'local> PluginHost<'local> {
}
}

pub fn process_module(&mut self, s: &str, module: Module) -> Module {
pub fn process_module(&mut self, s: &str, module: Module) -> Result<Module> {
let java_class = unsafe { JAVA_CLASS_I_PLUGIN_HOST.as_ref().unwrap() };
let mut map = ByteToIndexMap::new();
module.register_with_map(&mut map);
Expand All @@ -51,10 +51,10 @@ impl<'local> PluginHost<'local> {
module
};
delete_local_ref!(self.env, java_module);
module
Ok(module)
}

pub fn process_program(&mut self, s: &str, program: Program) -> Program {
pub fn process_program(&mut self, s: &str, program: Program) -> Result<Program> {
let java_class = unsafe { JAVA_CLASS_I_PLUGIN_HOST.as_ref().unwrap() };
let mut map = ByteToIndexMap::new();
program.register_with_map(&mut map);
Expand All @@ -66,10 +66,10 @@ impl<'local> PluginHost<'local> {
program
};
delete_local_ref!(self.env, java_program);
program
Ok(program)
}

pub fn process_script(&mut self, s: &str, script: Script) -> Script {
pub fn process_script(&mut self, s: &str, script: Script) -> Result<Script> {
let java_class = unsafe { JAVA_CLASS_I_PLUGIN_HOST.as_ref().unwrap() };
let mut map = ByteToIndexMap::new();
script.register_with_map(&mut map);
Expand All @@ -81,7 +81,7 @@ impl<'local> PluginHost<'local> {
script
};
delete_local_ref!(self.env, java_script);
script
Ok(script)
}
}

Expand Down

0 comments on commit b60221c

Please sign in to comment.