From b73d1ad06ac11e2686318c6971f65cdc9a28f547 Mon Sep 17 00:00:00 2001 From: Dave Pagurek Date: Sun, 14 Apr 2024 09:19:57 -0600 Subject: [PATCH] Add typescript .d.ts generation --- .gitignore | 1 + package-lock.json | 14 ++++++++++++++ package.json | 4 +++- src/accessibility/outputs.js | 5 ++++- src/core/main.js | 1 + tsconfig.json | 21 +++++++++++++++++++++ 6 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore index 01b3bc0b20..bbcd57864a 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ experiments/* lib_old/* lib/p5.* lib/modules +types/ docs/reference/* !*.gitkeep examples/3d/ diff --git a/package-lock.json b/package-lock.json index effbad1a33..828a589b08 100644 --- a/package-lock.json +++ b/package-lock.json @@ -55,6 +55,7 @@ "regenerator-runtime": "^0.13.3", "rollup": "^4.9.6", "rollup-plugin-string": "^3.0.0", + "typescript": "^5.4.5", "unplugin-swc": "^1.4.2", "vite": "^5.0.2", "vite-plugin-string": "^1.2.2", @@ -17083,6 +17084,19 @@ "integrity": "sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA==", "dev": true }, + "node_modules/typescript": { + "version": "5.4.5", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.4.5.tgz", + "integrity": "sha512-vcI4UpRgg81oIRUFwR0WSIHKt11nJ7SAVlYNIu+QpqeyXP+gpQJy/Z4+F0aGxSE4MqwjyXvW/TzgkLAx2AGHwQ==", + "dev": true, + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + }, "node_modules/ufo": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/ufo/-/ufo-1.3.2.tgz", diff --git a/package.json b/package.json index b6b9a885b5..766f39d633 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,8 @@ "docs": "documentation build ./src/**/*.js ./src/**/**/*.js -o ./docs/data.json && node ./utils/convert.js", "test": "vitest run", "lint": "eslint .", - "lint:fix": "eslint --fix ." + "lint:fix": "eslint --fix .", + "types": "npx -p typescript tsc" }, "lint-staged": { "Gruntfile.js": "eslint", @@ -68,6 +69,7 @@ "regenerator-runtime": "^0.13.3", "rollup": "^4.9.6", "rollup-plugin-string": "^3.0.0", + "typescript": "^5.4.5", "unplugin-swc": "^1.4.2", "vite": "^5.0.2", "vite-plugin-string": "^1.2.2", diff --git a/src/accessibility/outputs.js b/src/accessibility/outputs.js index 7a9feda007..9ac6796472 100644 --- a/src/accessibility/outputs.js +++ b/src/accessibility/outputs.js @@ -496,7 +496,10 @@ p5.prototype._accsOutput = function(f, args) { if (!this.ingredients.shapes[f]) { this.ingredients.shapes[f] = [include]; //if other shapes of this type have been created - } else if (this.ingredients.shapes[f] !== [include]) { + } else if ( + this.ingredients.shapes[f].length !== 1 || + this.ingredients.shapes[f][0] !== include + ) { //for every shape of this type for (let y in this.ingredients.shapes[f]) { //compare it with current shape and if it already exists make add false diff --git a/src/core/main.js b/src/core/main.js index c86ddef8cd..3bcc624067 100644 --- a/src/core/main.js +++ b/src/core/main.js @@ -164,6 +164,7 @@ class p5 { } }; + /** @type {() => void} */ this._runIfPreloadsAreDone = function() { const context = this._isGlobal ? window : this; if (context._preloadCount === 0) { diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000000..a8c6057e01 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,21 @@ +{ + // Change this to match your project + "include": ["lib/p5.rollup.esm.js"], + "compilerOptions": { + // Tells TypeScript to read JS files, as + // normally they are ignored as source files + "allowJs": true, + // Generate d.ts files + "declaration": true, + // This compiler run should + // only output d.ts files + "emitDeclarationOnly": true, + // Types should go into this directory. + // Removing this would place the .d.ts files + // next to the .js files + "outDir": "types", + // go to js file when using IDE functions like + // "Go to Definition" in VSCode + "declarationMap": true + } +}