diff --git a/packages/string-templates/src/index.js b/packages/string-templates/src/index.js index fb5c13be26..480ed6825f 100644 --- a/packages/string-templates/src/index.js +++ b/packages/string-templates/src/index.js @@ -83,25 +83,21 @@ module.exports.processObjectSync = (object, context) => { * @returns {string} The enriched string, all templates should have been replaced if they can be. */ module.exports.processStringSync = (string, context) => { - const input = cloneDeep(string) + if (!exports.isValid(string)) { + return string + } let clonedContext = removeNull(cloneDeep(context)) clonedContext = addConstants(clonedContext) // remove any null/undefined properties if (typeof string !== "string") { throw "Cannot process non-string types." } - try { - string = processors.preprocess(string) - // this does not throw an error when template can't be fulfilled, have to try correct beforehand - const template = hbsInstance.compile(string, { - strict: false, - }) - return processors.postprocess(template(clonedContext)) - } catch (err) { - // suggested that we should always return input if an error occurs, incase string wasn't supposed to - // contain any handlebars statements - return input - } + string = processors.preprocess(string) + // this does not throw an error when template can't be fulfilled, have to try correct beforehand + const template = hbsInstance.compile(string, { + strict: false, + }) + return processors.postprocess(template(clonedContext)) } /** @@ -119,9 +115,9 @@ module.exports.makePropSafe = property => { * @returns {boolean} Whether or not the input string is valid. */ module.exports.isValid = string => { - const validCases = ["string", "number", "object", "array"] + const validCases = ["string", "number", "object", "array", "cannot read property"] // this is a portion of a specific string always output by handlebars in the case of a syntax error - const invalidCases = [`expecting 'id', 'string', 'number'`] + const invalidCases = [`expecting '`] // don't really need a real context to check if its valid const context = {} try { diff --git a/packages/string-templates/test/helpers.spec.js b/packages/string-templates/test/helpers.spec.js index 438a9047c3..cd659fcc78 100644 --- a/packages/string-templates/test/helpers.spec.js +++ b/packages/string-templates/test/helpers.spec.js @@ -317,9 +317,12 @@ describe("Cover a few complex use cases", () => { expect(validity).toBe(true) }) - it("should confirm an invalid string", () => { - const validity = isValid("{{ awdd () ") - expect(validity).toBe(false) + it("should confirm a bunch of invalid strings", () => { + const invalids = ["{{ awd )", "{{ awdd () ", "{{ awdwad ", "{{ awddawd }"] + for (let invalid of invalids) { + const validity = isValid(invalid) + expect(validity).toBe(false) + } }) it("input a garbage string, expect it to be returned", async () => {