Processors

In the Zapp code generation lifecycle, Processors play a crucial role in post-generation tasks. They are responsible for performing various operations on the generated code, such as formatting, analysis, and more. This section will provide a comprehensive overview of Processors in Zapp and how to use them effectively.

What Are Processors?

Processors are components within Zapp that act on the code generated by Generators. They allow you to automate tasks that should occur after code generation to ensure the generated code meets specific quality standards, formatting guidelines, or additional requirements.

Some common use cases for Processors include:

  • Code formatting to adhere to coding style standards.
  • Static code analysis to identify potential issues or vulnerabilities.
  • Adding or updating comments and documentation.
  • Post-generation code optimizations.

Configuring Processors

To configure Processors in your Zapp project, you need to define them with the processor property in your generators's configuration. Here's a basic example of how to configure a Processor:

export const ModelGenerator: IGenerator<IModelSpec> = (spec) => generate({
  // ...
  processor: PrettierProcessor,
  // ...
});

In this example, we're configuring a Processor named "CodeFormatter" with specific options to define the formatting style.

Available Processors

Zapp provides a set of built-in Processors, and you can also create custom Processors tailored to your project's needs. Some of the common built-in Processors include:

  • Code Formatter: Automatically formats generated code according to specified coding style guidelines (e.g., indentation, line breaks).
  • Static Code Analyzer: Runs static code analysis tools to identify potential issues or violations in the generated code.
  • Documentation Generator: Automatically generates or updates documentation comments based on code elements.
  • Optimization Processor: Applies code optimizations to enhance runtime performance.

Custom Processors

If the built-in Processors don't meet your specific requirements, Zapp allows you to create custom Processors. Custom Processors can execute arbitrary tasks on the generated code by defining JavaScript functions or scripts.

To create a custom Processor, follow these steps:

  1. Define the Processor in your project configuration, specifying the script or function to execute.

  2. Implement the custom logic in a JavaScript file or function that takes the generated code as input and returns the processed code.

  3. Configure the Processor in your Zapp project to use your custom implementation.

Executing Processors

Processors are executed automatically after code generation by Generators. Zapp processes the generated code by passing it through each configured Processor in the order specified in the project configuration.

Best Practices

When using Processors in Zapp, consider the following best practices:

  • Keep Processors modular and focused on specific tasks to maintain code readability.
  • Use Processors to enforce code quality standards consistently across your project.
  • Regularly review and update Processor configurations to adapt to changing project requirements.

By leveraging Processors effectively, you can ensure that the code generated by Zapp meets the desired quality and standards for your project.