From 723e9b3cba88cc9321bdc9c2cbf30e878f52dca3 Mon Sep 17 00:00:00 2001 From: Hiroyasu Nishiyama Date: Tue, 5 Feb 2019 14:47:30 +0900 Subject: [PATCH] make $parent access without key return undefined --- .../runtime/lib/nodes/context/index.js | 26 +++++++------------ .../runtime/lib/nodes/context/index_spec.js | 13 ++++------ 2 files changed, 14 insertions(+), 25 deletions(-) diff --git a/packages/node_modules/@node-red/runtime/lib/nodes/context/index.js b/packages/node_modules/@node-red/runtime/lib/nodes/context/index.js index 022c473a0..1423b5237 100644 --- a/packages/node_modules/@node-red/runtime/lib/nodes/context/index.js +++ b/packages/node_modules/@node-red/runtime/lib/nodes/context/index.js @@ -202,7 +202,7 @@ function getContextStorage(storage) { function followParentContext(parent, key) { if (key === "$parent") { - return [parent, ""]; + return [parent, undefined]; } else if (key.startsWith("$parent.")) { var len = "$parent.".length; @@ -275,24 +275,16 @@ function createContext(id,seed,parent) { var result = followParentContext(parent, key); if (result) { var [ctx, new_key] = result; - if (ctx) { - if (new_key === "") { - if (callback) { - return callback(ctx); - } - else { - return ctx; - } - } + if (ctx && new_key) { return ctx.get(new_key, storage, callback); } else { - if (callback) { - return callback(undefined); - } - else { - return undefined; - } + if (callback) { + return callback(undefined); + } + else { + return undefined; + } } } } else { @@ -365,7 +357,7 @@ function createContext(id,seed,parent) { var result = followParentContext(parent, key); if (result) { var [ctx, new_key] = result; - if (ctx) { + if (ctx && new_key) { return ctx.set(new_key, value, storage, callback); } else { diff --git a/test/unit/@node-red/runtime/lib/nodes/context/index_spec.js b/test/unit/@node-red/runtime/lib/nodes/context/index_spec.js index 1afc0f721..3c9030b03 100644 --- a/test/unit/@node-red/runtime/lib/nodes/context/index_spec.js +++ b/test/unit/@node-red/runtime/lib/nodes/context/index_spec.js @@ -226,33 +226,31 @@ describe('context', function() { }) describe("$parent", function() { - it('should access $parent', function() { + it('should get undefined for $parent without key', function() { var context0 = Context.get("0","flowA"); var context1 = Context.get("1","flowB", context0); var parent = context1.get("$parent"); - parent.should.equal(context0); + should.equal(parent, undefined); }); it('should get undefined for $parent of root', function() { var context0 = Context.get("0","flowA"); var context1 = Context.get("1","flowB", context0); - var parent = context1.get("$parent.$parent"); + var parent = context1.get("$parent.$parent.K"); should.equal(parent, undefined); }); - it('should get $parent', function() { + it('should get value in $parent', function() { var context0 = Context.get("0","flowA"); var context1 = Context.get("1","flowB", context0); - var parent = context1.get("$parent"); context0.set("K", "v"); var v = context1.get("$parent.K"); should.equal(v, "v"); }); - it('should set $parent', function() { + it('should set value in $parent', function() { var context0 = Context.get("0","flowA"); var context1 = Context.get("1","flowB", context0); - var parent = context1.get("$parent"); context1.set("$parent.K", "v"); var v = context0.get("K"); should.equal(v, "v"); @@ -270,7 +268,6 @@ describe('context', function() { }); }); - }); describe('external context storage',function() {