From cb60c9f630bbe7949136e680c1bb7e15b12efd52 Mon Sep 17 00:00:00 2001 From: inizio Date: Sun, 3 Mar 2019 13:23:56 +0800 Subject: [PATCH 1/2] support non-tricky async function --- javascript-stringify.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/javascript-stringify.js b/javascript-stringify.js index f888eee..cfccee4 100644 --- a/javascript-stringify.js +++ b/javascript-stringify.js @@ -91,6 +91,16 @@ return fn.constructor.name === 'GeneratorFunction'; } + /** + * Check if a function is an ES7 async function + * + * @param {Function} fn + * @return {boolean} + */ + function isAsyncFunction (fn) { + return fn.constructor.name === 'AsyncFunction'; + } + /** * Can be replaced with `str.startsWith(prefix)` if code is updated to ES6. * @@ -281,7 +291,7 @@ if (indent) { value = dedentFunction(value); } - var prefix = isGeneratorFunction(fn) ? '*' : ''; + var prefix = isGeneratorFunction(fn) ? '*' : isAsyncFunction(fn) ? 'async ' : ''; if (fn.name && stringStartsWith(value, prefix + fn.name + '(')) { // Method notation was used to define this function, but it was transplanted from another object. // Convert to regular function notation. @@ -320,6 +330,7 @@ '[object RegExp]': String, '[object Function]': stringifyFunction, '[object GeneratorFunction]': stringifyFunction, + '[object AsyncFunction]': stringifyFunction, '[object global]': toGlobalVariable, '[object Window]': toGlobalVariable }; From 8107e178d8f1382734e19635604a27131cea27f5 Mon Sep 17 00:00:00 2001 From: inizio Date: Sun, 3 Mar 2019 13:24:35 +0800 Subject: [PATCH 2/2] test: include async function --- test.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test.js b/test.js index 626e0e6..41ba89b 100644 --- a/test.js +++ b/test.js @@ -207,6 +207,10 @@ describe('javascript-stringify', function () { it('should stringify', testRoundTrip('function* (x) { yield x; }')); }); + describe('async', function () { + it('should stringify', testRoundTrip('async function (x) { await x; }')); + }); + describe('method notation', function () { it('should stringify', testRoundTrip('{a(b, c) { return b + c; }}')); @@ -222,6 +226,11 @@ describe('javascript-stringify', function () { testRoundTrip("{*'function a'(b, c) { return b + c; }}") ); + it( + 'should not be fooled by tricky async names', + testRoundTrip("{async 'function a'(b, c) { return b + c; }}") + ); + it( 'should not be fooled by empty names', testRoundTrip("{''(b, c) { return b + c; }}")