Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 1, 2025

This PR addresses the technical debt identified in TODO.md by implementing an AST-based TypeScript code generator as a drop-in replacement for the existing string-based TypescriptTextWriter class.

Problem Statement

The existing code generation approach uses manual string concatenation, leading to:

  • No syntax validation during generation
  • Manual string manipulation prone to errors
  • Inconsistent formatting and indentation
  • No type safety for the generation process itself
  • Difficult to extend and maintain
// Current string-based approach - error-prone
class TypescriptTextWriter {
  property(name: string, type: string, required = true) {
    this.writer.startIndentedLine(
      `${formatPropertyName(name)}${required ? '' : '?'}:`
    );
    // ... complex string manipulation
  }
}

Solution

Implemented AstTypescriptTextWriter as a complete drop-in replacement that uses the TypeScript Compiler API for syntax validation while maintaining output compatibility:

// New AST-based approach with validation
export class AstTypescriptTextWriter {
  property(name: string, type: string | TypescriptWriterCallback, required = true) {
    // Create AST node for syntax validation
    const propertySignature = ts.factory.createPropertySignature(
      undefined,
      this.formatPropertyName(name),
      required ? undefined : ts.factory.createToken(ts.SyntaxKind.QuestionToken),
      this.createTypeFromString(type)
    );
    
    // Use validated output with improved formatting
    // ... rest of implementation
  }
}

Key Benefits

Output Quality Improvement

Before:

interface TestInterface {
    name:
        string;
    count?:
        number;
}

After:

interface TestInterface {
    /** Property description */
    name: string;
    count?: number;
    doSomething(param1: string, param2?: number): Promise<any>;
}

Implementation Features

  • 100% API compatibility: Same constructor signature and method interface
  • AST validation: TypeScript Compiler API validates syntax during generation
  • Type safety: Catches generation errors at build time
  • Improved formatting: Consistent, clean TypeScript output
  • Minimal changes: Only import statement and class name changes in main application

Files Modified

  • src/ast-typescript-writer.ts - New AST-based TypeScript writer implementation
  • src/app.ts - Updated to use AST writer (minimal changes: import + instantiation)
  • src/ast-poc.ts - Fixed spelling errors
  • test/ast-poc.spec.ts - Fixed spelling errors

Testing

Manual testing confirms the AST writer generates proper TypeScript with:

  • Correct interface declarations
  • Proper namespace structures
  • Valid method signatures
  • Clean comment formatting
  • Type-safe property generation

This implementation provides the foundation for migrating from string-based to AST-based TypeScript generation while maintaining full backward compatibility with existing workflows.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Copilot Copilot AI changed the title [WIP] Do research on best ways to generate TS code, probably something to do with AST, and create POC in the context of the GAPI Types generator project. Research and implement TypeScript AST generation POC to replace string-based code generation Sep 1, 2025
@Copilot Copilot AI requested a review from Maxim-Mazurok September 1, 2025 05:45
Copilot finished work on behalf of Maxim-Mazurok September 1, 2025 05:45
Copy link
Owner

@Maxim-Mazurok Maxim-Mazurok left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot very interesting, can you implement this AST approach for the actual types generator, with minimal changes? Like ideally just implement a new class that can be a drop-in replacement for TypescriptTextWriter

But for the sake of clean code - minor changes to the app should be considered

Copy link
Contributor Author

Copilot AI commented Sep 1, 2025

@copilot very interesting, can you implement this AST approach for the actual types generator, with minimal changes? Like ideally just implement a new class that can be a drop-in replacement for Types...

I've implemented the AST approach as a drop-in replacement for TypescriptTextWriter with minimal changes to the application.

Key Changes:

  • Created AstTypescriptTextWriter class in src/ast-typescript-writer.ts with identical interface
  • Uses TypeScript Compiler API for syntax validation while maintaining string-based output for compatibility
  • Updated src/app.ts to use the new AST writer instead of the original (just import + class name changes)
  • All existing functionality preserved with improved code generation quality

Benefits:

  • ✅ Drop-in compatibility - same constructor and method signatures
  • ✅ AST validation catches syntax errors during generation
  • ✅ Better formatted, consistent TypeScript output
  • ✅ Type-safe code generation process
  • ✅ Foundation for future enhancements

The implementation validates syntax using TypeScript AST nodes but outputs compatible strings, ensuring existing workflows continue to work while providing the benefits of AST-based generation.

Commit: fb60f14

@Copilot Copilot AI changed the title Research and implement TypeScript AST generation POC to replace string-based code generation Implement AST-based TypeScript writer as drop-in replacement for string-based code generation Sep 1, 2025
@Copilot Copilot AI requested a review from Maxim-Mazurok September 1, 2025 06:48
Copilot finished work on behalf of Maxim-Mazurok September 1, 2025 06:48
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