From 7a376b1128c23a703de152400f06f1f30b84cdb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt?= Date: Thu, 12 Dec 2019 11:05:29 +0100 Subject: [PATCH] Fix ArrayNode --- package.json | 1 - src/convertNode.js | 4 ++-- src/nodeEvaluator.js | 2 +- src/nodeTypes/ArrayNode.js | 25 +++++++++++-------------- src/nodeTypes/Node.js | 4 ++-- test/nodeTypes/ArrayNode.test.js | 6 +++++- 6 files changed, 21 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index 5cbf41b..61c1bdd 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,6 @@ "version": "1.1.0", "description": "Library that resolves AWS Cloudformation templates with exact values", "main": "src/index.js", - "type": "module", "scripts": { "test": "mocha --recursive", "coverage": "nyc --all --reporter cobertura --reporter text --report-dir ./coverage npm run test", diff --git a/src/convertNode.js b/src/convertNode.js index 6452ab3..bae9499 100644 --- a/src/convertNode.js +++ b/src/convertNode.js @@ -22,7 +22,7 @@ const azMapping = require('./AZMap.json'); const convertNode = (node, nodeAccessor, srcObj, params, convRoot, enableVerboseLogging) => { // keeping backward compatibility because of typo in previos versions - const refResolvers = params.RefResolvers || params.RefResolevers; + const refResolvers = params.RefResolvers || params.RefResolevers || {}; switch (nodeAccessor.key) { case "Fn::FindInMap": @@ -55,7 +55,7 @@ const convertNode = (node, nodeAccessor, srcObj, params, convRoot, enableVerbose case "Ref": return new RefNode(node, nodeAccessor, enableVerboseLogging, refResolvers); case "Fn::GetAtt": - return new FnGetAttNode(node, nodeAccessor, enableVerboseLogging, params["Fn::GetAttResolvers"]); + return new FnGetAttNode(node, nodeAccessor, enableVerboseLogging, params["Fn::GetAttResolvers"] || {}); } if( _.isArray(nodeAccessor.node)){ diff --git a/src/nodeEvaluator.js b/src/nodeEvaluator.js index 0f809fa..3d48155 100644 --- a/src/nodeEvaluator.js +++ b/src/nodeEvaluator.js @@ -4,7 +4,7 @@ const { convertNode } = require("./convertNode"); class NodeEvaluator { constructor(srcObj, params, enableVerboseLogging){ this.srcObj = srcObj; - this.params = params; + this.params = params || {}; this.enableVerboseLogging = enableVerboseLogging; } diff --git a/src/nodeTypes/ArrayNode.js b/src/nodeTypes/ArrayNode.js index 637a88b..98b7644 100644 --- a/src/nodeTypes/ArrayNode.js +++ b/src/nodeTypes/ArrayNode.js @@ -27,21 +27,18 @@ class ArrayNode extends Node { super.log("Eval: ", this.nodeAccessor.path.join('/')); if(this.isLeaf){ super.log("Leaf: ", this.node, this.nodeAccessor.path.join('/')); - console.warn("Invalid Array: has no items, how to evaluate??"); - throw "Invalid Array: has no items, how to evaluate??"; - return ""; // Simple value (number, string) - } else { - const result = [] - this.directDependencies.forEach( (dep) => { - const depRes = dep.evaluate(); - result[dep.nodeAccessor.key] = depRes; - }); - - super.log("Array evaluated: "); - super.log(result); - - return this.evaluateResultedArray(result); } + + const result = [] + this.directDependencies.forEach( (dep) => { + const depRes = dep.evaluate(); + result[dep.nodeAccessor.key] = depRes; + }); + + super.log("Array evaluated: "); + super.log(result); + + return this.evaluateResultedArray(result); } } diff --git a/src/nodeTypes/Node.js b/src/nodeTypes/Node.js index 4fb0255..92f4be8 100644 --- a/src/nodeTypes/Node.js +++ b/src/nodeTypes/Node.js @@ -6,9 +6,9 @@ class Node { this.enableVerboseLogging = enableVerboseLogging; } - log(str) { + log(...str) { if(this.enableVerboseLogging){ - console.log(str); + console.log(...str); } } diff --git a/test/nodeTypes/ArrayNode.test.js b/test/nodeTypes/ArrayNode.test.js index 0a497b6..98dd8fc 100644 --- a/test/nodeTypes/ArrayNode.test.js +++ b/test/nodeTypes/ArrayNode.test.js @@ -32,5 +32,9 @@ describe('ArrayNode', () => { expect(actual).to.deep.equal(["item0", nestedArray , "item2"]) }); - // TODO: testcase when array is empty + it('evaluate empty arrays', () => { + const actual = target.evaluate(); + + expect(actual).to.deep.equal([]) + }); }); \ No newline at end of file