Skip to content

fixing typescript generation of map of struct arrays #4209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

joshuapare
Copy link
Contributor

@joshuapare joshuapare commented Apr 10, 2025

Description

The current generation of TS typings using the [] syntax in instance generation. However, this is the array accessor syntax on type instances of custom types, so the previous typings broke generations of maps with values being an array of a custom type. This PR fixes both the tests for this (which were invalid) and changes to the safer Array<type> syntax.

Fixes #4144

Type of change

Please select the option that is relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration using wails doctor.

  • Windows
  • macOS
  • Linux

If you checked Linux, please specify the distro and version.

Test Configuration

Please paste the output of wails doctor. If you are unable to run this command, please describe your environment in as much detail as possible.

Wails

Version | v2.10.1

System

┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
| OS | MacOS |
| Version | 14.7.5 |
| ID | ------ |
| Branding | |
| Go Version | go1.23.8 |
| Platform | darwin |
| Architecture | arm64 |
| CPU 1 | Apple M1 Pro |
| CPU 2 | Apple M1 Pro |
| GPU | Chipset Model: Apple M1 Pro Type: GPU Bus: Built-In Total Number of Cores: 16 Vendor: Apple (0x106b) Metal Support: Metal 3 |
| Memory | 16GB |
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

Dependencies

┌────────────────────────────────────────────────────────────────┐
| Dependency | Package Name | Status | Version |
| Xcode command line tools | N/A | Installed | 2408 |
| Nodejs | N/A | Installed | 20.18.1 |
| npm | N/A | Installed | 10.8.2 |
| *Xcode | N/A | Available | |
| *upx | N/A | Available | |
| *nsis | N/A | Available | |
| |
└─────────────────── * - Optional Dependency ────────────────────┘

Diagnosis

Optional package(s) installation details:

SUCCESS Your system is ready for Wails development!

Checklist:

  • I have updated website/src/pages/changelog.mdx with details of this PR
  • My code follows the general coding style of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Summary by CodeRabbit

  • Refactor

    • Standardized array type representations for improved clarity and consistency.
    • Enhanced the generated output for TypeScript integrations, offering a clearer, more predictable format for array types.
  • Bug Fixes

    • Fixed TypeScript generation of maps with keys of array of structs, improving the TypeScript generation process for such data structures.

Copy link
Contributor

coderabbitai bot commented Apr 10, 2025

Walkthrough

This pull request updates the type definitions for various map properties within the DeepElements class by switching from the Record<string, type> syntax to the Record<string, Array<type>> notation. It also refines the TypeScript code generation in the AddMapField method by simplifying a variable declaration and altering the logic to construct array types using a new prefix and suffix mechanism. Additionally, a changelog entry has been added to document a fix related to TypeScript generation.

Changes

File(s) Summary
v2/internal/binding/.../binding_deepelements_test.go Updated type definitions for properties in the DeepElements class from shorthand array types (e.g. number[]) to explicit Array<...> forms, including nested arrays of numbers and DeepMessage structures; adjusted constructor assignments accordingly.
v2/internal/typescriptify/typescriptify.go Simplified the declaration of jsVariableUnsafeChars and modified the AddMapField method in the typeScriptClassBuilder struct to generate TypeScript array types using a new prefix (Array<) and suffix (using > repeated) instead of the previous [] repetition.
website/src/pages/changelog.mdx Added a new entry in the "Fixed" section of the changelog to document the resolution of a bug related to TypeScript generation of maps with an array of structs as keys.

Assessment against linked issues

