Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

356
Visualizações
How to parse & extract javascript class methods?

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

So far I have tried below parsers:

  • acorn@8.5.0, esprima@4.0.1 in nodejs
  • esprima==4.0.1 in python.

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, ...).

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

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
about 4 years ago · Juan Pablo Isaza Relatório

0

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!`);
    }
}

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda