Skip to content

Commit

Permalink
🦄 refactor: Rename file_name to specifier
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Mar 9, 2024
1 parent b79fba7 commit 6b8404a
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 29 deletions.
3 changes: 1 addition & 2 deletions rust/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ use crate::{options, outputs};
const VERSION: &'static str = "0.1.0";

pub fn transpile<'local>(code: String, options: options::TranspileOptions) -> Result<outputs::TranspileOutput, String> {
let url = ModuleSpecifier::parse(&format!("file:///{}", options.file_name)).unwrap();
match parse_module(ParseParams {
specifier: url.to_string(),
specifier: options.specifier,
text_info: SourceTextInfo::from_string(code.to_string()),
media_type: options.media_type,
capture_tokens: false,
Expand Down
22 changes: 11 additions & 11 deletions rust/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ use deno_ast::MediaType;
use crate::converter;

struct JniCalls {
pub jmethod_id_transpile_options_get_file_name: JMethodID,
pub jmethod_id_transpile_options_get_media_type: JMethodID,
pub jmethod_id_transpile_options_get_specifier: JMethodID,
pub jmethod_id_media_type_get_id: JMethodID,
}
unsafe impl Send for JniCalls {}
Expand All @@ -38,16 +38,16 @@ pub fn init<'local>(env: &mut JNIEnv<'local>) {
let jclass_transpile_options = env
.find_class("com/caoccao/javet/swc4j/options/Swc4jTranspileOptions")
.expect("Couldn't find class Swc4jTranspileOptions");
let jmethod_id_transpile_options_get_file_name = env
.get_method_id(&jclass_transpile_options, "getFileName", "()Ljava/lang/String;")
.expect("Couldn't find method Swc4jTranspileOptions.getFileName");
let jmethod_id_transpile_options_get_media_type = env
.get_method_id(
&jclass_transpile_options,
"getMediaType",
"()Lcom/caoccao/javet/swc4j/enums/Swc4jMediaType;",
)
.expect("Couldn't find method Swc4jTranspileOptions.getMediaType");
let jmethod_id_transpile_options_get_specifier = env
.get_method_id(&jclass_transpile_options, "getSpecifier", "()Ljava/lang/String;")
.expect("Couldn't find method Swc4jTranspileOptions.getSpecifier");
let jclass_media_type = env
.find_class("com/caoccao/javet/swc4j/enums/Swc4jMediaType")
.expect("Couldn't find class Swc4jMediaType");
Expand All @@ -56,8 +56,8 @@ pub fn init<'local>(env: &mut JNIEnv<'local>) {
.expect("Couldn't find method Swc4jMediaType.getId");
unsafe {
JNI_CALLS = Some(JniCalls {
jmethod_id_transpile_options_get_file_name,
jmethod_id_transpile_options_get_media_type,
jmethod_id_transpile_options_get_specifier: jmethod_id_transpile_options_get_specifier,
jmethod_id_media_type_get_id,
});
}
Expand All @@ -69,23 +69,23 @@ pub trait FromJniType {

#[derive(Debug)]
pub struct TranspileOptions {
pub file_name: String,
pub media_type: MediaType,
pub specifier: String,
}

impl FromJniType for TranspileOptions {
fn from_jni_type<'local>(env: &mut JNIEnv<'local>, o: jobject) -> TranspileOptions {
let o = unsafe { JObject::from_raw(o) };
let file_name = unsafe {
let specifier = unsafe {
env.call_method_unchecked(
o.as_ref(),
JNI_CALLS.as_ref().unwrap().jmethod_id_transpile_options_get_file_name,
JNI_CALLS.as_ref().unwrap().jmethod_id_transpile_options_get_specifier,
ReturnType::Object,
&[],
)
};
let file_name = unsafe { file_name.unwrap().as_jni().l };
let file_name = converter::jstring_to_string(env, file_name);
let specifier = unsafe { specifier.unwrap().as_jni().l };
let specifier = converter::jstring_to_string(env, specifier);
// media_type
let media_type = unsafe {
env.call_method_unchecked(
Expand All @@ -107,6 +107,6 @@ impl FromJniType for TranspileOptions {
let media_type = unsafe { media_type.unwrap().as_jni().i };
let media_type = converter::media_type_id_to_media_type(media_type);
// construct
TranspileOptions { file_name, media_type }
TranspileOptions { specifier, media_type }
}
}
4 changes: 2 additions & 2 deletions rust/tests/test_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ fn test_transpile_type_script_inline_source_map() {
let expected_code = "function add(a, b) {\n return a + b;\n}\n";
let expected_source_map_prefix = "//# sourceMappingURL=data:application/json;base64,";
let options = options::TranspileOptions {
file_name: "abc.ts".to_owned(),
media_type: MediaType::TypeScript,
specifier: "file:///abc.ts".to_owned(),
};
let output = core::transpile(code.to_owned(), options);
assert!(output.is_ok());
Expand All @@ -48,8 +48,8 @@ fn test_transpile_wrong_media_type() {
+ " function add(a:number, b:number) { return a+b; }\n"
+ " ~";
let options = options::TranspileOptions {
file_name: "abc.ts".to_owned(),
media_type: MediaType::JavaScript,
specifier: "file:///abc.ts".to_owned(),
};
let output = core::transpile(code.to_owned(), options);
assert!(output.is_err());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,72 @@
import com.caoccao.javet.swc4j.enums.Swc4jMediaType;
import com.caoccao.javet.swc4j.utils.AssertionUtils;

/**
* The type Swc4j transpile options.
*
* @since 0.1.0
*/
public final class Swc4jTranspileOptions {
public static final String DEFAULT_FILE_NAME = "main.js";
private String fileName;
/**
* The constant DEFAULT_SPECIFIER.
*
* @since 0.1.0
*/
public static final String DEFAULT_SPECIFIER = "file:///main.js";
private Swc4jMediaType mediaType;
private String specifier;

/**
* Instantiates a new Swc4j transpile options.
*
* @since 0.1.0
*/
public Swc4jTranspileOptions() {
setFileName(DEFAULT_FILE_NAME);
setSpecifier(DEFAULT_SPECIFIER);
setMediaType(Swc4jMediaType.JavaScript);
}

public String getFileName() {
return fileName;
}

/**
* Gets Media type of the source text.
*
* @return the Media type of the source text
* @since 0.1.0
*/
public Swc4jMediaType getMediaType() {
return mediaType;
}

public Swc4jTranspileOptions setFileName(String fileName) {
this.fileName = AssertionUtils.notNull(fileName, "File name");
return this;
/**
* Gets Specifier of the source text.
*
* @return the Specifier of the source text
* @since 0.1.0
*/
public String getSpecifier() {
return specifier;
}

/**
* Sets Media type of the source text.
*
* @param mediaType the Media type of the source text
* @return the self
* @since 0.1.0
*/
public Swc4jTranspileOptions setMediaType(Swc4jMediaType mediaType) {
this.mediaType = AssertionUtils.notNull(mediaType, "Media type");
return this;
}

/**
* Sets Specifier of the source text.
*
* @param specifier the Specifier of the source text
* @return the self
* @since 0.1.0
*/
public Swc4jTranspileOptions setSpecifier(String specifier) {
this.specifier = AssertionUtils.notNull(specifier, "Specifier");
return this;
}
}
8 changes: 4 additions & 4 deletions src/test/java/com/caoccao/javet/swc4j/TestSwc4j.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public void testTranspileTypeScriptInlineSourceMap() throws Swc4jCoreException {
" return a + b;\n" +
"}\n";
String expectedSourceMapPrefix = "//# sourceMappingURL=data:application/json;base64,";
String fileName = "abc.ts";
String fileName = "file:///abc.ts";
Swc4jTranspileOptions options = new Swc4jTranspileOptions()
.setFileName(fileName)
.setSpecifier(fileName)
.setMediaType(Swc4jMediaType.TypeScript);
Swc4jTranspileOutput output = swc4j.transpile(code, options);
assertNotNull(output);
Expand All @@ -61,9 +61,9 @@ public void testTranspileTypeScriptInlineSourceMap() throws Swc4jCoreException {
@Test
public void testTranspileWrongMediaType() {
String code = "function add(a:number, b:number) { return a+b; }";
String fileName = "abc.ts";
String fileName = "file:///abc.ts";
Swc4jTranspileOptions options = new Swc4jTranspileOptions()
.setFileName(fileName)
.setSpecifier(fileName)
.setMediaType(Swc4jMediaType.JavaScript);
assertEquals(
"Expected ',', got ':' at file:///abc.ts:1:15\n" +
Expand Down

0 comments on commit 6b8404a

Please sign in to comment.