Objective Addressed Explanation
Ensure TypeScript array types are generated with proper syntax to fix TS1011 [#4144]

Suggested reviewers

  • leaanthony

Poem

I'm a rabbit hopping with delight,
Arrays now gleam in a brand-new light,
With "Array<" and ">" in a playful embrace,
Errors like TS1011 have lost their place,
Code hops along, smooth and free,
A joyous dance in perfect harmony!

Tip

⚡💬 Agentic Chat (Pro Plan, General Availability)
  • We're introducing multi-step agentic chat in review comments and issue comments, within and outside of PR's. This feature enhances review and issue discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments and add commits to existing pull requests.

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bc7b816 and 9a10be6.

📒 Files selected for processing (1)
  • website/src/pages/changelog.mdx (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • website/src/pages/changelog.mdx

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
v2/internal/typescriptify/typescriptify.go (1)

273-336: Consider adding a comment explaining the change to Array syntax.

While the implementation is correct, it would be helpful to add a comment explaining why the Array<type> syntax is preferred over the bracket notation, particularly for maps with array values.

 func (t *typeScriptClassBuilder) AddMapField(fieldName string, field reflect.StructField) {
+  // Using Array<type> syntax instead of type[] notation for better compatibility with
+  // TypeScript when generating maps with values that are arrays of custom types
   keyType := field.Type.Key()
   valueType := field.Type.Elem()
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 194b058 and d4b8315.

📒 Files selected for processing (2)
  • v2/internal/binding/binding_test/binding_deepelements_test.go (2 hunks)
  • v2/internal/typescriptify/typescriptify.go (4 hunks)
🔇 Additional comments (9)
v2/internal/typescriptify/typescriptify.go (5)

43-43: Code style improvement looks good.

The simplified single-line variable declaration is cleaner and maintains the same functionality.


278-278: Preparation for array type syntax change looks good.

Adding this variable to prepare for changing from bracket notation to generic notation.


291-293: Core fix for TypeScript array type notation.

This change switches from using bracket notation ([]) to generic notation (Array<type>) for representing arrays in TypeScript. This addresses the issue mentioned in PR #4144 where array syntax was causing problems with maps that contain arrays of custom types.

The combination of prefix and suffix ensures proper nesting of multiple array dimensions.


328-328: Implementation of new array type format in the field declaration.

Correctly applies the new valueTypePrefix and valueTypeSuffix in the TypeScript field declaration.


330-332: Updated constructor reference to match new type format.

The constructor body now correctly references the type with the new Array generic syntax format, maintaining consistency with the field declaration.

v2/internal/binding/binding_test/binding_deepelements_test.go (4)

66-71: Fixed primitive type array representation in maps.

Successfully updated the map type definitions from bracket notation to generic Array<type> notation for numeric arrays, ensuring consistency with the changes in the TypeScript code generation.


75-77: Fixed custom type array representation in maps.

Successfully updated the map type definitions for arrays of custom types (DeepMessage), which directly addresses the issue mentioned in PR #4144.


100-102: Updated constructor calls to match new type format.

The constructor calls to convertValues now correctly use the new Array generic syntax.


37-127:

❓ Verification inconclusive

Verify test coverage for all array depth scenarios.

The changes look good and cover multiple array depth scenarios. Consider adding explicit tests for maps with deeply nested arrays and complex custom types to ensure the fix works for all edge cases.


🏁 Script executed:

#!/bin/bash
# Check for any additional tests that might validate this fix for edge cases

# Look for any other test files that use map with array values
rg -l "map\[string\]\[\]" --type go

Length of output: 178


Action: Review and expand test coverage for deeply nested maps and custom types.

The updated DeepElements conversion in the test file for binding (i.e. binding_deepelements_test.go) effectively handles multiple array depth scenarios. Our search results indicate that map usages with array values also appear in other parts of the codebase (e.g. in pm.go and events.go), but explicit tests for these complex cases aren’t clearly defined.

  • Confirm that tests explicitly cover edge cases involving maps with deeply nested arrays.
  • Consider adding dedicated tests to validate conversions for complex custom types in maps.

@joshuapare joshuapare force-pushed the invalid-mapkey-array-arg-generation branch from d4b8315 to bc7b816 Compare April 10, 2025 13:50
@igor-spirin-indriver
Copy link

igor-spirin-indriver commented Apr 14, 2025

I tested it on the original issue from #4144, everything works! 🚀

@leaanthony leaanthony merged commit af65985 into wailsapp:master Apr 18, 2025
9 of 10 checks passed
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.

TS1011: An element access expression should take an argument
3 participants