Skip to content

Commit

Permalink
Add . between ? and ]
Browse files Browse the repository at this point in the history
  • Loading branch information
iwahbe committed Nov 28, 2022
1 parent 35f8427 commit 25e4c49
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
@@ -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);

0 comments on commit 25e4c49

Please sign in to comment.