Skip to content

Commit

Permalink
fix: fix single quote escaping
Browse files Browse the repository at this point in the history
wrapping single quoted strings is required
  • Loading branch information
ansgarm committed Jul 27, 2023
1 parent 6ea943f commit 3b42a53
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,17 @@ class MyConvertedCode extends TerraformStack {
}
"
`;

exports[`tfExpressions strings containing single outer quotes are supported snapshot typescript 1`] = `
"import { Construct } from "constructs";
import { TerraformOutput, TerraformStack } from "cdktf";
class MyConvertedCode extends TerraformStack {
constructor(scope: Construct, name: string) {
super(scope, name);
new TerraformOutput(this, "hash", {
value: "'static'",
});
}
}
"
`;
14 changes: 14 additions & 0 deletions packages/@cdktf/hcl2cdk/test/tfExpressions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,18 @@ ITEM
resources: [],
}
);

testCase.test(
"strings containing single outer quotes are supported",
`
output "hash" {
value = "'static'"
}`,
[],
Snapshot.yes,
Synth.yes,
{
resources: [],
}
);
});
2 changes: 1 addition & 1 deletion packages/@cdktf/hcl2json/lib/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export async function getReferencesInExpression(
// with a multi-line string, which is causing all kinds of problems
let offset = 0;
let quoteWrappedExpression = expression;
if (!expression.startsWith('"') && !expression.startsWith("'")) {
if (!expression.startsWith('"')) {
quoteWrappedExpression = `"${expression}"`;
offset = 1;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/@cdktf/hcl2json/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function wrapTerraformExpression(input: string): {
return { wrap: input, wrapOffset: 0 };
}

if (input.startsWith(`"`) || input.startsWith("'")) {
if (input.startsWith(`"`)) {
if (input.indexOf("\n") >= 0) {
const trimWrapped = input.substring(1, input.length - 1);
return {
Expand Down

0 comments on commit 3b42a53

Please sign in to comment.