From b77550a8dabeebdce01cc6c663cd29e001d3cc1a Mon Sep 17 00:00:00 2001 From: Sam Cao Date: Sun, 3 Nov 2024 15:18:23 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=B3=20chore:=20Upgrade=20deno=5Fast=20?= =?UTF-8?q?to=20v0.43.3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/release_notes.md | 8 ++ rust/Cargo.lock | 8 +- rust/Cargo.toml | 2 +- rust/src/core.rs | 14 +++- rust/src/enums.rs | 43 +++++++++- rust/src/options.rs | 65 ++++++++++++++++ rust/src/outputs.rs | 24 +++--- .../javet/swc4j/enums/Swc4jMediaType.java | 7 +- .../javet/swc4j/enums/Swc4jModuleKind.java | 78 +++++++++++++++++++ .../swc4j/options/Swc4jTranspileOptions.java | 66 +++++++++++++++- 10 files changed, 283 insertions(+), 32 deletions(-) create mode 100644 src/main/java/com/caoccao/javet/swc4j/enums/Swc4jModuleKind.java diff --git a/docs/release_notes.md b/docs/release_notes.md index 10794f35..49644d39 100644 --- a/docs/release_notes.md +++ b/docs/release_notes.md @@ -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 diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 8ba71871..249a58a3 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -310,9 +310,9 @@ dependencies = [ [[package]] name = "deno_ast" -version = "0.42.2" +version = "0.43.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2b9d03b1bbeeecdac54367f075d572131736d06c5be3bc49037855bc5ab1bbb" +checksum = "48d00b724e06d2081a141ec1155756a0b465d413d8e2a7515221f61d482eb2ee" dependencies = [ "base64 0.21.7", "deno_media_type", @@ -356,9 +356,9 @@ dependencies = [ [[package]] name = "deno_media_type" -version = "0.1.4" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a8978229b82552bf8457a0125aa20863f023619cfc21ebb007b1e571d68fd85b" +checksum = "7fcf552fbdedbe81c89705349d7d2485c7051382b000dfddbdbf7fc25931cf83" dependencies = [ "data-url", "serde", diff --git a/rust/Cargo.toml b/rust/Cargo.toml index faab3e96..901e1ad9 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -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", diff --git a/rust/src/core.rs b/rust/src/core.rs index a3bd9198..233fa86a 100644 --- a/rust/src/core.rs +++ b/rust/src/core.rs @@ -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(";") { @@ -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, @@ -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) } diff --git a/rust/src/enums.rs b/rust/src/enums.rs index 06c17909..873056d2 100644 --- a/rust/src/enums.rs +++ b/rust/src/enums.rs @@ -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, @@ -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)); @@ -1062,8 +1071,8 @@ impl IdentifiableEnum 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, } @@ -1082,8 +1091,8 @@ impl IdentifiableEnum 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, @@ -1109,6 +1118,32 @@ impl IdentifiableEnum for MethodKind { } } +#[derive(Default, Debug, Copy, Clone)] +pub enum ModuleKind { + #[default] + Auto, + Esm, + Cjs, +} + +impl IdentifiableEnum 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] diff --git a/rust/src/options.rs b/rust/src/options.rs index 4d5aec11..2e984c83 100644 --- a/rust/src/options.rs +++ b/rust/src/options.rs @@ -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, @@ -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 {} @@ -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, @@ -819,6 +828,13 @@ 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, @@ -826,6 +842,7 @@ impl JavaSwc4jTranspileOptions { 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, @@ -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, } } @@ -944,6 +962,24 @@ impl JavaSwc4jTranspileOptions { Ok(return_value) } + pub fn get_module_kind<'local, 'a>( + &self, + env: &mut JNIEnv<'local>, + obj: &JObject<'_>, + ) -> Result> + 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>, @@ -1290,6 +1326,22 @@ impl JavaSwc4jTranspileOptions { )?; Ok(return_value) } + + pub fn is_verbatim_module_syntax<'local>( + &self, + env: &mut JNIEnv<'local>, + obj: &JObject<'_>, + ) -> Result + { + let return_value = call_as_boolean!( + env, + obj, + self.method_is_verbatim_module_syntax, + &[], + "boolean is_verbatim_module_syntax()" + )?; + Ok(return_value) + } } /* JavaSwc4jTranspileOptions End */ @@ -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. @@ -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> { @@ -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, @@ -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, } @@ -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); @@ -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); @@ -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, @@ -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, })) diff --git a/rust/src/outputs.rs b/rust/src/outputs.rs index a43fe91c..f913d7c2 100644 --- a/rust/src/outputs.rs +++ b/rust/src/outputs.rs @@ -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()) @@ -356,10 +356,10 @@ pub struct TransformOutput { impl TransformOutput { pub fn new(parsed_source: &ParsedSource, code: String, source_map: Option) -> 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, @@ -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())) diff --git a/src/main/java/com/caoccao/javet/swc4j/enums/Swc4jMediaType.java b/src/main/java/com/caoccao/javet/swc4j/enums/Swc4jMediaType.java index c3252818..a5ed46eb 100644 --- a/src/main/java/com/caoccao/javet/swc4j/enums/Swc4jMediaType.java +++ b/src/main/java/com/caoccao/javet/swc4j/enums/Swc4jMediaType.java @@ -38,10 +38,11 @@ public enum Swc4jMediaType implements ISwc4jEnumId { Dcts(9), Tsx(10), Json(11), - Wasm(12), - TsBuildInfo(13), + Css(12), + Wasm(13), SourceMap(14), - Unknown(15); + Unknown(15), + ; private static final int LENGTH = values().length; private static final Swc4jMediaType[] TYPES = new Swc4jMediaType[LENGTH]; diff --git a/src/main/java/com/caoccao/javet/swc4j/enums/Swc4jModuleKind.java b/src/main/java/com/caoccao/javet/swc4j/enums/Swc4jModuleKind.java new file mode 100644 index 00000000..7ef27f4e --- /dev/null +++ b/src/main/java/com/caoccao/javet/swc4j/enums/Swc4jModuleKind.java @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2024. caoccao.com Sam Cao + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.caoccao.javet.swc4j.enums; + +import com.caoccao.javet.swc4j.interfaces.ISwc4jEnumId; + +import java.util.stream.Stream; + +/** + * The kind of module being transpiled. + * Defaults to being derived from the media type of the parsed source. + * + * @since 1.2.0 + */ +public enum Swc4jModuleKind implements ISwc4jEnumId { + /** + * Auto. + * + * @since 1.2.0 + */ + Auto(0), + /** + * Es Module. + * + * @since 1.2.0 + */ + Esm(1), + /** + * CommonJS Module. + * + * @since 1.2.0 + */ + Cjs(2), + ; + + private static final int LENGTH = values().length; + private static final Swc4jModuleKind[] TYPES = new Swc4jModuleKind[LENGTH]; + + static { + Stream.of(values()).forEach(v -> TYPES[v.getId()] = v); + } + + private final int id; + + Swc4jModuleKind(int id) { + this.id = id; + } + + /** + * Parse swc4j module kind. + * + * @param id the id + * @return the swc4j module kind + * @since 1.2.0 + */ + public static Swc4jModuleKind parse(int id) { + return id >= 0 && id < LENGTH ? TYPES[id] : Auto; + } + + @Override + public int getId() { + return id; + } +} diff --git a/src/main/java/com/caoccao/javet/swc4j/options/Swc4jTranspileOptions.java b/src/main/java/com/caoccao/javet/swc4j/options/Swc4jTranspileOptions.java index 3eda4ce1..089a7298 100644 --- a/src/main/java/com/caoccao/javet/swc4j/options/Swc4jTranspileOptions.java +++ b/src/main/java/com/caoccao/javet/swc4j/options/Swc4jTranspileOptions.java @@ -16,10 +16,7 @@ package com.caoccao.javet.swc4j.options; -import com.caoccao.javet.swc4j.enums.Swc4jImportsNotUsedAsValues; -import com.caoccao.javet.swc4j.enums.Swc4jMediaType; -import com.caoccao.javet.swc4j.enums.Swc4jParseMode; -import com.caoccao.javet.swc4j.enums.Swc4jSourceMapOption; +import com.caoccao.javet.swc4j.enums.*; import com.caoccao.javet.swc4j.jni2rust.Jni2RustClass; import com.caoccao.javet.swc4j.jni2rust.Jni2RustFilePath; import com.caoccao.javet.swc4j.jni2rust.Jni2RustMethod; @@ -112,6 +109,13 @@ public class Swc4jTranspileOptions extends Swc4jParseOptions { * @since 0.3.0 */ protected boolean keepComments; + /** + * The kind of module being transpiled. + * Defaults to being derived from the media type of the parsed source. + * + * @since 1.2.0 + */ + protected Swc4jModuleKind moduleKind; /** * Should JSX be precompiled into static strings that need to be concatenated * with dynamic content. Defaults to `false`, mutually exclusive with @@ -164,6 +168,12 @@ public class Swc4jTranspileOptions extends Swc4jParseOptions { * @since 0.1.0 */ protected boolean varDeclImports; + /** + * `true` changes type stripping behaviour so that _only_ `type` imports are stripped. + * + * @since 1.2.0 + */ + protected boolean verbatimModuleSyntax; /** * Instantiates a new Swc4j transpile options. @@ -181,12 +191,14 @@ public Swc4jTranspileOptions() { setJsxImportSource(null); setInlineSources(true); setKeepComments(false); + setModuleKind(Swc4jModuleKind.Auto); setPrecompileJsx(false); setPrecompileJsxDynamicProps(null); setPrecompileJsxSkipElements(null); setSourceMap(Swc4jSourceMapOption.Inline); setTransformJsx(true); setVarDeclImports(false); + setVerbatimModuleSyntax(false); setUseTsDecorators(false); } @@ -234,6 +246,17 @@ public String getJsxImportSource() { return jsxImportSource; } + /** + * Gets module kind. + * + * @return the module kind + * @since 1.2.0 + */ + @Jni2RustMethod + public Swc4jModuleKind getModuleKind() { + return moduleKind; + } + /** * Gets precompile jsx dynamic props. * @@ -377,6 +400,17 @@ public boolean isVarDeclImports() { return varDeclImports; } + /** + * Is verbatim module syntax. + * + * @return true : yes, false : no + * @since 1.2.0 + */ + @Jni2RustMethod + public boolean isVerbatimModuleSyntax() { + return verbatimModuleSyntax; + } + @Override public Swc4jTranspileOptions setCaptureAst(boolean captureAst) { super.setCaptureAst(captureAst); @@ -515,6 +549,18 @@ public Swc4jTranspileOptions setMediaType(Swc4jMediaType mediaType) { return this; } + /** + * Sets module kind. + * + * @param moduleKind the module kind + * @return the self + * @since 1.2.0 + */ + public Swc4jTranspileOptions setModuleKind(Swc4jModuleKind moduleKind) { + this.moduleKind = AssertionUtils.notNull(moduleKind, "Module kind"); + return this; + } + /** * Sets parse mode. * @@ -650,4 +696,16 @@ public Swc4jTranspileOptions setVarDeclImports(boolean varDeclImports) { this.varDeclImports = varDeclImports; return this; } + + /** + * Sets verbatim module syntax. + * + * @param verbatimModuleSyntax the verbatim module syntax + * @return the self + * @since 1.2.0 + */ + public Swc4jTranspileOptions setVerbatimModuleSyntax(boolean verbatimModuleSyntax) { + this.verbatimModuleSyntax = verbatimModuleSyntax; + return this; + } }