Skip to content

Commit

Permalink
Fix for #213: throw an error when user tries to iterate over a non-ar…
Browse files Browse the repository at this point in the history
…ray. Also added test that, without the fix, hangs indefinitely until Jest timeout kicks in.
  • Loading branch information
jjhbw committed May 31, 2021
1 parent 68b255a commit 165377b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/__tests__/__snapshots__/templating.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -28018,6 +28018,8 @@ Object {
}
`;

exports[`noSandbox Template processing throw when user tries to iterate over non-array 1`] = `"Invalid FOR command: can only iterate over Array: FOR name IN companies"`;

exports[`noSandbox Template processing works with macro-enabled (docm) templates 1`] = `
Object {
"_attrs": Object {
Expand Down Expand Up @@ -56216,6 +56218,8 @@ Object {
}
`;

exports[`sandbox Template processing throw when user tries to iterate over non-array 1`] = `"Invalid FOR command: can only iterate over Array: FOR name IN companies"`;

exports[`sandbox Template processing works with macro-enabled (docm) templates 1`] = `
Object {
"_attrs": Object {
Expand Down
Binary file modified src/__tests__/fixtures/forOverObject.docx
Binary file not shown.
Binary file added src/__tests__/fixtures/forOverObjectKeys.docx
Binary file not shown.
22 changes: 21 additions & 1 deletion src/__tests__/templating.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1046,11 +1046,31 @@ Morbi dignissim consequat ex, non finibus est faucibus sodales. Integer sed just
expect(result).toMatchSnapshot();
});

it('iterate over object properties and keys in FOR loop', async () => {
it('throw when user tries to iterate over non-array', async () => {
// Example to answer question posed in issue #149
const template = await fs.promises.readFile(
path.join(__dirname, 'fixtures', 'forOverObject.docx')
);
await expect(
createReport({
noSandbox,
template,
data: {
companies: {
one: 'FIRST',
two: 'SECOND',
three: 'THIRD',
},
},
})
).rejects.toThrowErrorMatchingSnapshot();
});

it('iterate over object properties and keys in FOR loop', async () => {
// Example to answer question posed in issue #149
const template = await fs.promises.readFile(
path.join(__dirname, 'fixtures', 'forOverObjectKeys.docx')
);
const result = await createReport(
{
noSandbox,
Expand Down
7 changes: 6 additions & 1 deletion src/processTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ const processForIf = async (
const parentLoopLevel = ctx.loops.length - 1;
const fParentIsExploring =
parentLoopLevel >= 0 && ctx.loops[parentLoopLevel].idx === -1;
let loopOver;
let loopOver: unknown[];
if (fParentIsExploring) {
loopOver = [];
} else if (isIf) {
Expand All @@ -660,6 +660,11 @@ const processForIf = async (
} else {
if (!forMatch) throw new InvalidCommandError('Invalid FOR command', cmd);
loopOver = await runUserJsAndGetRaw(data, forMatch[2], ctx);
if (!Array.isArray(loopOver))
throw new InvalidCommandError(
'Invalid FOR command: can only iterate over Array',
cmd
);
}
ctx.loops.push({
refNode: node,
Expand Down

0 comments on commit 165377b

Please sign in to comment.