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

Fix for #4943 "Calling an async function with default parameter as function for arguments checking handled synchonous" #5688

Merged
merged 14 commits into from May 4, 2017

Conversation

hulkish
Copy link
Contributor

@hulkish hulkish commented May 1, 2017

Q A
Patch: Bug Fix? yes
Major: Breaking Change? no
Minor: New Feature? no
Deprecations? no
Spec Compliancy? yes
Tests Added/Pass? yes
Fixed Tickets Fixes #4943
License MIT
Doc PR no
Dependency Changes no

This change reflects this suggestion: #4943 (comment)

@mention-bot
Copy link

@hulkish, thanks for your PR! By analyzing the history of the files in this pull request, we identified @loganfsmyth, @spicyj and @hzoo to be potential reviewers.

@codecov
Copy link

codecov bot commented May 1, 2017

Codecov Report

Merging #5688 into 7.0 will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##              7.0    #5688   +/-   ##
=======================================
  Coverage   84.56%   84.56%           
=======================================
  Files         283      283           
  Lines        9742     9742           
  Branches     2735     2735           
=======================================
  Hits         8238     8238           
- Misses        991      992    +1     
+ Partials      513      512    -1
Impacted Files Coverage Δ
packages/babel-helpers/src/helpers.js 100% <ø> (ø) ⬆️
packages/babel-traverse/src/visitors.js 85.71% <0%> (-0.96%) ⬇️
packages/babel-traverse/src/path/context.js 85.34% <0%> (ø) ⬆️
...kages/babel-generator/src/generators/statements.js 97.19% <0%> (+0.56%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 79c1eed...62c0fe7. Read the comment docs.

@sophiebits
Copy link
Contributor

This seems simple enough to me. Can you write a test for this?

@hulkish
Copy link
Contributor Author

hulkish commented May 2, 2017

@spicyj sure, is there an area i should focus on for this test? Like... should I look for if the arguments are being applied outside of the promise block?

@sophiebits
Copy link
Contributor

I think testing for the original issue – something that throws immediately upon execution – and verifying the correct behavior at runtime would be the best test here.

@hulkish
Copy link
Contributor Author

hulkish commented May 2, 2017

@spicyj gotcha, ill work on that now

@hulkish
Copy link
Contributor Author

hulkish commented May 2, 2017

@spicyj hows that?

@sophiebits
Copy link
Contributor

Sorry, I should have been clearer. Can you add an exec.js test as described in https://github.com/babel/babel/blob/7.0/CONTRIBUTING.md#writing-tests that verifies the code actually works at runtime? That is, not just checking the compiled output. That way, if someone changes this in the future, it will be obvious why the function call needs to be inside the new Promise().

Also, not sure it makes sense to have this test in packages/babel-core – any reason you didn't put both in the async-to-generator folder?

@hulkish
Copy link
Contributor Author

hulkish commented May 2, 2017

@spicyj no reason, ill move it

@hulkish
Copy link
Contributor Author

hulkish commented May 2, 2017

@spicyj better?

Copy link
Contributor

@sophiebits sophiebits left a comment

Choose a reason for hiding this comment

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

This seems all right although the test case could likely be as simple as:

assert.doesNotThrow(async function() {
throw new Error('should be thrown after a tick');
});

I'll let someone else review too.

@hulkish
Copy link
Contributor Author

hulkish commented May 2, 2017

@spicyj agree the test can be simpler, but i think its good to check for doesNotThrow & also a check to see that the asynchronously thrown error is caught properly.

@@ -216,8 +216,10 @@ helpers.asyncGeneratorDelegate = template(`
helpers.asyncToGenerator = template(`
(function (fn) {
return function () {
var gen = fn.apply(this, arguments);
var _args = arguments;
var _promiseWrapper = this;
return new Promise(function (resolve, reject) {
Copy link
Member

Choose a reason for hiding this comment

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

If you want to change this to an arrow function, that's fine with me. Seems like it would be worth it.

if (err instanceof AssertionError) {
throw err;
}
});
Copy link
Member

Choose a reason for hiding this comment

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

I believe the runner expects a promise to be returned if it is going to have async operations that need to be waited for. As it is, this will queue up a bunch of async operations, but not wait for them to complete before moving on to a new test.

I guess the easiest solution might be to wrap this whole test in return (async function(){ ... })();?

@hulkish
Copy link
Contributor Author

hulkish commented May 2, 2017

@spicyj @loganfsmyth ok:

  • simplified the tests
  • made the exec ran test return an async function
  • improved original change to asyncToGenerator helper template by using an arrow function on returned promise block. This gets transformed to the same behavior:
function _asyncToGenerator(fn) { return function () { var _this = this, _arguments = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(_this, _arguments); [...]

Also, this part is necessary to avoid the unhandled rejected promise warning:

      .then(() => {
        throw new Error('should not occcur');
      })
      .catch(() => true);

The then part is just a redundancy.

@hulkish
Copy link
Contributor Author

hulkish commented May 2, 2017

core-js@2.4.1 node_modules/core-js
make[1]: Leaving directory `/home/travis/build/babel/babel'
make test-only
make[1]: Entering directory `/home/travis/build/babel/babel'
./scripts/test.sh
undefined:233
    return new Promise((resolve, reject) => {
                                         ^^
SyntaxError: Unexpected token =>
    at Object.<anonymous> (/home/travis/build/babel/babel/packages/babel-helper-transform-fixture-test-runner/lib/index.js:186:61)

hmm.. im guessing this fails in ci builds (and not locally) because of node version differences.... should i revert my arrow function change back to final output as pasted in my last comment?

This is the line it dies on:

var babelHelpers = eval((0, _babelCore.buildExternalHelpers)(null, "var"));

Could be wrong, but although the asyncToGenerator helper template eventually gets transformed - it isn't transformed for this eval. Therefore I'm wondering if it's unrelated to this issue and just a bug with the test runner.

Maybe Unrelated to this issue, but possible solution:

  • Might be due to an incomplete lerna bootstrap from any variants of defined recursively-linked dependencies of babel/packages/*.
  • Maybe change this line:

    babel/Makefile

    Line 76 in c46ef65

    ./node_modules/.bin/lerna bootstrap
    to:
./node_modules/.bin/lerna bootstrap --hoist

@hulkish
Copy link
Contributor Author

hulkish commented May 2, 2017

Went ahead and removed the arrow function and just put the expected output in the helper template. That ok?

@xtuc
Copy link
Member

xtuc commented May 2, 2017

Thanks for your PR 👍

The PR is against master (Babel 6) which still support Node < 0.12 and doesn't support arrow functions syntax. I would like to port this to Babel 7 using an arrowFunction, ok for you @loganfsmyth ?

@existentialism
Copy link
Member

Yes, let's target to 7.0

@xtuc
Copy link
Member

xtuc commented May 2, 2017

Ok, as said by @loganfsmyth, would be great to change it to an arrowFunction then.

@hulkish
Copy link
Contributor Author

hulkish commented May 2, 2017

@xtuc do i need to do this? If so, can I get some advice on where to apply the change in order to add support for this per the issue explained here.

@hzoo
Copy link
Member

hzoo commented May 2, 2017

screen shot 2017-05-02 at 2 07 36 pm

Can you change the base of the PR to target 7.0? Then you can use arrow functions fine. Then there won't be a need to apply the es2015 preset either in the exec test.

@hulkish hulkish changed the base branch from master to 7.0 May 2, 2017 18:11
@hulkish
Copy link
Contributor Author

hulkish commented May 2, 2017

@hzoo so I made these changes, resolved the conflicts and pushed... but it's not being reflected here for some reason...what do?

@hulkish
Copy link
Contributor Author

hulkish commented May 2, 2017

Do I need to move my changes to the corresponding 7.0 branch in my forked repo?

@hulkish
Copy link
Contributor Author

hulkish commented May 2, 2017

ok now im a little confused.... i put the base to 7.0, but then there were conflicts... how do i fix this?

@hulkish
Copy link
Contributor Author

hulkish commented May 2, 2017

nvm, got it.

@xtuc
Copy link
Member

xtuc commented May 2, 2017

@hulkish are you fine with the arrowFunction change? The issue was from Node < 0.12. You need to correct the old tests fixtures which still use the function.

@hulkish
Copy link
Contributor Author

hulkish commented May 2, 2017

@xtuc yea im good with it.. I just fixed the test for it.

@hulkish
Copy link
Contributor Author

hulkish commented May 2, 2017

Ok, so back to this on the ci builds, lol:

make[1]: Leaving directory `/home/ubuntu/babel'
make test-only
make[1]: Entering directory `/home/ubuntu/babel'
./scripts/test.sh

undefined:233
    return new Promise((resolve, reject) => {
                                          ^
SyntaxError: Unexpected token >
    at Object.<anonymous> (/home/ubuntu/babel/packages/babel-helper-transform-fixture-test-runner/lib/index.js:186:61)
    at Module._compile (module.js:456:26)
    at Module._extensions..js (module.js:474:10)

hulkish and others added 2 commits May 2, 2017 20:38
…mrproved change to self/arguments refs by using arrow function on returned promise
@hulkish
Copy link
Contributor Author

hulkish commented May 3, 2017

prq6mcrjq4vno

@hulkish
Copy link
Contributor Author

hulkish commented May 3, 2017

@loganfsmyth anything else i need to do here?

@@ -0,0 +1,7 @@
{
"presets": ["es2015"],
Copy link
Member

Choose a reason for hiding this comment

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

Still needed post move to 7.0?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't realize this should be moved out for 7.0 branch, ill go ahead and do that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

how's that?

@loganfsmyth
Copy link
Member

The easiest fix for those failures would be to add use strict to those tests, I think.

@loganfsmyth
Copy link
Member

By the way, thanks so much for this, I know it's been a lot of back and forth, hope it's not too bad of an experience.

@hulkish
Copy link
Contributor Author

hulkish commented May 3, 2017

@loganfsmyth no no, its cool... its far more important that the outcome is desired than to rush this anyway

@hulkish
Copy link
Contributor Author

hulkish commented May 3, 2017

@loganfsmyth im kinda stuck now though...travis build fails but circlici build works haha

@jridgewell
Copy link
Member

Node 4 doesn't support destructuring, you'll have to enable the transform to run the exec test.

@hulkish
Copy link
Contributor Author

hulkish commented May 4, 2017

@jridgewell @loganfsmyth ok i think this is the one!!

@loganfsmyth
Copy link
Member

Thanks so much!

@hzoo hzoo added the PR: Bug Fix 🐛 A type of pull request used for our changelog categories label May 15, 2017
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 6, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Bug Fix 🐛 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants