Skip to content

Commit

Permalink
✨ feat: Add source text to outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Mar 19, 2024
1 parent 54aa99f commit 84e07c8
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 51 deletions.
25 changes: 12 additions & 13 deletions rust/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const VERSION: &'static str = "0.2.0";

pub fn parse<'local>(code: String, options: options::ParseOptions) -> Result<outputs::ParseOutput, String> {
let parse_params = ParseParams {
specifier: options.specifier,
specifier: options.specifier.to_owned(),
text_info: SourceTextInfo::from_string(code),
media_type: options.media_type,
capture_tokens: options.capture_tokens,
Expand All @@ -35,14 +35,14 @@ pub fn parse<'local>(code: String, options: options::ParseOptions) -> Result<out
_ => parse_module(parse_params),
};
match result {
Ok(parsed_source) => Ok(outputs::ParseOutput::new(parsed_source, options.capture_tokens)),
Ok(parsed_source) => Ok(outputs::ParseOutput::new(&options, &parsed_source)),
Err(e) => Err(e.to_string()),
}
}

pub fn transpile<'local>(code: String, options: options::TranspileOptions) -> Result<outputs::TranspileOutput, String> {
let parse_params = ParseParams {
specifier: options.specifier,
specifier: options.specifier.to_owned(),
text_info: SourceTextInfo::from_string(code),
media_type: options.media_type,
capture_tokens: options.capture_tokens,
Expand All @@ -57,26 +57,25 @@ 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.to_owned(),
inline_source_map: options.inline_source_map,
inline_sources: options.inline_sources,
jsx_automatic: options.jsx_automatic,
jsx_development: options.jsx_development,
jsx_factory: options.jsx_factory,
jsx_fragment_factory: options.jsx_fragment_factory,
jsx_import_source: options.jsx_import_source,
jsx_factory: options.jsx_factory.to_owned(),
jsx_fragment_factory: options.jsx_fragment_factory.to_owned(),
jsx_import_source: options.jsx_import_source.to_owned(),
precompile_jsx: options.precompile_jsx,
source_map: options.source_map,
transform_jsx: options.transform_jsx,
var_decl_imports: options.var_decl_imports,
};
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,
}),
Ok(transpiled_source) => Ok(outputs::TranspileOutput::new(
&options,
&parsed_source,
&transpiled_source,
)),
Err(e) => Err(e.to_string()),
}
}
Expand Down
65 changes: 53 additions & 12 deletions rust/src/outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ use jni::sys::jvalue;
use jni::JNIEnv;

use deno_ast::swc::parser::token::TokenAndSpan;
use deno_ast::ParsedSource;
use deno_ast::{ParsedSource, TranspiledSource};

use std::ptr::null_mut;

use crate::converter;
use crate::options::*;

struct JavaParseOutput {
class: GlobalRef,
Expand All @@ -42,21 +43,27 @@ impl JavaParseOutput {
.new_global_ref(class)
.expect("Couldn't globalize class Swc4jParseOutput");
let method_constructor = env
.get_method_id(&class, "<init>", "(ZZ)V")
.get_method_id(&class, "<init>", "(ZZLjava/lang/String;)V")
.expect("Couldn't find method Swc4jParseOutput.Swc4jParseOutput");
JavaParseOutput {
class,
method_constructor,
}
}

pub fn create<'local, 'a>(&self, env: &mut JNIEnv<'local>, module: jvalue, script: jvalue) -> JObject<'a>
pub fn create<'local, 'a>(
&self,
env: &mut JNIEnv<'local>,
module: jvalue,
script: jvalue,
source_map: jvalue,
) -> JObject<'a>
where
'local: 'a,
{
unsafe {
env
.new_object_unchecked(&self.class, self.method_constructor, &[module, script])
.new_object_unchecked(&self.class, self.method_constructor, &[module, script, source_map])
.expect("Couldn't create Swc4jParseOutput")
}
}
Expand All @@ -78,7 +85,11 @@ impl JavaTranspileOutput {
.new_global_ref(class)
.expect("Couldn't globalize class Swc4jTranspileOutput");
let method_constructor = env
.get_method_id(&class, "<init>", "(Ljava/lang/String;ZZLjava/lang/String;)V")
.get_method_id(
&class,
"<init>",
"(Ljava/lang/String;ZZLjava/lang/String;Ljava/lang/String;)V",
)
.expect("Couldn't find method Swc4jTranspileOutput.Swc4jTranspileOutput");
JavaTranspileOutput {
class,
Expand All @@ -93,6 +104,7 @@ impl JavaTranspileOutput {
module: jvalue,
script: jvalue,
source_map: jvalue,
source_text: jvalue,
) -> JObject<'a>
where
'local: 'a,
Expand All @@ -102,7 +114,7 @@ impl JavaTranspileOutput {
.new_object_unchecked(
&self.class,
self.method_constructor,
&[code, module, script, source_map],
&[code, module, script, source_map, source_text],
)
.expect("Couldn't create Swc4jTranspileOutput")
}
Expand All @@ -129,19 +141,24 @@ pub trait ToJniType {
pub struct ParseOutput {
pub module: bool,
pub script: bool,
pub source_text: String,
pub tokens: Option<Vec<TokenAndSpan>>,
}

impl ParseOutput {
pub fn new(parsed_source: ParsedSource, capture_tokens: bool) -> ParseOutput {
let tokens = if capture_tokens {
pub fn new(parse_options: &ParseOptions, parsed_source: &ParsedSource) -> ParseOutput {
let module = parsed_source.is_module();
let script = parsed_source.is_script();
let source_text = parsed_source.text_info().text().to_string();
let tokens = if parse_options.capture_tokens {
Some(parsed_source.tokens().to_vec())
} else {
None
};
ParseOutput {
module: parsed_source.is_module(),
script: parsed_source.is_script(),
module,
script,
source_text,
tokens,
}
}
Expand All @@ -158,7 +175,10 @@ impl ToJniType for ParseOutput {
let script = jvalue {
z: if self.script { 1u8 } else { 0u8 },
};
unsafe { JAVA_PARSE_OUTPUT.as_ref().unwrap() }.create(env, module, script)
let source_text = jvalue {
l: converter::string_to_jstring(env, &self.source_text).as_raw(),
};
unsafe { JAVA_PARSE_OUTPUT.as_ref().unwrap() }.create(env, module, script, source_text)
}
}

Expand All @@ -168,6 +188,24 @@ pub struct TranspileOutput {
pub module: bool,
pub script: bool,
pub source_map: Option<String>,
pub source_text: String,
}

impl TranspileOutput {
pub fn new(_: &TranspileOptions, parsed_source: &ParsedSource, transpiled_source: &TranspiledSource) -> TranspileOutput {
let code = transpiled_source.text.to_owned();
let module = parsed_source.is_module();
let script = parsed_source.is_script();
let source_map = transpiled_source.source_map.to_owned();
let source_text = parsed_source.text_info().text().to_string();
TranspileOutput {
code,
module,
script,
source_map,
source_text,
}
}
}

impl ToJniType for TranspileOutput {
Expand All @@ -190,6 +228,9 @@ impl ToJniType for TranspileOutput {
None => null_mut(),
},
};
unsafe { JAVA_TRANSPILE_OUTPUT.as_ref().unwrap() }.create(env, code, module, script, source_map)
let source_text = jvalue {
l: converter::string_to_jstring(env, &self.source_text).as_raw(),
};
unsafe { JAVA_TRANSPILE_OUTPUT.as_ref().unwrap() }.create(env, code, module, script, source_map, source_text)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,35 @@ public class Swc4jParseOutput {
* @since 0.2.0
*/
protected boolean script;
/**
* The Source text.
*
* @since 0.2.0
*/
protected String sourceText;

/**
* Instantiates a new Swc4j parse output.
*
* @param module the module
* @param script the script
* @param module the module
* @param script the script
* @param sourceText the source text
* @since 0.2.0
*/
public Swc4jParseOutput(boolean module, boolean script) {
public Swc4jParseOutput(boolean module, boolean script, String sourceText) {
setModule(module);
setScript(script);
setSourceText(sourceText);
}

/**
* Gets source text.
*
* @return the source text
* @since 0.2.0
*/
public String getSourceText() {
return sourceText;
}

/**
Expand Down Expand Up @@ -90,4 +108,16 @@ public Swc4jParseOutput setScript(boolean script) {
this.script = script;
return this;
}

/**
* Sets source text.
*
* @param sourceText the source text
* @return the self
* @since 0.2.0
*/
public Swc4jParseOutput setSourceText(String sourceText) {
this.sourceText = sourceText;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,18 @@
* @since 0.1.0
*/
public class Swc4jTranspileOutput extends Swc4jParseOutput {
private String code;
private String sourceMap;
/**
* The Code.
*
* @since 0.1.0
*/
protected String code;
/**
* The Source map.
*
* @since 0.1.0
*/
protected String sourceMap;

/**
* Instantiates a new Swc4j transpile output.
Expand All @@ -41,20 +51,21 @@ public Swc4jTranspileOutput() {
* @since 0.1.0
*/
public Swc4jTranspileOutput(String code) {
this(code, false, false, null);
this(code, false, false, null, null);
}

/**
* Instantiates a new Swc4j transpile output.
*
* @param code the code
* @param module the module
* @param script the script
* @param sourceMap the source map
* @param code the code
* @param module the module
* @param script the script
* @param sourceMap the source map
* @param sourceText the source text
* @since 0.1.0
*/
public Swc4jTranspileOutput(String code, boolean module, boolean script, String sourceMap) {
super(module, script);
public Swc4jTranspileOutput(String code, boolean module, boolean script, String sourceMap, String sourceText) {
super(module, script, sourceText);
setCode(code);
setSourceMap(sourceMap);
}
Expand Down Expand Up @@ -91,25 +102,13 @@ public Swc4jTranspileOutput setCode(String code) {
return this;
}

/**
* Sets module.
*
* @param module the module
* @return the self
* @since 0.1.0
*/
@Override
public Swc4jTranspileOutput setModule(boolean module) {
super.setModule(module);
return this;
}

/**
* Sets script.
*
* @param script the script
* @return the self
* @since 0.1.0
*/
@Override
public Swc4jTranspileOutput setScript(boolean script) {
super.setScript(script);
return this;
Expand All @@ -126,4 +125,10 @@ public Swc4jTranspileOutput setSourceMap(String sourceMap) {
this.sourceMap = sourceMap;
return this;
}

@Override
public Swc4jTranspileOutput setSourceText(String sourceText) {
super.setSourceText(sourceText);
return this;
}
}
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 @@ -59,6 +59,7 @@ public void testParseJsxWithDefaultOptions() throws Swc4jCoreException {
assertNotNull(output);
assertTrue(output.isModule());
assertFalse(output.isScript());
assertEquals(code, output.getSourceText());
}

@Test
Expand Down Expand Up @@ -131,6 +132,7 @@ public void testTranspileJsxWithDefaultOptions() throws Swc4jCoreException {
.setMediaType(Swc4jMediaType.Jsx);
Swc4jTranspileOutput output = swc4j.transpile(code, options);
assertNotNull(output);
assertEquals(code, output.getSourceText());
assertEquals(expectedCode, output.getCode().substring(0, expectedCode.length()));
assertTrue(output.isModule());
assertFalse(output.isScript());
Expand Down

0 comments on commit 84e07c8

Please sign in to comment.