Skip to content

Commit

Permalink
fix: allow spaces in message template function arguments
Browse files Browse the repository at this point in the history
Also, run prettier on affected .js files.
  • Loading branch information
DinoChiesa committed Sep 27, 2023
1 parent 726763b commit 2a2719c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 43 deletions.
3 changes: 3 additions & 0 deletions lib/package/plugins/_pluginUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ const validateFunctionArgs = (expr, fnStart, argsStart, end) => {
// if (argsString.includes(' ')) {
// return {fn, error: 'spaces in argument string'};
// }
if (argsString != "" && argsString.trim() == "") {
return { fn, error: "spaces between parens" };
}
if (argsString == "" && !ZERO_ARG_FUNCTIONS.includes(fn)) {
return { fn, error: `empty arg string` };
}
Expand Down
103 changes: 60 additions & 43 deletions test/specs/testTemplateCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,62 +17,79 @@
/* jslint esversion:9 */

const assert = require("assert"),
pu = require("../../lib/package/plugins/_pluginUtil.js");
pu = require("../../lib/package/plugins/_pluginUtil.js");

describe("TemplateCheck", function() {
describe("TemplateCheck", function () {
const testCases = [
["{}", "empty function name at position 1"],
["{a}", undefined],
["{{a}", 'unexpected character at position 1: {'],
["{{a}}", 'unexpected character at position 1: {'],
["{[]}", 'unexpected character at position 1: ['],
["{a[]}", 'unexpected character at position 2: ['],
["{a", 'unterminated curly brace'],
["}a", 'unexpected close curly at position 0'],
["{a}}", 'unexpected close curly at position 3'],
["}a{", 'unexpected close curly at position 0'],
["{a}b", undefined],
["{a}{b}", undefined],
["{a}.{b}", undefined],
["{a.b}", undefined],
["{3}", 'unexpected character at position 1: 3'],
["{timeFormatUTCMs(propertyset.set1.timeformat,system.timestamp)}", undefined],
["{timeFormatUTCMs(propertyset.set1.timeformat,system.timestamp) }",
'unexpected character at position 62: '],
[`{ "organization": "{organization.name}", "environment": "{environment.name}" } `, undefined],
[`{ "organization": "{organization.name}", "environment": "{environment.{name}}" } `,
'unexpected character at position 71: {'],
[`{ "organization": "{organization.name}", "other": {"environment": "{environment.name}" } } `,
undefined],
["{createUuid()}", undefined],
["{createUuid( )}", 'spaces in argument string for function createUuid'],
["{timeFormatUTCMs()}", 'empty arg string for function timeFormatUTCMs'],
["{notARealfunction()}", 'unsupported function name (notARealfunction)'],
["{createUuid[]}", 'unexpected character at position 11: ['],
["{ createUuid() }", undefined], // but ineffective
];
["{}", "empty function name at position 1"],
["{a}", undefined],
["{{a}", "unexpected character at position 1: {"],
["{{a}}", "unexpected character at position 1: {"],
["{[]}", "unexpected character at position 1: ["],
["{a[]}", "unexpected character at position 2: ["],
["{a", "unterminated curly brace"],
["}a", "unexpected close curly at position 0"],
["{a}}", "unexpected close curly at position 3"],
["}a{", "unexpected close curly at position 0"],
["{a}b", undefined],
["{a}{b}", undefined],
["{a}.{b}", undefined],
["{a.b}", undefined],
["{3}", "unexpected character at position 1: 3"],
[
"{timeFormatUTCMs(propertyset.set1.timeformat,system.timestamp)}",
undefined,
],
[
"{timeFormatUTCMs(propertyset.set1.timeformat,system.timestamp) }",
"unexpected character at position 62: ",
],
[
`{ "organization": "{organization.name}", "environment": "{environment.name}" } `,
undefined,
],
[
`{ "organization": "{organization.name}", "environment": "{environment.{name}}" } `,
"unexpected character at position 71: {",
],
[
`{ "organization": "{organization.name}", "other": {"environment": "{environment.name}" } } `,
undefined,
],
["{createUuid()}", undefined],
["{createUuid( )}", "spaces between parens for function createUuid"],
["{timeFormatUTCMs()}", "empty arg string for function timeFormatUTCMs"],
["{notARealfunction()}", "unsupported function name (notARealfunction)"],
["{createUuid[]}", "unexpected character at position 11: ["],
["{ createUuid() }", undefined], // but ineffective
];

testCases.forEach( (item, _ix) => {
testCases.forEach((item, _ix) => {
const [expression, expectedError] = item;
it(`validateTemplate (${expression}) should return ${expectedError}`, function() {
it(`validateTemplate (${expression}) should return ${expectedError}`, function () {
const actualResult = pu.validateTemplate(expression);
assert.equal(actualResult, expectedError);
});
});
});

describe("PropertySetRefCheck", function() {
describe("PropertySetRefCheck", function () {
const testCases = [
["{foo.bar}", undefined],
["{foo.bar}.{var2}.setting", 'there is more than one dot in the template result'],
["foo.bar.var2.setting", 'there are 3 dots ( !=1 ) in the template result'],
["{foo.bar}.setting", undefined],
["{foo.bar}.{flow.var2}", undefined],
];
["{foo.bar}", undefined],
[
"{foo.bar}.{var2}.setting",
"there is more than one dot in the template result",
],
["foo.bar.var2.setting", "there are 3 dots ( !=1 ) in the template result"],
["{foo.bar}.setting", undefined],
["{foo.bar}.{flow.var2}", undefined],
];

testCases.forEach( (item, _ix) => {
testCases.forEach((item, _ix) => {
const [expression, expectedResult] = item;
it(`PropertySetRef (${expression}) should be ${expectedResult?'valid':'not valid'}`, function() {
it(`PropertySetRef (${expression}) should be ${
expectedResult ? "valid" : "not valid"
}`, function () {
const actualResult = pu.validatePropertySetRef(expression);
assert.equal(actualResult, expectedResult);
});
Expand Down

0 comments on commit 2a2719c

Please sign in to comment.