Skip to content

Commit 96fbd47

Browse files
committed
✨ feat: Enhance ident
1 parent d50af3b commit 96fbd47

File tree

10 files changed

+391
-58
lines changed

10 files changed

+391
-58
lines changed

rust/src/ast_utils.rs

+130-37
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use std::ptr::null_mut;
2929
struct JavaSwc4jAstFactory {
3030
#[allow(dead_code)]
3131
class: GlobalRef,
32+
method_create_binding_ident: JStaticMethodID,
3233
method_create_ident: JStaticMethodID,
3334
method_create_module: JStaticMethodID,
3435
method_create_script: JStaticMethodID,
@@ -46,11 +47,18 @@ impl JavaSwc4jAstFactory {
4647
let class = env
4748
.new_global_ref(class)
4849
.expect("Couldn't globalize class Swc4jAstFactory");
50+
let method_create_binding_ident = env
51+
.get_static_method_id(
52+
&class,
53+
"createBindingIdent",
54+
"(Lcom/caoccao/javet/swc4j/ast/expr/Swc4jAstIdent;Lcom/caoccao/javet/swc4j/ast/ts/Swc4jAstTsTypeAnn;II)Lcom/caoccao/javet/swc4j/ast/pat/Swc4jAstBindingIdent;",
55+
)
56+
.expect("Couldn't find method Swc4jAstFactory.createBindingIdent");
4957
let method_create_ident = env
5058
.get_static_method_id(
5159
&class,
5260
"createIdent",
53-
"(Ljava/lang/String;ZII)Lcom/caoccao/javet/swc4j/ast/pat/Swc4jAstIdent;",
61+
"(Ljava/lang/String;ZII)Lcom/caoccao/javet/swc4j/ast/expr/Swc4jAstIdent;",
5462
)
5563
.expect("Couldn't find method Swc4jAstFactory.createIdent");
5664
let method_create_module = env
@@ -83,6 +91,7 @@ impl JavaSwc4jAstFactory {
8391
.expect("Couldn't find method Swc4jAstFactory.createVarDeclarator");
8492
JavaSwc4jAstFactory {
8593
class,
94+
method_create_binding_ident,
8695
method_create_ident,
8796
method_create_module,
8897
method_create_script,
@@ -91,6 +100,35 @@ impl JavaSwc4jAstFactory {
91100
}
92101
}
93102

103+
pub fn create_binding_ident<'local, 'a>(
104+
&self,
105+
env: &mut JNIEnv<'local>,
106+
id: &JObject<'_>,
107+
type_ann: &JObject<'_>,
108+
range: &Range<usize>,
109+
) -> JObject<'a>
110+
where
111+
'local: 'a,
112+
{
113+
let id = jvalue { l: id.as_raw() };
114+
let type_ann = jvalue { l: type_ann.as_raw() };
115+
let start_position = jvalue { i: range.start as i32 };
116+
let end_position = jvalue { i: range.end as i32 };
117+
let return_value = unsafe {
118+
env
119+
.call_static_method_unchecked(
120+
&self.class,
121+
self.method_create_binding_ident,
122+
ReturnType::Object,
123+
&[id, type_ann, start_position, end_position],
124+
)
125+
.expect("Couldn't create Swc4jAstBindingIdent by create_binding_ident()")
126+
.l()
127+
.expect("Couldn't convert Swc4jAstBindingIdent by create_binding_ident()")
128+
};
129+
return_value
130+
}
131+
94132
pub fn create_ident<'local, 'a>(
95133
&self,
96134
env: &mut JNIEnv<'local>,
@@ -298,8 +336,36 @@ pub mod span {
298336
};
299337
}
300338

339+
fn register_ident(byte_to_index_map: &mut ByteToIndexMap, ident: &Ident) {
340+
byte_to_index_map.register_by_span(&ident.span);
341+
}
342+
301343
fn register_module(byte_to_index_map: &mut ByteToIndexMap, module: &Module) {
302344
byte_to_index_map.register_by_span(&module.span);
345+
module
346+
.body
347+
.iter()
348+
.for_each(|module_item| register_module_item(byte_to_index_map, &module_item));
349+
}
350+
351+
fn register_module_decl(byte_to_index_map: &mut ByteToIndexMap, module_decl: &ModuleDecl) {
352+
match module_decl {
353+
_ => {} // TODO
354+
}
355+
}
356+
357+
fn register_module_item(byte_to_index_map: &mut ByteToIndexMap, module_item: &ModuleItem) {
358+
match module_item {
359+
ModuleItem::ModuleDecl(module_decl) => register_module_decl(byte_to_index_map, &module_decl),
360+
ModuleItem::Stmt(stmt) => register_stmt(byte_to_index_map, &stmt),
361+
}
362+
}
363+
364+
fn register_pat(byte_to_index_map: &mut ByteToIndexMap, pat: &Pat) {
365+
match &pat {
366+
Pat::Ident(binding_ident) => register_ident(byte_to_index_map, &binding_ident.id),
367+
_ => {}
368+
}
303369
}
304370

305371
fn register_script(byte_to_index_map: &mut ByteToIndexMap, script: &Script) {
@@ -335,7 +401,7 @@ pub mod span {
335401

336402
fn register_var_declarator(byte_to_index_map: &mut ByteToIndexMap, var_declarator: &VarDeclarator) {
337403
byte_to_index_map.register_by_span(&var_declarator.span);
338-
// TODO
404+
register_pat(byte_to_index_map, &var_declarator.name);
339405
}
340406
}
341407

@@ -353,49 +419,48 @@ pub mod program {
353419
use deno_ast::swc::ast::*;
354420
use deno_ast::swc::common::Spanned;
355421

356-
fn create_ident<'local, 'a>(
422+
fn create_binding_ident<'local, 'a>(
357423
env: &mut JNIEnv<'local>,
358424
byte_to_index_map: &ByteToIndexMap,
359-
ident: &Ident,
425+
binding_ident: &BindingIdent,
360426
) -> JObject<'a>
361427
where
362428
'local: 'a,
363429
{
364430
let java_ast_factory = unsafe { JAVA_AST_FACTORY.as_ref().unwrap() };
365-
let range = byte_to_index_map.get_range_by_span(&ident.span());
366-
let sym = ident.sym.as_str();
367-
let optional = ident.optional;
368-
java_ast_factory.create_ident(env, sym, optional, &range)
431+
let range = byte_to_index_map.get_range_by_span(&binding_ident.span());
432+
let java_id = create_ident(env, byte_to_index_map, &binding_ident.id);
433+
let type_ann: JObject<'_> = Default::default(); // TODO
434+
let return_value = java_ast_factory.create_binding_ident(env, &java_id, &type_ann, &range);
435+
env
436+
.delete_local_ref(java_id)
437+
.expect("Couldn't delete local binding ident");
438+
return_value
369439
}
370440

371-
pub fn create_program<'local, 'a>(
441+
fn create_expr<'local, 'a>(env: &mut JNIEnv<'local>, byte_to_index_map: &ByteToIndexMap, expr: &Expr) -> JObject<'a>
442+
where
443+
'local: 'a,
444+
{
445+
match expr {
446+
Expr::Ident(ident) => create_ident(env, byte_to_index_map, &ident),
447+
_ => Default::default(),
448+
}
449+
}
450+
451+
fn create_ident<'local, 'a>(
372452
env: &mut JNIEnv<'local>,
373453
byte_to_index_map: &ByteToIndexMap,
374-
program: &Option<Arc<Program>>,
454+
ident: &Ident,
375455
) -> JObject<'a>
376456
where
377457
'local: 'a,
378458
{
379-
match program {
380-
Some(arc_program) => {
381-
if arc_program.is_module() {
382-
create_module(
383-
env,
384-
byte_to_index_map,
385-
arc_program.as_module().expect("Couldn't get module"),
386-
)
387-
} else if arc_program.is_script() {
388-
create_script(
389-
env,
390-
byte_to_index_map,
391-
arc_program.as_script().expect("Couldn't get script"),
392-
)
393-
} else {
394-
Default::default()
395-
}
396-
}
397-
None => Default::default(),
398-
}
459+
let java_ast_factory = unsafe { JAVA_AST_FACTORY.as_ref().unwrap() };
460+
let range = byte_to_index_map.get_range_by_span(&ident.span());
461+
let sym = ident.sym.as_str();
462+
let optional = ident.optional;
463+
java_ast_factory.create_ident(env, sym, optional, &range)
399464
}
400465

401466
fn create_module<'local, 'a>(
@@ -432,15 +497,40 @@ pub mod program {
432497
where
433498
'local: 'a,
434499
{
435-
let java_ast_factory = unsafe { JAVA_AST_FACTORY.as_ref().unwrap() };
436-
let range = byte_to_index_map.get_range_by_span(&pat.span());
437500
match pat {
438-
Pat::Ident(bingding_ident) => {
439-
let java_id = create_ident(env, byte_to_index_map, &bingding_ident.id);
501+
Pat::Ident(bingding_ident) => create_binding_ident(env, byte_to_index_map, &bingding_ident),
502+
_ => Default::default(),
503+
}
504+
}
505+
506+
pub fn create_program<'local, 'a>(
507+
env: &mut JNIEnv<'local>,
508+
byte_to_index_map: &ByteToIndexMap,
509+
program: &Option<Arc<Program>>,
510+
) -> JObject<'a>
511+
where
512+
'local: 'a,
513+
{
514+
match program {
515+
Some(arc_program) => {
516+
if arc_program.is_module() {
517+
create_module(
518+
env,
519+
byte_to_index_map,
520+
arc_program.as_module().expect("Couldn't get module"),
521+
)
522+
} else if arc_program.is_script() {
523+
create_script(
524+
env,
525+
byte_to_index_map,
526+
arc_program.as_script().expect("Couldn't get script"),
527+
)
528+
} else {
529+
Default::default()
530+
}
440531
}
441-
_ => {}
532+
None => Default::default(),
442533
}
443-
Default::default()
444534
}
445535

446536
fn create_stmt<'local, 'a>(
@@ -542,7 +632,10 @@ pub mod program {
542632
{
543633
let java_ast_factory = unsafe { JAVA_AST_FACTORY.as_ref().unwrap() };
544634
let definite = var_declarator.definite;
545-
let init: Option<JObject> = None; // TODO
635+
let init: Option<JObject> = match &var_declarator.init {
636+
Some(box_expr) => Some(create_expr(env, byte_to_index_map, &box_expr.as_ref())),
637+
None => None,
638+
};
546639
let name = create_pat(env, byte_to_index_map, &var_declarator.name);
547640
let range = byte_to_index_map.get_range_by_span(&var_declarator.span());
548641
let return_value = java_ast_factory.create_var_declarator(env, &name, &init, definite, &range);

src/main/java/com/caoccao/javet/swc4j/ast/Swc4jAst.java

+27
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ public abstract class Swc4jAst {
4242
* @since 0.2.0
4343
*/
4444
protected final Swc4jAstType type;
45+
/**
46+
* The Parent.
47+
*
48+
* @since 0.2.0
49+
*/
50+
protected Swc4jAst parent;
4551

4652
/**
4753
* Instantiates a new Swc4j ast.
@@ -53,6 +59,7 @@ public abstract class Swc4jAst {
5359
*/
5460
protected Swc4jAst(Swc4jAstType type, int startPosition, int endPosition) {
5561
this.endPosition = endPosition;
62+
parent = null;
5663
this.startPosition = startPosition;
5764
this.type = type;
5865
}
@@ -67,6 +74,16 @@ public int getEndPosition() {
6774
return endPosition;
6875
}
6976

77+
/**
78+
* Gets parent.
79+
*
80+
* @return the parent
81+
* @since 0.2.0
82+
*/
83+
public Swc4jAst getParent() {
84+
return parent;
85+
}
86+
7087
/**
7188
* Gets start position.
7289
*
@@ -86,4 +103,14 @@ public int getStartPosition() {
86103
public Swc4jAstType getType() {
87104
return type;
88105
}
106+
107+
/**
108+
* Sets parent.
109+
*
110+
* @param parent the parent
111+
* @since 0.2.0
112+
*/
113+
public void setParent(Swc4jAst parent) {
114+
this.parent = parent;
115+
}
89116
}

src/main/java/com/caoccao/javet/swc4j/ast/Swc4jAstFactory.java

+22-1
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
package com.caoccao.javet.swc4j.ast;
1818

1919
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstExpr;
20-
import com.caoccao.javet.swc4j.ast.pat.Swc4jAstIdent;
20+
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstIdent;
21+
import com.caoccao.javet.swc4j.ast.pat.Swc4jAstBindingIdent;
2122
import com.caoccao.javet.swc4j.ast.pat.Swc4jAstPat;
2223
import com.caoccao.javet.swc4j.ast.program.Swc4jAstModule;
2324
import com.caoccao.javet.swc4j.ast.program.Swc4jAstScript;
2425
import com.caoccao.javet.swc4j.ast.stmt.decl.Swc4jAstVarDecl;
2526
import com.caoccao.javet.swc4j.ast.stmt.decl.Swc4jAstVarDeclKind;
2627
import com.caoccao.javet.swc4j.ast.stmt.decl.Swc4jAstVarDeclarator;
28+
import com.caoccao.javet.swc4j.ast.ts.Swc4jAstTsTypeAnn;
2729
import com.caoccao.javet.swc4j.jni2rust.*;
2830

2931
import java.util.List;
@@ -38,6 +40,25 @@ public final class Swc4jAstFactory {
3840
private Swc4jAstFactory() {
3941
}
4042

43+
/**
44+
* Create module ast binding ident.
45+
*
46+
* @param id the id
47+
* @param typeAnn the type ann
48+
* @param startPosition the start position
49+
* @param endPosition the end position
50+
* @return the ast binding ident
51+
* @since 0.2.0
52+
*/
53+
@Jni2RustMethod
54+
public static Swc4jAstBindingIdent createBindingIdent(
55+
Swc4jAstIdent id,
56+
Swc4jAstTsTypeAnn typeAnn,
57+
@Jni2RustParamStartPosition int startPosition,
58+
@Jni2RustParamEndPosition int endPosition) {
59+
return new Swc4jAstBindingIdent(id, typeAnn, startPosition, endPosition);
60+
}
61+
4162
/**
4263
* Create module ast ident.
4364
*

0 commit comments

Comments
 (0)