Skip to content
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

fix(jsii): class members named after the class result in illegal C# #1903

Merged
merged 12 commits into from
Aug 24, 2020

Conversation

RomainMuller
Copy link
Contributor

@RomainMuller RomainMuller commented Aug 17, 2020

It is illegal in C# to name a class' member (method, property) with the
same name as the class itself (even if the member is static). Since both
class and member names are pascal-cased in C#, this means a class'
members cannot share the same PascalCased representation.

This adds a compile-time validation for this condition, effectively
calling out member names to have the same PascalCased representation
as their declaring class' name.

This is currently only a warning, as this is "made to work" by altering
the type name by appending _ at the end of it, which is ugly and
dangerous but works, and is currently done in several places).

As all warnings, this turns into an error when operating under --strict,
and future work (i.e: #1931) will allow more granular configuration
of these errors, so we can hopefully opt all new codebases into the
strict behavior and eventually drop the slugification.

Fixes #1880


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@RomainMuller RomainMuller added bug This issue is a bug. effort/small Small work item – less than a day of effort contribution/core This is a PR that came from AWS. module/compiler Issues affecting the JSII compiler labels Aug 17, 2020
@RomainMuller RomainMuller requested a review from a team August 17, 2020 12:37
@RomainMuller RomainMuller self-assigned this Aug 17, 2020
Comment on lines +98 to +129
### Overriding

The visibility of a type member cannot be changed when it is overridden, even if
the change increases the visibility of said member, as this would result in
illegal **C#** code:

```ts
export class Base {
protected method(): void { /* ... */ }
}

export class Child {
// ⛔️ Illegal change of visibility when overriding a member
public method(): void { /* ... */ }
}
```

Additionally, **C#** does not allow changing the type signature of members while
overriding them, even if the updated type signature is a strict specialization
of the original one, and this is consequently also forbidden in `jsii`:

```ts
export class Base {
public method(): any { /* ... */ }
}

export class Child {
// ⛔️ Illegal change of signature when overriding a member
public method(): string { /* ... */ }
}
```

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Those restrictions already existed, but were not documented...

@RomainMuller RomainMuller added this to the August Release milestone Aug 19, 2020
It is illegal in C# to name a class' member (method, property) with the
same name as the class itself (even if the member is static). Since both
class and member names are pascal-cased in C#, this means a class'
members cannot share the same PascalCased representation.

This adds a compile-time validation for this condition, effectively
prohibiting member names to have the same PascalCased representation as
their declaring class' name.

Fixes #1880
@RomainMuller RomainMuller force-pushed the rmuller/disallow-member-naled-after-class branch from 8e673df to 88e8c12 Compare August 21, 2020 14:42
@RomainMuller RomainMuller force-pushed the rmuller/disallow-member-naled-after-class branch from e3be451 to 5253684 Compare August 21, 2020 15:35
@RomainMuller RomainMuller force-pushed the rmuller/disallow-member-naled-after-class branch from 91fc2af to c392304 Compare August 24, 2020 17:09
@RomainMuller RomainMuller merged commit bc71154 into master Aug 24, 2020
@RomainMuller RomainMuller deleted the rmuller/disallow-member-naled-after-class branch August 24, 2020 17:09
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-Blkkw9bQFn8A
  • Commit ID: c392304
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. contribution/core This is a PR that came from AWS. effort/small Small work item – less than a day of effort module/compiler Issues affecting the JSII compiler p1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

jsii should reject a member name with the same name as the class
3 participants