Skip to content
This repository was archived by the owner on Feb 8, 2020. It is now read-only.

Commit 9bf8268

Browse files
TooTallNatetunnckoCore
authored andcommitted
fix(arrows): handle async arrow functions
* add case for async arrow functions when wrapping Fixes #61. * add missing `done()` to test case
1 parent daa010c commit 9bf8268

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,12 @@ export default function parseFunction (opts) {
105105

106106
const isFunction = result.value.startsWith('function')
107107
const isAsyncFn = result.value.startsWith('async function')
108+
const isAsyncArrow = result.value.startsWith('async')
109+
&& result.value.includes('=>')
108110
// eslint-disable-next-line no-useless-escape
109111
const isMethod = /^\*?.+\([\s\S\w\W]*\)\s*\{/i.test(result.value)
110112

111-
if (!(isFunction || isAsyncFn) && isMethod) {
113+
if (!(isFunction || isAsyncFn || isAsyncArrow) && isMethod) {
112114
result.value = `{ ${result.value} }`
113115
}
114116

test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,3 +403,16 @@ test('should call fn returned from plugin only when `parse` is called', (done) =
403403
test.strictEqual(res.params, 'a, b')
404404
done()
405405
})
406+
407+
// https://github.com/tunnckoCore/parse-function/issues/61
408+
test('should work with an async arrow function with an `if` statement', (done) => {
409+
const app = parseFunction()
410+
const parsed = app.parse('async (v) => { if (v) {} }')
411+
test.deepEqual(parsed, {
412+
name: null,
413+
body: ' if (v) {} ',
414+
args: [ 'v' ],
415+
params: 'v'
416+
})
417+
done()
418+
})

0 commit comments

Comments
 (0)