NEW @W-21102140@ - Add PMD AST dump API for programmatic AST generation #434
Merged
aruntyagiTutu merged 11 commits intodevfrom Mar 11, 2026
Merged
NEW @W-21102140@ - Add PMD AST dump API for programmatic AST generation #434aruntyagiTutu merged 11 commits intodevfrom
aruntyagiTutu merged 11 commits intodevfrom
Conversation
- Add 11 new tests covering all validation logic and edge cases - Test null/empty language and fileToDump validation - Test encoding defaults to UTF-8 when null/empty - Test invalid encoding, directory instead of file - Test empty files and different encodings (ISO-8859-1) - All 18 tests passing with 100% code coverage - Remove implementation plan document as feature is complete
PROBLEM: - PmdAstDumper.readFileContent() loaded entire file into memory - Return value was discarded - only used for validation - Could cause OutOfMemoryError with large files (>100MB) - PMD's TreeExporter already reads the file internally SOLUTION: - Replace readFileContent() with validateFilePath() - Only check file exists, is regular file, and encoding is valid - No longer loads file content into memory - Lightweight validation without memory overhead IMPACT: - Prevents OOM errors with large Apex/Visualforce files - Same validation behavior, better performance - All 69 tests pass (18 AST dump + 51 other tests)
PROBLEM: - PmdEngine and CpdEngine were exported as public API in index.ts - These are internal implementation classes that should not be directly accessed - Exposing them creates unwanted API surface and support burden - Users could bypass the plugin system by directly instantiating engines - Future internal changes would become breaking changes SOLUTION: - Remove class exports from index.ts (lines 14-15) - Keep only type exports: PmdAstDumpResults, GenerateAstOptions, PmdProcessingError - Users access engines through PmdCpdEnginesPlugin (correct pattern) - Engines remain accessible internally for testing IMPACT: - Cleaner public API with minimal surface area - Users must use plugin system (intended design pattern) - Internal implementation can evolve without breaking changes - All 127 TypeScript tests pass with 98.84% coverage - Type exports still available for consumers of AST dump functionality
CONTEXT: - Previous commit removed PmdEngine export to keep it internal - MCP provider (internal Salesforce tool) needs direct access to PmdEngine - They use it to call generateAst() API for AST XML generation PROBLEM: - MCP provider code broke: "Module has no exported member 'PmdEngine'" - They instantiate PmdEngine directly: new PmdEngine(config) - Then call generateAst() method for on-demand AST generation SOLUTION: - Re-export PmdEngine from index.ts - Add clear documentation: use for AST generation, prefer plugin for normal usage - Keep CpdEngine internal (not needed by consumers) - Document that direct instantiation is for specialized use cases RATIONALE: - generateAst() is a valid public API use case - MCP provider is internal Salesforce code, not third-party - Alternative (factory function) adds unnecessary complexity - Clear documentation guides proper usage IMPACT: - MCP provider builds successfully - All 127 tests pass with 98.84% coverage - API surface: minimal (only PmdEngine + types, not CpdEngine)
namrata111f
reviewed
Mar 11, 2026
| results.file = inputData.fileToDump; | ||
|
|
||
| try { | ||
| System.out.println("Generating AST for file '" + inputData.fileToDump + "' with language '" + inputData.language + "'"); |
Contributor
There was a problem hiding this comment.
Do we need to log this instead?
Contributor
Author
There was a problem hiding this comment.
We are intentionally using System.out.println here to maintain consistency
with the rest of the codebase (see PmdRuleDescriber.java:99 and
CpdRunner.java:55). The project uses slf4j-nop and relies on stdout for
inter-process communication with the TypeScript side.
am I missing something @namrata111f ?
Contributor
There was a problem hiding this comment.
Understood, was not aware that inter-process communication relies on stdout. Just curious for debugging do we log these stdout on the TypeScript side?
namrata111f
reviewed
Mar 11, 2026
...r-pmd-engine/pmd-cpd-wrappers/src/main/java/com/salesforce/sfca/pmdwrapper/PmdAstDumper.java
Show resolved
Hide resolved
namrata111f
reviewed
Mar 11, 2026
...r-pmd-engine/pmd-cpd-wrappers/src/main/java/com/salesforce/sfca/pmdwrapper/PmdAstDumper.java
Show resolved
Hide resolved
namrata111f
reviewed
Mar 11, 2026
...r-pmd-engine/pmd-cpd-wrappers/src/main/java/com/salesforce/sfca/pmdwrapper/PmdAstDumper.java
Show resolved
Hide resolved
namrata111f
reviewed
Mar 11, 2026
...r-pmd-engine/pmd-cpd-wrappers/src/main/java/com/salesforce/sfca/pmdwrapper/PmdAstDumper.java
Show resolved
Hide resolved
...r-pmd-engine/pmd-cpd-wrappers/src/main/java/com/salesforce/sfca/pmdwrapper/PmdAstDumper.java
Show resolved
Hide resolved
...zer-pmd-engine/pmd-cpd-wrappers/src/main/java/com/salesforce/sfca/pmdwrapper/PmdWrapper.java
Show resolved
Hide resolved
nikhil-mittal-165
approved these changes
Mar 11, 2026
namrata111f
approved these changes
Mar 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
@W-21102140@
Summary
Adds new AST (Abstract Syntax Tree) dump functionality to the PMD engine, enabling programmatic generation of XML AST representations for Apex and Visualforce code without requiring PMD CLI.
What's New
Key Features
Changes Made
- PmdAstDumper.java - Core AST generation logic
- PmdAstDumpInputData.java - Input structure
- PmdAstDumpResults.java - Output structure
- Updated PmdWrapper.java - Added "ast-dump" command handler
- Added generateAst() method to PmdEngine class
- Exported PmdEngine for direct instantiation (needed for MCP provider)
- Type exports for public API consumption
- 18 Java unit tests (100% coverage)
- 13 TypeScript integration tests
- Edge cases: validation, encodings, error handling, empty files
API Example
const pmdEngine = new PmdEngine(config);
const result = await pmdEngine.generateAst('apex', '/path/to/file.cls', {
encoding: 'UTF-8',
workingFolder: '/tmp/ast'
});
// result.ast contains XML AST or result.error contains error details