Skip to content
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

Support async function serialization. #1311

Merged
merged 9 commits into from
May 3, 2018
Merged

Support async function serialization. #1311

merged 9 commits into from
May 3, 2018

Conversation

CyrusNajmabadi
Copy link
Contributor

@CyrusNajmabadi CyrusNajmabadi commented May 2, 2018

Fixes: #1306

Note: i've only manually validated this. Making a unit test for this has proven annoying. Integration test is here: pulumi/pulumi-cloud#471

Copy link
Contributor

@swgillespie swgillespie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One correctness nit, otherwise LGTM!

let isAsync = false;
if (funcString.startsWith("async ")) {
isAsync = true;
funcString = funcString.substr("async ".length);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this runs into trouble when there's more than one space trailing an async:

> closure.parseFunction('async  function bar() {}')
[ '',
  { funcExprWithoutName: 'async function () {}',
    funcExprWithName: 'async function () {}',
    functionDeclarationName: undefined,
    isArrowFunction: false,
    capturedVariables: { required: Map {}, optional: Map {} },
    usesNonLexicalThis: false } ]
> closure.parseFunction('async function bar() {}')
[ '',
  { funcExprWithoutName: 'async function /*bar*/() {}',
    funcExprWithName: 'async function bar() {}',
    functionDeclarationName: 'bar',
    isArrowFunction: false,
    capturedVariables: { required: Map {}, optional: Map {} },
    usesNonLexicalThis: false } ]

The spec guarantees there won't be a newline between async and function so maybe we should eat all whitespace following the async?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do. we trimleft a lot here.

@CyrusNajmabadi
Copy link
Contributor Author

Matt had a good idea to just write javascript unit tests. They demonstrate the fix works :)

// Note, i can't think of a better way to determine this. This is particularly hard because
// we can't even necessary refer to async function objects here as this code is rewritten by
// TS, converting all async functions to non async functions.
const isAsyncFunction = func.constructor && func.constructor.name === "AsyncFunction";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we take the same approach -- throw JS along Typescript -- to get access to the AsyncFunction constructor to test for equality?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is slightly more annoying. mostly because ts doesn't allow you to compile ts alongside js and while producing a declaration file. We could do it jsut as standalone .js file that we import and add to our makefile...

i'm not sure if i want to go to that length...

@CyrusNajmabadi CyrusNajmabadi merged commit 5387e78 into master May 3, 2018
@CyrusNajmabadi CyrusNajmabadi deleted the asyncFunction branch May 3, 2018 19:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants