]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Turn on some TypeScript checks (#7116)
authorBen McCann <322311+benmccann@users.noreply.github.com>
Thu, 20 Feb 2020 14:43:16 +0000 (06:43 -0800)
committerGitHub <noreply@github.com>
Thu, 20 Feb 2020 14:43:16 +0000 (09:43 -0500)
Turn on some TypeScript checks

src/controllers/controller.line.js
src/core/core.controller.js
src/core/core.scale.js
src/helpers/helpers.segment.js
src/platform/platform.dom.js
src/scales/scale.category.js
src/scales/scale.linearbase.js
src/scales/scale.logarithmic.js
src/scales/scale.radialLinear.js
src/scales/scale.time.js
tsconfig.json

index dc8ceadbad710865d97a39867c588b1c46b39f2c..5dd458477119b713e931fe37c3228ac71039d9ce 100644 (file)
@@ -29,7 +29,7 @@ class LineController extends DatasetController {
        constructor(chart, datasetIndex) {
                super(chart, datasetIndex);
 
-               this._showLine = undefined;
+               this._showLine = false;
        }
 
        update(mode) {
index f777f8c9acef50272a07cb810e168cbbab47cfea..33cc21495b6ed5493419df5154612a3c0f80bab5 100644 (file)
@@ -206,7 +206,8 @@ class Chart {
                this.active = undefined;
                this.lastActive = undefined;
                this._lastEvent = undefined;
-               this._listeners = {resize: undefined};
+               /** @type {{resize?: function}} */
+               this._listeners = {};
                this._sortedMetasets = [];
                this._updating = false;
                this.scales = {};
index 234f6b4acf1e276628bcb7ea90425bfd2df6e9a5..1b9d31e011dca6f856e1cbc3496f34c0dc6ac96f 100644 (file)
@@ -6,6 +6,10 @@ import {_factorize, toDegrees, toRadians} from '../helpers/helpers.math';
 import {_parseFont, resolve, toPadding} from '../helpers/helpers.options';
 import Ticks from './core.ticks';
 
+/**
+ * @typedef { import("./core.controller").default } Chart
+ */
+
 defaults.set('scale', {
        display: true,
        offset: false,
@@ -227,10 +231,13 @@ class Scale extends Element {
 
                /** @type {string} */
                this.id = cfg.id;
+               /** @type {string} */
                this.type = cfg.type;
                /** @type {object} */
                this.options = cfg.options;
+               /** @type {CanvasRenderingContext2D} */
                this.ctx = cfg.ctx;
+               /** @type {Chart} */
                this.chart = cfg.chart;
 
                // implements box
@@ -273,26 +280,24 @@ class Scale extends Element {
                this.min = undefined;
                this.max = undefined;
                /** @type {object[]} */
-               this.ticks = null;
+               this.ticks = [];
                /** @type {object[]|null} */
                this._gridLineItems = null;
                /** @type {object[]|null} */
                this._labelItems = null;
                /** @type {object|null} */
                this._labelSizes = null;
-               /** @type {number} */
-               this._length = undefined;
-               /** @type {object} */
+               this._length = 0;
                this._longestTextCache = {};
                /** @type {number} */
                this._startPixel = undefined;
                /** @type {number} */
                this._endPixel = undefined;
-               this._reversePixels = undefined;
+               this._reversePixels = false;
                this._userMax = undefined;
                this._userMin = undefined;
-               this._ticksLength = undefined;
-               this._borderValue = undefined;
+               this._ticksLength = 0;
+               this._borderValue = 0;
        }
 
        /**
@@ -1418,7 +1423,9 @@ class Scale extends Element {
                const position = options.position;
                const isReverse = me.options.reverse;
                let rotation = 0;
-               let scaleLabelX, scaleLabelY, textAlign;
+               /** @type CanvasTextAlign */
+               let textAlign;
+               let scaleLabelX, scaleLabelY;
 
                if (me.isHorizontal()) {
                        switch (scaleLabelAlign) {
index 2f8eadc7717b7eb6eda9138725d13fdd19faedfa..e60a7c4db29e70eab82c5ffa4d4d8406a7ae2ab4 100644 (file)
@@ -190,6 +190,7 @@ function solidSegments(points, start, max, loop) {
                        if (!prev.skip) {
                                loop = false;
                                result.push({start: start % count, end: (end - 1) % count, loop});
+                               // @ts-ignore
                                start = last = cur.stop ? end : null;
                        }
                } else {
index df8c29cf466a14b3ebf371db10dd7f1ba737d4f8..e0186ab072b11ad4cb4169c3ddc6ab4ef3f4a621 100644 (file)
@@ -42,7 +42,7 @@ const EVENT_TYPES = {
 function readUsedSize(element, property) {
        const value = helpers.dom.getStyle(element, property);
        const matches = value && value.match(/^(\d+)(\.\d+)?px$/);
-       return matches ? Number(matches[1]) : undefined;
+       return matches ? +matches[1] : undefined;
 }
 
 /**
@@ -110,19 +110,22 @@ function initCanvas(canvas, config) {
  * @private
  */
 const supportsEventListenerOptions = (function() {
-       let supports = false;
+       let passiveSupported = false;
        try {
-               const options = Object.defineProperty({}, 'passive', {
-                       // eslint-disable-next-line getter-return
-                       get() {
-                               supports = true;
+               const options = {
+                       get passive() { // This function will be called when the browser attempts to access the passive property.
+                               passiveSupported = true;
+                               return false;
                        }
-               });
-               window.addEventListener('e', null, options);
+               };
+               // @ts-ignore
+               window.addEventListener('test', null, options);
+               // @ts-ignore
+               window.removeEventListener('test', null, options);
        } catch (e) {
                // continue regardless of error
        }
-       return supports;
+       return passiveSupported;
 }());
 
 // Default passive to true as expected by Chrome for 'touchstart' and 'touchend' events.
index fef277f2c82b7a85910b456f7b9209fda0556a62..a30409a6a17ea8f35f8872784ebb0d1bc2c49bdd 100644 (file)
@@ -8,12 +8,10 @@ class CategoryScale extends Scale {
        constructor(cfg) {
                super(cfg);
 
-               /** @type {number} */
-               this._numLabels = undefined;
+               this._numLabels = 0;
                /** @type {number} */
                this._startValue = undefined;
-               /** @type {number} */
-               this._valueRange = undefined;
+               this._valueRange = 0;
        }
 
        /**
index 07d3f2ae37cba53162903b5adbe15c97240956c8..d8b2e360ac1ea62dd053676f36e196c07e7b0f18 100644 (file)
@@ -119,8 +119,7 @@ class LinearScaleBase extends Scale {
                this._startValue = undefined;
                /** @type {number} */
                this._endValue = undefined;
-               /** @type {number} */
-               this._valueRange = undefined;
+               this._valueRange = 0;
        }
 
        /**
index f6487d1725d92b2258ba97477ff8842352f7bc35..2b21b78e5f55c885a8d0ab30e1d1744d320b816a 100644 (file)
@@ -68,8 +68,7 @@ class LogarithmicScale extends Scale {
                this.end = undefined;
                /** @type {number} */
                this._startValue = undefined;
-               /** @type {number} */
-               this._valueRange = undefined;
+               this._valueRange = 0;
        }
 
        /**
index 3b37bd69152d4dc861c683650ed6c2f4c73a5056..bf74e92fc057b5361a8e2d34cd25c5d5d168abd4 100644 (file)
@@ -302,7 +302,7 @@ class RadialLinearScale extends LinearScaleBase {
                /** @type {number} */
                this.drawingArea = undefined;
                /** @type {string[]} */
-               this.pointLabels = undefined;
+               this.pointLabels = [];
        }
 
        setDimensions() {
index 9b9dc806e253675b465903f5a5b2be3b19c91c9f..7f4fcd6a77e7672f3fdfb00a8de75f723b2f2432 100644 (file)
@@ -7,30 +7,31 @@ import {_lookup, _lookupByKey} from '../helpers/helpers.collection';
 
 /**
  * @typedef { import("../core/core.adapters").Unit } Unit
+ * @typedef {{common: boolean, size: number, steps?: number}} Interval
  */
 
 // Integer constants are from the ES6 spec.
 const MAX_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991;
 
 /**
- * @type {Map<Unit, {common: boolean, size: number, steps?: number}>}
+ * @type {Object<Unit, Interval>}
  */
-const INTERVALS = new Map();
-INTERVALS.set('millisecond', {common: true, size: 1, steps: 1000});
-INTERVALS.set('second', {common: true, size: 1000, steps: 60});
-INTERVALS.set('minute', {common: true, size: 60000, steps: 60});
-INTERVALS.set('hour', {common: true, size: 3600000, steps: 24});
-INTERVALS.set('day', {common: true, size: 86400000, steps: 30});
-INTERVALS.set('week', {common: false, size: 604800000, steps: 4});
-INTERVALS.set('month', {common: true, size: 2.628e9, steps: 12});
-INTERVALS.set('quarter', {common: false, size: 7.884e9, steps: 4});
-INTERVALS.set('year', {common: true, size: 3.154e10});
+const INTERVALS = {
+       millisecond: {common: true, size: 1, steps: 1000},
+       second: {common: true, size: 1000, steps: 60},
+       minute: {common: true, size: 60000, steps: 60},
+       hour: {common: true, size: 3600000, steps: 24},
+       day: {common: true, size: 86400000, steps: 30},
+       week: {common: false, size: 604800000, steps: 4},
+       month: {common: true, size: 2.628e9, steps: 12},
+       quarter: {common: false, size: 7.884e9, steps: 4},
+       year: {common: true, size: 3.154e10}
+};
 
 /**
  * @type {Unit[]}
  */
-const UNITS = [];
-INTERVALS.forEach((v, k) => UNITS.push(k));
+const UNITS = /** @type Unit[] */(Object.keys(INTERVALS));
 
 /**
  * @param {number} a
@@ -60,7 +61,7 @@ function arrayUnique(items) {
 
 /**
  * @param {TimeScale} scale
- * {*} input
+ * @param {*} input
  */
 function parse(scale, input) {
        if (isNullOrUndef(input)) {
@@ -256,11 +257,10 @@ function interpolate(table, skey, sval, tkey) {
  */
 function determineUnitForAutoTicks(minUnit, min, max, capacity) {
        const ilen = UNITS.length;
-       let i, interval, factor;
 
-       for (i = UNITS.indexOf(minUnit); i < ilen - 1; ++i) {
-               interval = INTERVALS.get(UNITS[i]);
-               factor = interval.steps ? interval.steps : MAX_INTEGER;
+       for (let i = UNITS.indexOf(minUnit); i < ilen - 1; ++i) {
+               const interval = INTERVALS[UNITS[i]];
+               const factor = interval.steps ? interval.steps : MAX_INTEGER;
 
                if (interval.common && Math.ceil((max - min) / (factor * interval.size)) <= capacity) {
                        return UNITS[i];
@@ -282,7 +282,7 @@ function determineUnitForAutoTicks(minUnit, min, max, capacity) {
 function determineUnitForFormatting(scale, numTicks, minUnit, min, max) {
        for (let i = UNITS.length - 1; i >= UNITS.indexOf(minUnit); i--) {
                const unit = UNITS[i];
-               if (INTERVALS.get(unit).common && scale._adapter.diff(max, min, unit) >= numTicks - 1) {
+               if (INTERVALS[unit].common && scale._adapter.diff(max, min, unit) >= numTicks - 1) {
                        return unit;
                }
        }
@@ -296,7 +296,7 @@ function determineUnitForFormatting(scale, numTicks, minUnit, min, max) {
  */
 function determineMajorUnit(unit) {
        for (let i = UNITS.indexOf(unit) + 1, ilen = UNITS.length; i < ilen; ++i) {
-               if (INTERVALS.get(UNITS[i]).common) {
+               if (INTERVALS[UNITS[i]].common) {
                        return UNITS[i];
                }
        }
@@ -438,6 +438,7 @@ function setMajorTicks(scale, ticks, map, majorUnit) {
  */
 function ticksFromTimestamps(scale, values, majorUnit) {
        const ticks = [];
+       /** @type {Object<number,object>} */
        const map = {};
        const ilen = values.length;
        let i, value;
@@ -575,7 +576,7 @@ class TimeScale extends Scale {
                const time = options.time || (options.time = {});
                const adapter = this._adapter = new adapters._date(options.adapters.date);
 
-
+               /** @type {{data: number[], labels: number[], all: number[]}} */
                this._cache = {
                        data: [],
                        labels: [],
@@ -584,7 +585,7 @@ class TimeScale extends Scale {
 
                /** @type {Unit} */
                this._unit = 'day';
-               /** @type {Unit | undefined} */
+               /** @type {Unit=} */
                this._majorUnit = undefined;
                /** @type {object} */
                this._offsets = {};
index 405f9e3211200275e01e95eb998847bf6900e4d5..67450a43dd577327febf2c35cc7ad2fd964d0eaf 100644 (file)
@@ -5,7 +5,10 @@
     "allowSyntheticDefaultImports": true,
     "allowJs": true,
     "checkJs": true,
-    "noEmit": true
+    "noEmit": true,
+    "alwaysStrict": true,
+    "strictBindCallApply": true,
+    "strictFunctionTypes": true
   },
   "typedocOptions": {
     "name": "Chart.js",