@vlocode/apex
TypeScript icon, indicating that this package has built-in type declarations

1.24.0 • Public • Published

CircleCI GitHub top language Bugs Vulnerabilities NPM Version

@vlocode/apex Salesforce APEX Language Parser

Antlr4 based APEX Parser and Grammar. This library uses antlr4ng as runtime library which is a port of the original antlr4 runtime library to TypeScript.

The library exposes the APEX Lexer and APEX parser generated from the .g4 grammar files allowing parsing of APEX source code using Visitor or Listener patterns.

Features

  • Full APEX and SOQL grammar support
  • Extract code structure from APEX source code
  • Identify classes, fields, methods, properties and access modifiers
  • Identify which classes are tested by which test classes

Example: Identifying test classes for a given class

Below code demonstrates how to identify which classes are tested by which test classes. And prints the test classes for a given class name.

import { TestIdentifier } from '@vlocode/apex';

const testIdentifier = container.create(TestIdentifier);
await testIdentifier.loadApexClasses(['path/to/apex/classes']);
const testClasses = testIdentifier.getTestClasses('MyClass');
console.log(testClasses);

Example: Extracting class information from APEX source code

Below code demonstrates how to extract class information from an APEX source fragment and prints the class name, fields and methods as well as their access modifiers.

import { Parser } from '@vlocode/apex';

const sourceCode = `public class Person {
    private String name;
    private Integer age;

    public String nameProperty {
        get { return this.name; }
    }

    public Integer getAge(Integer arg) {
        return this.age;
    }

    public Date getBirthDate() {
        return Date.now();
    }
}`;

const parser = new Parser(sourceCode);
const struct = parser.getCodeStructure();

for (const classInfo of struct.classes) {
    console.log(`Class ${classInfo.name}`);

    console.log(` Fields: ${classInfo.fields.length}`);
    classInfo.fields.forEach((field, i) =>
        console.log(`  ${i + 1}) ${field.name} (${field.access})`)
    );

    console.log(` Methods: ${classInfo.methods.length}`);
    classInfo.methods.forEach((method, i) =>
        console.log(`  ${i + 1}) ${method.name} (${method.access})`)
    );
}

Outputs the following:

Class Person
 Fields: 2
  1) name (private)
  2) age (private)
 Methods: 2
  2) getAge (public)
  3) getBirthDate (public)

Credits

The grammar files in this library are based on the grammar files from the apex-parser NPM library originally maintained by Andrey Gavrikov. The original library is no longer maintained and the grammar files have been updated to work with the latest version of Salesforce and the antlr4ng runtime library.

Package Sidebar

Install

npm i @vlocode/apex

Weekly Downloads

25

Version

1.24.0

License

MIT

Unpacked Size

1.19 MB

Total Files

54

Last publish

Collaborators

  • codeneos