Skip to content

Commit

Permalink
Merge pull request #16 from botsman/fix-parenthesis-handling
Browse files Browse the repository at this point in the history
fix parenthesis handling
  • Loading branch information
botsman authored Oct 2, 2021
2 parents 1a2ec13 + b396ef3 commit daad977
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 20 deletions.
4 changes: 2 additions & 2 deletions haxelib.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"javascript"
],
"description": "This library provides a macros to add 'async' and 'await' keywords for Python and JavaScript and does nothing for other platoforms.",
"version": "1.1.0",
"version": "1.1.1",
"classPath": "src",
"releasenote": "Move type inference under flag",
"releasenote": "Fix handling parenthesis",
"contributors": [
"botsman"
],
Expand Down
45 changes: 31 additions & 14 deletions src/hxasync/AsyncMacro.hx
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,18 @@ class AsyncMacro {
return false;
}

public static inline function handleEMeta(expr: Expr, isAsyncContext: Bool) {
public static inline function handleEMeta(
expr: Expr,
isAsyncContext: Bool,
addParenthesis: Bool = false
) {
switch expr.expr {
case EMeta(s, e):
if (s.name == "await") {
if (!isAsyncContext) {
Context.error("await allowed only inside async function", e.pos);
}
transformToAwait(expr);
transformToAwait(expr, addParenthesis);
} else if (s.name == "async") {
switch e.expr {
case EFunction(kind, f):
Expand Down Expand Up @@ -121,7 +125,12 @@ class AsyncMacro {
handleAny(field.expr, isAsyncContext);
}
case EParenthesis(e):
handleAny(e, isAsyncContext);
switch e.expr {
case EMeta(s, expr):
handleEMeta(e, isAsyncContext, true);
default:
handleAny(e, isAsyncContext);
}
case ECheckType(e, t):
handleAny(e, isAsyncContext);
case EIf(econd, eif, eelse):
Expand Down Expand Up @@ -410,11 +419,11 @@ class AsyncMacro {
if (fun.ret != null) {
return fun.ret;
}
var complexType =
try {
var typed = Context.typeExpr({expr: EFunction(null, fun), pos:fun.expr.pos});
var complexType = try {
var typed = Context.typeExpr({expr: EFunction(null, fun), pos: fun.expr.pos});
typed.t.followWithAbstracts().toComplexType();
} catch (e) {
}
catch (e) {
null;
};

Expand All @@ -430,17 +439,21 @@ class AsyncMacro {
var returnType = inferReturnType(fun);
return switch returnType {
case TPath({name: "StdTypes", sub: "Void"}):
macro: hxasync.Abstracts.Awaitable<hxasync.Abstracts.NoReturn>;
macro
: hxasync.Abstracts.Awaitable<hxasync.Abstracts.NoReturn>;
case TPath(p):
macro: hxasync.Abstracts.Awaitable<$returnType>;
macro
: hxasync.Abstracts.Awaitable<$returnType>;
case null:
null; // TODO: fix. Temporary fallback solution for cases when we failed to infer return type
null; // TODO: fix. Temporary fallback solution for cases when we failed to infer return type
case TAnonymous(fields):
macro: hxasync.Abstracts.Awaitable<$returnType>;
macro
: hxasync.Abstracts.Awaitable<$returnType>;
default:
trace('Unexpected return type: ${returnType}');
trace(fun.expr.pos);
macro: hxasync.Abstracts.Awaitable<$returnType>;
macro
: hxasync.Abstracts.Awaitable<$returnType>;
}
}

Expand Down Expand Up @@ -469,11 +482,15 @@ class AsyncMacro {
}
}

public static function transformToAwait(e: Expr) {
public static function transformToAwait(e: Expr, addParenthesis: Bool) {
switch (e.expr) {
case EMeta(s, metaE):
processAwaitedFuncArgs(metaE);
e.expr = (macro hxasync.AsyncMacroUtils.await(${metaE})).expr;
if (addParenthesis) {
e.expr = (macro hxasync.AsyncMacroUtils.awaitWithParenthesis(${metaE})).expr;
} else {
e.expr = (macro hxasync.AsyncMacroUtils.await(${metaE})).expr;
}
default:
Context.error("Invalid expression", e.pos);
}
Expand Down
10 changes: 10 additions & 0 deletions src/hxasync/AsyncMacroUtils.hx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ class AsyncMacroUtils {
#end
}

public static extern inline function awaitWithParenthesis<T>(arg: Awaitable<T>): T {
#if js
return std.js.Syntax.code("(await {0})", arg);
#elseif python
return std.python.Syntax.code("(await {0})", arg);
#else
return cast arg;
#end
}

#if macro
public static inline function count(text: String, char: String): Int {
var counter = 0;
Expand Down
15 changes: 11 additions & 4 deletions tests/Tests.hx
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package tests;



class Cases {
public var some = "some variable";

public function new() {
}
public function new() {}

@async public static function testBasic() {
return "basic func called";
Expand Down Expand Up @@ -51,6 +48,15 @@ class Cases {
@await nestedFunction();
}

@async public static function testBrackets() {
var nestedFunction = @async function() {
return {
a: 10
};
}
return (@await nestedFunction()).a;
}

@async public function returnDynamic() {
var a = 10;
return {
Expand All @@ -65,6 +71,7 @@ class Cases {
@await testArrowFunction();
@await testFunctionWithDefaultArgs();
@await testNestedFunction();
@await testBrackets();
}
}

Expand Down

0 comments on commit daad977

Please sign in to comment.