Skip to content

Commit

Permalink
✨ feat: Add isScript() to output
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Mar 10, 2024
1 parent c693943 commit 29e8abb
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 11 deletions.
4 changes: 2 additions & 2 deletions rust/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn transpile<'local>(code: String, options: options::TranspileOptions) -> Re
Ok(parsed_source) => {
let emit_options = EmitOptions {
emit_metadata: options.emit_metadata,
// imports_not_used_as_values: options.imports_not_used_as_values,
imports_not_used_as_values: options.imports_not_used_as_values,
inline_source_map: options.inline_source_map,
inline_sources: options.inline_sources,
jsx_automatic: options.jsx_automatic,
Expand All @@ -50,12 +50,12 @@ pub fn transpile<'local>(code: String, options: options::TranspileOptions) -> Re
source_map: options.source_map,
transform_jsx: options.transform_jsx,
var_decl_imports: options.var_decl_imports,
..Default::default()
};
match parsed_source.transpile(&emit_options) {
Ok(transpiled_js_code) => Ok(outputs::TranspileOutput {
code: transpiled_js_code.text,
module: parsed_source.is_module(),
script: parsed_source.is_script(),
source_map: transpiled_js_code.source_map,
}),
Err(e) => Err(e.to_string()),
Expand Down
11 changes: 8 additions & 3 deletions rust/src/outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl JavaTranspileOutput {
.new_global_ref(class)
.expect("Couldn't globalize class Swc4jTranspileOutput");
let method_constructor = env
.get_method_id(&class, "<init>", "(Ljava/lang/String;Ljava/lang/String;Z)V")
.get_method_id(&class, "<init>", "(Ljava/lang/String;ZZLjava/lang/String;)V")
.expect("Couldn't find method Swc4jTranspileOutput.Swc4jTranspileOutput");
JavaTranspileOutput {
class,
Expand All @@ -52,14 +52,15 @@ impl JavaTranspileOutput {
env: &mut JNIEnv<'local>,
code: jvalue,
module: jvalue,
script: jvalue,
source_map: jvalue,
) -> JObject<'a>
where
'local: 'a,
{
unsafe {
env
.new_object_unchecked(&self.class, self.method_constructor, &[code, source_map, module])
.new_object_unchecked(&self.class, self.method_constructor, &[code, module, script, source_map])
.expect("Couldn't create Swc4jTranspileOutput")
}
}
Expand All @@ -83,6 +84,7 @@ pub trait ToJniType {
pub struct TranspileOutput {
pub code: String,
pub module: bool,
pub script: bool,
pub source_map: Option<String>,
}

Expand All @@ -97,12 +99,15 @@ impl ToJniType for TranspileOutput {
let module = jvalue {
z: if self.module { 1u8 } else { 0u8 },
};
let script = jvalue {
z: if self.script { 1u8 } else { 0u8 },
};
let source_map = jvalue {
l: match &self.source_map {
Some(s) => converter::string_to_jstring(env, &s).as_raw(),
None => null_mut(),
},
};
unsafe { JAVA_TRANSPILE_OUTPUT.as_ref().unwrap() }.create(env, code, module, source_map)
unsafe { JAVA_TRANSPILE_OUTPUT.as_ref().unwrap() }.create(env, code, module, script, source_map)
}
}
10 changes: 8 additions & 2 deletions rust/tests/test_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,14 @@ fn test_transpile_type_script_without_inline_source_map() {
assert!(output.is_ok());
let output = output.unwrap();
match parse_mode {
enums::ParseMode::Script => assert!(!output.module),
_ => assert!(output.module),
enums::ParseMode::Script => {
assert!(!output.module);
assert!(output.script);
}
_ => {
assert!(output.module);
assert!(!output.script);
}
}
let output_code = output.code;
assert_eq!(expected_code, output_code);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
public final class Swc4jTranspileOutput {
private String code;
private boolean module;
private boolean script;
private String sourceMap;

/**
Expand All @@ -42,20 +43,22 @@ public Swc4jTranspileOutput() {
* @since 0.1.0
*/
public Swc4jTranspileOutput(String code) {
this(code, null, false);
this(code, false, false, null);
}

/**
* Instantiates a new Swc4j transpile output.
*
* @param code the code
* @param sourceMap the source map
* @param module the module
* @param script the script
* @param sourceMap the source map
* @since 0.1.0
*/
public Swc4jTranspileOutput(String code, String sourceMap, boolean module) {
public Swc4jTranspileOutput(String code, boolean module, boolean script, String sourceMap) {
setCode(code);
setModule(module);
setScript(script);
setSourceMap(sourceMap);
}

Expand All @@ -82,13 +85,23 @@ public String getSourceMap() {
/**
* Gets if this source is a module.
*
* @return the boolean
* @return true : module, false : not module
* @since 0.1.0
*/
public boolean isModule() {
return module;
}

/**
* Gets if this source is a script.
*
* @return true : script, false : not script
* @since 0.1.0
*/
public boolean isScript() {
return script;
}

/**
* Sets code.
*
Expand All @@ -113,6 +126,18 @@ public Swc4jTranspileOutput setModule(boolean module) {
return this;
}

/**
* Sets script.
*
* @param script the script
* @return the self
* @since 0.1.0
*/
public Swc4jTranspileOutput setScript(boolean script) {
this.script = script;
return this;
}

/**
* Sets source map.
*
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/com/caoccao/javet/swc4j/TestSwc4j.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,11 @@ public void testTranspileTypeScriptWithoutInlineSourceMap(Swc4jParseMode parseMo
switch (parseMode) {
case Script:
assertFalse(output.isModule());
assertTrue(output.isScript());
break;
default:
assertTrue(output.isModule());
assertFalse(output.isScript());
break;
}
assertNotNull(output.getSourceMap());
Expand Down

0 comments on commit 29e8abb

Please sign in to comment.