Skip to content

Commit

Permalink
fix(Chart): Handle nullish properties from API extendings safely
Browse files Browse the repository at this point in the history
Chart APIs having null/undefined properties should not break the chart construction now

Ref #2132
Close #2134
  • Loading branch information
Aziz Gazanchiyan (ZIZ) authored and netil committed Jun 15, 2021
1 parent b82b865 commit 6cbf64a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions AUTHORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ Marc Faber <faber@genodata.de>
Donald Pipowitch <pipo@senaeh.de>
Sung Oh Park <sung-oh.park@navercorp.com>
Adrian Schmutzler <dev@schmutzler.it>
Aziz Gazanchiyan <ZIZ@KMDPoland.pl>
9 changes: 6 additions & 3 deletions src/Chart/Chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
import ChartInternal from "../ChartInternal/ChartInternal";
import {loadConfig} from "../config/config";
import {extend, isFunction} from "../module/util";
import {extend, isFunction, notEmpty} from "../module/util";

import apiChart from "./api/chart";
import apiColor from "./api/color";
Expand Down Expand Up @@ -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 = notEmpty(fn[key]);
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);
Expand Down
26 changes: 25 additions & 1 deletion test/internals/bb-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import sinon from "sinon";
import bb from "../../src";
import util from "../assets/util";
import CLASS from "../../src/config/classes";
import {convertInputType} from "../../src/module/util";
import Chart from "../../src/Chart/Chart";
import {convertInputType, extend} from "../../src/module/util";

describe("Interface & initialization", () => {
let chart;
Expand Down Expand Up @@ -113,6 +114,29 @@ describe("Interface & initialization", () => {

expect(chart.$.chart.classed(bindtoClassName)).to.be.true;
});

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.null;
expect((extendedInstance as Extended).voidProperty).to.be.undefined;
});
});

describe("auto resize", () => {
Expand Down

0 comments on commit 6cbf64a

Please sign in to comment.