Skip to content

Commit

Permalink
fix(template-strings): do not apply helper functions on unresolved st…
Browse files Browse the repository at this point in the history
…ring (#4710)

* fix(template-strings): do not apply helper functions on unresolved string

* test: add unit test for helper on unresolved template string

* chore: fix lint error
  • Loading branch information
shumailxyz authored Jun 26, 2023
1 parent 929ef61 commit 8d51800
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
14 changes: 14 additions & 0 deletions core/src/template-string/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { validateSchema } from "../config/validation"
import { safeLoad, safeLoadAll } from "js-yaml"
import { safeDumpYaml } from "../util/util"
import indentString from "indent-string"
import { maybeTemplateString } from "./template-string"

interface ExampleArgument {
input: any[]
Expand Down Expand Up @@ -508,6 +509,19 @@ export function callHelperFunction({
context: `argument '${argName}' for ${functionName} helper function`,
ErrorClass: TemplateStringError,
})

// do not apply helper function for an unresolved template string
if (maybeTemplateString(value)) {
if (allowPartial) {
return { resolved: "${" + text + "}" }
} else {
const _error = new TemplateStringError(`Function '${functionName}' cannot be applied on unresolved string`, {
functionName,
text,
})
return { _error }
}
}
} catch (_error) {
if (allowPartial) {
return { resolved: text }
Expand Down
13 changes: 12 additions & 1 deletion core/test/unit/src/template-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
import { ConfigContext } from "../../../src/config/template-contexts/base"
import { expectError } from "../../helpers"
import stripAnsi = require("strip-ansi")
import { dedent } from "../../../src/util/string"
import { dedent, deline } from "../../../src/util/string"

/* tslint:disable:no-invalid-template-strings */

Expand Down Expand Up @@ -1137,6 +1137,17 @@ describe("resolveTemplateString", async () => {
)
})

it(
deline`does not apply helper function on unresolved template string and
returns string as-is, when allowPartial=true`,
() => {
const res = resolveTemplateString("${base64Encode('${environment.namespace}')}", new TestContext({}), {
allowPartial: true,
})
expect(res).to.equal("${base64Encode('${environment.namespace}')}")
}
)

context("concat", () => {
it("allows empty strings", () => {
const res = resolveTemplateString("${concat('', '')}", new TestContext({}))
Expand Down

0 comments on commit 8d51800

Please sign in to comment.