Skip to content

Commit

Permalink
✨ feat: Add prop name to ast
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Apr 1, 2024
1 parent 8a576b2 commit f4a4955
Show file tree
Hide file tree
Showing 12 changed files with 196 additions and 18 deletions.
65 changes: 58 additions & 7 deletions rust/src/ast_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ struct JavaSwc4jAstFactory {
method_create_class_decl: JStaticMethodID,
method_create_class_method: JStaticMethodID,
method_create_class_prop: JStaticMethodID,
method_create_computed_prop_name: JStaticMethodID,
method_create_constructor: JStaticMethodID,
method_create_debugger_stmt: JStaticMethodID,
method_create_decorator: JStaticMethodID,
Expand Down Expand Up @@ -157,6 +158,13 @@ impl JavaSwc4jAstFactory {
"(Lcom/caoccao/javet/swc4j/ast/interfaces/ISwc4jAstKey;Lcom/caoccao/javet/swc4j/ast/interfaces/ISwc4jAstExpr;Lcom/caoccao/javet/swc4j/ast/ts/Swc4jAstTsTypeAnn;ZLjava/util/List;IZZZZZZII)Lcom/caoccao/javet/swc4j/ast/clazz/Swc4jAstClassProp;",
)
.expect("Couldn't find method Swc4jAstFactory.createClassProp");
let method_create_computed_prop_name = env
.get_static_method_id(
&class,
"createComputedPropName",
"(Lcom/caoccao/javet/swc4j/ast/interfaces/ISwc4jAstExpr;II)Lcom/caoccao/javet/swc4j/ast/clazz/Swc4jAstComputedPropName;",
)
.expect("Couldn't find method Swc4jAstFactory.createComputedPropName");
let method_create_constructor = env
.get_static_method_id(
&class,
Expand Down Expand Up @@ -315,14 +323,14 @@ impl JavaSwc4jAstFactory {
.get_static_method_id(
&class,
"createPrivateMethod",
"(Lcom/caoccao/javet/swc4j/ast/expr/Swc4jAstPrivateName;Lcom/caoccao/javet/swc4j/ast/clazz/Swc4jAstFunction;IZIZZZII)Lcom/caoccao/javet/swc4j/ast/clazz/Swc4jAstPrivateMethod;",
"(Lcom/caoccao/javet/swc4j/ast/clazz/Swc4jAstPrivateName;Lcom/caoccao/javet/swc4j/ast/clazz/Swc4jAstFunction;IZIZZZII)Lcom/caoccao/javet/swc4j/ast/clazz/Swc4jAstPrivateMethod;",
)
.expect("Couldn't find method Swc4jAstFactory.createPrivateMethod");
let method_create_private_name = env
.get_static_method_id(
&class,
"createPrivateName",
"(Lcom/caoccao/javet/swc4j/ast/expr/Swc4jAstIdent;II)Lcom/caoccao/javet/swc4j/ast/expr/Swc4jAstPrivateName;",
"(Lcom/caoccao/javet/swc4j/ast/expr/Swc4jAstIdent;II)Lcom/caoccao/javet/swc4j/ast/clazz/Swc4jAstPrivateName;",
)
.expect("Couldn't find method Swc4jAstFactory.createPrivateName");
let method_create_private_prop = env
Expand Down Expand Up @@ -476,6 +484,7 @@ impl JavaSwc4jAstFactory {
method_create_class_decl,
method_create_class_method,
method_create_class_prop,
method_create_computed_prop_name,
method_create_constructor,
method_create_debugger_stmt,
method_create_decorator,
Expand Down Expand Up @@ -798,6 +807,29 @@ impl JavaSwc4jAstFactory {
return_value
}

pub fn create_computed_prop_name<'local, 'a>(
&self,
env: &mut JNIEnv<'local>,
expr: &JObject<'_>,
range: &Range<usize>,
) -> JObject<'a>
where
'local: 'a,
{
let expr = object_to_jvalue!(expr);
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_computed_prop_name,
&[expr, start_position, end_position],
"Swc4jAstComputedPropName create_computed_prop_name()"
);
return_value
}

pub fn create_constructor<'local, 'a>(
&self,
env: &mut JNIEnv<'local>,
Expand Down Expand Up @@ -3792,6 +3824,18 @@ pub mod program {
return_type
}

fn create_computed_prop_name<'local, 'a>(env: &mut JNIEnv<'local>, map: &ByteToIndexMap, node: &ComputedPropName) -> JObject<'a>
where
'local: 'a,
{
let java_ast_factory = unsafe { JAVA_AST_FACTORY.as_ref().unwrap() };
let range = map.get_range_by_span(&node.span);
let java_expr = enum_create_expr(env, map, &node.expr);
let return_type = java_ast_factory.create_computed_prop_name(env, &java_expr, &range);
delete_local_ref!(env, java_expr);
return_type
}

fn create_constructor<'local, 'a>(env: &mut JNIEnv<'local>, map: &ByteToIndexMap, node: &Constructor) -> JObject<'a>
where
'local: 'a,
Expand Down Expand Up @@ -4182,7 +4226,11 @@ pub mod program {
return_type
}

fn create_private_method<'local, 'a>(env: &mut JNIEnv<'local>, map: &ByteToIndexMap, node: &PrivateMethod) -> JObject<'a>
fn create_private_method<'local, 'a>(
env: &mut JNIEnv<'local>,
map: &ByteToIndexMap,
node: &PrivateMethod,
) -> JObject<'a>
where
'local: 'a,
{
Expand Down Expand Up @@ -4843,8 +4891,8 @@ pub mod program {
'local: 'a,
{
match node {
default => panic!("{:?}", default),
// TODO
Key::Private(node) => create_private_name(env, map, node),
Key::Public(node) => enum_create_prop_name(env, map, node),
}
}

Expand Down Expand Up @@ -5026,8 +5074,11 @@ pub mod program {
'local: 'a,
{
match node {
default => panic!("{:?}", default),
// TODO
PropName::BigInt(node) => create_big_int(env, map, node),
PropName::Computed(node) => create_computed_prop_name(env, map, node),
PropName::Ident(node) => create_ident(env, map, node),
PropName::Num(node) => create_number(env, map, node),
PropName::Str(node) => create_str(env, map, node),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.caoccao.javet.swc4j.ast.clazz.*;
import com.caoccao.javet.swc4j.ast.enums.*;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstIdent;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstPrivateName;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstUnaryExpr;
import com.caoccao.javet.swc4j.ast.expr.lit.*;
import com.caoccao.javet.swc4j.ast.interfaces.*;
Expand Down Expand Up @@ -159,6 +158,14 @@ public static Swc4jAstClassProp createClassProp(
startPosition, endPosition);
}

@Jni2RustMethod
public static Swc4jAstComputedPropName createComputedPropName(
ISwc4jAstExpr expr,
@Jni2RustParamStartPosition int startPosition,
@Jni2RustParamEndPosition int endPosition) {
return new Swc4jAstComputedPropName(expr, startPosition, endPosition);
}

@Jni2RustMethod
public static Swc4jAstConstructor createConstructor(
ISwc4jAstPropName key,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.ast.Swc4jAst;
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstType;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstExpr;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstPropName;
import com.caoccao.javet.swc4j.utils.AssertionUtils;
import com.caoccao.javet.swc4j.utils.SimpleList;

public class Swc4jAstComputedPropName
extends Swc4jAst
implements ISwc4jAstPropName {
protected final ISwc4jAstExpr expr;

public Swc4jAstComputedPropName(
ISwc4jAstExpr expr,
int startPosition,
int endPosition) {
super(startPosition, endPosition);
this.expr = AssertionUtils.notNull(expr, "Expr");
children = SimpleList.immutableOf(expr);
updateParent();
}

public ISwc4jAstExpr getExpr() {
return expr;
}

@Override
public Swc4jAstType getType() {
return Swc4jAstType.ComputedPropName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstAccessibility;
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstMethodKind;
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstType;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstPrivateName;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstClassMember;
import com.caoccao.javet.swc4j.utils.AssertionUtils;
import com.caoccao.javet.swc4j.utils.SimpleList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024. caoccao.com Sam Cao
* Copyright (c) 2024-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.
Expand All @@ -14,16 +14,17 @@
* limitations under the License.
*/

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

import com.caoccao.javet.swc4j.ast.Swc4jAst;
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstType;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAst;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstIdent;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstKey;
import com.caoccao.javet.swc4j.utils.AssertionUtils;

public class Swc4jAstPrivateName
extends Swc4jAst
implements ISwc4jAst {
implements ISwc4jAstKey {
protected final Swc4jAstIdent id;

public Swc4jAstPrivateName(Swc4jAstIdent id, int startPosition, int endPosition) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@

public class Swc4jAstIdent
extends Swc4jAst
implements ISwc4jAstExpr, ISwc4jAstProp, ISwc4jAstModuleRef, ISwc4jAstModuleExportName, ISwc4jAstTsEntityName {
implements ISwc4jAstExpr, ISwc4jAstProp, ISwc4jAstModuleRef, ISwc4jAstModuleExportName, ISwc4jAstTsEntityName,
ISwc4jAstPropName {
protected static final String QUESTION_MARK = "?";
protected final boolean optional;
protected final String sym;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstBigIntSign;
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstType;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstLit;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstPropName;
import com.caoccao.javet.swc4j.utils.AssertionUtils;
import com.caoccao.javet.swc4j.utils.StringUtils;

import java.math.BigInteger;

public class Swc4jAstBigInt
extends Swc4jAst
implements ISwc4jAstLit {
implements ISwc4jAstLit, ISwc4jAstPropName {
@Nullable
protected final String raw;
protected final Swc4jAstBigIntSign sign;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
import com.caoccao.javet.swc4j.ast.Swc4jAst;
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstType;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstLit;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstPropName;

public class Swc4jAstNumber
extends Swc4jAst
implements ISwc4jAstLit {
implements ISwc4jAstLit, ISwc4jAstPropName {
@Nullable
protected final String raw;
protected final double value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstType;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstLit;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstModuleExportName;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstPropName;
import com.caoccao.javet.swc4j.utils.AssertionUtils;

public class Swc4jAstStr
extends Swc4jAst
implements ISwc4jAstLit, ISwc4jAstModuleExportName {
implements ISwc4jAstLit, ISwc4jAstModuleExportName, ISwc4jAstPropName {
@Nullable
protected final String raw;
protected final String value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@

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

public interface ISwc4jAstPropName extends ISwc4jAst {
public interface ISwc4jAstPropName extends ISwc4jAstKey {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.interfaces;

import com.caoccao.javet.swc4j.ast.clazz.Swc4jAstPrivateName;
import org.junit.jupiter.api.Test;

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

public class TestISwc4jAstKey {
@Test
public void testAssignable() {
assertTrue(ISwc4jAstKey.class.isAssignableFrom(Swc4jAstPrivateName.class));
assertTrue(ISwc4jAstKey.class.isAssignableFrom(ISwc4jAstPropName.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.interfaces;

import com.caoccao.javet.swc4j.ast.clazz.Swc4jAstComputedPropName;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstIdent;
import com.caoccao.javet.swc4j.ast.expr.lit.Swc4jAstBigInt;
import com.caoccao.javet.swc4j.ast.expr.lit.Swc4jAstNumber;
import com.caoccao.javet.swc4j.ast.expr.lit.Swc4jAstStr;
import org.junit.jupiter.api.Test;

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

public class TestISwc4jAstPropName {
@Test
public void testAssignable() {
assertTrue(ISwc4jAstPropName.class.isAssignableFrom(Swc4jAstBigInt.class));
assertTrue(ISwc4jAstPropName.class.isAssignableFrom(Swc4jAstComputedPropName.class));
assertTrue(ISwc4jAstPropName.class.isAssignableFrom(Swc4jAstIdent.class));
assertTrue(ISwc4jAstPropName.class.isAssignableFrom(Swc4jAstNumber.class));
assertTrue(ISwc4jAstPropName.class.isAssignableFrom(Swc4jAstStr.class));
}
}

0 comments on commit f4a4955

Please sign in to comment.