Skip to content
This repository has been archived by the owner on Feb 8, 2020. It is now read-only.

Class methods #15

Closed
rkaw92 opened this issue Apr 12, 2016 · 7 comments
Closed

Class methods #15

rkaw92 opened this issue Apr 12, 2016 · 7 comments

Comments

@rkaw92
Copy link

rkaw92 commented Apr 12, 2016

Parsing class methods seems to yield empty bodies, anonymous names and empty arg lists.

Example:

'use strict';

var parseFunction = require('parse-function');

class EqualityChecker {
    isEqual(a, b) {
        return a === b;
    }
}

var checker = new EqualityChecker();
console.log('Prototype:', parseFunction(EqualityChecker.prototype.isEqual));
console.log('Instance:', parseFunction(checker.isEqual));

Both console.logs will print { name: 'anonymous', args: [], params: '', body: '' }.

I suspect it is because .toString() on methods returns the exact representation from the code, from the first to the last token, so in our case (tabs originally in the string):

isEqual(a, b) {
                return a === b;
        }

The same problem occurs with this compacted ES6 "function-less" notation:

parseFunction({
    isEqual(a, b) {
        return a === b;
    }
}.isEqual)
@tunnckoCore
Copy link
Owner

Hey, very thanks! I'll look into it, I think we just should look more on acorn ast.

@tunnckoCore
Copy link
Owner

I'm starting to think it may be acorn issue.

acorn.parse(EqualityChecker.prototype.isEqual.toString(), {ecmaVersion: 7})

throws a syntax error. Or maybe this is the reason why acorn.parse_dammit exists. And that's why I using it, because such cases. I'm looking on ast that parse_dammit returns for that, may need more ifs in the walk :)

@tunnckoCore
Copy link
Owner

There are few ways:

  1. detect if result of toString() begins with function or at least the letter f and if not prepend it
  2. add if in the walk to listen to CallExpression

Second feels kinda wrong to me and I'm thinking it would mess the things more than it solve them.

@tunnckoCore tunnckoCore added bug and removed bug labels Apr 12, 2016
@tunnckoCore
Copy link
Owner

What you think about the fix?

edit: Ooops, labels should stay.

@rkaw92
Copy link
Author

rkaw92 commented Apr 13, 2016

As much as I am bewildered by the choice of substring check mechanism ([0] and [1] - I assume it's for performance reasons), this is exactly the workaround that I did in my code:

let functionString = methodFunction.toString();
if (functionString.slice(0, 8) !== 'function') {
    functionString = 'function ' + functionString;
}

I agree that it is the sane thing to do. Checking for function calls is probably best left alone - there is another npm module for that (extracts call parameters), and it may be wise not to expand into that field.

@tunnckoCore
Copy link
Owner

Yea.

extracts call parameters

You can try http://npm.im/function-arguments, it works for regular, generator and arrow functions. If you need support only for regular functions can use Sindre's fn-args, it's lighter and probably faster.

It would be better choice to use one of them if you only need getting arguments.

@tunnckoCore
Copy link
Owner

Oh sorry, i forget to publish. I doing it now :) v2.3.2

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants