Skip to content

Commit

Permalink
✨ feat: Check await expr
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed May 12, 2024
1 parent a902acc commit d73382a
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
import com.caoccao.javet.sanitizer.options.JavetSanitizerOptions;
import com.caoccao.javet.swc4j.ast.clazz.Swc4jAstFunction;
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstVarDeclKind;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstArrowExpr;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstAssignExpr;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstIdent;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstYieldExpr;
import com.caoccao.javet.swc4j.ast.expr.*;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAst;
import com.caoccao.javet.swc4j.ast.module.*;
import com.caoccao.javet.swc4j.ast.stmt.*;
Expand Down Expand Up @@ -91,6 +88,14 @@ public Swc4jAstVisitorResponse visitAssignExpr(Swc4jAstAssignExpr node) {
return super.visitAssignExpr(node);
}

@Override
public Swc4jAstVisitorResponse visitAwaitExpr(Swc4jAstAwaitExpr node) {
if (!options.isKeywordAwaitEnabled()) {
raiseError(JavetSanitizerException.keywordNotAllowed(AWAIT), node);
}
return super.visitAwaitExpr(node);
}

@Override
public Swc4jAstVisitorResponse visitClassDecl(Swc4jAstClassDecl node) {
validateBuiltInObject(options, node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ public class Swc4jAstUsingDecl
implements ISwc4jAstDecl, ISwc4jAstVarDeclOrExpr, ISwc4jAstForHead {
protected final List<Swc4jAstVarDeclarator> decls;
@Jni2RustField(name = "is_await")
protected boolean await;
protected boolean _await;

@Jni2RustMethod
public Swc4jAstUsingDecl(
@Jni2RustParam(name = "is_await") boolean await,
@Jni2RustParam(name = "is_await") boolean _await,
List<Swc4jAstVarDeclarator> decls,
Swc4jSpan span) {
super(span);
setAwait(await);
setAwait(_await);
this.decls = AssertionUtils.notNull(decls, "Decls");
updateParent();
}
Expand All @@ -67,11 +67,11 @@ public Swc4jAstType getType() {

@Jni2RustMethod
public boolean isAwait() {
return await;
return _await;
}

public Swc4jAstUsingDecl setAwait(boolean await) {
this.await = await;
public Swc4jAstUsingDecl setAwait(boolean _await) {
this._await = _await;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.caoccao.javet.sanitizer.options.JavetSanitizerOptions;
import com.caoccao.javet.swc4j.ast.module.Swc4jAstExportDecl;
import com.caoccao.javet.swc4j.ast.module.Swc4jAstImportDecl;
import com.caoccao.javet.swc4j.enums.Swc4jMediaType;
import com.caoccao.javet.swc4j.utils.SimpleList;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -33,7 +34,9 @@
public class TestJavetSanitizerModuleChecker extends BaseTestSuiteCheckers {
@BeforeEach
public void beforeEach() {
checker = new JavetSanitizerModuleChecker();
checker = new JavetSanitizerModuleChecker(JavetSanitizerOptions.Default.toClone()
.setMediaType(Swc4jMediaType.TypeScript)
.seal());
}

@Test
Expand All @@ -43,6 +46,51 @@ public void testInvalidCases() {
code,
JavetSanitizerError.EmptyCodeString,
JavetSanitizerError.EmptyCodeString.getFormat()));
assertException(
"async () => {};",
JavetSanitizerError.KeywordNotAllowed,
"Keyword async is not allowed.\n" +
"Source: async () => {}\n" +
"Line: 1\n" +
"Column: 1\n" +
"Start: 0\n" +
"End: 14");
assertException(
"async function a() {};",
JavetSanitizerError.KeywordNotAllowed,
"Keyword async is not allowed.\n" +
"Source: async function a() {}\n" +
"Line: 1\n" +
"Column: 1\n" +
"Start: 0\n" +
"End: 21");
assertException(
"await a;",
JavetSanitizerError.KeywordNotAllowed,
"Keyword await is not allowed.\n" +
"Source: await a\n" +
"Line: 1\n" +
"Column: 1\n" +
"Start: 0\n" +
"End: 7");
assertException(
"for await (const a of b) {}",
JavetSanitizerError.KeywordNotAllowed,
"Keyword await is not allowed.\n" +
"Source: for await (const a of b) {}\n" +
"Line: 1\n" +
"Column: 1\n" +
"Start: 0\n" +
"End: 27");
assertException(
"await using a = b;",
JavetSanitizerError.KeywordNotAllowed,
"Keyword await is not allowed.\n" +
"Source: await using a = b\n" +
"Line: 1\n" +
"Column: 1\n" +
"Start: 0\n" +
"End: 17");
assertException(
"import a from 'a'; a;",
JavetSanitizerError.KeywordNotAllowed,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.expr;

import com.caoccao.javet.swc4j.ast.BaseTestSuiteSwc4jAst;
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstType;
import com.caoccao.javet.swc4j.ast.program.Swc4jAstScript;
import com.caoccao.javet.swc4j.ast.stmt.Swc4jAstExprStmt;
import com.caoccao.javet.swc4j.exceptions.Swc4jCoreException;
import com.caoccao.javet.swc4j.outputs.Swc4jParseOutput;
import org.junit.jupiter.api.Test;

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

public class TestSwc4jAstAwaitExpr extends BaseTestSuiteSwc4jAst {
@Test
public void test() throws Swc4jCoreException {
String code = "await a;";
Swc4jParseOutput output = swc4j.parse(code, jsScriptParseOptions);
Swc4jAstScript script = output.getProgram().as(Swc4jAstScript.class);
Swc4jAstExprStmt exprStmt = assertAst(
script, script.getBody().get(0).as(Swc4jAstExprStmt.class), Swc4jAstType.ExprStmt, 0, 8);
Swc4jAstAwaitExpr awaitExpr = assertAst(
exprStmt, exprStmt.getExpr().as(Swc4jAstAwaitExpr.class), Swc4jAstType.AwaitExpr, 0, 7);
Swc4jAstIdent ident = assertAst(
awaitExpr, awaitExpr.getArg().as(Swc4jAstIdent.class), Swc4jAstType.Ident, 6, 7);
assertEquals("a", ident.getSym());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void testUsingInTypeScript() throws Swc4jCoreException {
Swc4jAstScript script = output.getProgram().as(Swc4jAstScript.class);
Swc4jAstUsingDecl usingDecl = assertAst(
script, script.getBody().get(0).as(Swc4jAstUsingDecl.class), Swc4jAstType.UsingDecl, 0, 7);
assertFalse(usingDecl.await);
assertFalse(usingDecl.isAwait());
Swc4jAstVarDeclarator varDeclarator = assertAst(
usingDecl, usingDecl.getDecls().get(0), Swc4jAstType.VarDeclarator, 6, 7);
Swc4jAstBindingIdent bindingIdent = assertAst(
Expand Down

0 comments on commit d73382a

Please sign in to comment.