Skip to content

Commit

Permalink
🐳 chore: Upgrade deno_ast to v0.43.3
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Nov 3, 2024
1 parent b09602e commit b77550a
Show file tree
Hide file tree
Showing 10 changed files with 283 additions and 32 deletions.
8 changes: 8 additions & 0 deletions docs/release_notes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Release Notes

## 1.2.0

* Upgraded deno_ast to v0.43.3
* Removed `TsBuildInfo` from `Swc4jMediaType`
* Added `Css` to `Swc4jMediaType`
* Added `verbatimModuleSyntax` to `Swc4jTranspileOptions`
* Added `Swc4jModuleKind` to `Swc4jTranspileOptions`

## 1.1.0

* Upgraded deno_ast to v0.42.2
Expand Down
8 changes: 4 additions & 4 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ crate-type = ["cdylib", "rlib"]
base64 = "0.22.1"
log = "0.4.22"
env_logger = "0.11.5"
deno_ast = { version = "0.42.2", features = [
deno_ast = { version = "0.43.3", features = [
"bundler",
"cjs",
"codegen",
Expand Down
14 changes: 11 additions & 3 deletions rust/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ pub fn transform<'local>(code: String, options: options::TransformOptions) -> Re
wr: writer,
};
match parsed_source.program_ref() {
Program::Module(module) => emitter.emit_module(module)?,
Program::Script(script) => emitter.emit_script(script)?,
ProgramRef::Module(module) => emitter.emit_module(module)?,
ProgramRef::Script(script) => emitter.emit_script(script)?,
};
let mut code = String::from_utf8(buffer.to_vec()).map_err(Error::msg)?;
if options.omit_last_semi && code.ends_with(";") {
Expand Down Expand Up @@ -201,9 +201,17 @@ pub fn transpile<'local>(code: String, options: options::TranspileOptions) -> Re
precompile_jsx_skip_elements: options.precompile_jsx_skip_elements.to_owned(),
transform_jsx: options.transform_jsx,
var_decl_imports: options.var_decl_imports,
verbatim_module_syntax: options.verbatim_module_syntax,
use_decorators_proposal: options.use_decorators_proposal,
use_ts_decorators: options.use_ts_decorators,
};
let transpile_module_options = TranspileModuleOptions {
module_kind: match options.module_kind {
enums::ModuleKind::Auto => None,
enums::ModuleKind::Esm => Some(ModuleKind::Esm),
enums::ModuleKind::Cjs => Some(ModuleKind::Cjs),
},
};
let emit_options = EmitOptions {
inline_sources: options.inline_sources,
remove_comments: !options.keep_comments,
Expand All @@ -213,7 +221,7 @@ pub fn transpile<'local>(code: String, options: options::TranspileOptions) -> Re
};
parsed_source
.clone()
.transpile(&transpile_options, &emit_options)
.transpile(&transpile_options, &transpile_module_options, &emit_options)
.map(|transpile_result| outputs::TranspileOutput::new(&options, &parsed_source, &transpile_result))
.map_err(Error::msg)
}
Expand Down
43 changes: 39 additions & 4 deletions rust/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@ declare_identifiable_enum!(
"Swc4jMediaType"
);

declare_identifiable_enum!(
JavaModuleKind,
JAVA_CLASS_MODULE_KIND,
ModuleKind,
"enums/",
"Swc4jModuleKind"
);

declare_identifiable_enum!(
JavaMetaPropKind,
JAVA_CLASS_META_PROP_KIND,
Expand Down Expand Up @@ -282,6 +290,7 @@ pub fn init<'local>(env: &mut JNIEnv<'local>) {
JAVA_CLASS_MEDIA_TYPE = Some(JavaMediaType::new(env));
JAVA_CLASS_META_PROP_KIND = Some(JavaMetaPropKind::new(env));
JAVA_CLASS_METHOD_KIND = Some(JavaMethodKind::new(env));
JAVA_CLASS_MODULE_KIND = Some(JavaModuleKind::new(env));
JAVA_CLASS_PARSE_MODE = Some(JavaParseMode::new(env));
JAVA_CLASS_SOURCE_MAP_OPTION = Some(JavaSourceMapOption::new(env));
JAVA_CLASS_TOKEN_TYPE = Some(JavaTokenType::new(env));
Expand Down Expand Up @@ -1062,8 +1071,8 @@ impl IdentifiableEnum<MediaType> for MediaType {
MediaType::Dcts => 9,
MediaType::Tsx => 10,
MediaType::Json => 11,
MediaType::Wasm => 12,
MediaType::TsBuildInfo => 13,
MediaType::Css => 12,
MediaType::Wasm => 13,
MediaType::SourceMap => 14,
MediaType::Unknown => 15,
}
Expand All @@ -1082,8 +1091,8 @@ impl IdentifiableEnum<MediaType> for MediaType {
9 => MediaType::Dcts,
10 => MediaType::Tsx,
11 => MediaType::Json,
12 => MediaType::Wasm,
13 => MediaType::TsBuildInfo,
12 => MediaType::Css,
13 => MediaType::Wasm,
14 => MediaType::SourceMap,
15 => MediaType::Unknown,
_ => MediaType::Unknown,
Expand All @@ -1109,6 +1118,32 @@ impl IdentifiableEnum<MethodKind> for MethodKind {
}
}

#[derive(Default, Debug, Copy, Clone)]
pub enum ModuleKind {
#[default]
Auto,
Esm,
Cjs,
}

impl IdentifiableEnum<ModuleKind> for ModuleKind {
fn get_id(&self) -> i32 {
match self {
ModuleKind::Auto => 0,
ModuleKind::Esm => 1,
ModuleKind::Cjs => 2,
}
}
fn parse_by_id(id: i32) -> ModuleKind {
match id {
0 => ModuleKind::Auto,
1 => ModuleKind::Esm,
2 => ModuleKind::Cjs,
_ => ModuleKind::Auto,
}
}
}

#[derive(Default, Debug, Copy, Clone)]
pub enum ParseMode {
#[default]
Expand Down
65 changes: 65 additions & 0 deletions rust/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ struct JavaSwc4jTranspileOptions {
method_get_jsx_fragment_factory: JMethodID,
method_get_jsx_import_source: JMethodID,
method_get_media_type: JMethodID,
method_get_module_kind: JMethodID,
method_get_parse_mode: JMethodID,
method_get_plugin_host: JMethodID,
method_get_precompile_jsx_dynamic_props: JMethodID,
Expand All @@ -631,6 +632,7 @@ struct JavaSwc4jTranspileOptions {
method_is_use_decorators_proposal: JMethodID,
method_is_use_ts_decorators: JMethodID,
method_is_var_decl_imports: JMethodID,
method_is_verbatim_module_syntax: JMethodID,
}
unsafe impl Send for JavaSwc4jTranspileOptions {}
unsafe impl Sync for JavaSwc4jTranspileOptions {}
Expand Down Expand Up @@ -679,6 +681,13 @@ impl JavaSwc4jTranspileOptions {
"()Lcom/caoccao/javet/swc4j/enums/Swc4jMediaType;",
)
.expect("Couldn't find method Swc4jTranspileOptions.getMediaType");
let method_get_module_kind = env
.get_method_id(
&class,
"getModuleKind",
"()Lcom/caoccao/javet/swc4j/enums/Swc4jModuleKind;",
)
.expect("Couldn't find method Swc4jTranspileOptions.getModuleKind");
let method_get_parse_mode = env
.get_method_id(
&class,
Expand Down Expand Up @@ -819,13 +828,21 @@ impl JavaSwc4jTranspileOptions {
"()Z",
)
.expect("Couldn't find method Swc4jTranspileOptions.isVarDeclImports");
let method_is_verbatim_module_syntax = env
.get_method_id(
&class,
"isVerbatimModuleSyntax",
"()Z",
)
.expect("Couldn't find method Swc4jTranspileOptions.isVerbatimModuleSyntax");
JavaSwc4jTranspileOptions {
class,
method_get_imports_not_used_as_values,
method_get_jsx_factory,
method_get_jsx_fragment_factory,
method_get_jsx_import_source,
method_get_media_type,
method_get_module_kind,
method_get_parse_mode,
method_get_plugin_host,
method_get_precompile_jsx_dynamic_props,
Expand All @@ -846,6 +863,7 @@ impl JavaSwc4jTranspileOptions {
method_is_use_decorators_proposal,
method_is_use_ts_decorators,
method_is_var_decl_imports,
method_is_verbatim_module_syntax,
}
}

Expand Down Expand Up @@ -944,6 +962,24 @@ impl JavaSwc4jTranspileOptions {
Ok(return_value)
}

pub fn get_module_kind<'local, 'a>(
&self,
env: &mut JNIEnv<'local>,
obj: &JObject<'_>,
) -> Result<JObject<'a>>
where
'local: 'a,
{
let return_value = call_as_object!(
env,
obj,
self.method_get_module_kind,
&[],
"Swc4jModuleKind get_module_kind()"
)?;
Ok(return_value)
}

pub fn get_parse_mode<'local, 'a>(
&self,
env: &mut JNIEnv<'local>,
Expand Down Expand Up @@ -1290,6 +1326,22 @@ impl JavaSwc4jTranspileOptions {
)?;
Ok(return_value)
}

pub fn is_verbatim_module_syntax<'local>(
&self,
env: &mut JNIEnv<'local>,
obj: &JObject<'_>,
) -> Result<bool>
{
let return_value = call_as_boolean!(
env,
obj,
self.method_is_verbatim_module_syntax,
&[],
"boolean is_verbatim_module_syntax()"
)?;
Ok(return_value)
}
}
/* JavaSwc4jTranspileOptions End */

Expand Down Expand Up @@ -1532,6 +1584,8 @@ pub struct TranspileOptions<'a> {
pub keep_comments: bool,
/// Media type of the source text.
pub media_type: MediaType,
/// Module kind.
pub module_kind: ModuleKind,
/// Should the code to be parsed as Module or Script,
pub parse_mode: ParseMode,
/// AST plugin host.
Expand Down Expand Up @@ -1562,6 +1616,9 @@ pub struct TranspileOptions<'a> {
/// a dynamic import. This is useful for import & export declaration support
/// in script contexts such as the Deno REPL. Defaults to `false`.
pub var_decl_imports: bool,
/// `true` changes type stripping behaviour so that _only_ `type` imports
/// are stripped.
pub verbatim_module_syntax: bool,
}

impl<'a> TranspileOptions<'a> {
Expand All @@ -1586,6 +1643,7 @@ impl<'a> Default for TranspileOptions<'a> {
jsx_import_source: None,
keep_comments: false,
media_type: MediaType::TypeScript,
module_kind: ModuleKind::Auto,
parse_mode: ParseMode::Program,
plugin_host: None,
precompile_jsx: false,
Expand All @@ -1596,6 +1654,7 @@ impl<'a> Default for TranspileOptions<'a> {
specifier: "file:///main.js".to_owned(),
transform_jsx: true,
var_decl_imports: false,
verbatim_module_syntax: false,
use_decorators_proposal: false,
use_ts_decorators: false,
}
Expand All @@ -1622,6 +1681,9 @@ impl<'local> FromJava<'local> for TranspileOptions<'local> {
let java_media_type = java_transpile_options.get_media_type(env, obj)?;
let media_type = *MediaType::from_java(env, &java_media_type)?;
delete_local_ref!(env, java_media_type);
let java_module_kind = java_transpile_options.get_module_kind(env, obj)?;
let module_kind = *ModuleKind::from_java(env, &java_module_kind)?;
delete_local_ref!(env, java_module_kind);
let java_parse_mode = java_transpile_options.get_parse_mode(env, obj)?;
let parse_mode = *ParseMode::from_java(env, &java_parse_mode)?;
delete_local_ref!(env, java_parse_mode);
Expand Down Expand Up @@ -1673,6 +1735,7 @@ impl<'local> FromJava<'local> for TranspileOptions<'local> {
let specifier = url_to_string(env, &specifier)?;
let transform_jsx = java_transpile_options.is_transform_jsx(env, obj)?;
let var_decl_imports = java_transpile_options.is_var_decl_imports(env, obj)?;
let verbatim_module_syntax = java_transpile_options.is_verbatim_module_syntax(env, obj)?;
let use_decorators_proposal = java_transpile_options.is_use_decorators_proposal(env, obj)?;
let use_ts_decorators = java_transpile_options.is_use_ts_decorators(env, obj)?;
delete_local_ref!(env, java_source_map);
Expand All @@ -1690,6 +1753,7 @@ impl<'local> FromJava<'local> for TranspileOptions<'local> {
jsx_import_source,
keep_comments,
media_type,
module_kind,
parse_mode,
plugin_host,
precompile_jsx,
Expand All @@ -1700,6 +1764,7 @@ impl<'local> FromJava<'local> for TranspileOptions<'local> {
specifier,
transform_jsx,
var_decl_imports,
verbatim_module_syntax,
use_decorators_proposal,
use_ts_decorators,
}))
Expand Down
24 changes: 11 additions & 13 deletions rust/src/outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,10 @@ impl ParseOutput {
None
};
let media_type = parsed_source.media_type();
let parse_mode = if parsed_source.is_module() {
ParseMode::Module
} else {
let parse_mode = if parsed_source.compute_is_script() {
ParseMode::Script
} else {
ParseMode::Module
};
let program = if parse_options.capture_ast {
Some(parsed_source.program())
Expand Down Expand Up @@ -356,10 +356,10 @@ pub struct TransformOutput {
impl TransformOutput {
pub fn new(parsed_source: &ParsedSource, code: String, source_map: Option<String>) -> Self {
let media_type = parsed_source.media_type();
let parse_mode = if parsed_source.is_module() {
ParseMode::Module
} else {
let parse_mode = if parsed_source.compute_is_script() {
ParseMode::Script
} else {
ParseMode::Module
};
TransformOutput {
code,
Expand Down Expand Up @@ -411,21 +411,19 @@ impl TranspileOutput {
None
};
let emitted_source = transpile_result.clone().into_source();
let code = String::from_utf8(emitted_source.source).unwrap_or_default();
let code = emitted_source.text.clone();
let media_type = parsed_source.media_type();
let parse_mode = if parsed_source.is_module() {
ParseMode::Module
} else {
let parse_mode = if parsed_source.compute_is_script() {
ParseMode::Script
} else {
ParseMode::Module
};
let program = if transpile_options.capture_ast {
Some(parsed_source.program())
} else {
None
};
let source_map = emitted_source
.source_map
.map(|source_map| String::from_utf8(source_map).unwrap_or_default());
let source_map = emitted_source.source_map.clone();
let source_text = parsed_source.text().to_string();
let tokens = if transpile_options.capture_tokens {
Some(Arc::new(parsed_source.tokens().to_vec()))
Expand Down
Loading

0 comments on commit b77550a

Please sign in to comment.