Skip to content

Conversation

@tunnckoCore
Copy link

add support for accepting string type and check against

var isArrowFunction = require('is-arrow-function');
console.log(isArrowFunction('(a, b) => a * b'));
// => true

@ljharb
Copy link
Member

ljharb commented Jan 26, 2016

What's the use case here?

@tunnckoCore
Copy link
Author

I'm working on update for https://github.com/tunnckoCore/parse-function, which also accepts string, so it is something like this

var fnRegex = require('function-regex')
var isArrow = require('is-arrow-function')
var fnToStr = Function.prototype.toString

function parseFunction (val) {
  var type = typeof val
  if (type !== 'string' && type !== 'function') {
    return {}
  }
  if (type === 'function') {
    val = fnToStr.call(val)
  }
  if (isArrow(val)) {
    var match = arrowFnWithParensRegex.exec(val)
    match = match === null ? arrowFnWithoutParensRegex.exec(val) : match

    if (!match) return {}

    var args = arraify(match[1])
    return {
      name: 'anonymous',
      params: match[1],
      parameters: match[1],
      args: args,
      arguments: args,
      body: match[2] || ''
    }
  }

  var match = fnRegex().exec(val)
  var args = arraify(match[2])
  return {
    name: match[1] || 'anonymous',
    params: match[2],
    parameters: match[2],
    args: args,
    arguments: args,
    body: match[3] || ''
  }
}

so example is

parseFunction('(a, b) => a * b')
// => { name: 'anonymous',
//   params: 'a, b',
//   parameters: 'a, b',
//   args: [ 'a', 'b' ],
//   arguments: [ 'a', 'b' ],
//   body: 'a * b' }

parseFunction(x => x * 2)
// => { name: 'anonymous',
//  params: 'x',
//  parameters: 'x',
//  args: [ 'x' ],
//  arguments: [ 'x' ],
//  body: 'x * 2' }

In short. It should check that value of the string is arrow function then run some regex on that string.

@ljharb
Copy link
Member

ljharb commented Jan 26, 2016

I see how this could be useful for you - but this module is called "is arrow function" - and, by using Function#toString, it takes advantage of source code normalization that the string approach would not benefit from.

I don't think this is a good use case for this module - if you want to parse actual code, you should be using an actual JS parser that produces an AST, not using string operations.

@ljharb ljharb closed this Jan 26, 2016
@tunnckoCore
Copy link
Author

if you want to parse actual code, you should be using an actual JS parser that produces an AST, not using string operations.

Partially agree. But some people still don't want esprima or acorn or babel to do simple things. Like some users of my libs.

But okey, thanks!

Cheers,
Charlike.

@tunnckoCore
Copy link
Author

Also you can't get function body or arguments names so easy with such parsers as esprima/acorn, they are used for other things, or more low-level things.

@ljharb
Copy link
Member

ljharb commented Jan 26, 2016

you can definitely get argument names, thats part of the AST. you're right that you lose the original body text.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants