Skip to content

Commit

Permalink
✨ feat: Add ts index signature to ast
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Apr 1, 2024
1 parent fcc3fbe commit 2089f9c
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 10 deletions.
81 changes: 72 additions & 9 deletions rust/src/ast_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ struct JavaSwc4jAstFactory {
method_create_ts_expr_with_type_args: JStaticMethodID,
method_create_ts_external_module_ref: JStaticMethodID,
method_create_ts_import_equals_decl: JStaticMethodID,
method_create_ts_index_signature: JStaticMethodID,
method_create_ts_namespace_export_decl: JStaticMethodID,
method_create_ts_type_ann: JStaticMethodID,
method_create_ts_type_param: JStaticMethodID,
Expand Down Expand Up @@ -338,6 +339,13 @@ impl JavaSwc4jAstFactory {
"(ZZLcom/caoccao/javet/swc4j/ast/expr/Swc4jAstIdent;Lcom/caoccao/javet/swc4j/ast/interfaces/ISwc4jAstModuleRef;II)Lcom/caoccao/javet/swc4j/ast/module/Swc4jAstTsImportEqualsDecl;",
)
.expect("Couldn't find method Swc4jAstFactory.createTsImportEqualsDecl");
let method_create_ts_index_signature = env
.get_static_method_id(
&class,
"createTsIndexSignature",
"(Ljava/util/List;Lcom/caoccao/javet/swc4j/ast/ts/Swc4jAstTsTypeAnn;ZZII)Lcom/caoccao/javet/swc4j/ast/clazz/Swc4jAstTsIndexSignature;",
)
.expect("Couldn't find method Swc4jAstFactory.createTsIndexSignature");
let method_create_ts_namespace_export_decl = env
.get_static_method_id(
&class,
Expand Down Expand Up @@ -439,6 +447,7 @@ impl JavaSwc4jAstFactory {
method_create_ts_expr_with_type_args,
method_create_ts_external_module_ref,
method_create_ts_import_equals_decl,
method_create_ts_index_signature,
method_create_ts_namespace_export_decl,
method_create_ts_type_ann,
method_create_ts_type_param,
Expand Down Expand Up @@ -1379,6 +1388,35 @@ impl JavaSwc4jAstFactory {
return_value
}

pub fn create_ts_index_signature<'local, 'a>(
&self,
env: &mut JNIEnv<'local>,
params: &JObject<'_>,
type_ann: &Option<JObject>,
readonly: bool,
is_static: bool,
range: &Range<usize>,
) -> JObject<'a>
where
'local: 'a,
{
let params = object_to_jvalue!(params);
let type_ann = optional_object_to_jvalue!(type_ann);
let readonly = boolean_to_jvalue!(readonly);
let is_static = boolean_to_jvalue!(is_static);
let start_position = int_to_jvalue!(range.start);
let end_position = int_to_jvalue!(range.end);
let return_value =
call_static_as_object!(
env,
&self.class,
self.method_create_ts_index_signature,
&[params, type_ann, readonly, is_static, start_position, end_position],
"Swc4jAstTsIndexSignature create_ts_index_signature()"
);
return_value
}

pub fn create_ts_namespace_export_decl<'local, 'a>(
&self,
env: &mut JNIEnv<'local>,
Expand Down Expand Up @@ -3225,9 +3263,7 @@ pub mod program {
java_array_list.add(env, &java_decorators, &java_node);
delete_local_ref!(env, java_node);
});
let accessibility = node
.accessibility
.map_or_else(|| -1, |node| node.get_id());
let accessibility = node.accessibility.map_or_else(|| -1, |node| node.get_id());
let return_type = java_ast_factory.create_auto_accessor(
env,
&java_key,
Expand Down Expand Up @@ -3267,10 +3303,10 @@ pub mod program {
let java_ast_factory = unsafe { JAVA_AST_FACTORY.as_ref().unwrap() };
let range = map.get_range_by_span(&node.span);
let java_id = create_ident(env, map, &node.id);
let java_type_ann = node.type_ann.as_ref().map(|node| create_ts_type_ann(env, map, node));
let return_value = java_ast_factory.create_binding_ident(env, &java_id, &java_type_ann, &range);
let java_option_type_ann = node.type_ann.as_ref().map(|node| create_ts_type_ann(env, map, node));
let return_value = java_ast_factory.create_binding_ident(env, &java_id, &java_option_type_ann, &range);
delete_local_ref!(env, java_id);
delete_local_optional_ref!(env, java_type_ann);
delete_local_optional_ref!(env, java_option_type_ann);
return_value
}

Expand Down Expand Up @@ -3387,9 +3423,7 @@ pub mod program {
delete_local_ref!(env, java_node);
});
let java_body = node.body.as_ref().map(|node| create_block_stmt(env, map, node));
let accessibility = node
.accessibility
.map_or_else(|| -1, |node| node.get_id());
let accessibility = node.accessibility.map_or_else(|| -1, |node| node.get_id());
let is_optional = node.is_optional;
let return_type = java_ast_factory.create_constructor(
env,
Expand Down Expand Up @@ -3849,6 +3883,33 @@ pub mod program {
return_value
}

fn create_ts_index_signature<'local, 'a>(
env: &mut JNIEnv<'local>,
map: &ByteToIndexMap,
node: &TsIndexSignature,
) -> JObject<'a>
where
'local: 'a,
{
let java_ast_factory = unsafe { JAVA_AST_FACTORY.as_ref().unwrap() };
let java_array_list = unsafe { JAVA_ARRAY_LIST.as_ref().unwrap() };
let range = map.get_range_by_span(&node.span);
let java_params = java_array_list.construct(env, node.params.len());
node.params.iter().for_each(|node| {
let java_node = enum_create_ts_fn_param(env, map, node);
java_array_list.add(env, &java_params, &java_node);
delete_local_ref!(env, java_node);
});
let java_option_type_ann = node.type_ann.as_ref().map(|node| create_ts_type_ann(env, map, node));
let readonly = node.readonly;
let is_static = node.is_static;
let return_value =
java_ast_factory.create_ts_index_signature(env, &java_params, &java_option_type_ann, readonly, is_static, &range);
delete_local_ref!(env, java_params);
delete_local_optional_ref!(env, java_option_type_ann);
return_value
}

fn create_ts_namespace_export_decl<'local, 'a>(
env: &mut JNIEnv<'local>,
map: &ByteToIndexMap,
Expand Down Expand Up @@ -4055,7 +4116,9 @@ pub mod program {
match node {
ClassMember::AutoAccessor(node) => create_auto_accessor(env, map, node),
ClassMember::Constructor(node) => create_constructor(env, map, node),
ClassMember::Empty(node) => create_empty_stmt(env, map, node),
ClassMember::StaticBlock(node) => create_static_block(env, map, node),
ClassMember::TsIndexSignature(node) => create_ts_index_signature(env, map, node),
default => panic!("{:?}", default),
// TODO
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/caoccao/javet/swc4j/ast/Swc4jAstFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,17 @@ public static Swc4jAstTsImportEqualsDecl createTsImportEqualsDecl(
return new Swc4jAstTsImportEqualsDecl(export, typeOnly, id, moduleRef, startPosition, endPosition);
}

@Jni2RustMethod
public static Swc4jAstTsIndexSignature createTsIndexSignature(
List<ISwc4jAstTsFnParam> params,
@Jni2RustParam(optional = true) Swc4jAstTsTypeAnn typeAnn,
boolean readonly,
boolean isStatic,
@Jni2RustParamStartPosition int startPosition,
@Jni2RustParamEndPosition int endPosition) {
return new Swc4jAstTsIndexSignature(params, typeAnn, readonly, isStatic, startPosition, endPosition);
}

@Jni2RustMethod
public static Swc4jAstTsNamespaceExportDecl createTsNamespaceExportDecl(
Swc4jAstIdent id,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (c) 2024. caoccao.com Sam Cao
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.caoccao.javet.swc4j.ast.clazz;

import com.caoccao.javet.swc4j.annotations.Nullable;
import com.caoccao.javet.swc4j.ast.Swc4jAst;
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstType;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstClassMember;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstTsFnParam;
import com.caoccao.javet.swc4j.ast.ts.Swc4jAstTsTypeAnn;
import com.caoccao.javet.swc4j.utils.AssertionUtils;
import com.caoccao.javet.swc4j.utils.SimpleList;

import java.util.Collections;
import java.util.List;

public class Swc4jAstTsIndexSignature
extends Swc4jAst
implements ISwc4jAstClassMember {
protected final boolean _static;
protected final List<ISwc4jAstTsFnParam> params;
protected final boolean readonly;
@Nullable
protected final Swc4jAstTsTypeAnn typeAnn;

public Swc4jAstTsIndexSignature(
List<ISwc4jAstTsFnParam> params,
Swc4jAstTsTypeAnn typeAnn,
boolean readonly,
boolean _static,
int startPosition,
int endPosition) {
super(startPosition, endPosition);
this._static = _static;
this.params = AssertionUtils.notNull(params, "Params");
this.readonly = readonly;
this.typeAnn = typeAnn;
children = SimpleList.copyOf(params);
children.add(typeAnn);
children = Collections.unmodifiableList(children);
updateParent();
}

public List<ISwc4jAstTsFnParam> getParams() {
return params;
}

@Override
public Swc4jAstType getType() {
return Swc4jAstType.Decorator;
}

public Swc4jAstTsTypeAnn getTypeAnn() {
return typeAnn;
}

public boolean isReadonly() {
return readonly;
}

public boolean isStatic() {
return _static;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@

import com.caoccao.javet.swc4j.ast.Swc4jAst;
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstType;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstClassMember;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstStmt;

public class Swc4jAstEmptyStmt
extends Swc4jAst
implements ISwc4jAstStmt {
implements ISwc4jAstStmt, ISwc4jAstClassMember {
public Swc4jAstEmptyStmt(int startPosition, int endPosition) {
super(startPosition, endPosition);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,22 @@

package com.caoccao.javet.swc4j.ast.interfaces;

import com.caoccao.javet.swc4j.ast.clazz.Swc4jAstAutoAccessor;
import com.caoccao.javet.swc4j.ast.clazz.Swc4jAstConstructor;
import com.caoccao.javet.swc4j.ast.clazz.Swc4jAstStaticBlock;
import com.caoccao.javet.swc4j.ast.clazz.Swc4jAstTsIndexSignature;
import com.caoccao.javet.swc4j.ast.stmt.Swc4jAstEmptyStmt;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class TestISwc4jAstClassMember {
@Test
public void testAssignable() {
assertTrue(ISwc4jAstClassMember.class.isAssignableFrom(Swc4jAstAutoAccessor.class));
assertTrue(ISwc4jAstClassMember.class.isAssignableFrom(Swc4jAstConstructor.class));
assertTrue(ISwc4jAstClassMember.class.isAssignableFrom(Swc4jAstEmptyStmt.class));
assertTrue(ISwc4jAstClassMember.class.isAssignableFrom(Swc4jAstStaticBlock.class));
assertTrue(ISwc4jAstClassMember.class.isAssignableFrom(Swc4jAstTsIndexSignature.class));
}
}

0 comments on commit 2089f9c

Please sign in to comment.