Skip to content

Commit b4ecc79

Browse files
committed
Python: Fix some more async parsing problems
Turns out we were not setting the `is_async` field on anything except `async for` statements. This commit makes it so that we also do this for `async def` and `async with`, and adds a test that this produces the same behaviour as the old parser.
1 parent e710c0a commit b4ecc79

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
async def foo():
2+
await bar() + await baz()
3+
4+
async with foo() as bar, baz() as quux:
5+
pass
6+
7+
async for spam in eggs:
8+
pass

python/extractor/tsg-python/python.tsg

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2650,6 +2650,14 @@
26502650
let @with.first = @first.node
26512651
}
26522652

2653+
; Async status
2654+
; NOTE: We only set the `is_async` field on the _first_ clause of the `with` statement,
2655+
; as this is the behaviour of the old parser.
2656+
(with_statement "async" "with" @with_keyword (with_clause . (with_item) @with))
2657+
{
2658+
attr (@with.node) is_async = #true
2659+
}
2660+
26532661
(with_item
26542662
value: (_) @value
26552663
) @with
@@ -3253,6 +3261,16 @@
32533261
}
32543262
}
32553263

3264+
; Async status
3265+
(function_definition "async" "def" @def_keyword) @funcdef
3266+
{
3267+
let start = (location-start @def_keyword)
3268+
attr (@funcdef.function) is_async = #true
3269+
attr (@funcdef.node) _location_start = start
3270+
attr (@funcdef.function) _location_start = start
3271+
attr (@funcdef.funcexpr) _location_start = start
3272+
}
3273+
32563274
;;; Decorators
32573275

32583276
(decorated_definition

0 commit comments

Comments
 (0)