From b4bb4147651214315625f9084b2d9c01dbaf7eec Mon Sep 17 00:00:00 2001 From: "Aziz Gazanchiyan (ZIZ)" Date: Fri, 11 Jun 2021 10:08:34 +0200 Subject: [PATCH] fix(Chart): Handle nullish properties from API extendings safely Chart APIs having null/undefined properties should not break the chart construction now Ref #2132 --- src/Chart/Chart.ts | 7 +++++-- test/chart-spec.ts | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 test/chart-spec.ts diff --git a/src/Chart/Chart.ts b/src/Chart/Chart.ts index b5a694579..79720f46c 100644 --- a/src/Chart/Chart.ts +++ b/src/Chart/Chart.ts @@ -89,12 +89,15 @@ export default class Chart { Object.keys(fn).forEach(key => { const isFunc = isFunction(fn[key]); const isChild = target !== argThis; - const hasChild = Object.keys(fn[key]).length > 0; + const isNotNil = fn[key] != null; + const hasChild = isNotNil && Object.keys(fn[key]).length > 0; if (isFunc && ((!isChild && hasChild) || isChild)) { target[key] = fn[key].bind(argThis); - } else if (!isFunc) { + } else if (isNotNil && !isFunc) { target[key] = {}; + } else { + target[key] = fn[key]; } hasChild && bindThis(fn[key], target[key], argThis); diff --git a/test/chart-spec.ts b/test/chart-spec.ts new file mode 100644 index 000000000..898ba6ef4 --- /dev/null +++ b/test/chart-spec.ts @@ -0,0 +1,34 @@ +/** + * Copyright (c) 2017 ~ present NAVER Corp. + * billboard.js project is licensed under the MIT license + */ +import {expect} from "chai"; +import {extend} from "../src/module/util"; + +import Chart from "../src/Chart/Chart"; + +describe("Chart", () => { + it("should bind correctly with nullish properties", () => { + const options = { + data: { + columns: [["data1", 0]] + } + }; + + class Extended extends Chart { + nullProperty; + voidProperty; + } + + extend(Chart.prototype, { + nullProperty: null, + voidProperty: undefined + }); + + const extendedInstance = new Chart(options); + + expect((extendedInstance as Extended).nullProperty).to.be.a("null"); + + expect((extendedInstance as Extended).voidProperty).to.be.an("undefined"); + }); +});