python-ast
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

python-ast

Python (3) Parser for JavaScript/TypeScript, based on antlr4ts, grammar taken from antlr4's python grammar too (so please report bugs and open pull requests related to grammars upstream)

npm version

code style: prettier

Singificantly based on java-ast - all credit to Urish

Usage Example

import { parse, createVisitor } from 'python-ast';
 
const countMethods = (source: string) => {
  let ast = parse(source);
 
  return createVisitor({
    visitFuncdef: () => 1,
    defaultResult: () => 0,
    aggregateResult: (a, b) => a + b,
  }).visit(ast);
};
 
console.log(
  countMethods(`
class A:
    a: int
 
    def b(self):
        pass
    
    def c(self):
        pass
class B:
    def z(self, i):
        pass
  `),
); // logs 3

/python-ast/

    Package Sidebar

    Install

    npm i python-ast

    Weekly Downloads

    28

    Version

    0.1.0

    License

    MIT

    Unpacked Size

    595 kB

    Total Files

    21

    Last publish

    Collaborators

    • lexant