2

How can I parse a javascript class in order to get a list of all its methods?

So far I have tried below parsers:

Here's all my attempts:

Nodejs

import * as esprima from "esprima";
import * as acorn from "acorn";

const snippets = [
    "class Foo {}", // 0
    "class Foo { constructor() {} }", // 1
    "class Foo { constructor() {}; bar() {} }", // 2
    "class Foo { constructor() {}; bar() {}; baz = () => {}; }", // 3
    "let baz = () => {}", // 4
]

console.log("--------esprima--------");
for(let [i,snippet] of Object.entries(snippets)) {
    try {
        esprima.parseScript(snippet);
        console.log(`snippet ${i}: ok`);
    } catch(e) {
        console.log(`snippet ${i}: failed`);
    }
}
console.log("--------acorn--------");
for(let [i,snippet] of Object.entries(snippets)) {
    try {
        acorn.parse(snippet);
        console.log(`snippet ${i}: ok`);
    } catch(e) {
        console.log(`snippet ${i}: failed`);
    }
}

output:

--------esprima--------
snippet 0: ok
snippet 1: ok
snippet 2: ok
snippet 3: failed
snippet 4: ok
--------acorn--------
snippet 0: ok
snippet 1: ok
snippet 2: ok
snippet 3: failed
snippet 4: ok

Python

import esprima

snippets = [
    "class Foo {}",  # 0
    "class Foo { constructor() {} }",  # 1
    "class Foo { constructor() {}; bar() {} }",  # 2
    "class Foo { constructor() {}; bar() {}; baz = () => {}; }",  # 3
    "let baz = () => {}",  # 4
]

print("--------esprima--------")
for [i, snippet] in enumerate(snippets):
    try:
        esprima.parseScript(snippet)
        print(f"snippet {i}: ok")
    except:
        print(f"snippet {i}: failed")

output:

--------esprima--------
snippet 0: ok
snippet 1: ok
snippet 2: ok
snippet 3: failed
snippet 4: ok

As you can see when using arrow functions as methods the above parsers will fail. That's not good as the classes from the project I'm trying to parse/analize will be mostly es9 syntax (await, async, spread operator, arrow function methods, ...).

2
  • Class fields aren’t part of the official standard yet. They will be officially included around June 2022 (and the likelihood they won’t is practically zero). What exactly is your question about? Supporting class fields or extracting class elements? Commented Nov 18, 2021 at 3:34
  • @SebastianSimon The goal is being able to extract class methods (either normal functions or arrow functions) so I'll be able to generate automatically unit tests placeholders for a large project. Commented Nov 19, 2021 at 10:02

2 Answers 2

3
+25

You can extend acorn to handle class fields with:

npm install acorn-class-fields

Here is my working code for your example that uses acorn:

const acorn = require('acorn');

const MyParser = acorn.Parser.extend(require('acorn-class-fields'));

const snippets = [
  'class Foo {}', // 0
  'class Foo { constructor() {} }', // 1
  'class Foo { constructor() {}; bar() {} }', // 2
  'class Foo { constructor() {}; bar() {}; baz = () => {}; }', // 3
  'let baz = () => {}', // 4
];

console.log('--------acorn--------');
for (let [i, snippet] of Object.entries(snippets)) {
  try {
    MyParser.parse(snippet, { ecmaVersion: 'latest' });
    console.log(`snippet ${i}: ok`);
  } catch (e) {
    console.log(`snippet ${i}: failed`);
  }
}

Output:

--------acorn--------
snippet 0: ok
snippet 1: ok
snippet 2: ok
snippet 3: ok
snippet 4: ok
Sign up to request clarification or add additional context in comments.

Comments

1

The specification of class fields has been finalized in April 2021, but it will be published with ECMAScript 2022, expected next year. For this reason, class fields are not universally supported yet.

Now, Esprima only supports ECMAVersion 2019 according to their documentation, and class fields support is not implemented yet (November 2021).

I had better luck playing with acorn:

acorn.parse(snippet, { ecmaVersion: 2022 });

The current version of acorn supports class fields out of the box if you set the ecmaVersion option to 2022 or "latest".

The way you further proceed to extract the method names depends entirely on the specifics of your problem. For example, do you want to include function declarations out of a class body like let baz = () => {}? What about static methods? And inherited methods? Will the snippets only include classes or also other elements like import statements? Do you have any getters or setters? Or computed method names? The list is long...

That said, if you trust the code in your project, under certain assumptions, you could get the method names just by parsing the source with the Function constructor and extracting the method names from the object returned.

const snippets = [
    "class Foo {}", // 0
    "class Foo { constructor() {} }", // 1
    "class Foo { constructor() {}; bar() {} }", // 2
    "class Foo { constructor() {}; bar() {}; baz = () => {}; }", // 3
    "let baz = () => {}", // 4
];

for (let [i,snippet] of Object.entries(snippets)) {
    try {
        const names =
          Object.getOwnPropertyNames(Function('return ' + snippet)().prototype)
          .filter(name => typeof name !== 'function' && !(name in Function.prototype));
        console.log(`snippet ${i}: ${names}`);
    } catch(e) {
        console.log(`snippet ${i}: failed!`);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.