-
-
Notifications
You must be signed in to change notification settings - Fork 6
Arrow function with inner function fails to parse #179
Comments
Thanks for the report. Interesting, I'll look on it. |
For some reason, wrapping it as string works. import parseFunction from 'parse-function';
const res = parseFunction().parse(
`(arg, bar) =>
new Promise((resolve) => {
function inner() {}
inner();
})`,
);
console.log(res);
/*
{ name: null,
body:
'new Promise((resolve) => {\n function inner() {}\n\n inner();\n })',
args: [ 'arg', 'bar' ],
params: 'arg, bar' }
*/ |
Actually, forget that. When I initially pasted it and executed it really it failed. But when it is formatted, your snippet works too. import parseFunction from 'parse-function';
parseFunction().parse(
(arg) =>
new Promise((resolve) => {
function inner() {}
inner();
}),
); Does not fail. But you are kind of right, it's probably something with the wrapping. But it's done that way because we use This works too parseFunction().parse(arg => {
return new Promise(resolve => {
function inner() {}
inner();
});
}); but this doesn't parseFunction().parse(arg => new Promise(resolve => {
function inner() {}
inner();
});
); So, please use Eslint and/or Prettier. |
Thanks for the quick update. Actually I am using eslint already and Really strange adding a linebreak works, from all the things I did to debug this thats one of the only things I didnt try ;) Just wondering, if it is about the parseFunction().parse(arg => (new Promise(resolve => {
function inner() {}
inner();
}));
); |
@pimlie yea, I'm not sure why it is happening too. I have a pretty big eslint config, so I'm not sure if ESLint-only approach will help either. That's one of the things why Prettier is amazing. It's kinda strange because it's perfectly valid javascript. 'use strict'
const foo = arg => new Promise(resolve => {
function inner() {}
inner()
})
foo(123) |
Agree on that too. Hey, @pimlie did you found something? I'm just looking around because finished moving it to tunnckoCore/opensource#62 |
Nope, unfortunately not. But in the mean time my requirements changed and I also needed to be able to parse full code snippets, not just functions, which means I am currently using babel to parse/transform the code. So feel free to close this issue if you want and thanks again for all the feedback! |
The following snippet fails to parse because
result.value
is erroneously(?) wrapped in{ ... }
on line https://github.com/tunnckoCoreLabs/parse-function/blob/master/src/index.js#L119-L121babylon
/@babel/parser
throws the following error:Maybe just add
isArrow
to the if on line 119? Or might that cause problems with arrow functions in normal function bodies because theincludes('=>')
is too greedy and should only look at the start?The text was updated successfully, but these errors were encountered: