Skip to content

Conversation

@devin-ai-integration
Copy link
Contributor

Enhanced AST Code Generation with Runtime Schema Support

Overview

This PR enhances the generateTsAstCodeFromPgAst() function by adding a new generateTsAstCodeFromPgAstWithSchema() function that uses runtime schema information to determine whether nodes should be wrapped or not, generating code with correct builder paths.

Changes Made

New Function: generateTsAstCodeFromPgAstWithSchema

  • Takes both a PostgreSQL AST and runtime schema as parameters
  • Uses the runtime schema's isNode property to determine node wrapping
  • Generates t.ast.*() calls for unwrapped nodes and t.nodes.*() calls for wrapped nodes
  • Includes sophisticated field-based node type detection for complex AST structures

Key Features

  • Schema-aware code generation: Dynamically determines correct builder paths based on runtime schema
  • Field matching algorithm: Identifies node types by comparing object fields against schema specifications
  • Array handling: Properly preserves array structures in generated code
  • Backward compatibility: Original generateTsAstCodeFromPgAst function remains unchanged

Implementation Details

  • Added findNodeTypeByFields() helper function for dynamic node type identification
  • Enhanced getValueNode() function with proper array handling
  • Created schema lookup map for efficient runtime schema access
  • Supports both required and optional field matching

Tests Added

  • Enhanced AST generation test: Verifies correct wrapped vs unwrapped node generation
  • Complex AST test: Tests mixed wrapping patterns with WithClause and nested structures
  • Snapshot testing: Validates generated code structure and correctness

Example Usage

import { generateTsAstCodeFromPgAstWithSchema } from '@pgsql/proto-parser';
import { runtimeSchema } from './runtime-schema';

const ast = /* your PostgreSQL AST */;
const enhancedCode = generateTsAstCodeFromPgAstWithSchema(ast, runtimeSchema);

Generated Code Examples

The enhanced function now correctly generates:

  • t.nodes.selectStmt({...}) for wrapped SelectStmt nodes
  • t.nodes.withClause({...}) for wrapped WithClause nodes
  • t.ast.limitOption(...) for unwrapped enum values

Testing

  • ✅ All existing tests pass (42/42)
  • ✅ New tests added with comprehensive coverage
  • ✅ Snapshot tests validate correct code generation
  • ✅ No breaking changes to existing functionality

Link to Devin run

https://app.devin.ai/sessions/3fc9c4d281d84299922c41d82c9c0477

Requested by

Dan Lynch ([email protected])

- Add generateTsAstCodeFromPgAstWithSchema function that uses runtime schema to determine node wrapping
- Implement field-based node type detection for complex AST structures
- Add comprehensive tests for wrapped vs unwrapped node generation
- Support correct builder paths: t.ast.*() for unwrapped nodes, t.nodes.*() for wrapped nodes
- Maintain backward compatibility with existing generateTsAstCodeFromPgAst function

Co-Authored-By: Dan Lynch <[email protected]>
@devin-ai-integration
Copy link
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

devin-ai-integration bot and others added 8 commits June 23, 2025 21:00
- Fixed multi-field object processing in traverse() to check parent field specs
- WithClause now correctly generates t.ast.withClause instead of t.nodes.withClause
- Updated snapshot test to reflect correct expected output
- All 42 tests passing

Co-Authored-By: Dan Lynch <[email protected]>
…a system

- Remove isNode field from FieldSpec interface in runtime-schema/types.ts
- Update runtime schema generator to not include isNode in FieldSpec objects
- Remove isNode field from NodeSpec interface in both types.ts and store.ts
- Update generateRuntimeSchemaTypeScript method to match new interface
- Fix logic in generateTsAstCodeFromPgAstWithSchema to determine node wrapping by checking field type directly
- Update README documentation to reflect schema changes
- All tests now pass with the simplified schema structure

Co-Authored-By: Dan Lynch <[email protected]>
- Update 7 test snapshots in __fixtures__/output/runtime-schema/
- Remove isNode fields from all generated runtime schema outputs
- All runtime-schema tests now pass with the updated schema structure

Co-Authored-By: Dan Lynch <[email protected]>
- Include the main snapshot file that was missed in previous commit
- Complete the snapshot updates for isNode field removal

Co-Authored-By: Dan Lynch <[email protected]>
…ed from FieldSpec

- Add isNode field back to NodeSpec interface while keeping it removed from FieldSpec
- Update runtime schema generator to include isNode in NodeSpec objects
- Update TypeScript generation to reflect correct interface structure
- Regenerate all runtime schema files and test snapshots
- Maintain enhanced AST generation functionality with corrected schema

Co-Authored-By: Dan Lynch <[email protected]>
… interface and objects

- Add isNode: boolean field to NodeSpec interface in test utility file
- Add isNode: true to all NodeSpec objects to match corrected interface structure
- Resolves TypeScript compilation error in CI meta tests
- Meta tests now pass locally with updated schema structure

Co-Authored-By: Dan Lynch <[email protected]>
- Enhanced ensureCorrectExtension() method to be more robust:
  - Handle edge cases with null/empty inputs
  - Ensure expectedExt starts with dot
  - Better handling of multiple extensions (e.g., .d.ts, .spec.ts)
- Standardized all file writing methods to use ensureCorrectExtension():
  - writeTypes(), writeEnums(), writeUtilsEnums()
  - writeAstHelpers(), writeWrappedAstHelpers()
  - writeCodeToFile(), writeEnumMaps(), writeRuntimeSchema()
- Consistent extension handling for both TypeScript (.ts) and JSON (.json) files
- Updated test snapshots to reflect corrected file extension behavior
- Prevents double extension bugs (e.g., .ts.ts) across all file generation

Co-Authored-By: Dan Lynch <[email protected]>
@pyramation pyramation closed this Jun 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants