Skip to content

Fix export async function failure: 'SyntaxError: Cannot use keyword 'await' outside an async function' #469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 7 additions & 17 deletions src/parsing/preserve-named-constant-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@
* limitations under the License.
*/

import {
ExpressionStatement,
AssignmentExpression,
FunctionExpression,
MemberExpression,
} from 'estree';
import { ExpressionStatement, AssignmentExpression, FunctionExpression, MemberExpression } from 'estree';
import { ExportDetails, Range } from '../types';
import MagicString from 'magic-string';

Expand All @@ -37,21 +32,21 @@ function PreserveFunction(
const functionExpression = assignmentExpression.right as FunctionExpression;
const [memberExpressionObjectStart] = memberExpression.object.range as Range;
const functionName = exportInline ? exportDetails.exported : exportDetails.local;

const functionAsync = functionExpression.async;
if (functionExpression.params.length > 0) {
const [paramsStart] = functionExpression.params[0].range as Range;
// FunctionExpression has parameters.
source.overwrite(
memberExpressionObjectStart,
paramsStart,
`${exportInline ? 'export ' : ''}function ${functionName}(`,
`${exportInline ? 'export ' : ''}${functionAsync ? 'async ' : ''}function ${functionName}(`,
);
} else {
const [bodyStart] = functionExpression.body.range as Range;
source.overwrite(
memberExpressionObjectStart,
bodyStart,
`${exportInline ? 'export ' : ''}function ${functionName}()`,
`${exportInline ? 'export ' : ''}${functionAsync ? 'async ' : ''}function ${functionName}()`,
);
}

Expand All @@ -74,21 +69,16 @@ function PreserveIdentifier(

if (exportInline) {
const output =
(exportDetails.exported === 'default'
? `export default `
: `export var ${exportDetails.exported}=`) + `${code.substring(rightStart, rightEnd)};`;
(exportDetails.exported === 'default' ? `export default ` : `export var ${exportDetails.exported}=`) +
`${code.substring(rightStart, rightEnd)};`;
source.overwrite(ancestorStart, ancestorEnd, output);
} else if (exportDetails.source === null && 'name' in right) {
// This is a locally defined identifier with a name we can use.
exportDetails.local = right.name;
source.remove(leftStart, ancestorEnd);
return true;
} else {
source.overwrite(
ancestorStart,
ancestorEnd,
`var ${exportDetails.local}=${code.substring(rightStart, rightEnd)};`,
);
source.overwrite(ancestorStart, ancestorEnd, `var ${exportDetails.local}=${code.substring(rightStart, rightEnd)};`);
}

return !exportInline;
Expand Down
3 changes: 3 additions & 0 deletions test/export-default/async-function.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { generator } from '../generator';

generator('export-default', 'async-function', false, ['esm'], { stable: {} });
1 change: 1 addition & 0 deletions test/export-default/fixtures/async-function.esm.stable.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions test/export-default/fixtures/async-function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export async function asyncTest() {
await Promise.resolve('async-test');
console.log('async-test')
}

export async function asyncTestWithArgument(argument) {
await Promise.resolve('async-test-with-argument');
console.log(argument)
}