From: Dan Onoshko Date: Mon, 22 Aug 2022 15:58:57 +0000 (+0400) Subject: Preparing the project for TypeScript (#10595) X-Git-Tag: v4.0.0~48 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2031cdf0512a94d2897a682ca156d150f0a83bb3;p=thirdparty%2FChart.js.git Preparing the project for TypeScript (#10595) * Add Typescript to the build * Converts the `helpers.core` to Typescript as an example * Converts the `core.element` to Typescript --- diff --git a/.eslintrc.yml b/.eslintrc.yml index f3ca59d8e..7b8c853a9 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -36,3 +36,24 @@ rules: es/no-regexp-s-flag: "error" es/no-regexp-unicode-property-escapes: "error" es/no-dynamic-import: "off" + +overrides: + - files: ['**/*.ts'] + parser: '@typescript-eslint/parser' + plugins: + - '@typescript-eslint' + extends: + - chartjs + - plugin:@typescript-eslint/recommended + + rules: + # Replace stock eslint rules with typescript-eslint equivalents for proper + # TypeScript support. + indent: "off" + "@typescript-eslint/indent": ["error", 2] + no-use-before-define: "off" + '@typescript-eslint/no-use-before-define': "error" + no-shadow: "off" + '@typescript-eslint/no-shadow': "error" + space-before-function-paren: "off" + '@typescript-eslint/space-before-function-paren': [2, never] diff --git a/.size-limit.cjs b/.size-limit.cjs index b2026cd49..e201a0ca6 100644 --- a/.size-limit.cjs +++ b/.size-limit.cjs @@ -7,7 +7,7 @@ function modifyWebpackConfig(config) { module.exports = [ { path: 'dist/chart.js', - limit: '75 KB', + limit: '77 KB', webpack: false, running: false }, diff --git a/auto/auto.d.ts b/auto/auto.d.ts index 566f043ac..487acb610 100644 --- a/auto/auto.d.ts +++ b/auto/auto.d.ts @@ -1,4 +1,4 @@ -import {Chart} from '../types'; +import {Chart} from '../dist/types'; -export * from '../types'; +export * from '../dist/types'; export default Chart; diff --git a/docs/developers/charts.md b/docs/developers/charts.md index c37cace2c..feb63fc41 100644 --- a/docs/developers/charts.md +++ b/docs/developers/charts.md @@ -130,8 +130,8 @@ When adding a new chart type, `ChartTypeRegistry` must contains the declarations For example, to provide typings for a new chart type that extends from a bubble chart, you would add a `.d.ts` containing: -```ts -import { ChartTypeRegistry } from 'chart.js' +```typescript +import { ChartTypeRegistry } from 'chart.js'; declare module 'chart.js' { interface ChartTypeRegistry { diff --git a/helpers/helpers.d.ts b/helpers/helpers.d.ts index 2c3468e72..587d0fd55 100644 --- a/helpers/helpers.d.ts +++ b/helpers/helpers.d.ts @@ -1 +1 @@ -export * from '../types/helpers'; +export * from '../dist/helpers/types'; diff --git a/karma.conf.cjs b/karma.conf.cjs index cb8839fce..f9764bafc 100644 --- a/karma.conf.cjs +++ b/karma.conf.cjs @@ -27,11 +27,9 @@ module.exports = async function(karma) { } if (args.coverage) { - build.plugins = [ - json(), - resolve(), + build.plugins.push( istanbul({exclude: ['node_modules/**/*.js', 'package.json']}) - ]; + ); } karma.set({ @@ -92,14 +90,14 @@ module.exports = async function(karma) { 'node_modules/moment-timezone/builds/moment-timezone-with-data.min.js', {pattern: 'test/index.js', watched: false}, {pattern: 'test/BasicChartWebWorker.js', included: false}, - {pattern: 'src/index.umd.js', watched: false}, + {pattern: 'src/index.umd.ts', watched: false}, 'node_modules/chartjs-adapter-moment/dist/chartjs-adapter-moment.js', {pattern: specPattern} ], preprocessors: { 'test/index.js': ['rollup'], - 'src/index.umd.js': ['sources'] + 'src/index.umd.ts': ['sources'] }, rollupPreprocessor: { diff --git a/package-lock.json b/package-lock.json index 2c110bede..c968bcfa5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,6 +16,7 @@ "@rollup/plugin-node-resolve": "^13.3.0", "@simonbrunel/vuepress-plugin-versions": "^0.2.0", "@size-limit/preset-big-lib": "^8.0.0", + "@swc/core": "^1.2.224", "@types/estree": "^1.0.0", "@types/offscreencanvas": "^2019.7.0", "@typescript-eslint/eslint-plugin": "^5.32.0", @@ -56,6 +57,7 @@ "rollup": "^2.77.2", "rollup-plugin-cleanup": "^3.2.1", "rollup-plugin-istanbul": "^3.0.0", + "rollup-plugin-swc3": "^0.3.0", "rollup-plugin-terser": "^7.0.2", "size-limit": "^8.0.0", "typedoc": "^0.23.10", @@ -2634,6 +2636,306 @@ "node": ">=10.13.0" } }, + "node_modules/@swc/core": { + "version": "1.2.224", + "resolved": "https://registry.npmjs.org/@swc/core/-/core-1.2.224.tgz", + "integrity": "sha512-K0B0QKT0eSpPlL4amWJzllYJigQdE7+ha6VQVks6g/oiko1yMYP8lGcCKOKb+KuvW1ltPzlyFqi7h7ryEVG2vQ==", + "dev": true, + "hasInstallScript": true, + "bin": { + "swcx": "run_swcx.js" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/swc" + }, + "optionalDependencies": { + "@swc/core-android-arm-eabi": "1.2.224", + "@swc/core-android-arm64": "1.2.224", + "@swc/core-darwin-arm64": "1.2.224", + "@swc/core-darwin-x64": "1.2.224", + "@swc/core-freebsd-x64": "1.2.224", + "@swc/core-linux-arm-gnueabihf": "1.2.224", + "@swc/core-linux-arm64-gnu": "1.2.224", + "@swc/core-linux-arm64-musl": "1.2.224", + "@swc/core-linux-x64-gnu": "1.2.224", + "@swc/core-linux-x64-musl": "1.2.224", + "@swc/core-win32-arm64-msvc": "1.2.224", + "@swc/core-win32-ia32-msvc": "1.2.224", + "@swc/core-win32-x64-msvc": "1.2.224" + } + }, + "node_modules/@swc/core-android-arm-eabi": { + "version": "1.2.224", + "resolved": "https://registry.npmjs.org/@swc/core-android-arm-eabi/-/core-android-arm-eabi-1.2.224.tgz", + "integrity": "sha512-viVOYrhSqNxdDOCNu2UUfiAK0qjkmk/fB9mObdSb+48JlHv4kYBnSLjaIhj0NlXCsxgetH7QFbjrKXRJ+gpHqw==", + "cpu": [ + "arm" + ], + "dev": true, + "optional": true, + "os": [ + "android" + ], + "dependencies": { + "@swc/wasm": "1.2.122" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-android-arm64": { + "version": "1.2.224", + "resolved": "https://registry.npmjs.org/@swc/core-android-arm64/-/core-android-arm64-1.2.224.tgz", + "integrity": "sha512-HCfdnVyslhMX25BDOqE7rOcl3a6QHs34O3xLLY2J/wg2ICtbxehpbpBPrp+VBG3Ngv7VGD9OPhmFgGxElFtZLQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "android" + ], + "dependencies": { + "@swc/wasm": "1.2.130" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-android-arm64/node_modules/@swc/wasm": { + "version": "1.2.130", + "resolved": "https://registry.npmjs.org/@swc/wasm/-/wasm-1.2.130.tgz", + "integrity": "sha512-rNcJsBxS70+pv8YUWwf5fRlWX6JoY/HJc25HD/F8m6Kv7XhJdqPPMhyX6TKkUBPAG7TWlZYoxa+rHAjPy4Cj3Q==", + "dev": true, + "optional": true + }, + "node_modules/@swc/core-darwin-arm64": { + "version": "1.2.224", + "resolved": "https://registry.npmjs.org/@swc/core-darwin-arm64/-/core-darwin-arm64-1.2.224.tgz", + "integrity": "sha512-jzv8Eop0GDe4owRDWr02n/xT7Bm6eTFWoM3nFXOM865gNYfASGGm3HHm4z20yndIxq5xuKHcOOH9QXQQhe/lQA==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-darwin-x64": { + "version": "1.2.224", + "resolved": "https://registry.npmjs.org/@swc/core-darwin-x64/-/core-darwin-x64-1.2.224.tgz", + "integrity": "sha512-hpdJt/BJ45+hcgs461nJxgMJVNN/2uJL8TLvFxeOJiDpF4r1elRxYfDC1W8ctMWB3eB3Wepz3Ku2z8l9UgPnyQ==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-freebsd-x64": { + "version": "1.2.224", + "resolved": "https://registry.npmjs.org/@swc/core-freebsd-x64/-/core-freebsd-x64-1.2.224.tgz", + "integrity": "sha512-bVIXwwjYA1ZdMKTwrAosxG808nQqvHhzs/fOKUrl1VeZ9CTHA0FzpSXwaBf1shbUVKvQVPKydY0K5q9dkSUkpw==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "freebsd" + ], + "dependencies": { + "@swc/wasm": "1.2.130" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-freebsd-x64/node_modules/@swc/wasm": { + "version": "1.2.130", + "resolved": "https://registry.npmjs.org/@swc/wasm/-/wasm-1.2.130.tgz", + "integrity": "sha512-rNcJsBxS70+pv8YUWwf5fRlWX6JoY/HJc25HD/F8m6Kv7XhJdqPPMhyX6TKkUBPAG7TWlZYoxa+rHAjPy4Cj3Q==", + "dev": true, + "optional": true + }, + "node_modules/@swc/core-linux-arm-gnueabihf": { + "version": "1.2.224", + "resolved": "https://registry.npmjs.org/@swc/core-linux-arm-gnueabihf/-/core-linux-arm-gnueabihf-1.2.224.tgz", + "integrity": "sha512-MY3UQI3IOjME5TpuIhwI0lqKHsU3x7BwlU4n/mLNe2lH3DLaP0rrGHCMI2iJqfiYKU1Rg1r01HXmysuiriF1TA==", + "cpu": [ + "arm" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "dependencies": { + "@swc/wasm": "1.2.130" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-linux-arm-gnueabihf/node_modules/@swc/wasm": { + "version": "1.2.130", + "resolved": "https://registry.npmjs.org/@swc/wasm/-/wasm-1.2.130.tgz", + "integrity": "sha512-rNcJsBxS70+pv8YUWwf5fRlWX6JoY/HJc25HD/F8m6Kv7XhJdqPPMhyX6TKkUBPAG7TWlZYoxa+rHAjPy4Cj3Q==", + "dev": true, + "optional": true + }, + "node_modules/@swc/core-linux-arm64-gnu": { + "version": "1.2.224", + "resolved": "https://registry.npmjs.org/@swc/core-linux-arm64-gnu/-/core-linux-arm64-gnu-1.2.224.tgz", + "integrity": "sha512-tEpaGq4wp79tR+4s0h8xbzcxW6tZOfb2/Jf1vrmRYfRBqryBRTdtn14Rfy4qlK3s/SC3NfI2MKxZRgcGnMCeWw==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-linux-arm64-musl": { + "version": "1.2.224", + "resolved": "https://registry.npmjs.org/@swc/core-linux-arm64-musl/-/core-linux-arm64-musl-1.2.224.tgz", + "integrity": "sha512-jnrYqXc7aRzBnEqEp3nAi9tjuUhBnN0pSKiHJytlBP1QkXnH7HD44Da9udmKUFYB5hHpwaXE0NIh7jK0nSMnhw==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-linux-x64-gnu": { + "version": "1.2.224", + "resolved": "https://registry.npmjs.org/@swc/core-linux-x64-gnu/-/core-linux-x64-gnu-1.2.224.tgz", + "integrity": "sha512-UBkeDlG+PrIXDH1sR4EIXN5qK4a677IHb6RBghbvBDJS61X9/nTDxtCF7/zCqDxJRahhUrT6lDsYpuLCws2hiw==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-linux-x64-musl": { + "version": "1.2.224", + "resolved": "https://registry.npmjs.org/@swc/core-linux-x64-musl/-/core-linux-x64-musl-1.2.224.tgz", + "integrity": "sha512-CQMGDzxKvkzf6TOdaWnmhb6uk1XEhM/mM3BDfX+hx9j3Hg3bFw9qmPvrkoWI2G8J50MvpoR1iPBYyG2LNeQWeg==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-win32-arm64-msvc": { + "version": "1.2.224", + "resolved": "https://registry.npmjs.org/@swc/core-win32-arm64-msvc/-/core-win32-arm64-msvc-1.2.224.tgz", + "integrity": "sha512-03V4apubsOhLKQNmfWGlgvDCJkhlh0ZOHcGddxb7bD4PeP6U0lnABG3hlz2uicwcIGBPu/p7jtm5/hezeiXE6Q==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "win32" + ], + "dependencies": { + "@swc/wasm": "1.2.130" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-win32-arm64-msvc/node_modules/@swc/wasm": { + "version": "1.2.130", + "resolved": "https://registry.npmjs.org/@swc/wasm/-/wasm-1.2.130.tgz", + "integrity": "sha512-rNcJsBxS70+pv8YUWwf5fRlWX6JoY/HJc25HD/F8m6Kv7XhJdqPPMhyX6TKkUBPAG7TWlZYoxa+rHAjPy4Cj3Q==", + "dev": true, + "optional": true + }, + "node_modules/@swc/core-win32-ia32-msvc": { + "version": "1.2.224", + "resolved": "https://registry.npmjs.org/@swc/core-win32-ia32-msvc/-/core-win32-ia32-msvc-1.2.224.tgz", + "integrity": "sha512-gPOmIdhCwkb5BbDiPs/4t1LIElFLoQwiv45XwK5wADh1qzLD3A8EtpnpXfLsjL/fUMlLIGCgHQ6BQ0x04VrI1Q==", + "cpu": [ + "ia32" + ], + "dev": true, + "optional": true, + "os": [ + "win32" + ], + "dependencies": { + "@swc/wasm": "1.2.130" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-win32-ia32-msvc/node_modules/@swc/wasm": { + "version": "1.2.130", + "resolved": "https://registry.npmjs.org/@swc/wasm/-/wasm-1.2.130.tgz", + "integrity": "sha512-rNcJsBxS70+pv8YUWwf5fRlWX6JoY/HJc25HD/F8m6Kv7XhJdqPPMhyX6TKkUBPAG7TWlZYoxa+rHAjPy4Cj3Q==", + "dev": true, + "optional": true + }, + "node_modules/@swc/core-win32-x64-msvc": { + "version": "1.2.224", + "resolved": "https://registry.npmjs.org/@swc/core-win32-x64-msvc/-/core-win32-x64-msvc-1.2.224.tgz", + "integrity": "sha512-xALNoKi/UAQ2NflIkCBaZ4Nib75a4YwFp2pZ5Yen3vR8hxK2UJYlaNwyfsBwWfMOMsNxQ1Q1aaqoo0L+XON53Q==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/wasm": { + "version": "1.2.122", + "resolved": "https://registry.npmjs.org/@swc/wasm/-/wasm-1.2.122.tgz", + "integrity": "sha512-sM1VCWQxmNhFtdxME+8UXNyPNhxNu7zdb6ikWpz0YKAQQFRGT5ThZgJrubEpah335SUToNg8pkdDF7ibVCjxbQ==", + "dev": true, + "optional": true + }, "node_modules/@szmarczak/http-timer": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/@szmarczak/http-timer/-/http-timer-1.1.2.tgz", @@ -12857,6 +13159,15 @@ "node": ">=8" } }, + "node_modules/joycon": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/joycon/-/joycon-3.1.1.tgz", + "integrity": "sha512-34wB/Y7MW7bzjKRjUKTa46I2Z7eV62Rkhva+KkopW7Qvv/OSWBqvkSY7vusOPrNuZcUG3tApvdVgNB8POj3SPw==", + "dev": true, + "engines": { + "node": ">=10" + } + }, "node_modules/js-cleanup": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/js-cleanup/-/js-cleanup-1.2.0.tgz", @@ -17968,6 +18279,101 @@ "semver": "bin/semver.js" } }, + "node_modules/rollup-plugin-swc3": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/rollup-plugin-swc3/-/rollup-plugin-swc3-0.3.0.tgz", + "integrity": "sha512-ZQK2XxYxSspmT8j6/Y4CaxRxAlZHbNnxI+m+yJ5I87ZLp5uH7CYL4hFlJk1jkcZ+Q2QC19jIg7AClB/7+XFljw==", + "dev": true, + "dependencies": { + "@rollup/pluginutils": "^4.2.1", + "deepmerge": "^4.2.2", + "joycon": "^3.1.1", + "jsonc-parser": "^3.0.0", + "typedoc": "^0.22.15" + }, + "engines": { + "node": ">=12" + }, + "peerDependencies": { + "@swc/core": ">=1.2.165", + "rollup": "^2.0.0" + } + }, + "node_modules/rollup-plugin-swc3/node_modules/@rollup/pluginutils": { + "version": "4.2.1", + "resolved": "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-4.2.1.tgz", + "integrity": "sha512-iKnFXr7NkdZAIHiIWE+BX5ULi/ucVFYWD6TbAV+rZctiRTY2PL6tsIKhoIOaoskiWAkgu+VsbXgUVDNLHf+InQ==", + "dev": true, + "dependencies": { + "estree-walker": "^2.0.1", + "picomatch": "^2.2.2" + }, + "engines": { + "node": ">= 8.0.0" + } + }, + "node_modules/rollup-plugin-swc3/node_modules/brace-expansion": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz", + "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==", + "dev": true, + "dependencies": { + "balanced-match": "^1.0.0" + } + }, + "node_modules/rollup-plugin-swc3/node_modules/glob": { + "version": "8.0.3", + "resolved": "https://registry.npmjs.org/glob/-/glob-8.0.3.tgz", + "integrity": "sha512-ull455NHSHI/Y1FqGaaYFaLGkNMMJbavMrEGFXG/PGrg6y7sutWHUHrz6gy6WEBH6akM1M414dWKCNs+IhKdiQ==", + "dev": true, + "dependencies": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^5.0.1", + "once": "^1.3.0" + }, + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/rollup-plugin-swc3/node_modules/minimatch": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-5.1.0.tgz", + "integrity": "sha512-9TPBGGak4nHfGZsPBohm9AWg6NoT7QTCehS3BIJABslyZbzxfV78QM2Y6+i741OPZIafFAaiiEMh5OyIrJPgtg==", + "dev": true, + "dependencies": { + "brace-expansion": "^2.0.1" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/rollup-plugin-swc3/node_modules/typedoc": { + "version": "0.22.18", + "resolved": "https://registry.npmjs.org/typedoc/-/typedoc-0.22.18.tgz", + "integrity": "sha512-NK9RlLhRUGMvc6Rw5USEYgT4DVAUFk7IF7Q6MYfpJ88KnTZP7EneEa4RcP+tX1auAcz7QT1Iy0bUSZBYYHdoyA==", + "dev": true, + "dependencies": { + "glob": "^8.0.3", + "lunr": "^2.3.9", + "marked": "^4.0.16", + "minimatch": "^5.1.0", + "shiki": "^0.10.1" + }, + "bin": { + "typedoc": "bin/typedoc" + }, + "engines": { + "node": ">= 12.10.0" + }, + "peerDependencies": { + "typescript": "4.0.x || 4.1.x || 4.2.x || 4.3.x || 4.4.x || 4.5.x || 4.6.x || 4.7.x" + } + }, "node_modules/rollup-plugin-terser": { "version": "7.0.2", "resolved": "https://registry.npmjs.org/rollup-plugin-terser/-/rollup-plugin-terser-7.0.2.tgz", @@ -24629,6 +25035,188 @@ } } }, + "@swc/core": { + "version": "1.2.224", + "resolved": "https://registry.npmjs.org/@swc/core/-/core-1.2.224.tgz", + "integrity": "sha512-K0B0QKT0eSpPlL4amWJzllYJigQdE7+ha6VQVks6g/oiko1yMYP8lGcCKOKb+KuvW1ltPzlyFqi7h7ryEVG2vQ==", + "dev": true, + "requires": { + "@swc/core-android-arm-eabi": "1.2.224", + "@swc/core-android-arm64": "1.2.224", + "@swc/core-darwin-arm64": "1.2.224", + "@swc/core-darwin-x64": "1.2.224", + "@swc/core-freebsd-x64": "1.2.224", + "@swc/core-linux-arm-gnueabihf": "1.2.224", + "@swc/core-linux-arm64-gnu": "1.2.224", + "@swc/core-linux-arm64-musl": "1.2.224", + "@swc/core-linux-x64-gnu": "1.2.224", + "@swc/core-linux-x64-musl": "1.2.224", + "@swc/core-win32-arm64-msvc": "1.2.224", + "@swc/core-win32-ia32-msvc": "1.2.224", + "@swc/core-win32-x64-msvc": "1.2.224" + } + }, + "@swc/core-android-arm-eabi": { + "version": "1.2.224", + "resolved": "https://registry.npmjs.org/@swc/core-android-arm-eabi/-/core-android-arm-eabi-1.2.224.tgz", + "integrity": "sha512-viVOYrhSqNxdDOCNu2UUfiAK0qjkmk/fB9mObdSb+48JlHv4kYBnSLjaIhj0NlXCsxgetH7QFbjrKXRJ+gpHqw==", + "dev": true, + "optional": true, + "requires": { + "@swc/wasm": "1.2.122" + } + }, + "@swc/core-android-arm64": { + "version": "1.2.224", + "resolved": "https://registry.npmjs.org/@swc/core-android-arm64/-/core-android-arm64-1.2.224.tgz", + "integrity": "sha512-HCfdnVyslhMX25BDOqE7rOcl3a6QHs34O3xLLY2J/wg2ICtbxehpbpBPrp+VBG3Ngv7VGD9OPhmFgGxElFtZLQ==", + "dev": true, + "optional": true, + "requires": { + "@swc/wasm": "1.2.130" + }, + "dependencies": { + "@swc/wasm": { + "version": "1.2.130", + "resolved": "https://registry.npmjs.org/@swc/wasm/-/wasm-1.2.130.tgz", + "integrity": "sha512-rNcJsBxS70+pv8YUWwf5fRlWX6JoY/HJc25HD/F8m6Kv7XhJdqPPMhyX6TKkUBPAG7TWlZYoxa+rHAjPy4Cj3Q==", + "dev": true, + "optional": true + } + } + }, + "@swc/core-darwin-arm64": { + "version": "1.2.224", + "resolved": "https://registry.npmjs.org/@swc/core-darwin-arm64/-/core-darwin-arm64-1.2.224.tgz", + "integrity": "sha512-jzv8Eop0GDe4owRDWr02n/xT7Bm6eTFWoM3nFXOM865gNYfASGGm3HHm4z20yndIxq5xuKHcOOH9QXQQhe/lQA==", + "dev": true, + "optional": true + }, + "@swc/core-darwin-x64": { + "version": "1.2.224", + "resolved": "https://registry.npmjs.org/@swc/core-darwin-x64/-/core-darwin-x64-1.2.224.tgz", + "integrity": "sha512-hpdJt/BJ45+hcgs461nJxgMJVNN/2uJL8TLvFxeOJiDpF4r1elRxYfDC1W8ctMWB3eB3Wepz3Ku2z8l9UgPnyQ==", + "dev": true, + "optional": true + }, + "@swc/core-freebsd-x64": { + "version": "1.2.224", + "resolved": "https://registry.npmjs.org/@swc/core-freebsd-x64/-/core-freebsd-x64-1.2.224.tgz", + "integrity": "sha512-bVIXwwjYA1ZdMKTwrAosxG808nQqvHhzs/fOKUrl1VeZ9CTHA0FzpSXwaBf1shbUVKvQVPKydY0K5q9dkSUkpw==", + "dev": true, + "optional": true, + "requires": { + "@swc/wasm": "1.2.130" + }, + "dependencies": { + "@swc/wasm": { + "version": "1.2.130", + "resolved": "https://registry.npmjs.org/@swc/wasm/-/wasm-1.2.130.tgz", + "integrity": "sha512-rNcJsBxS70+pv8YUWwf5fRlWX6JoY/HJc25HD/F8m6Kv7XhJdqPPMhyX6TKkUBPAG7TWlZYoxa+rHAjPy4Cj3Q==", + "dev": true, + "optional": true + } + } + }, + "@swc/core-linux-arm-gnueabihf": { + "version": "1.2.224", + "resolved": "https://registry.npmjs.org/@swc/core-linux-arm-gnueabihf/-/core-linux-arm-gnueabihf-1.2.224.tgz", + "integrity": "sha512-MY3UQI3IOjME5TpuIhwI0lqKHsU3x7BwlU4n/mLNe2lH3DLaP0rrGHCMI2iJqfiYKU1Rg1r01HXmysuiriF1TA==", + "dev": true, + "optional": true, + "requires": { + "@swc/wasm": "1.2.130" + }, + "dependencies": { + "@swc/wasm": { + "version": "1.2.130", + "resolved": "https://registry.npmjs.org/@swc/wasm/-/wasm-1.2.130.tgz", + "integrity": "sha512-rNcJsBxS70+pv8YUWwf5fRlWX6JoY/HJc25HD/F8m6Kv7XhJdqPPMhyX6TKkUBPAG7TWlZYoxa+rHAjPy4Cj3Q==", + "dev": true, + "optional": true + } + } + }, + "@swc/core-linux-arm64-gnu": { + "version": "1.2.224", + "resolved": "https://registry.npmjs.org/@swc/core-linux-arm64-gnu/-/core-linux-arm64-gnu-1.2.224.tgz", + "integrity": "sha512-tEpaGq4wp79tR+4s0h8xbzcxW6tZOfb2/Jf1vrmRYfRBqryBRTdtn14Rfy4qlK3s/SC3NfI2MKxZRgcGnMCeWw==", + "dev": true, + "optional": true + }, + "@swc/core-linux-arm64-musl": { + "version": "1.2.224", + "resolved": "https://registry.npmjs.org/@swc/core-linux-arm64-musl/-/core-linux-arm64-musl-1.2.224.tgz", + "integrity": "sha512-jnrYqXc7aRzBnEqEp3nAi9tjuUhBnN0pSKiHJytlBP1QkXnH7HD44Da9udmKUFYB5hHpwaXE0NIh7jK0nSMnhw==", + "dev": true, + "optional": true + }, + "@swc/core-linux-x64-gnu": { + "version": "1.2.224", + "resolved": "https://registry.npmjs.org/@swc/core-linux-x64-gnu/-/core-linux-x64-gnu-1.2.224.tgz", + "integrity": "sha512-UBkeDlG+PrIXDH1sR4EIXN5qK4a677IHb6RBghbvBDJS61X9/nTDxtCF7/zCqDxJRahhUrT6lDsYpuLCws2hiw==", + "dev": true, + "optional": true + }, + "@swc/core-linux-x64-musl": { + "version": "1.2.224", + "resolved": "https://registry.npmjs.org/@swc/core-linux-x64-musl/-/core-linux-x64-musl-1.2.224.tgz", + "integrity": "sha512-CQMGDzxKvkzf6TOdaWnmhb6uk1XEhM/mM3BDfX+hx9j3Hg3bFw9qmPvrkoWI2G8J50MvpoR1iPBYyG2LNeQWeg==", + "dev": true, + "optional": true + }, + "@swc/core-win32-arm64-msvc": { + "version": "1.2.224", + "resolved": "https://registry.npmjs.org/@swc/core-win32-arm64-msvc/-/core-win32-arm64-msvc-1.2.224.tgz", + "integrity": "sha512-03V4apubsOhLKQNmfWGlgvDCJkhlh0ZOHcGddxb7bD4PeP6U0lnABG3hlz2uicwcIGBPu/p7jtm5/hezeiXE6Q==", + "dev": true, + "optional": true, + "requires": { + "@swc/wasm": "1.2.130" + }, + "dependencies": { + "@swc/wasm": { + "version": "1.2.130", + "resolved": "https://registry.npmjs.org/@swc/wasm/-/wasm-1.2.130.tgz", + "integrity": "sha512-rNcJsBxS70+pv8YUWwf5fRlWX6JoY/HJc25HD/F8m6Kv7XhJdqPPMhyX6TKkUBPAG7TWlZYoxa+rHAjPy4Cj3Q==", + "dev": true, + "optional": true + } + } + }, + "@swc/core-win32-ia32-msvc": { + "version": "1.2.224", + "resolved": "https://registry.npmjs.org/@swc/core-win32-ia32-msvc/-/core-win32-ia32-msvc-1.2.224.tgz", + "integrity": "sha512-gPOmIdhCwkb5BbDiPs/4t1LIElFLoQwiv45XwK5wADh1qzLD3A8EtpnpXfLsjL/fUMlLIGCgHQ6BQ0x04VrI1Q==", + "dev": true, + "optional": true, + "requires": { + "@swc/wasm": "1.2.130" + }, + "dependencies": { + "@swc/wasm": { + "version": "1.2.130", + "resolved": "https://registry.npmjs.org/@swc/wasm/-/wasm-1.2.130.tgz", + "integrity": "sha512-rNcJsBxS70+pv8YUWwf5fRlWX6JoY/HJc25HD/F8m6Kv7XhJdqPPMhyX6TKkUBPAG7TWlZYoxa+rHAjPy4Cj3Q==", + "dev": true, + "optional": true + } + } + }, + "@swc/core-win32-x64-msvc": { + "version": "1.2.224", + "resolved": "https://registry.npmjs.org/@swc/core-win32-x64-msvc/-/core-win32-x64-msvc-1.2.224.tgz", + "integrity": "sha512-xALNoKi/UAQ2NflIkCBaZ4Nib75a4YwFp2pZ5Yen3vR8hxK2UJYlaNwyfsBwWfMOMsNxQ1Q1aaqoo0L+XON53Q==", + "dev": true, + "optional": true + }, + "@swc/wasm": { + "version": "1.2.122", + "resolved": "https://registry.npmjs.org/@swc/wasm/-/wasm-1.2.122.tgz", + "integrity": "sha512-sM1VCWQxmNhFtdxME+8UXNyPNhxNu7zdb6ikWpz0YKAQQFRGT5ThZgJrubEpah335SUToNg8pkdDF7ibVCjxbQ==", + "dev": true, + "optional": true + }, "@szmarczak/http-timer": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/@szmarczak/http-timer/-/http-timer-1.1.2.tgz", @@ -32753,6 +33341,12 @@ } } }, + "joycon": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/joycon/-/joycon-3.1.1.tgz", + "integrity": "sha512-34wB/Y7MW7bzjKRjUKTa46I2Z7eV62Rkhva+KkopW7Qvv/OSWBqvkSY7vusOPrNuZcUG3tApvdVgNB8POj3SPw==", + "dev": true + }, "js-cleanup": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/js-cleanup/-/js-cleanup-1.2.0.tgz", @@ -36825,6 +37419,75 @@ } } }, + "rollup-plugin-swc3": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/rollup-plugin-swc3/-/rollup-plugin-swc3-0.3.0.tgz", + "integrity": "sha512-ZQK2XxYxSspmT8j6/Y4CaxRxAlZHbNnxI+m+yJ5I87ZLp5uH7CYL4hFlJk1jkcZ+Q2QC19jIg7AClB/7+XFljw==", + "dev": true, + "requires": { + "@rollup/pluginutils": "^4.2.1", + "deepmerge": "^4.2.2", + "joycon": "^3.1.1", + "jsonc-parser": "^3.0.0", + "typedoc": "^0.22.15" + }, + "dependencies": { + "@rollup/pluginutils": { + "version": "4.2.1", + "resolved": "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-4.2.1.tgz", + "integrity": "sha512-iKnFXr7NkdZAIHiIWE+BX5ULi/ucVFYWD6TbAV+rZctiRTY2PL6tsIKhoIOaoskiWAkgu+VsbXgUVDNLHf+InQ==", + "dev": true, + "requires": { + "estree-walker": "^2.0.1", + "picomatch": "^2.2.2" + } + }, + "brace-expansion": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz", + "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==", + "dev": true, + "requires": { + "balanced-match": "^1.0.0" + } + }, + "glob": { + "version": "8.0.3", + "resolved": "https://registry.npmjs.org/glob/-/glob-8.0.3.tgz", + "integrity": "sha512-ull455NHSHI/Y1FqGaaYFaLGkNMMJbavMrEGFXG/PGrg6y7sutWHUHrz6gy6WEBH6akM1M414dWKCNs+IhKdiQ==", + "dev": true, + "requires": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^5.0.1", + "once": "^1.3.0" + } + }, + "minimatch": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-5.1.0.tgz", + "integrity": "sha512-9TPBGGak4nHfGZsPBohm9AWg6NoT7QTCehS3BIJABslyZbzxfV78QM2Y6+i741OPZIafFAaiiEMh5OyIrJPgtg==", + "dev": true, + "requires": { + "brace-expansion": "^2.0.1" + } + }, + "typedoc": { + "version": "0.22.18", + "resolved": "https://registry.npmjs.org/typedoc/-/typedoc-0.22.18.tgz", + "integrity": "sha512-NK9RlLhRUGMvc6Rw5USEYgT4DVAUFk7IF7Q6MYfpJ88KnTZP7EneEa4RcP+tX1auAcz7QT1Iy0bUSZBYYHdoyA==", + "dev": true, + "requires": { + "glob": "^8.0.3", + "lunr": "^2.3.9", + "marked": "^4.0.16", + "minimatch": "^5.1.0", + "shiki": "^0.10.1" + } + } + } + }, "rollup-plugin-terser": { "version": "7.0.2", "resolved": "https://registry.npmjs.org/rollup-plugin-terser/-/rollup-plugin-terser-7.0.2.tgz", diff --git a/package.json b/package.json index 52559702a..e983352bd 100644 --- a/package.json +++ b/package.json @@ -9,15 +9,15 @@ "./auto/auto.js", "./dist/chart.umd.js" ], - "jsdelivr": "dist/chart.umd.js", - "unpkg": "dist/chart.umd.js", - "main": "dist/chart.js", + "jsdelivr": "./dist/chart.umd.js", + "unpkg": "./dist/chart.umd.js", + "main": "./dist/chart.js", "exports": { ".": "./dist/chart.js", "./auto": "./auto/auto.js", "./helpers": "./helpers/helpers.js" }, - "types": "types/index.d.ts", + "types": "./dist/types.d.ts", "keywords": [ "canvas", "charts", @@ -41,14 +41,14 @@ ], "scripts": { "autobuild": "rollup -c -w", - "build": "rollup -c", + "emitDeclarations": "tsc --emitDeclarationOnly", + "build": "rollup -c && npm run emitDeclarations", "dev": "karma start ./karma.conf.cjs --auto-watch --no-single-run --browsers chrome --grep", "dev:ff": "karma start ./karma.conf.cjs --auto-watch --no-single-run --browsers firefox --grep", "docs": "npm run build && vuepress build docs --no-cache", "docs:dev": "npm run build && vuepress dev docs --no-cache", - "lint-js": "eslint \"src/**/*.js\" \"test/**/*.js\" \"docs/**/*.js\"", + "lint-js": "eslint \"src/**/*.{js,ts}\" \"test/**/*.js\" \"docs/**/*.js\"", "lint-md": "eslint \"**/*.md\"", - "lint-tsc": "tsc", "lint-types": "eslint \"types/**/*.ts\" && npm run build && node types/tests/autogen.js && tsc -p types/tests/", "lint": "concurrently \"npm:lint-*\"", "test-size": "size-limit", @@ -65,6 +65,7 @@ "@rollup/plugin-node-resolve": "^13.3.0", "@simonbrunel/vuepress-plugin-versions": "^0.2.0", "@size-limit/preset-big-lib": "^8.0.0", + "@swc/core": "^1.2.224", "@types/estree": "^1.0.0", "@types/offscreencanvas": "^2019.7.0", "@typescript-eslint/eslint-plugin": "^5.32.0", @@ -105,6 +106,7 @@ "rollup": "^2.77.2", "rollup-plugin-cleanup": "^3.2.1", "rollup-plugin-istanbul": "^3.0.0", + "rollup-plugin-swc3": "^0.3.0", "rollup-plugin-terser": "^7.0.2", "size-limit": "^8.0.0", "typedoc": "^0.23.10", diff --git a/rollup.config.js b/rollup.config.js index 974121260..b6ab1e826 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,8 +1,9 @@ import cleanup from 'rollup-plugin-cleanup'; import json from '@rollup/plugin-json'; import resolve from '@rollup/plugin-node-resolve'; +import {swc} from 'rollup-plugin-swc3'; import {terser} from 'rollup-plugin-terser'; -import { readFileSync } from "fs"; +import {readFileSync} from 'fs'; const {version, homepage} = JSON.parse(readFileSync('./package.json')); @@ -12,21 +13,42 @@ const banner = `/*! * (c) ${(new Date(process.env.SOURCE_DATE_EPOCH ? (process.env.SOURCE_DATE_EPOCH * 1000) : new Date().getTime())).getFullYear()} Chart.js Contributors * Released under the MIT License */`; +const extensions = ['.js', '.ts']; +const plugins = (minify) => + [ + json(), + resolve({ + extensions + }), + swc({ + jsc: { + parser: { + syntax: 'typescript' + }, + target: 'es2022' + }, + module: { + type: 'es6' + }, + sourceMaps: true + }), + minify + ? terser({ + output: { + preamble: banner + } + }) + : cleanup({ + comments: ['some', /__PURE__/] + }) + ]; export default [ // UMD build // dist/chart.umd.js { - input: 'src/index.umd.js', - plugins: [ - json(), - resolve(), - terser({ - output: { - preamble: banner - } - }), - ], + input: 'src/index.umd.ts', + plugins: plugins(true), output: { name: 'Chart', file: 'dist/chart.umd.js', @@ -41,16 +63,10 @@ export default [ // helpers/*.js { input: { - 'dist/chart': 'src/index.js', - 'dist/helpers': 'src/helpers/index.js' + 'dist/chart': 'src/index.ts', + 'dist/helpers': 'src/helpers/index.ts' }, - plugins: [ - json(), - resolve(), - cleanup({ - comments: ['some', /__PURE__/], - }), - ], + plugins: plugins(), output: { dir: './', chunkFileNames: 'dist/chunks/[name].js', diff --git a/src/core/core.adapters.js b/src/core/core.adapters.js deleted file mode 100644 index 0859ea04d..000000000 --- a/src/core/core.adapters.js +++ /dev/null @@ -1,125 +0,0 @@ -/** - * @namespace Chart._adapters - * @since 2.8.0 - * @private - */ - -/** - * @typedef { import("../../types").ChartOptions } ChartOptions - */ - -/** - * @return {*} - */ -function abstract() { - throw new Error('This method is not implemented: Check that a complete date adapter is provided.'); -} - -/** - * Date adapter (current used by the time scale) - * @namespace Chart._adapters._date - * @memberof Chart._adapters - * @private - */ - -/** - * Currently supported unit string values. - * @typedef {('millisecond'|'second'|'minute'|'hour'|'day'|'week'|'month'|'quarter'|'year')} Unit - * @memberof Chart._adapters._date - */ - -export class DateAdapter { - - static override(members) { - Object.assign(DateAdapter.prototype, members); - } - - constructor(options) { - this.options = options || {}; - } - - /** - * Will called with chart options after adapter creation. - * @param {ChartOptions} chartOptions - */ - // eslint-disable-next-line no-unused-vars - init(chartOptions) {} - - /** - * Returns a map of time formats for the supported formatting units defined - * in Unit as well as 'datetime' representing a detailed date/time string. - * @returns {{string: string}} - */ - formats() { - return abstract(); - } - - /** - * Parses the given `value` and return the associated timestamp. - * @param {any} value - the value to parse (usually comes from the data) - * @param {string} [format] - the expected data format - * @returns {(number|null)} - */ - parse(value, format) { // eslint-disable-line no-unused-vars - return abstract(); - } - - /** - * Returns the formatted date in the specified `format` for a given `timestamp`. - * @param {number} timestamp - the timestamp to format - * @param {string} format - the date/time token - * @return {string} - */ - format(timestamp, format) { // eslint-disable-line no-unused-vars - return abstract(); - } - - /** - * Adds the specified `amount` of `unit` to the given `timestamp`. - * @param {number} timestamp - the input timestamp - * @param {number} amount - the amount to add - * @param {Unit} unit - the unit as string - * @return {number} - */ - add(timestamp, amount, unit) { // eslint-disable-line no-unused-vars - return abstract(); - } - - /** - * Returns the number of `unit` between the given timestamps. - * @param {number} a - the input timestamp (reference) - * @param {number} b - the timestamp to subtract - * @param {Unit} unit - the unit as string - * @return {number} - */ - diff(a, b, unit) { // eslint-disable-line no-unused-vars - return abstract(); - } - - /** - * Returns start of `unit` for the given `timestamp`. - * @param {number} timestamp - the input timestamp - * @param {Unit|'isoWeek'} unit - the unit as string - * @param {number} [weekday] - the ISO day of the week with 1 being Monday - * and 7 being Sunday (only needed if param *unit* is `isoWeek`). - * @return {number} - */ - startOf(timestamp, unit, weekday) { // eslint-disable-line no-unused-vars - return abstract(); - } - - /** - * Returns end of `unit` for the given `timestamp`. - * @param {number} timestamp - the input timestamp - * @param {Unit|'isoWeek'} unit - the unit as string - * @return {number} - */ - endOf(timestamp, unit) { // eslint-disable-line no-unused-vars - return abstract(); - } - -} - -export default { - _date: DateAdapter -}; diff --git a/src/core/core.adapters.ts b/src/core/core.adapters.ts new file mode 100644 index 000000000..6132b46b7 --- /dev/null +++ b/src/core/core.adapters.ts @@ -0,0 +1,133 @@ +/** + * @namespace Chart._adapters + * @since 2.8.0 + * @private + */ + +import type {AnyObject} from '../../types/basic'; +import type {ChartOptions} from '../../types'; + +export type TimeUnit = 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year'; + +export interface DateAdapter { + readonly options: T; + /** + * Will called with chart options after adapter creation. + */ + init(this: DateAdapter, chartOptions: ChartOptions): void; + /** + * Returns a map of time formats for the supported formatting units defined + * in Unit as well as 'datetime' representing a detailed date/time string. + */ + formats(this: DateAdapter): Record; + /** + * Parses the given `value` and return the associated timestamp. + * @param value - the value to parse (usually comes from the data) + * @param [format] - the expected data format + */ + parse(this: DateAdapter, value: unknown, format?: TimeUnit): number | null; + /** + * Returns the formatted date in the specified `format` for a given `timestamp`. + * @param timestamp - the timestamp to format + * @param format - the date/time token + */ + format(this: DateAdapter, timestamp: number, format: TimeUnit): string; + /** + * Adds the specified `amount` of `unit` to the given `timestamp`. + * @param timestamp - the input timestamp + * @param amount - the amount to add + * @param unit - the unit as string + */ + add(this: DateAdapter, timestamp: number, amount: number, unit: TimeUnit): number; + /** + * Returns the number of `unit` between the given timestamps. + * @param a - the input timestamp (reference) + * @param b - the timestamp to subtract + * @param unit - the unit as string + */ + diff(this: DateAdapter, a: number, b: number, unit: TimeUnit): number; + /** + * Returns start of `unit` for the given `timestamp`. + * @param timestamp - the input timestamp + * @param unit - the unit as string + * @param [weekday] - the ISO day of the week with 1 being Monday + * and 7 being Sunday (only needed if param *unit* is `isoWeek`). + */ + startOf(this: DateAdapter, timestamp: number, unit: TimeUnit | 'isoWeek', weekday?: number): number; + /** + * Returns end of `unit` for the given `timestamp`. + * @param timestamp - the input timestamp + * @param unit - the unit as string + */ + endOf(this: DateAdapter, timestamp: number, unit: TimeUnit | 'isoWeek'): number; +} + +function abstract(): T { + throw new Error('This method is not implemented: Check that a complete date adapter is provided.'); +} + +/** + * Date adapter (current used by the time scale) + * @namespace Chart._adapters._date + * @memberof Chart._adapters + * @private + */ +class DateAdapterBase implements DateAdapter { + + /** + * Override default date adapter methods. + * Accepts type parameter to define options type. + * @example + * Chart._adapters._date.override<{myAdapterOption: string}>({ + * init() { + * console.log(this.options.myAdapterOption); + * } + * }) + */ + static override( + members: Partial, 'options'>> + ) { + Object.assign(DateAdapterBase.prototype, members); + } + + readonly options: AnyObject; + + constructor(options: AnyObject) { + this.options = options || {}; + } + + // eslint-disable-next-line @typescript-eslint/no-empty-function + init() {} + + formats(): Record { + return abstract(); + } + + parse(): number | null { + return abstract(); + } + + format(): string { + return abstract(); + } + + add(): number { + return abstract(); + } + + diff(): number { + return abstract(); + } + + startOf(): number { + return abstract(); + } + + endOf(): number { + return abstract(); + } +} + +export default { + _date: DateAdapterBase +}; diff --git a/src/core/core.element.js b/src/core/core.element.js deleted file mode 100644 index d3fe490d8..000000000 --- a/src/core/core.element.js +++ /dev/null @@ -1,53 +0,0 @@ -import {isNumber} from '../helpers/helpers.math'; - -export default class Element { - - /** - * @type any - */ - static defaults = {}; - - /** - * @type any - */ - static defaultRoutes = undefined; - - constructor() { - this.x = undefined; - this.y = undefined; - this.active = false; - this.options = undefined; - this.$animations = undefined; - } - - /** - * @param {boolean} [useFinalPosition] - */ - tooltipPosition(useFinalPosition) { - const {x, y} = this.getProps(['x', 'y'], useFinalPosition); - return {x, y}; - } - - hasValue() { - return isNumber(this.x) && isNumber(this.y); - } - - /** - * Gets the current or final value of each prop. Can return extra properties (whole object). - * @param {string[]} props - properties to get - * @param {boolean} [final] - get the final value (animation target) - * @return {object} - */ - getProps(props, final) { - const anims = this.$animations; - if (!final || !anims) { - // let's not create an object, if not needed - return this; - } - const ret = {}; - props.forEach(prop => { - ret[prop] = anims[prop] && anims[prop].active() ? anims[prop]._to : this[prop]; - }); - return ret; - } -} diff --git a/src/core/core.element.ts b/src/core/core.element.ts new file mode 100644 index 000000000..b42df0892 --- /dev/null +++ b/src/core/core.element.ts @@ -0,0 +1,43 @@ +import type {AnyObject} from '../../types/basic'; +import type {Point} from '../../types/geometric'; +import type {Animation} from '../../types/animation'; +import {isNumber} from '../helpers/helpers.math'; + +export default class Element { + + static defaults = {}; + static defaultRoutes = undefined; + + x: number; + y: number; + active = false; + options: O; + $animations: Record; + + tooltipPosition(useFinalPosition: boolean): Point { + const {x, y} = this.getProps(['x', 'y'], useFinalPosition); + return {x, y} as Point; + } + + hasValue() { + return isNumber(this.x) && isNumber(this.y); + } + + /** + * Gets the current or final value of each prop. Can return extra properties (whole object). + * @param props - properties to get + * @param [final] - get the final value (animation target) + */ + getProps

(props: P, final?: boolean): Pick { + const anims = this.$animations; + if (!final || !anims) { + // let's not create an object, if not needed + return this as Pick; + } + const ret: Partial> = {}; + props.forEach((prop) => { + ret[prop] = anims[prop] && anims[prop].active() ? anims[prop]._to : this[prop as string]; + }); + return ret as Pick; + } +} diff --git a/src/core/core.ticks.js b/src/core/core.ticks.js index f054ef66a..333a7306e 100644 --- a/src/core/core.ticks.js +++ b/src/core/core.ticks.js @@ -14,7 +14,7 @@ const formatters = { * @return {string|string[]} the label to display */ values(value) { - return isArray(value) ? value : '' + value; + return isArray(value) ? /** @type {string[]} */ (value) : '' + value; }, /** diff --git a/src/core/index.js b/src/core/index.ts similarity index 92% rename from src/core/index.js rename to src/core/index.ts index af0b30963..706884b05 100644 --- a/src/core/index.js +++ b/src/core/index.ts @@ -1,3 +1,4 @@ +export type {DateAdapter, TimeUnit} from './core.adapters'; export {default as _adapters} from './core.adapters'; export {default as Animation} from './core.animation'; export {default as Animations} from './core.animations'; diff --git a/src/elements/element.arc.js b/src/elements/element.arc.js index f9327d72a..42ca16738 100644 --- a/src/elements/element.arc.js +++ b/src/elements/element.arc.js @@ -3,6 +3,8 @@ import {_angleBetween, getAngleFromPoint, TAU, HALF_PI, valueOrDefault} from '.. import {PI, _isBetween, _limitValue} from '../helpers/helpers.math'; import {_readValueToProps} from '../helpers/helpers.options'; +/** @typedef {{ x: number, y: number, startAngle: number, endAngle: number, innerRadius: number, outerRadius: number, circumference: number }} ArcProps */ + function clipArc(ctx, element, endAngle) { const {startAngle, pixelMargin, x, y, outerRadius, innerRadius} = element; let angleMargin = pixelMargin / outerRadius; @@ -310,13 +312,13 @@ export default class ArcElement extends Element { inRange(chartX, chartY, useFinalPosition) { const point = this.getProps(['x', 'y'], useFinalPosition); const {angle, distance} = getAngleFromPoint(point, {x: chartX, y: chartY}); - const {startAngle, endAngle, innerRadius, outerRadius, circumference} = this.getProps([ + const {startAngle, endAngle, innerRadius, outerRadius, circumference} = /** @type {ArcProps} */ (this.getProps([ 'startAngle', 'endAngle', 'innerRadius', 'outerRadius', 'circumference' - ], useFinalPosition); + ], useFinalPosition)); const rAdjust = this.options.spacing / 2; const _circumference = valueOrDefault(circumference, endAngle - startAngle); const betweenAngles = _circumference >= TAU || _angleBetween(angle, startAngle, endAngle); @@ -329,7 +331,7 @@ export default class ArcElement extends Element { * @param {boolean} [useFinalPosition] */ getCenterPoint(useFinalPosition) { - const {x, y, startAngle, endAngle, innerRadius, outerRadius} = this.getProps([ + const {x, y, startAngle, endAngle, innerRadius, outerRadius} = /** @type {ArcProps} */ (this.getProps([ 'x', 'y', 'startAngle', @@ -337,7 +339,7 @@ export default class ArcElement extends Element { 'innerRadius', 'outerRadius', 'circumference', - ], useFinalPosition); + ], useFinalPosition)); const {offset, spacing} = this.options; const halfAngle = (startAngle + endAngle) / 2; const halfRadius = (innerRadius + outerRadius + spacing + offset) / 2; diff --git a/src/elements/element.bar.js b/src/elements/element.bar.js index c70f2d84f..5f2f75150 100644 --- a/src/elements/element.bar.js +++ b/src/elements/element.bar.js @@ -3,6 +3,8 @@ import {isObject, _isBetween, _limitValue} from '../helpers'; import {addRoundedRectPath} from '../helpers/helpers.canvas'; import {toTRBL, toTRBLCorners} from '../helpers/helpers.options'; +/** @typedef {{ x: number, y: number, base: number, horizontal: boolean, width: number, height: number }} BarProps */ + /** * Helper function to get the bounds of the bar regardless of the orientation * @param {BarElement} bar the bar @@ -11,7 +13,7 @@ import {toTRBL, toTRBLCorners} from '../helpers/helpers.options'; * @private */ function getBarBounds(bar, useFinalPosition) { - const {x, y, base, width, height} = bar.getProps(['x', 'y', 'base', 'width', 'height'], useFinalPosition); + const {x, y, base, width, height} = /** @type {BarProps} */ (bar.getProps(['x', 'y', 'base', 'width', 'height'], useFinalPosition)); let left, right, top, bottom, half; @@ -211,7 +213,7 @@ export default class BarElement extends Element { } getCenterPoint(useFinalPosition) { - const {x, y, base, horizontal} = this.getProps(['x', 'y', 'base', 'horizontal'], useFinalPosition); + const {x, y, base, horizontal} = /** @type {BarProps} */ (this.getProps(['x', 'y', 'base', 'horizontal'], useFinalPosition)); return { x: horizontal ? (x + base) / 2 : x, y: horizontal ? y : (y + base) / 2 diff --git a/src/elements/element.point.js b/src/elements/element.point.js index 8eb9f48ec..b651d65b1 100644 --- a/src/elements/element.point.js +++ b/src/elements/element.point.js @@ -48,7 +48,7 @@ export default class PointElement extends Element { inRange(mouseX, mouseY, useFinalPosition) { const options = this.options; - const {x, y} = this.getProps(['x', 'y'], useFinalPosition); + const {x, y} = /** @type {{ x: number, y: number }} */ (this.getProps(['x', 'y'], useFinalPosition)); return ((Math.pow(mouseX - x, 2) + Math.pow(mouseY - y, 2)) < Math.pow(options.hitRadius + options.radius, 2)); } diff --git a/src/helpers/helpers.core.js b/src/helpers/helpers.core.ts similarity index 51% rename from src/helpers/helpers.core.js rename to src/helpers/helpers.core.ts index 1b1d1ff78..304394c47 100644 --- a/src/helpers/helpers.core.js +++ b/src/helpers/helpers.core.ts @@ -1,40 +1,40 @@ /** * @namespace Chart.helpers */ + +import type {AnyObject} from '../../types/basic'; +import type {ActiveDataPoint, ChartEvent} from '../../types'; + /** * An empty function that can be used, for example, for optional callback. */ -export function noop() {} +export function noop() { + /* noop */ +} /** * Returns a unique id, sequentially generated from a global variable. - * @returns {number} - * @function */ -export const uid = (function() { +export const uid = (() => { let id = 0; - return function() { - return id++; - }; -}()); + return () => id++; +})(); /** * Returns true if `value` is neither null nor undefined, else returns false. - * @param {*} value - The value to test. - * @returns {boolean} + * @param value - The value to test. * @since 2.7.0 */ -export function isNullOrUndef(value) { +export function isNullOrUndef(value: unknown): value is null | undefined { return value === null || typeof value === 'undefined'; } /** * Returns true if `value` is an array (including typed arrays), else returns false. - * @param {*} value - The value to test. - * @returns {boolean} + * @param value - The value to test. * @function */ -export function isArray(value) { +export function isArray(value: unknown): value is T[] { if (Array.isArray && Array.isArray(value)) { return true; } @@ -47,50 +47,48 @@ export function isArray(value) { /** * Returns true if `value` is an object (excluding null), else returns false. - * @param {*} value - The value to test. - * @returns {boolean} + * @param value - The value to test. * @since 2.7.0 */ -export function isObject(value) { +export function isObject(value: unknown): value is AnyObject { return value !== null && Object.prototype.toString.call(value) === '[object Object]'; } /** * Returns true if `value` is a finite number, else returns false - * @param {*} value - The value to test. - * @returns {boolean} + * @param value - The value to test. */ -const isNumberFinite = (value) => (typeof value === 'number' || value instanceof Number) && isFinite(+value); +function isNumberFinite(value: unknown): value is number { + return (typeof value === 'number' || value instanceof Number) && isFinite(+value); +} export { isNumberFinite as isFinite, }; /** * Returns `value` if finite, else returns `defaultValue`. - * @param {*} value - The value to return if defined. - * @param {*} defaultValue - The value to return if `value` is not finite. - * @returns {*} + * @param value - The value to return if defined. + * @param defaultValue - The value to return if `value` is not finite. */ -export function finiteOrDefault(value, defaultValue) { +export function finiteOrDefault(value: unknown, defaultValue: number) { return isNumberFinite(value) ? value : defaultValue; } /** * Returns `value` if defined, else returns `defaultValue`. - * @param {*} value - The value to return if defined. - * @param {*} defaultValue - The value to return if `value` is undefined. - * @returns {*} + * @param value - The value to return if defined. + * @param defaultValue - The value to return if `value` is undefined. */ -export function valueOrDefault(value, defaultValue) { +export function valueOrDefault(value: T | undefined, defaultValue: T) { return typeof value === 'undefined' ? defaultValue : value; } -export const toPercentage = (value, dimension) => +export const toPercentage = (value: number | string, dimension: number) => typeof value === 'string' && value.endsWith('%') ? parseFloat(value) / 100 - : value / dimension; + : +value / dimension; -export const toDimension = (value, dimension) => +export const toDimension = (value: number | string, dimension: number) => typeof value === 'string' && value.endsWith('%') ? parseFloat(value) / 100 * dimension : +value; @@ -98,12 +96,15 @@ export const toDimension = (value, dimension) => /** * Calls `fn` with the given `args` in the scope defined by `thisArg` and returns the * value returned by `fn`. If `fn` is not a function, this method returns undefined. - * @param {function} fn - The function to call. - * @param {Array|undefined|null} args - The arguments with which `fn` should be called. - * @param {object} [thisArg] - The value of `this` provided for the call to `fn`. - * @returns {*} + * @param fn - The function to call. + * @param args - The arguments with which `fn` should be called. + * @param [thisArg] - The value of `this` provided for the call to `fn`. */ -export function callback(fn, args, thisArg) { +export function callback R, TA, R>( + fn: T | undefined, + args: unknown[], + thisArg?: TA +): R | undefined { if (fn && typeof fn.call === 'function') { return fn.apply(thisArg, args); } @@ -113,13 +114,30 @@ export function callback(fn, args, thisArg) { * Note(SB) for performance sake, this method should only be used when loopable type * is unknown or in none intensive code (not called often and small loopable). Else * it's preferable to use a regular for() loop and save extra function calls. - * @param {object|Array} loopable - The object or array to be iterated. - * @param {function} fn - The function to call for each item. - * @param {object} [thisArg] - The value of `this` provided for the call to `fn`. - * @param {boolean} [reverse] - If true, iterates backward on the loopable. + * @param loopable - The object or array to be iterated. + * @param fn - The function to call for each item. + * @param [thisArg] - The value of `this` provided for the call to `fn`. + * @param [reverse] - If true, iterates backward on the loopable. */ -export function each(loopable, fn, thisArg, reverse) { - let i, len, keys; +export function each( + loopable: Record, + fn: (this: TA, v: T, i: string) => void, + thisArg?: TA, + reverse?: boolean +): void; +export function each( + loopable: T[], + fn: (this: TA, v: T, i: number) => void, + thisArg?: TA, + reverse?: boolean +): void; +export function each( + loopable: T[] | Record, + fn: (this: TA, v: T, i: any) => void, + thisArg?: TA, + reverse?: boolean +) { + let i: number, len: number, keys: string[]; if (isArray(loopable)) { len = loopable.length; if (reverse) { @@ -142,13 +160,12 @@ export function each(loopable, fn, thisArg, reverse) { /** * Returns true if the `a0` and `a1` arrays have the same content, else returns false. - * @param {Array} a0 - The array to compare - * @param {Array} a1 - The array to compare - * @returns {boolean} + * @param a0 - The array to compare + * @param a1 - The array to compare * @private */ -export function _elementsEqual(a0, a1) { - let i, ilen, v0, v1; +export function _elementsEqual(a0: ActiveDataPoint[], a1: ActiveDataPoint[]) { + let i: number, ilen: number, v0: ActiveDataPoint, v1: ActiveDataPoint; if (!a0 || !a1 || a0.length !== a1.length) { return false; @@ -168,12 +185,11 @@ export function _elementsEqual(a0, a1) { /** * Returns a deep copy of `source` without keeping references on objects and arrays. - * @param {*} source - The value to clone. - * @returns {*} + * @param source - The value to clone. */ -export function clone(source) { +export function clone(source: T): T { if (isArray(source)) { - return source.map(clone); + return source.map(clone) as unknown as T; } if (isObject(source)) { @@ -192,7 +208,7 @@ export function clone(source) { return source; } -function isValidKey(key) { +function isValidKey(key: string) { return ['__proto__', 'prototype', 'constructor'].indexOf(key) === -1; } @@ -201,7 +217,7 @@ function isValidKey(key) { * Note(SB): also used by mergeConfig and mergeScaleConfig as fallback. * @private */ -export function _merger(key, target, source, options) { +export function _merger(key: string, target: AnyObject, source: AnyObject, options: AnyObject) { if (!isValidKey(key)) { return; } @@ -210,42 +226,58 @@ export function _merger(key, target, source, options) { const sval = source[key]; if (isObject(tval) && isObject(sval)) { - // eslint-disable-next-line no-use-before-define + // eslint-disable-next-line @typescript-eslint/no-use-before-define merge(tval, sval, options); } else { target[key] = clone(sval); } } +export interface MergeOptions { + merger?: (key: string, target: AnyObject, source: AnyObject, options?: AnyObject) => void; +} + /** * Recursively deep copies `source` properties into `target` with the given `options`. * IMPORTANT: `target` is not cloned and will be updated with `source` properties. - * @param {object} target - The target object in which all sources are merged into. - * @param {object|object[]} source - Object(s) to merge into `target`. - * @param {object} [options] - Merging options: - * @param {function} [options.merger] - The merge method (key, target, source, options) - * @returns {object} The `target` object. + * @param target - The target object in which all sources are merged into. + * @param source - Object(s) to merge into `target`. + * @param [options] - Merging options: + * @param [options.merger] - The merge method (key, target, source, options) + * @returns The `target` object. */ -export function merge(target, source, options) { +export function merge(target: T, source: [], options?: MergeOptions): T; +export function merge(target: T, source: S1, options?: MergeOptions): T & S1; +export function merge(target: T, source: [S1], options?: MergeOptions): T & S1; +export function merge(target: T, source: [S1, S2], options?: MergeOptions): T & S1 & S2; +export function merge(target: T, source: [S1, S2, S3], options?: MergeOptions): T & S1 & S2 & S3; +export function merge( + target: T, + source: [S1, S2, S3, S4], + options?: MergeOptions +): T & S1 & S2 & S3 & S4; +export function merge(target: T, source: AnyObject[], options?: MergeOptions): AnyObject; +export function merge(target: T, source: AnyObject[], options?: MergeOptions): AnyObject { const sources = isArray(source) ? source : [source]; const ilen = sources.length; if (!isObject(target)) { - return target; + return target as AnyObject; } options = options || {}; const merger = options.merger || _merger; + let current: AnyObject; for (let i = 0; i < ilen; ++i) { - source = sources[i]; - if (!isObject(source)) { + current = sources[i]; + if (!isObject(current)) { continue; } - const keys = Object.keys(source); + const keys = Object.keys(current); for (let k = 0, klen = keys.length; k < klen; ++k) { - merger(keys[k], target, source, options); + merger(keys[k], target, current, options as AnyObject); } } @@ -255,20 +287,27 @@ export function merge(target, source, options) { /** * Recursively deep copies `source` properties into `target` *only* if not defined in target. * IMPORTANT: `target` is not cloned and will be updated with `source` properties. - * @param {object} target - The target object in which all sources are merged into. - * @param {object|object[]} source - Object(s) to merge into `target`. - * @returns {object} The `target` object. + * @param target - The target object in which all sources are merged into. + * @param source - Object(s) to merge into `target`. + * @returns The `target` object. */ -export function mergeIf(target, source) { - // eslint-disable-next-line no-use-before-define - return merge(target, source, {merger: _mergerIf}); +export function mergeIf(target: T, source: []): T; +export function mergeIf(target: T, source: S1): T & S1; +export function mergeIf(target: T, source: [S1]): T & S1; +export function mergeIf(target: T, source: [S1, S2]): T & S1 & S2; +export function mergeIf(target: T, source: [S1, S2, S3]): T & S1 & S2 & S3; +export function mergeIf(target: T, source: [S1, S2, S3, S4]): T & S1 & S2 & S3 & S4; +export function mergeIf(target: T, source: AnyObject[]): AnyObject; +export function mergeIf(target: T, source: AnyObject[]): AnyObject { + // eslint-disable-next-line @typescript-eslint/no-use-before-define + return merge(target, source, {merger: _mergerIf}); } /** * Merges source[key] in target[key] only if target[key] is undefined. * @private */ -export function _mergerIf(key, target, source) { +export function _mergerIf(key: string, target: AnyObject, source: AnyObject) { if (!isValidKey(key)) { return; } @@ -286,10 +325,10 @@ export function _mergerIf(key, target, source) { /** * @private */ -export function _deprecated(scope, value, previous, current) { +export function _deprecated(scope: string, value: unknown, previous: string, current: string) { if (value !== undefined) { console.warn(scope + ': "' + previous + - '" is deprecated. Please use "' + current + '" instead'); + '" is deprecated. Please use "' + current + '" instead'); } } @@ -302,12 +341,26 @@ const keyResolvers = { y: o => o.y }; -export function resolveObjectKey(obj, key) { - const resolver = keyResolvers[key] || (keyResolvers[key] = _getKeyResolver(key)); - return resolver(obj); +/** + * @private + */ +export function _splitKey(key: string) { + const parts = key.split('.'); + const keys: string[] = []; + let tmp = ''; + for (const part of parts) { + tmp += part; + if (tmp.endsWith('\\')) { + tmp = tmp.slice(0, -1) + '.'; + } else { + keys.push(tmp); + tmp = ''; + } + } + return keys; } -function _getKeyResolver(key) { +function _getKeyResolver(key: string) { const keys = _splitKey(key); return obj => { for (const k of keys) { @@ -322,39 +375,25 @@ function _getKeyResolver(key) { }; } -/** - * @private - */ -export function _splitKey(key) { - const parts = key.split('.'); - const keys = []; - let tmp = ''; - for (const part of parts) { - tmp += part; - if (tmp.endsWith('\\')) { - tmp = tmp.slice(0, -1) + '.'; - } else { - keys.push(tmp); - tmp = ''; - } - } - return keys; +export function resolveObjectKey(obj: AnyObject, key: string): AnyObject { + const resolver = keyResolvers[key] || (keyResolvers[key] = _getKeyResolver(key)); + return resolver(obj); } /** * @private */ -export function _capitalize(str) { +export function _capitalize(str: string) { return str.charAt(0).toUpperCase() + str.slice(1); } -export const defined = (value) => typeof value !== 'undefined'; +export const defined = (value: unknown) => typeof value !== 'undefined'; -export const isFunction = (value) => typeof value === 'function'; +export const isFunction = (value: unknown) => typeof value === 'function'; // Adapted from https://stackoverflow.com/questions/31128855/comparing-ecma6-sets-for-equality#31129384 -export const setsEqual = (a, b) => { +export const setsEqual = (a: Set, b: Set) => { if (a.size !== b.size) { return false; } @@ -369,10 +408,9 @@ export const setsEqual = (a, b) => { }; /** - * @param {import('../../types').ChartEvent} e - The event - * @returns {boolean} + * @param e - The event * @private */ -export function _isClickEvent(e) { +export function _isClickEvent(e: ChartEvent) { return e.type === 'mouseup' || e.type === 'click' || e.type === 'contextmenu'; } diff --git a/src/helpers/helpers.options.js b/src/helpers/helpers.options.js index 4c901c85d..67afade08 100644 --- a/src/helpers/helpers.options.js +++ b/src/helpers/helpers.options.js @@ -40,6 +40,10 @@ export function toLineHeight(value, size) { const numberOrZero = v => +v || 0; +/** + * @param {any} value + * @param {string[] | Record} props + */ export function _readValueToProps(value, props) { const ret = {}; const objProps = isObject(props); diff --git a/src/helpers/index.js b/src/helpers/index.ts similarity index 100% rename from src/helpers/index.js rename to src/helpers/index.ts diff --git a/src/helpers/types.ts b/src/helpers/types.ts new file mode 100644 index 000000000..55b6e31d9 --- /dev/null +++ b/src/helpers/types.ts @@ -0,0 +1,8 @@ +/** + * Temporary entry point of the types at the time of the transition. + * After transition done need to remove it in favor of index.ts + */ + +// export * from '.'; +export * from './helpers.core'; +export * from '../../types/helpers'; diff --git a/src/index.js b/src/index.ts similarity index 100% rename from src/index.js rename to src/index.ts diff --git a/src/index.umd.js b/src/index.umd.ts similarity index 93% rename from src/index.umd.js rename to src/index.umd.ts index d3539015b..98088228c 100644 --- a/src/index.umd.js +++ b/src/index.umd.ts @@ -1,3 +1,4 @@ +// eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-nocheck /** @@ -5,7 +6,7 @@ */ import Chart from './core/core.controller'; -import * as helpers from './helpers/index'; +import * as helpers from './helpers'; import _adapters from './core/core.adapters'; import Animation from './core/core.animation'; import animator from './core/core.animator'; @@ -50,3 +51,4 @@ if (typeof window !== 'undefined') { } export default Chart; + diff --git a/src/plugins/plugin.filler/filler.options.js b/src/plugins/plugin.filler/filler.options.js index 70b740d77..8277825c5 100644 --- a/src/plugins/plugin.filler/filler.options.js +++ b/src/plugins/plugin.filler/filler.options.js @@ -44,6 +44,7 @@ export function _resolveTarget(sources, index, propagate) { * @param {number} count */ export function _decodeFill(line, index, count) { + /** @type {string | {value: number}} */ const fill = parseFillOption(line); if (isObject(fill)) { diff --git a/src/plugins/plugin.tooltip.js b/src/plugins/plugin.tooltip.js index 29f6e05f1..a6df147a4 100644 --- a/src/plugins/plugin.tooltip.js +++ b/src/plugins/plugin.tooltip.js @@ -11,6 +11,7 @@ import {createContext, drawPoint} from '../helpers'; * @typedef { import("../platform/platform.base").Chart } Chart * @typedef { import("../../types").ChartEvent } ChartEvent * @typedef { import("../../types").ActiveElement } ActiveElement + * @typedef { import("../core/core.interaction").InteractionItem } InteractionItem */ const positioners = { @@ -1168,10 +1169,10 @@ export class Tooltip extends Element { /** * Helper for determining the active elements for event * @param {ChartEvent} e - The event to handle - * @param {Element[]} lastActive - Previously active elements + * @param {InteractionItem[]} lastActive - Previously active elements * @param {boolean} [replay] - This is a replayed event (from update) * @param {boolean} [inChartArea] - The event is inside chartArea - * @returns {Element[]} - Active elements + * @returns {InteractionItem[]} - Active elements * @private */ _getActiveElements(e, lastActive, replay, inChartArea) { diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index cbd230d0d..d783231d6 100644 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -5,8 +5,9 @@ import Scale from '../core/core.scale'; import {_arrayUnique, _filterBetween, _lookup} from '../helpers/helpers.collection'; /** - * @typedef { import("../core/core.adapters").Unit } Unit + * @typedef { import("../core/core.adapters").TimeUnit } Unit * @typedef {{common: boolean, size: number, steps?: number}} Interval + * @typedef { import("../core/core.adapters").DateAdapter } DateAdapter */ /** @@ -58,7 +59,7 @@ function parse(scale, input) { // Only parse if its not a timestamp already if (!isFinite(value)) { value = typeof parser === 'string' - ? adapter.parse(value, parser) + ? adapter.parse(value, /** @type {Unit} */ (parser)) : adapter.parse(value); } @@ -264,6 +265,7 @@ export default class TimeScale extends Scale { init(scaleOpts, opts) { const time = scaleOpts.time || (scaleOpts.time = {}); + /** @type {DateAdapter} */ const adapter = this._adapter = new adapters._date(scaleOpts.adapters.date); adapter.init(opts); diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 000000000..cf780e115 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,50 @@ +/** + * Temporary entry point of the types at the time of the transition. + * After transition done need to remove it in favor of index.ts + */ + +export * from '.'; +/** + * Explicitly re-exporting to resolve the ambiguity. + */ +export { + BarController, + BubbleController, + DoughnutController, + LineController, + PieController, + PolarAreaController, + RadarController, + ScatterController, + Animation, + Animations, + Chart, + DatasetController, + Interaction, + Scale, + Ticks, + defaults, + layouts, + registry, + ArcElement, + BarElement, + LineElement, + PointElement, + BasePlatform, + BasicPlatform, + DomPlatform, + Decimation, + Filler, + Legend, + SubTitle, + Title, + Tooltip, + CategoryScale, + LinearScale, + LogarithmicScale, + RadialLinearScale, + TimeScale, + TimeSeriesScale, + registerables +} from '../types'; +export * from '../types'; diff --git a/tsconfig.json b/tsconfig.json index 9a2e9d66b..ad54c85ea 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,16 +1,31 @@ { "compilerOptions": { - "target": "ES6", - "moduleResolution": "Node", - "allowSyntheticDefaultImports": true, - "allowJs": true, - "checkJs": true, - "noEmit": true, + /* Type Checking */ "alwaysStrict": true, "strictBindCallApply": true, "strictFunctionTypes": true, + /* todo: uncomment after transition to TS */ + // "noFallthroughCasesInSwitch": true, + // "noImplicitOverride": true, + // "noImplicitReturns": true, + // "noUnusedLocals": true, + // "noUnusedParameters": true, + /* Modules */ + "moduleResolution": "Node", "types": ["offscreencanvas"], - "lib": ["es2018", "DOM"] + /* Emit */ + "declaration": true, + "outDir": "dist", + /* JavaScript Support */ + "allowJs": true, + "checkJs": true, + /* Interop Constraints */ + "allowSyntheticDefaultImports": true, + /* Language and Environment */ + "target": "ES6", + "lib": ["es2018", "DOM"], + /* Completeness */ + "skipLibCheck": true }, "typedocOptions": { "name": "Chart.js", @@ -21,7 +36,7 @@ "out": "./dist/docs/typedoc" }, "include": [ - "./src/**/*.js", + "./src/**/*", "./types" ], "exclude": [ diff --git a/types/.eslintrc.yml b/types/.eslintrc.yml index 14f736c75..ca79ca010 100644 --- a/types/.eslintrc.yml +++ b/types/.eslintrc.yml @@ -1,22 +1,4 @@ -parser: '@typescript-eslint/parser' - -plugins: - - '@typescript-eslint' - -extends: - - chartjs - - plugin:@typescript-eslint/recommended - rules: - # Replace stock eslint rules with typescript-eslint equivalents for proper - # TypeScript support. - indent: "off" - "@typescript-eslint/indent": ["error", 2] - no-use-before-define: "off" - '@typescript-eslint/no-use-before-define': "error" - no-shadow: "off" - '@typescript-eslint/no-shadow': "error" - # These rules were set to warning to make the linting pass initially, # without making any major changes to types. object-curly-spacing: ["warn", "always"] diff --git a/types/adapters.d.ts b/types/adapters.d.ts deleted file mode 100644 index 7e3e63c8e..000000000 --- a/types/adapters.d.ts +++ /dev/null @@ -1,70 +0,0 @@ -import type { ChartOptions } from '.'; - -export type TimeUnit = 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year'; - -export interface DateAdapter { - // Override one or multiple of the methods to adjust to the logic of the current date library. - override(members: Partial): void; - readonly options: unknown; - - /** - * Will called with chart options after adapter creation. - * @param {ChartOptions} chartOptions - */ - init(chartOptions: ChartOptions): void; - /** - * Returns a map of time formats for the supported formatting units defined - * in Unit as well as 'datetime' representing a detailed date/time string. - * @returns {{string: string}} - */ - formats(): { [key: string]: string }; - /** - * Parses the given `value` and return the associated timestamp. - * @param {unknown} value - the value to parse (usually comes from the data) - * @param {string} [format] - the expected data format - */ - parse(value: unknown, format?: TimeUnit): number | null; - /** - * Returns the formatted date in the specified `format` for a given `timestamp`. - * @param {number} timestamp - the timestamp to format - * @param {string} format - the date/time token - * @return {string} - */ - format(timestamp: number, format: TimeUnit): string; - /** - * Adds the specified `amount` of `unit` to the given `timestamp`. - * @param {number} timestamp - the input timestamp - * @param {number} amount - the amount to add - * @param {Unit} unit - the unit as string - * @return {number} - */ - add(timestamp: number, amount: number, unit: TimeUnit): number; - /** - * Returns the number of `unit` between the given timestamps. - * @param {number} a - the input timestamp (reference) - * @param {number} b - the timestamp to subtract - * @param {Unit} unit - the unit as string - * @return {number} - */ - diff(a: number, b: number, unit: TimeUnit): number; - /** - * Returns start of `unit` for the given `timestamp`. - * @param {number} timestamp - the input timestamp - * @param {Unit|'isoWeek'} unit - the unit as string - * @param {number} [weekday] - the ISO day of the week with 1 being Monday - * and 7 being Sunday (only needed if param *unit* is `isoWeek`). - * @return {number} - */ - startOf(timestamp: number, unit: TimeUnit | 'isoWeek', weekday?: number): number; - /** - * Returns end of `unit` for the given `timestamp`. - * @param {number} timestamp - the input timestamp - * @param {Unit|'isoWeek'} unit - the unit as string - * @return {number} - */ - endOf(timestamp: number, unit: TimeUnit | 'isoWeek'): number; -} - -export const _adapters: { - _date: DateAdapter; -}; diff --git a/types/animation.d.ts b/types/animation.d.ts index 0aece8eae..31d8d0b57 100644 --- a/types/animation.d.ts +++ b/types/animation.d.ts @@ -1,12 +1,13 @@ import { Chart } from '.'; import { AnyObject } from './basic'; -export class Animation { +export declare class Animation { constructor(cfg: AnyObject, target: AnyObject, prop: string, to?: unknown); active(): boolean; update(cfg: AnyObject, to: unknown, date: number): void; cancel(): void; tick(date: number): void; + readonly _to: unknown; } export interface AnimationEvent { @@ -16,7 +17,7 @@ export interface AnimationEvent { currentStep: number; } -export class Animator { +export declare class Animator { listen(chart: Chart, event: 'complete' | 'progress', cb: (event: AnimationEvent) => void): void; add(chart: Chart, items: readonly Animation[]): void; has(chart: Chart): boolean; @@ -26,7 +27,7 @@ export class Animator { remove(chart: Chart): boolean; } -export class Animations { +export declare class Animations { constructor(chart: Chart, animations: AnyObject); configure(animations: AnyObject): void; update(target: AnyObject, values: AnyObject): undefined | boolean; diff --git a/types/element.d.ts b/types/element.d.ts deleted file mode 100644 index 3b9359b3a..000000000 --- a/types/element.d.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { AnyObject } from './basic'; -import { Point } from './geometric'; - -export interface Element { - readonly x: number; - readonly y: number; - readonly active: boolean; - readonly options: O; - - tooltipPosition(useFinalPosition?: boolean): Point; - hasValue(): boolean; - getProps

(props: P, final?: boolean): Pick; -} -export const Element: { - prototype: Element; - new (): Element; -}; diff --git a/types/helpers/helpers.core.d.ts b/types/helpers/helpers.core.d.ts deleted file mode 100644 index bc376da02..000000000 --- a/types/helpers/helpers.core.d.ts +++ /dev/null @@ -1,157 +0,0 @@ -import { AnyObject } from '../basic'; - -/** - * An empty function that can be used, for example, for optional callback. - */ -export function noop(): void; - -/** - * Returns a unique id, sequentially generated from a global variable. - * @returns {number} - * @function - */ -export function uid(): number; -/** - * Returns true if `value` is neither null nor undefined, else returns false. - * @param {*} value - The value to test. - * @returns {boolean} - * @since 2.7.0 - */ -export function isNullOrUndef(value: unknown): value is null | undefined; -/** - * Returns true if `value` is an array (including typed arrays), else returns false. - * @param {*} value - The value to test. - * @returns {boolean} - * @function - */ -export function isArray(value: unknown): value is ArrayLike; -/** - * Returns true if `value` is an object (excluding null), else returns false. - * @param {*} value - The value to test. - * @returns {boolean} - * @since 2.7.0 - */ -export function isObject(value: unknown): value is AnyObject; -/** - * Returns true if `value` is a finite number, else returns false - * @param {*} value - The value to test. - * @returns {boolean} - */ -export function isFinite(value: unknown): value is number; - -/** - * Returns `value` if finite, else returns `defaultValue`. - * @param {*} value - The value to return if defined. - * @param {*} defaultValue - The value to return if `value` is not finite. - * @returns {*} - */ -export function finiteOrDefault(value: unknown, defaultValue: number): number; - -/** - * Returns `value` if defined, else returns `defaultValue`. - * @param {*} value - The value to return if defined. - * @param {*} defaultValue - The value to return if `value` is undefined. - * @returns {*} - */ -export function valueOrDefault(value: T | undefined, defaultValue: T): T; - -export function toPercentage(value: number | string, dimesion: number): number; -export function toDimension(value: number | string, dimension: number): number; - -/** - * Calls `fn` with the given `args` in the scope defined by `thisArg` and returns the - * value returned by `fn`. If `fn` is not a function, this method returns undefined. - * @param fn - The function to call. - * @param args - The arguments with which `fn` should be called. - * @param [thisArg] - The value of `this` provided for the call to `fn`. - * @returns {*} - */ -export function callback R, TA, R>( - fn: T | undefined, - args: unknown[], - thisArg?: TA -): R | undefined; - -/** - * Note(SB) for performance sake, this method should only be used when loopable type - * is unknown or in none intensive code (not called often and small loopable). Else - * it's preferable to use a regular for() loop and save extra function calls. - * @param loopable - The object or array to be iterated. - * @param fn - The function to call for each item. - * @param [thisArg] - The value of `this` provided for the call to `fn`. - * @param [reverse] - If true, iterates backward on the loopable. - */ -export function each( - loopable: T[], - fn: (this: TA, v: T, i: number) => void, - thisArg?: TA, - reverse?: boolean -): void; -/** - * Note(SB) for performance sake, this method should only be used when loopable type - * is unknown or in none intensive code (not called often and small loopable). Else - * it's preferable to use a regular for() loop and save extra function calls. - * @param loopable - The object or array to be iterated. - * @param fn - The function to call for each item. - * @param [thisArg] - The value of `this` provided for the call to `fn`. - * @param [reverse] - If true, iterates backward on the loopable. - */ -export function each( - loopable: { [key: string]: T }, - fn: (this: TA, v: T, k: string) => void, - thisArg?: TA, - reverse?: boolean -): void; - -/** - * Returns a deep copy of `source` without keeping references on objects and arrays. - * @param source - The value to clone. - */ -export function clone(source: T): T; - -export interface MergeOptions { - merger?: (key: string, target: AnyObject, source: AnyObject, options: AnyObject) => AnyObject; -} -/** - * Recursively deep copies `source` properties into `target` with the given `options`. - * IMPORTANT: `target` is not cloned and will be updated with `source` properties. - * @param target - The target object in which all sources are merged into. - * @param source - Object(s) to merge into `target`. - * @param {object} [options] - Merging options: - * @param {function} [options.merger] - The merge method (key, target, source, options) - * @returns {object} The `target` object. - */ -export function merge(target: T, source: [], options?: MergeOptions): T; -export function merge(target: T, source: S1, options?: MergeOptions): T & S1; -export function merge(target: T, source: [S1], options?: MergeOptions): T & S1; -export function merge(target: T, source: [S1, S2], options?: MergeOptions): T & S1 & S2; -export function merge(target: T, source: [S1, S2, S3], options?: MergeOptions): T & S1 & S2 & S3; -export function merge( - target: T, - source: [S1, S2, S3, S4], - options?: MergeOptions -): T & S1 & S2 & S3 & S4; -export function merge(target: T, source: AnyObject[], options?: MergeOptions): AnyObject; - -/** - * Recursively deep copies `source` properties into `target` *only* if not defined in target. - * IMPORTANT: `target` is not cloned and will be updated with `source` properties. - * @param target - The target object in which all sources are merged into. - * @param source - Object(s) to merge into `target`. - * @returns The `target` object. - */ -export function mergeIf(target: T, source: []): T; -export function mergeIf(target: T, source: S1): T & S1; -export function mergeIf(target: T, source: [S1]): T & S1; -export function mergeIf(target: T, source: [S1, S2]): T & S1 & S2; -export function mergeIf(target: T, source: [S1, S2, S3]): T & S1 & S2 & S3; -export function mergeIf(target: T, source: [S1, S2, S3, S4]): T & S1 & S2 & S3 & S4; -export function mergeIf(target: T, source: AnyObject[]): AnyObject; - -export function resolveObjectKey(obj: AnyObject, key: string): AnyObject; - -export function defined(value: unknown): boolean; - -export function isFunction(value: unknown): boolean; - -export function setsEqual(a: Set, b: Set): boolean; diff --git a/types/helpers/helpers.easing.d.ts b/types/helpers/helpers.easing.d.ts index 682a5d05a..7fb2a3877 100644 --- a/types/helpers/helpers.easing.d.ts +++ b/types/helpers/helpers.easing.d.ts @@ -2,4 +2,4 @@ import { EasingFunction } from '..'; export type EasingFunctionSignature = (t: number) => number; -export const easingEffects: Record; +export declare const easingEffects: Record; diff --git a/types/helpers/index.d.ts b/types/helpers/index.d.ts index 013326924..9753a4d97 100644 --- a/types/helpers/index.d.ts +++ b/types/helpers/index.d.ts @@ -1,7 +1,6 @@ export * from './helpers.canvas'; export * from './helpers.collection'; export * from './helpers.color'; -export * from './helpers.core'; export * from './helpers.curve'; export * from './helpers.dom'; export * from './helpers.easing'; diff --git a/types/index.d.ts b/types/index.d.ts index 4daf093ba..bb93f117c 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,17 +1,15 @@ import { DeepPartial, DistributiveArray, UnionToIntersection } from './utils'; -import { TimeUnit } from './adapters'; +import { TimeUnit } from '../src/core/core.adapters'; import { AnimationEvent } from './animation'; import { AnyObject, EmptyObject } from './basic'; import { Color } from './color'; -import { Element } from './element'; +import Element from '../src/core/core.element'; import { ChartArea, Padding, Point } from './geometric'; import { LayoutItem, LayoutPosition } from './layout'; -export { DateAdapter, TimeUnit, _adapters } from './adapters'; export { Animation, Animations, Animator, AnimationEvent } from './animation'; export { Color } from './color'; -export { Element } from './element'; export { ChartArea, Point } from './geometric'; export { LayoutItem, LayoutPosition } from './layout'; @@ -149,7 +147,7 @@ export interface BarControllerChartOptions { } export type BarController = DatasetController -export const BarController: ChartComponent & { +export declare const BarController: ChartComponent & { prototype: BarController; new (chart: Chart, datasetIndex: number): BarController; }; @@ -167,7 +165,7 @@ export interface BubbleDataPoint extends Point { } export type BubbleController = DatasetController -export const BubbleController: ChartComponent & { +export declare const BubbleController: ChartComponent & { prototype: BubbleController; new (chart: Chart, datasetIndex: number): BubbleController; }; @@ -213,7 +211,7 @@ export interface LineControllerChartOptions { } export type LineController = DatasetController -export const LineController: ChartComponent & { +export declare const LineController: ChartComponent & { prototype: LineController; new (chart: Chart, datasetIndex: number): LineController; }; @@ -225,7 +223,7 @@ export interface ScatterDataPoint extends Point {} export type ScatterControllerChartOptions = LineControllerChartOptions; export type ScatterController = LineController -export const ScatterController: ChartComponent & { +export declare const ScatterController: ChartComponent & { prototype: ScatterController; new (chart: Chart, datasetIndex: number): ScatterController; }; @@ -333,7 +331,7 @@ export interface DoughnutController extends DatasetController { calculateCircumference(value: number): number; } -export const DoughnutController: ChartComponent & { +export declare const DoughnutController: ChartComponent & { prototype: DoughnutController; new (chart: Chart, datasetIndex: number): DoughnutController; }; @@ -350,7 +348,7 @@ export type PieDataPoint = DoughnutDataPoint; export type PieMetaExtensions = DoughnutMetaExtensions; export type PieController = DoughnutController -export const PieController: ChartComponent & { +export declare const PieController: ChartComponent & { prototype: PieController; new (chart: Chart, datasetIndex: number): PieController; }; @@ -378,7 +376,7 @@ export interface PolarAreaControllerChartOptions { export interface PolarAreaController extends DoughnutController { countVisibleElements(): number; } -export const PolarAreaController: ChartComponent & { +export declare const PolarAreaController: ChartComponent & { prototype: PolarAreaController; new (chart: Chart, datasetIndex: number): PolarAreaController; }; @@ -411,7 +409,7 @@ export interface RadarControllerDatasetOptions export type RadarControllerChartOptions = LineControllerChartOptions; export type RadarController = DatasetController -export const RadarController: ChartComponent & { +export declare const RadarController: ChartComponent & { prototype: RadarController; new (chart: Chart, datasetIndex: number): RadarController; }; @@ -540,7 +538,7 @@ export declare class Chart< static unregister(...items: ChartComponentLike[]): void; } -export const registerables: readonly ChartComponentLike[]; +export declare const registerables: readonly ChartComponentLike[]; export declare type ChartItem = | string @@ -561,7 +559,7 @@ export declare enum UpdateModeEnum { export type UpdateMode = keyof typeof UpdateModeEnum; -export class DatasetController< +export declare class DatasetController< TType extends ChartType = ChartType, TElement extends Element = Element, TDatasetElement extends Element = Element, @@ -689,7 +687,7 @@ export type Overrides = { ChartTypeRegistry[key]['chartOptions']; } -export const defaults: Defaults; +export declare const defaults: Defaults; export interface InteractionOptions { axis?: string; intersect?: boolean; @@ -742,7 +740,7 @@ export interface InteractionModeMap { export type InteractionMode = keyof InteractionModeMap; -export const Interaction: { +export declare const Interaction: { modes: InteractionModeMap; /** @@ -757,7 +755,7 @@ export const Interaction: { ): InteractionItem[]; }; -export const layouts: { +export declare const layouts: { /** * Register a box to a chart. * A box is simply a reference to an object that requires layout. eg. Scales, Legend, Title. @@ -1119,7 +1117,7 @@ export interface Registry { getScale(id: string): Scale | undefined; } -export const registry: Registry; +export declare const registry: Registry; export interface Tick { value: number; @@ -1336,7 +1334,7 @@ export interface ScriptableScalePointLabelContext { } -export const Ticks: { +export declare const Ticks: { formatters: { /** * Formatter for value labels @@ -1743,7 +1741,7 @@ export interface ArcElement, VisualElement {} -export const ArcElement: ChartComponent & { +export declare const ArcElement: ChartComponent & { prototype: ArcElement; new (cfg: AnyObject): ArcElement; }; @@ -1833,7 +1831,7 @@ export interface LineElement extends Element, VisualElement {} -export const BarElement: ChartComponent & { +export declare const BarElement: ChartComponent & { prototype: BarElement; new (cfg: AnyObject): BarElement; }; @@ -2023,7 +2021,7 @@ export type ElementChartOptions = { elements: ElementOptionsByType }; -export class BasePlatform { +export declare class BasePlatform { /** * Called at chart construction time, returns a context2d instance implementing * the [W3C Canvas 2D Context API standard]{@link https://www.w3.org/TR/2dcontext/}. @@ -2080,12 +2078,12 @@ export class BasePlatform { updateConfig(config: ChartConfiguration | ChartConfigurationCustomTypesPerDataset): void; } -export class BasicPlatform extends BasePlatform {} -export class DomPlatform extends BasePlatform {} +export declare class BasicPlatform extends BasePlatform {} +export declare class DomPlatform extends BasePlatform {} -export const Decimation: Plugin; +export declare const Decimation: Plugin; -export const enum DecimationAlgorithm { +export declare const enum DecimationAlgorithm { lttb = 'lttb', minmax = 'min-max', } @@ -2105,7 +2103,7 @@ interface MinMaxDecimationOptions extends BaseDecimationOptions { export type DecimationOptions = LttbDecimationOptions | MinMaxDecimationOptions; -export const Filler: Plugin; +export declare const Filler: Plugin; export interface FillerOptions { drawTime: 'beforeDatasetDraw' | 'beforeDatasetsDraw'; propagate: boolean; @@ -2135,7 +2133,7 @@ export interface FillerControllerDatasetOptions { fill: FillTarget | ComplexFillTarget; } -export const Legend: Plugin; +export declare const Legend: Plugin; export interface LegendItem { /** @@ -2387,8 +2385,8 @@ export interface LegendOptions { }; } -export const SubTitle: Plugin; -export const Title: Plugin; +export declare const SubTitle: Plugin; +export declare const Title: Plugin; export interface TitleOptions { /** @@ -2535,7 +2533,7 @@ export interface Tooltip extends Plugin { readonly positioners: TooltipPositionerMap; } -export const Tooltip: Tooltip; +export declare const Tooltip: Tooltip; export interface TooltipCallbacks< TType extends ChartType, @@ -3135,7 +3133,7 @@ export type CategoryScaleOptions = Omit & }; export type CategoryScale = Scale -export const CategoryScale: ChartComponent & { +export declare const CategoryScale: ChartComponent & { prototype: CategoryScale; new (cfg: AnyObject): CategoryScale; }; @@ -3184,7 +3182,7 @@ export type LinearScaleOptions = CartesianScaleOptions & { }; export type LinearScale = Scale -export const LinearScale: ChartComponent & { +export declare const LinearScale: ChartComponent & { prototype: LinearScale; new (cfg: AnyObject): LinearScale; }; @@ -3208,7 +3206,7 @@ export type LogarithmicScaleOptions = CartesianScaleOptions & { }; export type LogarithmicScale = Scale -export const LogarithmicScale: ChartComponent & { +export declare const LogarithmicScale: ChartComponent & { prototype: LogarithmicScale; new (cfg: AnyObject): LogarithmicScale; }; @@ -3304,13 +3302,13 @@ export interface TimeScale extend normalize(values: number[]): number[]; } -export const TimeScale: ChartComponent & { +export declare const TimeScale: ChartComponent & { prototype: TimeScale; new (cfg: AnyObject): TimeScale; }; export type TimeSeriesScale = TimeScale -export const TimeSeriesScale: ChartComponent & { +export declare const TimeSeriesScale: ChartComponent & { prototype: TimeSeriesScale; new (cfg: AnyObject): TimeSeriesScale; }; @@ -3466,7 +3464,7 @@ export interface RadialLinearScale(cfg: AnyObject): RadialLinearScale; }; diff --git a/types/tests/animation.ts b/types/tests/animation.ts index 6e38e68ea..c998bf603 100644 --- a/types/tests/animation.ts +++ b/types/tests/animation.ts @@ -1,4 +1,4 @@ -import { Chart } from '..'; +import { Chart } from '../../src/types'; const chart = new Chart('id', { type: 'bar', diff --git a/types/tests/autogen.js b/types/tests/autogen.js index 584a33636..9d17efcb0 100644 --- a/types/tests/autogen.js +++ b/types/tests/autogen.js @@ -10,9 +10,9 @@ let fd; try { const fn = path.resolve(__dirname, 'autogen_helpers.ts'); fd = fs.openSync(fn, 'w+'); - fs.writeSync(fd, 'import * as helpers from \'../helpers\';\n\n'); + fs.writeSync(fd, 'import * as helpers from \'../../src/helpers/types\';\n\n'); - fs.writeSync(fd, 'const testKeys = [];\n'); + fs.writeSync(fd, 'const testKeys: unknown[] = [];\n'); for (const key of Object.keys(helpers)) { if (key[0] !== '_' && typeof helpers[key] === 'function') { fs.writeSync(fd, `testKeys.push(helpers.${key});\n`); diff --git a/types/tests/chart_types.ts b/types/tests/chart_types.ts index 32831901e..16ec83526 100644 --- a/types/tests/chart_types.ts +++ b/types/tests/chart_types.ts @@ -1,4 +1,4 @@ -import { Chart } from '..'; +import { Chart } from '../../src/types'; const chart = new Chart('chart', { type: 'bar', diff --git a/types/tests/controllers/bubble_chart_options.ts b/types/tests/controllers/bubble_chart_options.ts index 5924dd5f9..aa1ba5110 100644 --- a/types/tests/controllers/bubble_chart_options.ts +++ b/types/tests/controllers/bubble_chart_options.ts @@ -1,4 +1,4 @@ -import { Chart, ChartOptions } from '../..'; +import { Chart, ChartOptions } from '../../../src/types'; const chart = new Chart('test', { type: 'bubble', diff --git a/types/tests/controllers/doughnut_meta_total.ts b/types/tests/controllers/doughnut_meta_total.ts index 76eb6eb6f..ccd8f337a 100644 --- a/types/tests/controllers/doughnut_meta_total.ts +++ b/types/tests/controllers/doughnut_meta_total.ts @@ -1,4 +1,4 @@ -import { Chart, ChartMeta, Element } from '../..'; +import { Chart, ChartMeta, Element } from '../../../src/types'; const chart = new Chart('id', { type: 'doughnut', diff --git a/types/tests/controllers/doughnut_offset.ts b/types/tests/controllers/doughnut_offset.ts index cf2d06943..a238b2690 100644 --- a/types/tests/controllers/doughnut_offset.ts +++ b/types/tests/controllers/doughnut_offset.ts @@ -1,4 +1,4 @@ -import { Chart, ChartMeta, Element } from '../..'; +import { Chart, ChartMeta, Element } from '../../../src/types'; const chart = new Chart('id', { type: 'doughnut', diff --git a/types/tests/controllers/doughnut_outer_radius.ts b/types/tests/controllers/doughnut_outer_radius.ts index e72d309c2..9f39caf6f 100644 --- a/types/tests/controllers/doughnut_outer_radius.ts +++ b/types/tests/controllers/doughnut_outer_radius.ts @@ -1,4 +1,4 @@ -import { Chart } from '../..'; +import { Chart } from '../../../src/types'; const chart = new Chart('id', { type: 'doughnut', diff --git a/types/tests/controllers/line_scriptable_parsed_data.ts b/types/tests/controllers/line_scriptable_parsed_data.ts index 7a726da37..0562250bb 100644 --- a/types/tests/controllers/line_scriptable_parsed_data.ts +++ b/types/tests/controllers/line_scriptable_parsed_data.ts @@ -1,4 +1,4 @@ -import { Chart } from '../..'; +import { Chart } from '../../../src/types'; const chart = new Chart('id', { type: 'line', diff --git a/types/tests/controllers/line_segments.ts b/types/tests/controllers/line_segments.ts index 5d07063a9..6478cdaa9 100644 --- a/types/tests/controllers/line_segments.ts +++ b/types/tests/controllers/line_segments.ts @@ -1,4 +1,4 @@ -import { Chart } from '../..'; +import { Chart } from '../../../src/types'; const chart = new Chart('id', { type: 'line', diff --git a/types/tests/controllers/line_span_gaps.ts b/types/tests/controllers/line_span_gaps.ts index 0025641bd..da6c8bcb7 100644 --- a/types/tests/controllers/line_span_gaps.ts +++ b/types/tests/controllers/line_span_gaps.ts @@ -1,4 +1,4 @@ -import { Chart } from '../..'; +import { Chart } from '../../../src/types'; const chart = new Chart('id', { type: 'line', diff --git a/types/tests/controllers/line_styling_array.ts b/types/tests/controllers/line_styling_array.ts index daf2b7be7..b5dce9d83 100644 --- a/types/tests/controllers/line_styling_array.ts +++ b/types/tests/controllers/line_styling_array.ts @@ -1,4 +1,4 @@ -import { Chart } from '../..'; +import { Chart } from '../../../src/types'; const chart = new Chart('id', { type: 'line', diff --git a/types/tests/controllers/radar_dataset_indexable_options.ts b/types/tests/controllers/radar_dataset_indexable_options.ts index 7ef753619..d55924f3e 100644 --- a/types/tests/controllers/radar_dataset_indexable_options.ts +++ b/types/tests/controllers/radar_dataset_indexable_options.ts @@ -1,4 +1,4 @@ -import { Chart, ChartOptions } from '../..'; +import { Chart, ChartOptions } from '../../../src/types'; const chart = new Chart('test', { type: 'radar', diff --git a/types/tests/data_types.ts b/types/tests/data_types.ts index 0e6a2d87f..7c1ca05b3 100644 --- a/types/tests/data_types.ts +++ b/types/tests/data_types.ts @@ -1,4 +1,4 @@ -import { Chart } from '..'; +import { Chart } from '../../src/types'; const chart = new Chart('chart', { type: 'bar', diff --git a/types/tests/dataset_null_data.ts b/types/tests/dataset_null_data.ts index bb86a0aae..3ee85f2ab 100644 --- a/types/tests/dataset_null_data.ts +++ b/types/tests/dataset_null_data.ts @@ -1,4 +1,4 @@ -import { ChartDataset } from '..'; +import { ChartDataset } from '../../src/types'; const dataset: ChartDataset = { data: [10, null, 20], diff --git a/types/tests/date_adapter.ts b/types/tests/date_adapter.ts new file mode 100644 index 000000000..a6e284452 --- /dev/null +++ b/types/tests/date_adapter.ts @@ -0,0 +1,14 @@ +import { _adapters } from '../../src/types'; + +_adapters._date.override<{myOption: boolean}>({ + init() { + const booleanOption: boolean = this.options.myOption; + + // @ts-expect-error Options is readonly. + this.options = {}; + }, + // @ts-expect-error Should return string. + format(timestamp) { + const numberArg: number = timestamp; + } +}); diff --git a/types/tests/defaults.ts b/types/tests/defaults.ts index c4c591000..096eafab5 100644 --- a/types/tests/defaults.ts +++ b/types/tests/defaults.ts @@ -1,4 +1,4 @@ -import { Chart } from '..'; +import { Chart } from '../../src/types'; Chart.defaults.scales.time.time.minUnit = 'day'; diff --git a/types/tests/elements/scriptable_element_options.ts b/types/tests/elements/scriptable_element_options.ts index 791684c64..3792fc37e 100644 --- a/types/tests/elements/scriptable_element_options.ts +++ b/types/tests/elements/scriptable_element_options.ts @@ -1,4 +1,4 @@ -import { Chart } from '../..'; +import { Chart } from '../../../src/types'; const chart = new Chart('id', { type: 'line', diff --git a/types/tests/extensions/plugin.ts b/types/tests/extensions/plugin.ts index b0a89c5b0..f03b27b5c 100644 --- a/types/tests/extensions/plugin.ts +++ b/types/tests/extensions/plugin.ts @@ -1,4 +1,4 @@ -import { Chart } from '../..'; +import { Chart } from '../../../src/types'; Chart.register({ id: 'my-plugin', diff --git a/types/tests/extensions/scale.ts b/types/tests/extensions/scale.ts index 0b6a10dcc..e78c41745 100644 --- a/types/tests/extensions/scale.ts +++ b/types/tests/extensions/scale.ts @@ -1,5 +1,5 @@ import { AnyObject } from '../../basic'; -import { CartesianScaleOptions, Chart, Scale } from '../..'; +import { CartesianScaleOptions, Chart, Scale } from '../../../src/types'; export type TestScaleOptions = CartesianScaleOptions & { testOption?: boolean diff --git a/types/tests/interaction.ts b/types/tests/interaction.ts index 3450bbfe0..46de29b6c 100644 --- a/types/tests/interaction.ts +++ b/types/tests/interaction.ts @@ -1,6 +1,6 @@ import { Chart, ChartData, ChartConfiguration, Element -} from '..'; +} from '../../src/types'; const data: ChartData<'line'> = { datasets: [] }; const chartItem = 'item'; diff --git a/types/tests/layout/position.ts b/types/tests/layout/position.ts index 544ed4f3e..25007207f 100644 --- a/types/tests/layout/position.ts +++ b/types/tests/layout/position.ts @@ -1,4 +1,4 @@ -import { LayoutPosition } from '../..'; +import { LayoutPosition } from '../../../src/types'; const left: LayoutPosition = 'left'; const right: LayoutPosition = 'right'; diff --git a/types/tests/options.ts b/types/tests/options.ts index 2ec8b895c..231a83b79 100644 --- a/types/tests/options.ts +++ b/types/tests/options.ts @@ -1,4 +1,4 @@ -import { Chart } from '..'; +import { Chart } from '../../src/types'; const chart = new Chart('test', { type: 'bar', diff --git a/types/tests/overrides.ts b/types/tests/overrides.ts index 1e184e561..28701cbbb 100644 --- a/types/tests/overrides.ts +++ b/types/tests/overrides.ts @@ -1,4 +1,4 @@ -import { Chart } from '..'; +import { Chart } from '../../src/types'; Chart.overrides.bar.scales.x.type = 'time'; diff --git a/types/tests/parsed.data.type.ts b/types/tests/parsed.data.type.ts index 54b207a4b..6123ce9c0 100644 --- a/types/tests/parsed.data.type.ts +++ b/types/tests/parsed.data.type.ts @@ -1,4 +1,4 @@ -import { ParsedDataType } from '..'; +import { ParsedDataType } from '../../src/types'; interface test { pie: ParsedDataType<'pie'>, diff --git a/types/tests/plugins/defaults.ts b/types/tests/plugins/defaults.ts index 9ca1edea6..cc2896b68 100644 --- a/types/tests/plugins/defaults.ts +++ b/types/tests/plugins/defaults.ts @@ -1,4 +1,4 @@ -import { defaults } from '../..'; +import { defaults } from '../../../src/types'; // https://github.com/chartjs/Chart.js/issues/8711 const original = defaults.plugins.legend.labels.generateLabels; diff --git a/types/tests/plugins/plugin.decimation/decimation_algorithm.ts b/types/tests/plugins/plugin.decimation/decimation_algorithm.ts index a6659bac7..2ca4afbed 100644 --- a/types/tests/plugins/plugin.decimation/decimation_algorithm.ts +++ b/types/tests/plugins/plugin.decimation/decimation_algorithm.ts @@ -1,4 +1,4 @@ -import { Chart, DecimationAlgorithm } from '../../..'; +import { Chart, DecimationAlgorithm } from '../../../../src/types'; const chart = new Chart('id', { type: 'bubble', diff --git a/types/tests/plugins/plugin.filler/fill_target_true.ts b/types/tests/plugins/plugin.filler/fill_target_true.ts index 3768ddeda..e43a94fe1 100644 --- a/types/tests/plugins/plugin.filler/fill_target_true.ts +++ b/types/tests/plugins/plugin.filler/fill_target_true.ts @@ -1,4 +1,4 @@ -import { ChartDataset } from '../../..'; +import { ChartDataset } from '../../../../src/types'; const dataset: ChartDataset = { data: [], diff --git a/types/tests/plugins/plugin.tooltip/chart.tooltip.ts b/types/tests/plugins/plugin.tooltip/chart.tooltip.ts index 56b37f295..6f32fe20b 100644 --- a/types/tests/plugins/plugin.tooltip/chart.tooltip.ts +++ b/types/tests/plugins/plugin.tooltip/chart.tooltip.ts @@ -1,4 +1,4 @@ -import { Chart } from '../../..'; +import { Chart } from '../../../../src/types'; const chart = new Chart('id', { type: 'line', diff --git a/types/tests/plugins/plugin.tooltip/tooltip_dataset_type.ts b/types/tests/plugins/plugin.tooltip/tooltip_dataset_type.ts index 343828d8d..aae63fcb7 100644 --- a/types/tests/plugins/plugin.tooltip/tooltip_dataset_type.ts +++ b/types/tests/plugins/plugin.tooltip/tooltip_dataset_type.ts @@ -1,4 +1,4 @@ -import { Chart } from '../../..'; +import { Chart } from '../../../../src/types'; const chart = new Chart('id', { type: 'line', diff --git a/types/tests/plugins/plugin.tooltip/tooltip_parsed_data.ts b/types/tests/plugins/plugin.tooltip/tooltip_parsed_data.ts index d7fb45959..52c58bea0 100644 --- a/types/tests/plugins/plugin.tooltip/tooltip_parsed_data.ts +++ b/types/tests/plugins/plugin.tooltip/tooltip_parsed_data.ts @@ -1,4 +1,4 @@ -import { Chart } from '../../..'; +import { Chart } from '../../../../src/types'; const chart = new Chart('id', { type: 'bar', diff --git a/types/tests/plugins/plugin.tooltip/tooltip_parsed_data_chart_defaults.ts b/types/tests/plugins/plugin.tooltip/tooltip_parsed_data_chart_defaults.ts index d1488b681..3817b0261 100644 --- a/types/tests/plugins/plugin.tooltip/tooltip_parsed_data_chart_defaults.ts +++ b/types/tests/plugins/plugin.tooltip/tooltip_parsed_data_chart_defaults.ts @@ -1,4 +1,4 @@ -import { Chart } from '../../..'; +import { Chart } from '../../../../src/types'; Chart.overrides.bubble.plugins.tooltip.callbacks.label = (item) => { const { x, y, _custom: r } = item.parsed; diff --git a/types/tests/plugins/plugin.tooltip/tooltip_scriptable_background_color.ts b/types/tests/plugins/plugin.tooltip/tooltip_scriptable_background_color.ts index 36c54d7e0..bc90008f6 100644 --- a/types/tests/plugins/plugin.tooltip/tooltip_scriptable_background_color.ts +++ b/types/tests/plugins/plugin.tooltip/tooltip_scriptable_background_color.ts @@ -1,4 +1,4 @@ -import { Chart } from '../../..'; +import { Chart } from '../../../../src/types'; const chart = new Chart('id', { type: 'bar', diff --git a/types/tests/register.ts b/types/tests/register.ts index 49979d7a8..ddc5f5e24 100644 --- a/types/tests/register.ts +++ b/types/tests/register.ts @@ -24,7 +24,7 @@ import { Title, SubTitle, Tooltip -} from '..'; +} from '../../src/types'; Chart.register( ArcElement, diff --git a/types/tests/scales/chart_options.ts b/types/tests/scales/chart_options.ts index c097601a9..fa8c806a7 100644 --- a/types/tests/scales/chart_options.ts +++ b/types/tests/scales/chart_options.ts @@ -1,4 +1,4 @@ -import { ChartOptions } from '../..'; +import { ChartOptions } from '../../../src/types'; const chartOptions: ChartOptions<'line'> = { scales: { diff --git a/types/tests/scales/options.ts b/types/tests/scales/options.ts index a521f0150..65debaffa 100644 --- a/types/tests/scales/options.ts +++ b/types/tests/scales/options.ts @@ -1,4 +1,4 @@ -import { Chart, ScaleOptions } from '../..'; +import { Chart, ScaleOptions } from '../../../src/types'; const chart = new Chart('test', { type: 'bar', diff --git a/types/tests/scales/time_string_max.ts b/types/tests/scales/time_string_max.ts index 9c1c38bf4..377c3e268 100644 --- a/types/tests/scales/time_string_max.ts +++ b/types/tests/scales/time_string_max.ts @@ -1,4 +1,4 @@ -import { Chart } from '../..'; +import { Chart } from '../../../src/types'; const chart = new Chart('id', { type: 'line', diff --git a/types/tests/scriptable.ts b/types/tests/scriptable.ts index bf9cec805..248441cda 100644 --- a/types/tests/scriptable.ts +++ b/types/tests/scriptable.ts @@ -1,4 +1,4 @@ -import { ChartType, Scriptable, ScriptableContext } from '..'; +import { ChartType, Scriptable, ScriptableContext } from '../../src/types'; interface test { pie?: Scriptable>, diff --git a/types/tests/scriptable_core_chart_options.ts b/types/tests/scriptable_core_chart_options.ts index 3e2115f65..59fe94af9 100644 --- a/types/tests/scriptable_core_chart_options.ts +++ b/types/tests/scriptable_core_chart_options.ts @@ -1,4 +1,4 @@ -import { ChartConfiguration } from '..'; +import { ChartConfiguration } from '../../src/types'; const getConfig = (): ChartConfiguration<'bar'> => { return { diff --git a/types/tests/test_instance_assignment.ts b/types/tests/test_instance_assignment.ts index ea3951d2e..5e8823a7e 100644 --- a/types/tests/test_instance_assignment.ts +++ b/types/tests/test_instance_assignment.ts @@ -1,4 +1,4 @@ -import { Chart } from '..'; +import { Chart } from '../../src/types'; const chart = new Chart('id', { type: 'scatter', diff --git a/types/tests/tsconfig.json b/types/tests/tsconfig.json index a8494a0a8..62d8a4c56 100644 --- a/types/tests/tsconfig.json +++ b/types/tests/tsconfig.json @@ -1,13 +1,10 @@ { + "extends": "../../tsconfig.json", "compilerOptions": { - "target": "ES6", - "moduleResolution": "Node", - "alwaysStrict": true, - "strict": true, "noEmit": true }, "include": [ - "./**/*.ts", - "../index.d.ts" + "../", + "../../src/" ] } diff --git a/types/utils.d.ts b/types/utils.d.ts index 8313d9fae..a8533f22d 100644 --- a/types/utils.d.ts +++ b/types/utils.d.ts @@ -18,4 +18,3 @@ export type DistributiveArray = [T] extends [unknown] ? Array : never // https://stackoverflow.com/a/50375286 export type UnionToIntersection = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never; -