Repository navigation
docs: replace object-literal IResourceWithPolicyV2 example with named class - #38461
Conversation
aws-cdk-automation
left a comment
There was a problem hiding this comment.
The pull request linter fails with the following errors:
❌ The title prefix of this pull request must be one of "feat|fix|build|chore|ci|docs|style|refactor|perf|test|revert"
If you believe this pull request should receive an exemption, please comment and provide a justification. A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed, add Clarification Request to a comment.
|
@AmariahAK - for cleanliness, instead of a merge from main, could you rebase instead and push (force with lease) a single squashed commit? |
91fdb32 to
868d325
Compare
…th named class The object-literal pattern with inline methods in the forResource() return value does not translate across jsii/rosetta to non-TypeScript languages (Ruby, Python, Java, C#, Go). Replace it with a proper MyResourceWithPolicy class that implements IResourceWithPolicyV2, matching the existing CfnBucketWithPolicy and CfnKeyWithPolicy patterns in default-traits.ts. closes aws#38452 Co-authored-by: atlarix-agent <[email protected]>
79fe45c to
ac26af6
Compare
|
@omarqureshi should be good now |
|
@omarqureshi not too sure, ive just done a merge with changes from main so that it appears something is being done |
Fixes aws#3772. Renderer bugs exposed while investigating aws/aws-cdk#38452: rewriting the `aws-s3`/`aws-kms` README example as a named class (the doc-side fix, aws/aws-cdk#38461) showed the C# and Go visitors emit non-compiling output for ordinary class shapes. ### C#: dropped `this.` produces a self-assignment `propertyAccessExpression` suppressed `this.` unconditionally. Public members are PascalCased and can never collide with a camelCase parameter, but private fields keep their camelCase name — so a constructor assigning a same-named field rendered: ```csharp public MyResource(string resource) { resource = resource; // CS1717 — the field is never assigned } ``` The qualifier is now kept exactly when the member's *rendered* name (via the existing `privatePropertyNames` context — the same mechanism that decides casing) matches a parameter of an enclosing function: ```csharp this.resource = resource; ``` All other output is unchanged — PascalCased members still drop `this.`. ### Go: constructor naming mismatch `newExpression` hardcoded the `New` prefix (the code carried `// Should this be "new" if the class is unexported?`) while `constructorDeclaration` renders `new` for unexported classes — so a snippet-local class defined `func newMyResource` but was called as the non-existent `NewMyResource`. The call site now resolves the class identifier and matches the declaration. Classes that model library types (jsii-resolvable, including `/// fake-from-jsii` test fixtures) keep the exported call form, since only their call sites render — verified against the 6 existing corpus snippets that rely on this. ### Go: assignment wrapped the wrong side of the pointer Parameters and properties are both pointer-valued in Go, but they do not *render* alike, and what the right-hand side has to be follows the rendering rather than the declaration: - a **property** renders as the pointer field itself — `this.env`, a `*string`; - a **parameter** renders dereferenced — `*name`, a `string` (the identifier rule at the bottom of `go.ts`). So a property target wants a pointer on the right (leave a pointer alone, wrap a literal) and a parameter target wants a value (dereference a pointer, leave a literal raw) — opposite rules. The visitor set `isPtrAssignmentRValue` for both kinds and wrapped for neither, which broke each of them in one direction: | TypeScript | rendered | why it fails | | --- | --- | --- | | `this.env = 'production'` | `this.env = "production"` | `*string = string` | | `next = other` (both parameters) | `*next = other` | `string = *string` | Both flags now key off whether the *target* renders as a pointer, which only a property does: | TypeScript | rendered | | --- | --- | | `this.env = 'production'` | `this.env = jsii.String("production")` | | `this.env = env` | `this.env = env` | | `name = 'fallback'` | `*name = "fallback"` | | `next = other` | `*next = *other` | > **Review follow-up.** The first version of this change keyed off the declaration kind, and so wrapped literals for parameter targets too — producing `*name = jsii.String("fallback")`, the same mismatch in the opposite direction. Thanks @dgandhi62 for catching it. Corrected in 58de1d9, which also picks up the parameter-to-parameter case (`*next = other`) that the corrected rule covers and that was broken before this PR as well. ### Testing Developed test-first, with two translations-corpus snippets carrying expectations for **all four languages** — Python, Java and C# pinned at their existing output, so the diff itself shows each change is confined to its own target: - `classes/constructor_parameter_shadows_field` — the C# `this.` fix and the Go constructor naming fix; - `expressions/assignment_to_parameter` — a parameter assigned from another parameter, from a string literal and from a number literal. Each was run red before the corresponding change. Full suite passes: 25 suites, 549 tests, 6 pre-existing skips (corpus files that predate this PR and lack an expectation for one language). `CSharpVisitor.VERSION` and `GoVisitor.VERSION` are bumped to invalidate cached translations. --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Fable 5 <[email protected]>
|
@Mergifyio queue |
Merge Queue Status🛑 Queue command has been cancelled |
Title fixed; validate-pr passes on the current head.
|
Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.
|
Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
|
@Mergifyio queue |
Merge Queue Status
This pull request spent 13 seconds in the queue, including 1 second running CI. Required conditions to merge
|
|
Comments on closed issues and PRs are hard for our team to see. |
Description
Closes #38452
Reason for this change
The
IResourcePolicyFactory/IResourceWithPolicyV2usage example in the S3and KMS READMEs used a TypeScript object-literal-with-inline-method pattern
for the return value of
forResource():While this satisfies the structural interface in TypeScript, jsii/Rosetta
cannot translate anonymous objects with methods to other languages. The
generated Ruby docs showed an inline
definside a hash, Python had rawfunction bodies inside dicts, and Java produced
Map.of()with a brokenmethod body. See the full breakdown in #38452.
Description of changes
Updated the
nofixturecode blocks in both READMEs to use a proper namedclass
MyResourceWithPolicythat explicitly implementsIResourceWithPolicyV2:packages/aws-cdk-lib/aws-s3/README.md— replaced object-literal withMyResourceWithPolicyclasspackages/aws-cdk-lib/aws-kms/README.md— identical fix, adapted forAWS::KMS::KeyBoth now follow the same class-based pattern as
CfnBucketWithPolicyandCfnKeyWithPolicyin their respectivedefault-traits.tssource files.Additional imports added:
AddToResourcePolicyResult(fromaws-cdk-lib/aws-iam) andResourceEnvironment(fromaws-cdk-lib/interfaces). Removed unusedIConstructimport.Note: this is NOT the full fix for #38452. This PR only addresses the
README examples. The Rosetta transpiler itself still has bugs in the C#,
Golang, and Java targets (e.g.
resource = resource;self-assignment in C#,incorrect pointer usage in Go, missing
getEnv()in Java) that will need aseparate PR in the Rosetta codebase. See @omarqureshi's analysis in the issue
for details on those remaining transpiler issues.
Describe any new or updated permissions being added
None — documentation-only change.
Description of how you validated changes
CfnBucketWithPolicyand
CfnKeyWithPolicyimplementations indefault-traits.tsexactlyAddToResourcePolicyResult,ResourceEnvironment,IResourceWithPolicyV2,IResourcePolicyFactory,PolicyStatement,ResourceWithPolicies) are exported from theirrespective modules
idiomatic Ruby, Python, and Java for the class-based pattern
yarn rosetta:extract --strict) should be runon a machine with ≥16GB RAM to verify cross-language translation before
merge
Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license