Skip to content

Conversation

@zanuka
Copy link

@zanuka zanuka commented Mar 15, 2025

This PR addresses issue #536 where parameter properties in TypeScript class constructors
were not properly supported during transformation. Parameter properties like
constructor(public name: string) are now correctly transformed into class properties
and assignments in the constructor body.

The fix modifies the constructor declaration visitor to properly handle parameter
property modifiers and generate the appropriate class properties.

Tests have been run and verified that the transformation now correctly handles all
parameter property cases.

@zanuka
Copy link
Author

zanuka commented Mar 15, 2025

@microsoft-github-policy-service agree company="Zanuka Labs"

1 similar comment
@zanuka
Copy link
Author

zanuka commented Mar 15, 2025

@microsoft-github-policy-service agree company="Zanuka Labs"

@BansalShivanshu
Copy link

Is this a direct port from TS codebase or new addition to the codebase? Looking at issue #536 seems like constructor body transformation from constructor parameters to class properties hasn't been ported yet.

@zanuka
Copy link
Author

zanuka commented Mar 16, 2025

@BansalShivanshu This is not a direct port of specific code from the TypeScript codebase, but rather a new implementation following the same transformation pattern that TypeScript uses.

Since the TS compiler handles parameter properties through its transformation pipeline, it's my understanding that this core pattern involves:

  1. Identifying parameters with property modifiers (public, private, protected, readonly)
  2. Creating property declarations in the class
  3. Generating property assignments in the constructor body

I noticed that issue #536 correctly identified that this functionality wasn't properly implemented, so this PR fills this gap by adding logic to:

  • Correctly detect parameter properties using ast.IsParameterPropertyDeclaration()
  • Strip property modifiers from parameters in the transformed constructor
  • Generate the appropriate assignments in the constructor body

It follows TypeScript's pattern conceptually, though the specific code structure differs due to the language differences (Go vs TypeScript) and the architectural differences between the codebases.

I think all edge cases have been handled including:

  • Multiple parameter properties in a single constructor
  • Mixed regular parameters and parameter properties
  • Parameter properties with type annotations
  • Constructor super calls and other existing functionality

Happy to make adjustments as needed if anything was missed. I fixed the extra blank lines that had caused the initial check fail as well.

Copy link
Contributor

@frodi-karlsson frodi-karlsson left a comment

Choose a reason for hiding this comment

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

I believe the issue was actually fixed 4 days ago here: 8b95466#diff-602f9f2ee92480e93b3b71e218795f3b947dd7745781272ef927d1f2dd431ec1R718

this.x = x;
}
}`},
{title: "multiple parameter properties", input: `class Project {
Copy link
Contributor

@frodi-karlsson frodi-karlsson Mar 16, 2025

Choose a reason for hiding this comment

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

Running this test without the changes on up to date main, it also passes. I'm not so sure this is fixing anything, as there doesn't seem to be an issue at first look. Are you experiencing something different?

Theres of course still an issue with a super call's position as it relates to the parameter initializations, which I think just needs a minimal fix (#547)

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the comments, much appreciated. After further investigation and testing, I've confirmed that this PR is redundant. The parameter property transformation functionality is already properly implemented in the codebase through commit 8b95466 (PR #479).

I checked the following:

  1. All tests pass on the main branch without these changes
  2. The existing implementation already handles:
    • Parameter property detection
    • Modifier stripping
    • Property assignments in constructor
    • Super call handling
    • Multiple parameter properties

I'll close this PR since it would be duplicating existing functionality.

@zanuka zanuka closed this Mar 18, 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.

3 participants