Giter Club home page Giter Club logo

Comments (7)

pimlie avatar pimlie commented on May 24, 2024 1

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!

from parse-function.

tunnckoCore avatar tunnckoCore commented on May 24, 2024

Thanks for the report.

Interesting, I'll look on it.

from parse-function.

tunnckoCore avatar tunnckoCore commented on May 24, 2024

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' }
*/

from parse-function.

tunnckoCore avatar tunnckoCore commented on May 24, 2024

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 .parseExpression.

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.

from parse-function.

pimlie avatar pimlie commented on May 24, 2024

Thanks for the quick update. Actually I am using eslint already and it didnt report anytning because it has 'arrow-parens': [2, 'as-needed', { requireForBlockBody: true }], set and the rule doesnt think I need a block'ed body I guess.
-- edit --
hmm sorry, this rule is about () around the args ofc. I extend some other configs, will have a look at them what they do
-- /edit --

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 new Promise expression I would've expected the following to work too (but it doesnt):

parseFunction().parse(arg => (new Promise(resolve => {
    function inner() {}
    inner();
  }));
);

from parse-function.

tunnckoCore avatar tunnckoCore commented on May 24, 2024

@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)

from parse-function.

tunnckoCore avatar tunnckoCore commented on May 24, 2024

I would've expected the following to work too

Agree on that too.

Hey, @pimlie did you found something? I'm just looking around because finished moving it to tunnckoCore/opensource#62

from parse-function.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.