profile
viewpoint
Ryan Petrich rpetrich New York City, NY https://rpetri.ch/ Reverse-engineer, forward-engineer, backwards-engineer

rpetrich/AppList 245

Dynamic access to installed application list, their icons and simple preferences panes

rpetrich/babel-plugin-transform-async-to-promises 228

Transform async/await to somewhat idiomatic JavaScript promise chains

andrewwiik/iOS-Blocks 76

The iOS Blocks Project - Android has Widgets, Windows has Live Tiles and now iOS has Blocks

grp/amfi_interpose 58

aka failbreak — portable, fully userland jailbreak for (around) 5.0 to 6.1

grp/WeeLoader 33

Weeeeeeeeeeeeeeeeee!

grp/Infiniapps 26

The three Infiniapps.

rpetrich/battery_stat 16

Simple program to read Apple battery information as json, text or in a format suitable for a shell prompt

rpetrich/AttachmentEncryptor 9

Fix for http://www.andreas-kurtz.de/2014/04/what-apple-missed-to-fix-in-ios-711.html

issue commentrpetrich/babel-plugin-transform-async-to-promises

break transform to return

Which version are you using? I've imported the following test case:

async () => {
	const r = await test1();
	switch (r) {
		case "1":
			console.log("1111");
			break;
		case "2":
			console.log("2222");
			break;
	}
	console.log("33333333333");
}

And get the following output on version 0.8.16:

function(){try{return Promise.resolve(test1()).then(function(r){switch(r){case"1":console.log("1111");break;case"2":console.log("2222");break;}console.log("33333333333");});}catch(e){return Promise.reject(e);}}

Which unminifies to:

function() {
    try {
        return Promise.resolve(test1()).then(function(r) {
            switch (r) {
                case "1":
                    console.log("1111");
                    break;
                case "2":
                    console.log("2222");
                    break;
            }
            console.log("33333333333");
        });
    } catch (e) {
        return Promise.reject(e);
    }
}
chinayu9

comment created time in 7 days

pull request commentrpetrich/babel-plugin-transform-async-to-promises

docs: add export so that code works with plugin

The underlying issue has been fixed in 504a1d9.

kentcdodds

comment created time in 10 days

push eventrpetrich/babel-plugin-transform-async-to-promises

Ryan Petrich

commit sha 072d329f27405d181aa19246cf67f1b7ff954fd5

Public Release 0.8.16

view details

push time in 11 days

created tagrpetrich/babel-plugin-transform-async-to-promises

tagv0.8.16

Transform async/await to somewhat idiomatic JavaScript promise chains

created time in 11 days

push eventrpetrich/babel-plugin-transform-async-to-promises

Kent C. Dodds

commit sha e535d4bf36f3c65c7d6e4f2ad8a252cac8cf4014

docs: add note about `externalHelpers` (#57) Closes #52

view details

push time in 11 days

PullRequestReviewEvent

PR closed rpetrich/babel-plugin-transform-async-to-promises

Bump tmpl from 1.0.4 to 1.0.5 dependencies

Bumps tmpl from 1.0.4 to 1.0.5. <details> <summary>Commits</summary> <ul> <li>See full diff in <a href="https://github.com/daaku/nodejs-tmpl/commits/v1.0.5">compare view</a></li> </ul> </details> <br />

Dependabot compatibility score

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


<details> <summary>Dependabot commands and options</summary> <br />

You can trigger Dependabot actions by commenting on this PR:

  • @dependabot rebase will rebase this PR
  • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
  • @dependabot merge will merge this PR after your CI passes on it
  • @dependabot squash and merge will squash and merge this PR after your CI passes on it
  • @dependabot cancel merge will cancel a previously requested merge and block automerging
  • @dependabot reopen will reopen this PR if it is closed
  • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
  • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
  • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
  • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
  • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
  • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
  • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
  • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language

You can disable automated security fix PRs for this repo from the Security Alerts page.

</details>

+3 -3

0 comment

1 changed file

dependabot[bot]

pr closed time in 11 days

PR closed rpetrich/babel-plugin-transform-async-to-promises

Bump path-parse from 1.0.6 to 1.0.7 dependencies

Bumps path-parse from 1.0.6 to 1.0.7. <details> <summary>Commits</summary> <ul> <li>See full diff in <a href="https://github.com/jbgutierrez/path-parse/commits/v1.0.7">compare view</a></li> </ul> </details> <br />

Dependabot compatibility score

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


<details> <summary>Dependabot commands and options</summary> <br />

You can trigger Dependabot actions by commenting on this PR:

  • @dependabot rebase will rebase this PR
  • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
  • @dependabot merge will merge this PR after your CI passes on it
  • @dependabot squash and merge will squash and merge this PR after your CI passes on it
  • @dependabot cancel merge will cancel a previously requested merge and block automerging
  • @dependabot reopen will reopen this PR if it is closed
  • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
  • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
  • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
  • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
  • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
  • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
  • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
  • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language

You can disable automated security fix PRs for this repo from the Security Alerts page.

</details>

+3 -3

0 comment

1 changed file

dependabot[bot]

pr closed time in 11 days

push eventrpetrich/babel-plugin-transform-async-to-promises

Ryan Petrich

commit sha 13d5ce3e07d7d9c20c376713d6024892959ef291

Fix cases where all paths break in a while loop (closes #47)

view details

Ryan Petrich

commit sha a67137f773a2425209386a2262df5f7fa75badf1

Update TypeScript and node typings

view details

push time in 11 days

issue closedrpetrich/babel-plugin-transform-async-to-promises

break does not work?

Consider the following snippet:

(async () => {
	while (true) {
		console.log("loop");
		await null; // important
		break;
	}
})();

If I run this with node it only prints the string once:

$ node test.js 
loop

but if I run it via transform-async-to-promises I get an infinite loop:

$ ./node_modules/.bin/babel test.js | node
loop
loop
loop
loop
loop
... (keeps going indefinetly)

closed time in 11 days

redneb

push eventrpetrich/babel-plugin-transform-async-to-promises

Ryan Petrich

commit sha 58341265ab6d8dc912cd39e14a62f504ecc8af3e

Add test cases for named async function expressions (closes #50)

view details

push time in 12 days

issue closedrpetrich/babel-plugin-transform-async-to-promises

Named async function expression returning a variable value does not return a promise

Minimal repro case:

const foo = async function _foo() {
  return bar;
};

Output:

const foo = function _foo() {
  try {
    return bar;
  } catch (e) {
    return Promise.reject(e);
  }
};

return bar() will also reproduce it.

Deleting const foo = (changing from a function expression to a function declaration), removing the name of the function, or returning a constant value (e.g. 3) will cause it to behave correctly.

closed time in 12 days

scq

push eventrpetrich/babel-plugin-transform-async-to-promises

Ryan Petrich

commit sha d27a1eb74b86f85ef68c00a6987759675ce368d5

Properly track whether break/continue apply to an inner switch or loop (closes #49)

view details

push time in 12 days

issue closedrpetrich/babel-plugin-transform-async-to-promises

Incorrect transforming break inside switch-case

I create a repo with reproduction: https://github.com/Djaler/babel-plugin-transform-async-to-promises-switch-bug

Brief comparison of the original code with transformed:

export async function doSomething() {
    await sleep(1000)
    const errorCode = 2; //why not

    let message = 'Something wrong';

    switch (errorCode) {
        case 2:
            message = "Error 2"
            break
    }

    alert(message)
}
var doSomething = _async(function () {
  return _await(sleep(1000), function () {
    var errorCode = 2; //why not

    var message = 'Something wrong';

    switch (errorCode) {
      case 2:
        message = "Error 2";
        return;
    }

    alert(message);
  });
});

As you can see, break turns into return, and because of this alert is never called

closed time in 12 days

Djaler

push eventrpetrich/babel-plugin-transform-async-to-promises

Ryan Petrich

commit sha 65791b9ca1da98272ce4d6fc237a9a4031b77e01

Fix exit tracking when the argument expression of an early return statement throws (closes #53)

view details

push time in 13 days

issue closedrpetrich/babel-plugin-transform-async-to-promises

Incorrect compilation

Current Behavior

My function

export async function getAccount(provider: Provider): Promise<string | null> {
  try {
    const accounts: string[] = await provider.send('eth_accounts'); // 1. provider.send returns undefined
    return accounts[0] || null; // 2. throw error 'Cannot read property '0' of undefined'
  } catch {
    warning('Some text'); // 3. log warning
  }

  return 'fallback account'; // 4. return 'fallback account'
}

compiled to

var getAccount = function getAccount(provider) {
  try {
    var _exit2 = false;

    var _temp2 = _catch(function () {
      return Promise.resolve(provider.send('eth_accounts')).then(function (accounts) { // 1. accounts === undefined
        _exit2 = true;
        return accounts[0] || null; // 2. throw error 'Cannot read property '0' of undefined'
      });
    }, function () {
      process.env.NODE_ENV !== "production" ? warning('Some text') : void 0; // 3. log warning
    });

    return Promise.resolve(_temp2 && _temp2.then ? _temp2.then(function (_result) {
      return _exit2 ? _result : 'fallback account'; // 4. return undefined because _exit2 === true and _result === undefined
    }) : _exit2 ? _temp2 : 'fallback account');
  } catch (e) {
    return Promise.reject(e);
  }
};

Expected behavior

compiled to

var getAccount = function getAccount(provider) {
  try {
    var _exit2 = false;

    var _temp2 = _catch(function () {
      return Promise.resolve(provider.send('eth_accounts')).then(function (accounts) { // 1. accounts === undefined
-       _exit2 = true;
        return accounts[0] || null; // 2. throw error 'Cannot read property '0' of undefined'
+     }).then(function () {
+       _exit2 = true;
      });
    }, function () {
      process.env.NODE_ENV !== "production" ? warning('Some text') : void 0; // 3. log warning
    });

    return Promise.resolve(_temp2 && _temp2.then ? _temp2.then(function (_result) {
-     return _exit2 ? _result : 'fallback account'; // 4. return undefined because _exit2 === true and _result === undefined
+     return _exit2 ? _result : 'fallback account'; // 4. return 'fallback account' because _exit2 === false
    }) : _exit2 ? _temp2 : 'fallback account');
  } catch (e) {
    return Promise.reject(e);
  }
};

My environment

<!-- PLEASE FILL THIS OUT -->

Software Version(s)
Plugin 0.8.14
npm/Yarn npm 6.12.0/yarn 1.21.1
Node v10.15.0
Operating System macOS Catalina 10.15.2

closed time in 13 days

in19farkt

issue closedrpetrich/babel-plugin-transform-async-to-promises

Async function properties in subclass result in accessing this before super() call

Came across an issue with async function class properties in a subclass. For this code:

class Base {
	a = Promise.resolve(42)
}

class Sub extends Base {
	f = async () => {
		await this.a;
	}
};

new Sub();

The output is:

function _defineProperty(obj, key, value) {
	...
}

class Base {
	constructor() {
		_defineProperty(this, "a", Promise.resolve(42));
	}
}

class Sub extends Base {
	constructor(...args) {
		const _this = this;

		super(...args);

		_defineProperty(this, "f", _async(() => _awaitIgnored(_this.a)));
	}
};

new Sub();

The const _this = this; line causes problems at runtime due to the access of this prior to calling the superclass constructor.

It seems like this could potentially have a resolution similar to https://github.com/babel/babel/issues/9609.

Plugin version: 0.8.15

closed time in 13 days

jimmydief

issue commentrpetrich/babel-plugin-transform-async-to-promises

Async function properties in subclass result in accessing this before super() call

This was a tricky bug to resolve. Babel's Scope.push method will happily push declarations before the call to the super constructor. Resolved in bb666824fa522ab88d29a86c7c5aa4d37c6afc1b

jimmydief

comment created time in 13 days

PR merged rpetrich/babel-plugin-transform-async-to-promises

Add failing test case for bug with subclass async properties

Failing test for #54. Looked briefly into the solution from https://github.com/babel/babel/pull/9610, can investigate further if that seems like the right route.

+1612 -1404

0 comment

8 changed files

jimmydief

pr closed time in 13 days

push eventrpetrich/babel-plugin-transform-async-to-promises

James Diefenderfer

commit sha c982d7caf410bc13e0c73d77ffb88cf791e2d936

make test harness complain about unmapped plugins This avoids some potential confusion due to the plugins specified in a test’s options not being actual plugin package names.

view details

James Diefenderfer

commit sha 9e81df611b5649fd2c341eacce8d7af48e261344

add class properties transform to test harness This allows writing tests that require interaction with the class properties transform.

view details

James Diefenderfer

commit sha f585196b33512a94b2d779950349d7e526af7bea

add failing test for #54

view details

Ryan Petrich

commit sha 19d232542e69e7ce3b11ef89ea31d31734389268

Remove .DS_Store file

view details

Ryan Petrich

commit sha 2c31dddab0d3e280f471eada6943fbbad7da706c

Merge remote-tracking branch 'jimmydief/fix_subclass_async_properties'

view details

Ryan Petrich

commit sha bb666824fa522ab88d29a86c7c5aa4d37c6afc1b

Workaround Scope.push not taking into account calls to super within constructors

view details

Ryan Petrich

commit sha 504a1d9a0d7e2627c76fb596a11a474905318c56

Workaround inserting helpers failing due to lack of target (closes #51)

view details

push time in 13 days

issue closedrpetrich/babel-plugin-transform-async-to-promises

TypeError: unknown: Cannot read property 'isVariableDeclaration' of undefined

When trying to run the example I am getting this error:

$ node compile-babel.js
/project/node_modules/@babel/core/lib/transformation/index.js:58
    throw e;
    ^

TypeError: unknown: Cannot read property 'isVariableDeclaration' of undefined
    at insertHelper (/project/node_modules/babel-plugin-transform-async-to-promises/async-to-promises.js:2875:29)
    at helperReference (/project/node_modules/babel-plugin-transform-async-to-promises/async-to-promises.js:3007:33)
    at PluginPass.FunctionExpression (/project/node_modules/babel-plugin-transform-async-to-promises/async-to-promises.js:3547:71)
    at newFn (/project/node_modules/@babel/traverse/lib/visitors.js:179:21)
    at NodePath._call (/project/node_modules/@babel/traverse/lib/path/context.js:55:20)
    at NodePath.call (/project/node_modules/@babel/traverse/lib/path/context.js:42:17)
    at NodePath.visit (/project/node_modules/@babel/traverse/lib/path/context.js:90:31)
    at TraversalContext.visitQueue (/project/node_modules/@babel/traverse/lib/context.js:112:16)
    at TraversalContext.visitSingle (/project/node_modules/@babel/traverse/lib/context.js:84:19)
    at TraversalContext.visit (/project/node_modules/@babel/traverse/lib/context.js:140:19) {
code: 'BABEL_TRANSFORM_ERROR'
}

Here is the script:

const code = `
async function fetchAsObjectURL(url) {
    const response = await fetch(url);
    const blob = await response.blob();
    return URL.createObjectURL(blob);
}
`;
var babel = require('@babel/core');
var result = babel.transform(code, {
    plugins: ['babel-plugin-transform-async-to-promises'],
});
process.stdout.write(result.code + '\n');

closed time in 13 days

Petah

create barnchrpetrich/iphoneheaders

branch : activator

created branch time in 15 days

delete branch rpetrich/iphoneheaders

delete branch : activator

delete time in 15 days

create barnchrpetrich/iphoneheaders

branch : activator

created branch time in 15 days

issue closedrpetrich/babel-plugin-transform-async-to-promises

Any plans for the reversed process?

I want to convert some old fashioned Promise style code to async/await. Is there any plans for supporting this feature?

closed time in 15 days

liudonghua123

issue commentrpetrich/babel-plugin-transform-async-to-promises

Any plans for the reversed process?

I don't intend to build this. Producing idiomatic async JavaScript from promise chains is quite complicated and even with clever algorithms to detect common patterns, most of the time will require human intervention anyway.

liudonghua123

comment created time in 15 days

issue commentrpetrich/babel-plugin-transform-async-to-promises

Inline helpers without globals

I suspect that transform-async-to-promises is processing the output of your custom bundling step rather than the other way around. Could you describe your bundling setup in more detail if this is still an issue for you?

lennart-m

comment created time in 15 days

issue commentrpetrich/babel-plugin-transform-async-to-promises

Incorrect code for transformed class + conditional

Thanks for reporting. I traced this down to a bug with how return statements are rewritten inside named function expressions. There aren't any named function expressions in your source, but babel's transform-classes plugin converts methods to named function expressions before async-to-promises runs.

joshkel

comment created time in 15 days

more