# code-analyzer ast-dump

Dump the Abstract Syntax Tree (AST) for a given source file.

## Description for code-analyzer ast-dump

Generate and display the AST that PMD produces when parsing a source file. This is useful for understanding how PMD interprets your code and for developing custom PMD rules. The command supports Apex, Visualforce, HTML, XML, and JavaScript files.

## Flags

| Flag Name (Long) | Flag Name (Short) | Description                                                                                                                                                                                                                                                                                                                                                |
| ---------------- | ----------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `--file`         | N/A               | **Type:** Value<br>**Required**<br><br>Path to the source file to parse.<br><br>The file whose AST you want to generate. The file must exist and be a regular file (not a directory). If you don't specify the `--language` flag, the language is auto-detected from the file extension.                                                                   |
| `--flags-dir`    | N/A               | **Type:** Value<br><br>Import flag values from a directory.                                                                                                                                                                                                                                                                                                |
| `--format`       | N/A               | **Type:** Value<br>**Valid Values:** `json`, `xml`<br>**Default value:** `xml`<br><br>Output format for the AST.<br><br>Choose between `xml` (default) or `json`. The XML format outputs the raw PMD AST XML, preserving the full tree structure. The JSON format provides a flat array of nodes with names, attributes, parent, and ancestor information. |
| `--json`         | N/A               | **Type:** Boolean<br><br>Format output as json.                                                                                                                                                                                                                                                                                                            |
| `--language`     | `-l`              | **Type:** Value<br>**Valid Values:** `apex`, `visualforce`, `html`, `xml`, `javascript`<br>**Default value:** `apex`<br><br>Language of the source file.<br><br>Explicitly specify the language of the source file. Defaults to `apex`. Supported languages are: apex, visualforce, html, xml, and javascript.                                             |
| `--output-file`  | N/A               | **Type:** Value<br><br>Path to the file where the AST output is written.<br><br>If specified, the AST output is written to this file instead of being displayed in the terminal. The content format depends on the `--format` flag.                                                                                                                        |

## Examples for code-analyzer ast-dump

Dump the AST for an Apex class file in JSON format (default):

```shell
sf code-analyzer ast-dump --file ./force-app/main/default/classes/MyClass.cls
```

Dump the AST in XML format:

```shell
sf code-analyzer ast-dump --file ./force-app/main/default/classes/MyClass.cls --format xml
```

Dump the AST and write the output to a file:

```shell
sf code-analyzer ast-dump --file ./force-app/main/default/classes/MyClass.cls --output-file ast-output.json
```

Explicitly specify the language when the file extension is ambiguous:

```shell
sf code-analyzer ast-dump --file ./src/myfile.html --language html
```
