@apexdevtools/apex-parser

中文文档
5.3.0发布于 2026/10/16BSD-3-ClauseTypeScriptESM + CJS近 30 天下载 16.4万

Javascript parser for Salesforce Apex Language

原始使用文档
文档目录12 节
  1. Installation
  2. Maven
  3. NPM
  4. Usage
  5. Explore Parse Tree (TypeScript)
  6. Capturing syntax errors
  7. SOSL FIND quoting
  8. Development
  9. Prerequisites
  10. Building
  11. Testing
  12. Source & Licenses

apex-parser

Parser for Salesforce Apex (including triggers and inline SOQL/SOSL). This is based on an ANTLR4 grammar, see antlr/BaseApexParser.g4. Currently packaged for Java and JavaScript/TypeScript targets.

The packages include ANTLR4 generated types plus optional extras for convenience. The TypeScript package exports type aliases for ANTLR types, while both packages have abstractions like ApexParserFactory and ApexErrorListener. There are minimal examples below and in the test classes.

The grammar aims to match the platform compiler. Where it deliberately does not, the difference is recorded in Differences From Salesforce Behaviour.

Installation

Maven

<dependency>
    <groupId>io.github.apex-dev-tools</groupId>
    <artifactId>apex-parser</artifactId>
    <version><!-- version --></version>
</dependency>

NPM

npm i @apexdevtools/apex-parser

The npm package ships multiple JavaScript outputs:

  • ESM for modern Node and bundlers, selected by import.
  • CommonJS for require.
  • A browser ESM bundle, selected by browser-aware bundlers.
  • TypeScript declarations for the root package and exported subpaths.

These are exposed through the package exports map, so most consumers should import from the package name rather than from dist paths:

import { ApexParserFactory } from "@apexdevtools/apex-parser";

CommonJS consumers can use require:

const { ApexParserFactory } = require("@apexdevtools/apex-parser");

The browser bundle includes the parser APIs, but excludes Node-only file checking helpers such as check and checkProject.

Usage

ApexParser entry points to access tree:

  • compilationUnit(), a class file.
  • triggerUnit(), a trigger file.
  • anonymousUnit(), an apex script file.
  • query(), a raw SOQL query.

Explore Parse Tree (TypeScript)

import {
  ApexParserBaseListener,
  ApexParserBaseVisitor,
  ApexParseTreeWalker,
  ApexParserFactory,
} from "@apexdevtools/apex-parser";

const parser = ApexParserFactory.createParser("public class Hello {}");

/*
 * Use a visitor. Return value and manual control.
 */
class Visitor extends ApexParserBaseVisitor<any> {}

const visitor = new Visitor();
visitor.visit(parser.compilationUnit());


/*
 * Or walk with listener. Enter/exit operations - for whole tree.
 */
class Listener extends ApexParserBaseListener {}

const listener = new Listener();
ApexParseTreeWalker.DEFAULT.walk(listener, parser.compilationUnit());

Capturing syntax errors

Lexer errors (e.g. invalid string escape sequences) are only reported to listeners attached to the lexer, while parser errors are only reported to listeners attached to the parser. To capture both from a single listener, use createLexerAndParser, which wires the listener to both:

import { ApexParserFactory, ApexErrorListener } from "@apexdevtools/apex-parser";

class MyListener extends ApexErrorListener {
  apexSyntaxError(line: number, column: number, msg: string): void {
    console.log(`${line}:${column} ${msg}`);
  }
}

const { parser } = ApexParserFactory.createLexerAndParser(source, new MyListener());
parser.compilationUnit();

The Java API is equivalent:

ApexErrorListener listener = new MyListener();
LexerAndParser pair = ApexParserFactory.createLexerAndParser(
  CharStreams.fromString(source), listener);
pair.getParser().compilationUnit();

SOSL FIND quoting

SOSL FIND uses ' as a quoting character when embedded in Apex, in the API braces are used:

Find {something} RETURNING Account

To parse the API format there is an alternative parser rule, soslLiteralAlt, that you can use instead of soslLiteral. See SOSLParserTest for some examples of how these differ.

Development

Prerequisites

  • JDK 11+ (for ANTLR tool)
  • Maven
  • Node.js ^20.19.0, ^22.13.0, or >=24

Building

The outer package contains scripts to build both distributions:

# Run once - prepare for dev (installs deps, runs antlr gen)
npm run init

# Run antlr gen, compile and test
npm run build

Or you can setup and later build each distribution separately:

npm run init:npm
npm run build:npm

npm run init:jvm
npm run build:jvm

Testing

Unit Tests

More options for testing:

# From ./npm
npm run build
npm test
# File and test name regex filtering
npm test -- ApexParserTest -t Expression

# From ./jvm
mvn test

System Tests

The system tests use a collection of sample projects located in the apex-samples repository. Follow the README instructions in apex-samples to checkout the submodules at the version tag used by the build workflow. Both packages must be built beforehand, as the js system test spawns the jar as well.

To run the tests:

# Set SAMPLES env var to samples repo location
export SAMPLES=<abs path to apex-samples>

# From root dir
npm run build
npm run systest

System test failures relating to the snapshots may highlight regressions. Though if an error is expected or the samples have changed, instead use npm run systest:update to update the snapshots, then commit the changes.

Source & Licenses

All the source code included uses a 3-clause BSD license. The only third-party component included is the Apex Antlr4 grammar originally from Tooling-force.com, although this version used is now markedly different from the original.

版本与发布记录

@apexdevtools/apex-parser 共发布 24 个版本,首次发布于 2022/06/15;过去 12 个月发布了 5 个版本。

版本发布日期距今
5.3.0latest2026/10/1622 天前
5.2.02026/08/232 个月前
5.1.02026/07/044 个月前
5.1.0-beta.1beta2026/05/175 个月前
5.0.02026/04/226 个月前
5.0.0-beta.52025/08/141.2 年前

关于 @apexdevtools/apex-parser 的常见问题

@apexdevtools/apex-parser 是什么?

Javascript parser for Salesforce Apex Language

如何安装 @apexdevtools/apex-parser?

在项目目录运行 npm install @apexdevtools/apex-parser,或使用 pnpm add / yarn add / bun add。

@apexdevtools/apex-parser 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@apexdevtools/apex-parser 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@apexdevtools/apex-parser 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js ^20.19.0 || ^22.13.0 || >=24。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@apexdevtools/apex-parser 可以商用吗?

@apexdevtools/apex-parser 使用 BSD-3-Clause 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。另外不得用原作者名义为衍生产品背书。以上为概括,不构成法律意见,请以许可原文为准。

@apexdevtools/apex-parser 的下载量有多少?

根据 npm 官方下载统计,@apexdevtools/apex-parser 在 2026/10/06 – 2026/11/04 这 30 天内被下载 164,266 次,日均约 5,476 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@apexdevtools/apex-parser 还在维护吗?

最新版本 5.3.0 发布于 2026/10/16(22 天前);至今共发布 24 个版本,其中过去 12 个月发布了 5 个;首次发布于 2022/06/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。