Skip to content

Commit

Permalink
✨ feat: Add program to parse mode
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed May 5, 2024
1 parent c352b93 commit 4dbd652
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 53 deletions.
9 changes: 6 additions & 3 deletions rust/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ pub fn parse<'local>(code: String, options: options::ParseOptions) -> Result<out
scope_analysis: options.scope_analysis,
};
let result = match options.parse_mode {
enums::ParseMode::Module => parse_module(parse_params),
enums::ParseMode::Script => parse_script(parse_params),
_ => parse_module(parse_params),
_ => parse_program(parse_params),
};
match result {
Ok(parsed_source) => Ok(outputs::ParseOutput::new(&options, &parsed_source)),
Expand All @@ -55,8 +56,9 @@ pub fn transform<'local>(code: String, options: options::TransformOptions) -> Re
scope_analysis: false,
};
let result = match options.parse_mode {
enums::ParseMode::Module => parse_module(parse_params),
enums::ParseMode::Script => parse_script(parse_params),
_ => parse_module(parse_params),
_ => parse_program(parse_params),
};
match result {
Ok(parsed_source) => {
Expand Down Expand Up @@ -145,8 +147,9 @@ pub fn transpile<'local>(code: String, options: options::TranspileOptions) -> Re
scope_analysis: options.scope_analysis,
};
let result = match options.parse_mode {
enums::ParseMode::Module => parse_module(parse_params),
enums::ParseMode::Script => parse_script(parse_params),
_ => parse_module(parse_params),
_ => parse_program(parse_params),
};
match result {
Ok(parsed_source) => {
Expand Down
13 changes: 8 additions & 5 deletions rust/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1114,22 +1114,25 @@ impl IdentifiableEnum<MethodKind> for MethodKind {
#[derive(Default, Debug, Copy, Clone)]
pub enum ParseMode {
#[default]
Program,
Module,
Script,
}

impl IdentifiableEnum<ParseMode> for ParseMode {
fn get_id(&self) -> i32 {
match self {
ParseMode::Module => 0,
ParseMode::Script => 1,
ParseMode::Program => 0,
ParseMode::Module => 1,
ParseMode::Script => 2,
}
}
fn parse_by_id(id: i32) -> ParseMode {
match id {
0 => ParseMode::Module,
1 => ParseMode::Script,
_ => ParseMode::Module,
0 => ParseMode::Program,
1 => ParseMode::Module,
2 => ParseMode::Script,
_ => ParseMode::Program,
}
}
}
Expand Down
92 changes: 89 additions & 3 deletions rust/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct JavaSwc4jParseOptions {
class: GlobalRef,
method_get_media_type: JMethodID,
method_get_parse_mode: JMethodID,
method_get_plugin_host: JMethodID,
method_get_specifier: JMethodID,
method_is_capture_ast: JMethodID,
method_is_capture_comments: JMethodID,
Expand Down Expand Up @@ -63,6 +64,13 @@ impl JavaSwc4jParseOptions {
"()Lcom/caoccao/javet/swc4j/enums/Swc4jParseMode;",
)
.expect("Couldn't find method Swc4jParseOptions.getParseMode");
let method_get_plugin_host = env
.get_method_id(
&class,
"getPluginHost",
"()Lcom/caoccao/javet/swc4j/plugins/ISwc4jPluginHost;",
)
.expect("Couldn't find method Swc4jParseOptions.getPluginHost");
let method_get_specifier = env
.get_method_id(
&class,
Expand Down Expand Up @@ -102,6 +110,7 @@ impl JavaSwc4jParseOptions {
class,
method_get_media_type,
method_get_parse_mode,
method_get_plugin_host,
method_get_specifier,
method_is_capture_ast,
method_is_capture_comments,
Expand Down Expand Up @@ -146,6 +155,29 @@ impl JavaSwc4jParseOptions {
return_value
}

pub fn get_plugin_host<'local, 'a>(
&self,
env: &mut JNIEnv<'local>,
obj: &JObject<'_>,
) -> Option<JObject<'a>>
where
'local: 'a,
{
let return_value = call_as_object!(
env,
obj,
self.method_get_plugin_host,
&[],
"ISwc4jPluginHost get_plugin_host()"
);
let return_value = if return_value.is_null() {
None
} else {
Some(return_value)
};
return_value
}

pub fn get_specifier<'local, 'a>(
&self,
env: &mut JNIEnv<'local>,
Expand Down Expand Up @@ -236,6 +268,7 @@ struct JavaSwc4jTransformOptions {
class: GlobalRef,
method_get_media_type: JMethodID,
method_get_parse_mode: JMethodID,
method_get_plugin_host: JMethodID,
method_get_source_map: JMethodID,
method_get_specifier: JMethodID,
method_get_target: JMethodID,
Expand Down Expand Up @@ -272,6 +305,13 @@ impl JavaSwc4jTransformOptions {
"()Lcom/caoccao/javet/swc4j/enums/Swc4jParseMode;",
)
.expect("Couldn't find method Swc4jTransformOptions.getParseMode");
let method_get_plugin_host = env
.get_method_id(
&class,
"getPluginHost",
"()Lcom/caoccao/javet/swc4j/plugins/ISwc4jPluginHost;",
)
.expect("Couldn't find method Swc4jTransformOptions.getPluginHost");
let method_get_source_map = env
.get_method_id(
&class,
Expand Down Expand Up @@ -339,6 +379,7 @@ impl JavaSwc4jTransformOptions {
class,
method_get_media_type,
method_get_parse_mode,
method_get_plugin_host,
method_get_source_map,
method_get_specifier,
method_get_target,
Expand Down Expand Up @@ -387,6 +428,29 @@ impl JavaSwc4jTransformOptions {
return_value
}

pub fn get_plugin_host<'local, 'a>(
&self,
env: &mut JNIEnv<'local>,
obj: &JObject<'_>,
) -> Option<JObject<'a>>
where
'local: 'a,
{
let return_value = call_as_object!(
env,
obj,
self.method_get_plugin_host,
&[],
"ISwc4jPluginHost get_plugin_host()"
);
let return_value = if return_value.is_null() {
None
} else {
Some(return_value)
};
return_value
}

pub fn get_source_map<'local, 'a>(
&self,
env: &mut JNIEnv<'local>,
Expand Down Expand Up @@ -1188,6 +1252,8 @@ pub struct ParseOptions {
pub media_type: MediaType,
/// Should the code to be parsed as Module or Script.
pub parse_mode: ParseMode,
/// AST plugin host.
pub plugin_host: Option<PluginHost>,
/// Whether to apply swc's scope analysis.
pub scope_analysis: bool,
/// Specifier of the source text.
Expand All @@ -1208,7 +1274,8 @@ impl Default for ParseOptions {
capture_comments: false,
capture_tokens: false,
media_type: MediaType::TypeScript,
parse_mode: ParseMode::Module,
parse_mode: ParseMode::Program,
plugin_host: None,
scope_analysis: false,
specifier: "file:///main.js".to_owned(),
}
Expand All @@ -1226,6 +1293,13 @@ impl FromJava for ParseOptions {
let scope_analysis = java_parse_options.is_scope_analysis(env, obj);
let specifier = java_parse_options.get_specifier(env, obj);
let specifier = url_to_string(env, &specifier);
let java_optional_plugin_host = java_parse_options.get_plugin_host(env, obj);
let plugin_host = java_optional_plugin_host.map(|host| {
let host = env
.new_global_ref(host)
.expect("Failed to create global reference for plugin host");
PluginHost::new(host)
});
let java_parse_mode = java_parse_options.get_parse_mode(env, obj);
let parse_mode = ParseMode::from_java(env, &java_parse_mode);
delete_local_ref!(env, java_media_type);
Expand All @@ -1236,6 +1310,7 @@ impl FromJava for ParseOptions {
capture_tokens,
media_type,
parse_mode,
plugin_host,
scope_analysis,
specifier,
}
Expand Down Expand Up @@ -1263,6 +1338,8 @@ pub struct TransformOptions {
pub omit_last_semi: bool,
/// Should the code to be parsed as Module or Script.
pub parse_mode: ParseMode,
/// AST plugin host.
pub plugin_host: Option<PluginHost>,
/// How and if source maps should be generated.
pub source_map: SourceMapOption,
/// Specifier of the source text.
Expand Down Expand Up @@ -1296,7 +1373,8 @@ impl Default for TransformOptions {
media_type: MediaType::TypeScript,
minify: true,
omit_last_semi: false,
parse_mode: ParseMode::Module,
parse_mode: ParseMode::Program,
plugin_host: None,
source_map: SourceMapOption::Inline,
specifier: "file:///main.js".to_owned(),
target: EsVersion::latest(),
Expand All @@ -1319,6 +1397,13 @@ impl FromJava for TransformOptions {
let source_map = SourceMapOption::from_java(env, &java_source_map);
let java_parse_mode = java_transform_options.get_parse_mode(env, obj);
let parse_mode = ParseMode::from_java(env, &java_parse_mode);
let java_optional_plugin_host = java_transform_options.get_plugin_host(env, obj);
let plugin_host = java_optional_plugin_host.map(|host| {
let host = env
.new_global_ref(host)
.expect("Failed to create global reference for plugin host");
PluginHost::new(host)
});
let specifier = java_transform_options.get_specifier(env, obj);
let specifier = url_to_string(env, &specifier);
let java_target = java_transform_options.get_target(env, obj);
Expand All @@ -1336,6 +1421,7 @@ impl FromJava for TransformOptions {
minify,
omit_last_semi,
parse_mode,
plugin_host,
source_map,
specifier,
target,
Expand Down Expand Up @@ -1430,7 +1516,7 @@ impl Default for TranspileOptions {
jsx_import_source: None,
keep_comments: false,
media_type: MediaType::TypeScript,
parse_mode: ParseMode::Module,
parse_mode: ParseMode::Program,
plugin_host: None,
precompile_jsx: false,
scope_analysis: false,
Expand Down
6 changes: 1 addition & 5 deletions rust/src/plugin_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,10 @@ pub struct PluginHost {
host: GlobalRef,
}

impl<'a> PluginHost {
impl PluginHost {
pub fn new(host: GlobalRef) -> Self {
PluginHost { host }
}

pub fn process() -> bool {
false
}
}

/* JavaISwc4jPluginHost Begin */
Expand Down
8 changes: 4 additions & 4 deletions rust/tests/test_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn test_parse_typescript_with_capture_tokens() {
let output = core::parse(code.to_owned(), options);
assert!(output.is_ok());
let output = output.unwrap();
assert!(matches!(output.parse_mode, ParseMode::Module));
assert!(matches!(output.parse_mode, ParseMode::Script));
assert!(output.tokens.is_some());
let tokens = output.tokens.unwrap();
/*
Expand Down Expand Up @@ -113,7 +113,7 @@ fn test_parse_typescript_with_default_options() {
let output = core::parse(code.to_owned(), options);
assert!(output.is_ok());
let output = output.unwrap();
assert!(matches!(output.parse_mode, ParseMode::Module));
assert!(matches!(output.parse_mode, ParseMode::Script));
assert!(output.tokens.is_none());
}

Expand Down Expand Up @@ -187,7 +187,7 @@ fn test_transform_with_default_options() {
assert!(output.is_ok());
let output = output.unwrap();
assert_eq!(MediaType::TypeScript, output.media_type);
assert!(matches!(output.parse_mode, ParseMode::Module));
assert!(matches!(output.parse_mode, ParseMode::Script));
let output_code = output.code;
assert_eq!(expected_code, &output_code[0..expected_code.len()]);
assert!(output_code[expected_code.len()..].starts_with(expected_source_map_prefix));
Expand Down Expand Up @@ -264,7 +264,7 @@ fn test_transpile_type_script_with_inline_source_map() {
let output = core::transpile(code.to_owned(), options);
assert!(output.is_ok());
let output = output.unwrap();
assert!(matches!(output.parse_output.parse_mode, ParseMode::Module));
assert!(matches!(output.parse_output.parse_mode, ParseMode::Script));
let output_code = output.code;
assert_eq!(expected_code, &output_code[0..expected_code.len()]);
assert!(output_code[expected_code.len()..].starts_with(expected_source_map_prefix));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
* @since 0.1.0
*/
public enum Swc4jParseMode implements ISwc4jEnumId {
Module(0),
Script(1);
Program(0),
Module(1),
Script(2),
;

private static final int LENGTH = values().length;
private static final Swc4jParseMode[] TYPES = new Swc4jParseMode[LENGTH];
Expand All @@ -52,7 +54,7 @@ public enum Swc4jParseMode implements ISwc4jEnumId {
* @since 0.1.0
*/
public static Swc4jParseMode parse(int id) {
return id >= 0 && id < LENGTH ? TYPES[id] : Module;
return id >= 0 && id < LENGTH ? TYPES[id] : Program;
}

@Override
Expand Down
Loading

0 comments on commit 4dbd652

Please sign in to comment.