Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

349
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda