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
  • Loading branch information
Aziz Gazanchiyan (ZIZ) authored and Aziz Gazanchiyan (ZIZ) committed Jun 11, 2021
1 parent bf3eb46 commit b4bb414
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Chart/Chart.ts
Original file line number Diff line number Diff line change
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 = 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);
Expand Down
34 changes: 34 additions & 0 deletions test/chart-spec.ts
Original file line number Diff line number Diff line change
@@ -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");
});
});

0 comments on commit b4bb414

Please sign in to comment.