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

Add . between ? and ] #11477

Merged
merged 1 commit into from Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,4 @@
changes:
- type: fix
scope: programgen/nodejs
description: Add `.` between `?` and `[`.
18 changes: 15 additions & 3 deletions pkg/codegen/nodejs/gen_program_expressions.go
Expand Up @@ -594,8 +594,20 @@ func (g *generator) genRelativeTraversal(w io.Writer, traversal hcl.Traversal, p
contract.Failf("unexpected traversal part of type %T (%v)", part, part.SourceRange())
}

var indexPrefix string
if model.IsOptionalType(model.GetTraversableType(parts[i])) {
g.Fgen(w, "?")
// `expr?[expr]` is not valid typescript, since it looks like a ternary
// operator.
//
// Typescript solves this by inserting a `.` in before the `[`: `expr?.[expr]`
//
// We need to do the same when generating index based expressions.
indexPrefix = "."
}

genIndex := func(inner string, value interface{}) {
g.Fgenf(w, "%s["+inner+"]", indexPrefix, value)
}

switch key.Type() {
Expand All @@ -604,13 +616,13 @@ func (g *generator) genRelativeTraversal(w io.Writer, traversal hcl.Traversal, p
if isLegalIdentifier(keyVal) {
g.Fgenf(w, ".%s", keyVal)
} else {
g.Fgenf(w, "[%q]", keyVal)
genIndex("%q", keyVal)
}
case cty.Number:
idx, _ := key.AsBigFloat().Int64()
g.Fgenf(w, "[%d]", idx)
genIndex("%d", idx)
default:
g.Fgenf(w, "[%q]", key.AsString())
genIndex("%q", key.AsString())
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/codegen/testing/test/program_driver.go
Expand Up @@ -88,7 +88,7 @@ var PulumiPulumiProgramTests = []ProgramTest{
{
Directory: "aws-s3-logging",
Description: "AWS S3 with logging",
SkipCompile: allProgLanguages.Except("python").Except("dotnet"),
SkipCompile: codegen.NewStringSet("go"),
// Blocked on nodejs: TODO[pulumi/pulumi#8068]
// Flaky in go: TODO[pulumi/pulumi#8123]
},
Expand Down
Expand Up @@ -5,4 +5,4 @@ const logs = new aws.s3.Bucket("logs", {});
const bucket = new aws.s3.Bucket("bucket", {loggings: [{
targetBucket: logs.bucket,
}]});
export const targetBucket = bucket.loggings.apply(loggings => loggings?[0]?.targetBucket);
export const targetBucket = bucket.loggings.apply(loggings => loggings?.[0]?.targetBucket);