Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse-function fails for async class method containing a nested arrow function #152

Open
Neaox opened this issue Oct 24, 2020 · 4 comments
Labels
Pkg: parse-function Priority: High After critical issues are fixed, these should be dealt with before any further issues. Status: Accepted It's clear what the subject of the issue is about, and what the resolution should be. Status: Available No one has claimed for resolving this issue. Generally applied to bugs and enhancement issues. Type: Bug Inconsistencies or issues which will cause an issue or problem for users or implementors.

Comments

@Neaox
Copy link

Neaox commented Oct 24, 2020

Support plan

  • which support plan is this issue covered by? (e.g. Community, Sponsor, or
    Enterprise): Community
  • is this issue currently blocking your project? (yes/no): No
  • is this issue affecting a production system? (yes/no): No

Context

  • node version: v12.19.0
  • module version: 5.6.10
  • environment (e.g. node, browser, native): node
  • used with (i.e. popular names of modules): typescript
  • any other relevant information:

What are you trying to achieve or the steps to reproduce?

This seems to only occur under these specific curcumstances

  • Must be a class method
  • Must be async
  • Must contain a nested arrow function

If any of the above are not true, the issue does not occur.

Demo: https://repl.it/join/yibjtdqc-neaox

Code:

const parseFunction = require("parse-function");

class TestClass {
    async TestFunction() {
       () => true;
    }
}

const app = parseFunction();
app.parse(TestClass.prototype.TestFunction)

What was the result you got?

SyntaxError: Unexpected token, expected "=>" (1:18)

What result did you expect?

The function to parse correctly.

Workaround

Use a function declaration within the class method instead of an arrow function:

const parseFunction = require("parse-function");

class TestClass {
    async TestFunction() {
       function nested() { return true; }
    }
}

const app = parseFunction();
app.parse(TestClass.prototype.TestFunction)
@tunnckoCore
Copy link
Owner

I don't think ()=> this way is valid anyway. Or at least isn't used that much. It should be declaration or not arrow fn.

@Neaox
Copy link
Author

Neaox commented Nov 2, 2020

That was just a stripped back example to reproduce the issue.

Using an arrow function as a callback within the class method is a more likely example.

@Neaox
Copy link
Author

Neaox commented Nov 2, 2020

My actual use case:

class ItemApi {
  async GetItems() {
    const items = await this.cache.getOrSet(
      ITEMS_CACHE_KEY,
      () => this.db.getItems(),
      ITEMS_CACHE_TTL
    );

    const item = sample(items);

    if (!item) {
      throw new ConflictError();
    }

    return item;
  }
}

@tunnckoCore tunnckoCore added Pkg: parse-function Priority: High After critical issues are fixed, these should be dealt with before any further issues. Status: Accepted It's clear what the subject of the issue is about, and what the resolution should be. Status: Available No one has claimed for resolving this issue. Generally applied to bugs and enhancement issues. Type: Bug Inconsistencies or issues which will cause an issue or problem for users or implementors. labels Nov 26, 2020
@tunnckoCore
Copy link
Owner

tunnckoCore commented Nov 26, 2020

Ahh, yep. Accepted.

The problem is here. Should update this algorithm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Pkg: parse-function Priority: High After critical issues are fixed, these should be dealt with before any further issues. Status: Accepted It's clear what the subject of the issue is about, and what the resolution should be. Status: Available No one has claimed for resolving this issue. Generally applied to bugs and enhancement issues. Type: Bug Inconsistencies or issues which will cause an issue or problem for users or implementors.
Projects
None yet
Development

No branches or pull requests

2 participants