Giter Club home page Giter Club logo

Comments (5)

eush77 avatar eush77 commented on May 29, 2024

I think we need some expression parsing anyway to handle ES6 default parameters:

function (opts = { foo: { done: (x) => console.log({ value: x }) } }) {
  // ...
}

However, we don't actually need a full-fledged ES parser (like acorn) for that, and basic preparsing would allow us to skip default parameters and capture argument names very quickly.

I'm not familiar with snapdragon, but simple recursive descent would definitely work. For that we need to implement several simple tokenizers for identifiers, strings, numbers, punctuation, and comments.

Then I imagine the whole process to be something like this:

  1. Start tokenizing, see if the subject is an arrow function, generator, or plain ES5 function.
  2. Tokenize function name (an identifier).
  3. Parse argument list:
    1. Tokenize identifier (and save its value).
    2. Tokenize , or =.
    3. If = (argument has a default param), skip the following expression (just keep track of parentheses really, we don't need to actually parse anything).
  4. Save the rest of the string as the function body and stop.

This is some work, but not that hard, since we don't need to construct an AST. We can allow a much more loose grammar than EcmaScript defines:

function (opts = { foo foo:: { bar / } }) {
  // ...
}

from parse-function.

tunnckoCore avatar tunnckoCore commented on May 29, 2024

Hmmm. Okey, I made some benchmarks tests and with regex is 40-60x faster, lol. So we will back to regex approach. I already have ready regex that handles regular and arrow functions.

from parse-function.

eush77 avatar eush77 commented on May 29, 2024

@tunnckoCore What I'm saying is that regexes won't ever support ES6 default parameters feature (see #8).

There are many modules that do regex-based parsing (js-args-names is one example), but they all are broken for ES6. We can do better and be the first module that works correctly with ES6.

> parseFunction('function (opts = { foo: { done: (x) => console.log({ value: x }) } }, cb) { /* ... */ }')
{ name: 'anonymous',
  body: ' foo: { done: (x) => console.log({ value: x }) } }, cb) { /* ... */ ',
  args: [ 'opts' ],
  params: 'opts' }

from parse-function.

tunnckoCore avatar tunnckoCore commented on May 29, 2024

@eush77 you're right. PRs always welcome :)

from parse-function.

tunnckoCore avatar tunnckoCore commented on May 29, 2024

Again, very thanks! Hope this helps! Review the changelog.

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.