From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Thu, 20 Feb 2020 14:43:16 +0000 (-0800) Subject: Turn on some TypeScript checks (#7116) X-Git-Tag: v3.0.0-alpha~33 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2e5b0722510e80fe780959f0c4b9d9ddf961f4b5;p=thirdparty%2FChart.js.git Turn on some TypeScript checks (#7116) Turn on some TypeScript checks --- diff --git a/src/controllers/controller.line.js b/src/controllers/controller.line.js index dc8ceadba..5dd458477 100644 --- a/src/controllers/controller.line.js +++ b/src/controllers/controller.line.js @@ -29,7 +29,7 @@ class LineController extends DatasetController { constructor(chart, datasetIndex) { super(chart, datasetIndex); - this._showLine = undefined; + this._showLine = false; } update(mode) { diff --git a/src/core/core.controller.js b/src/core/core.controller.js index f777f8c9a..33cc21495 100644 --- a/src/core/core.controller.js +++ b/src/core/core.controller.js @@ -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 = {}; diff --git a/src/core/core.scale.js b/src/core/core.scale.js index 234f6b4ac..1b9d31e01 100644 --- a/src/core/core.scale.js +++ b/src/core/core.scale.js @@ -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) { diff --git a/src/helpers/helpers.segment.js b/src/helpers/helpers.segment.js index 2f8eadc77..e60a7c4db 100644 --- a/src/helpers/helpers.segment.js +++ b/src/helpers/helpers.segment.js @@ -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 { diff --git a/src/platform/platform.dom.js b/src/platform/platform.dom.js index df8c29cf4..e0186ab07 100644 --- a/src/platform/platform.dom.js +++ b/src/platform/platform.dom.js @@ -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. diff --git a/src/scales/scale.category.js b/src/scales/scale.category.js index fef277f2c..a30409a6a 100644 --- a/src/scales/scale.category.js +++ b/src/scales/scale.category.js @@ -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; } /** diff --git a/src/scales/scale.linearbase.js b/src/scales/scale.linearbase.js index 07d3f2ae3..d8b2e360a 100644 --- a/src/scales/scale.linearbase.js +++ b/src/scales/scale.linearbase.js @@ -119,8 +119,7 @@ class LinearScaleBase extends Scale { this._startValue = undefined; /** @type {number} */ this._endValue = undefined; - /** @type {number} */ - this._valueRange = undefined; + this._valueRange = 0; } /** diff --git a/src/scales/scale.logarithmic.js b/src/scales/scale.logarithmic.js index f6487d172..2b21b78e5 100644 --- a/src/scales/scale.logarithmic.js +++ b/src/scales/scale.logarithmic.js @@ -68,8 +68,7 @@ class LogarithmicScale extends Scale { this.end = undefined; /** @type {number} */ this._startValue = undefined; - /** @type {number} */ - this._valueRange = undefined; + this._valueRange = 0; } /** diff --git a/src/scales/scale.radialLinear.js b/src/scales/scale.radialLinear.js index 3b37bd691..bf74e92fc 100644 --- a/src/scales/scale.radialLinear.js +++ b/src/scales/scale.radialLinear.js @@ -302,7 +302,7 @@ class RadialLinearScale extends LinearScaleBase { /** @type {number} */ this.drawingArea = undefined; /** @type {string[]} */ - this.pointLabels = undefined; + this.pointLabels = []; } setDimensions() { diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index 9b9dc806e..7f4fcd6a7 100644 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -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} + * @type {Object} */ -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} */ 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 = {}; diff --git a/tsconfig.json b/tsconfig.json index 405f9e321..67450a43d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,7 +5,10 @@ "allowSyntheticDefaultImports": true, "allowJs": true, "checkJs": true, - "noEmit": true + "noEmit": true, + "alwaysStrict": true, + "strictBindCallApply": true, + "strictFunctionTypes": true }, "typedocOptions": { "name": "Chart.js",