-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Typescript declaration files script #7465
base: dev-2.0
Are you sure you want to change the base?
Conversation
2face78
to
35cb018
Compare
I think this still needs work, the format of the output files, and cases to be handled. But, @davepagurek Do you think this would be the correct approach to bypass |
I think that's probably the best way forward, since it seemed pretty hard to get tsc to understand how we're adding to p5 classes dynamically. Looking good so far! |
function generateTypeFromTag(param) { | ||
if (!param || !param.type) return 'any'; | ||
|
||
switch (param.type.type) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A more complex case you might want to try with is paletteLerp
, which has an array of tuples:
p5.js/src/color/creating_reading.js
Lines 1090 to 1091 in c643b7b
* @method paletteLerp | |
* @param {[p5.Color, Number][]} colors_stops color stops to interpolate from |
Ideally that out output basically back out to its original format, or possibly Array<[p5.Color, number]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
paletteLerp
wasn't present in dev-2.0
when I last checked it. I see that it's added now so I'll handle the respective case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@davepagurek documentation.js
does not generate the correct docs.json
for paletteLerp
. I updated the JSDocs for it and made changes to the code to handle this issue. If paletteLerp
is the only case involving an array of tuples and we need to simplify the script, we could also add it directly to patch.js
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What output were you seeing before? I was looking at it recently in #7497 as a test case and the docs.json
seemed to be correct (although utils/convert.js
had to be updated to handle those cases.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"params": [
{
"title": "param",
"name": "amt",
"lineNumber": 13,
"description": {
"type": "root",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "number to use to interpolate relative to color stops"
}
]
}
]
},
"type": {
"type": "NameExpression",
"name": "Number"
}
}
],
@davepagurek If I'm not wrong, the current docs/data.json
just generates one object in params
i.e. amt
. The array of tuples colors_stops
just has the following entry in docs/data.json
:
{
"title": "param",
"description": "color stops to interpolate from",
"lineNumber": 12,
"type": null,
"errors": [
"unexpected token"
],
"name": "colors_stops"
}
19c5fba
to
35cb018
Compare
Hi @asukaminato0721, I also wanted to get your feedback on this! The approach, I think, will end up being pretty similar to p5.ts where we aren't directly running In the p5.ts implementation, there are also some manual patches that get applied in addition to generating things directly from the docs: https://github.com/p5-types/p5.ts/tree/master/patches @diyaayay we should see if we can update this in p5 itself too if we can. |
Sure, I'll look into all the patches and see how many of these are still applicable on the |
This looks promise! I like it. |
I just noticed an issue while working on #7497 that exists in the docs parsing, where some methods were being attributed to a class without a p5 prefix (e.g. |
dc0672d
to
bb885af
Compare
a844230
to
7fab893
Compare
7fab893
to
3b42124
Compare
3b42124
to
3b425b1
Compare
@davepagurek |
That's a good question. I guess the reason why there's an instance type is because the variable So I think primarily the p5 variable you import is actually a // Import everything
import p5 from 'p5' // p5 is a namespace here
const mySketch = new p5(...) // mySketch is a p5Instance here
const myGraphics = mySketch.createGraphics(...) // myGraphics is a p5.Graphics
// ...but also:
import { Graphics } from 'p5'
console.log(myGraphics instanceof Graphics) // should be true ...so we have to do something to have it both ways. |
Would that mean we need both types for p5.js to support both global mode and instance mode, typically requiring two separate TypeScript declaration files? global.d.ts → Defines everything in global mode (e.g., setup(), draw(), etc.). |
The existing types have a separate file, We can maybe do something similar adding all the methods in two spots: // p5.d.ts
declare class p5 {
// methods here
}
// global.d.ts
declare global {
interface Window {
// methods here
}
} |
Right, I tried this, and global and instance declarations are picked up from the sketch. |
about the patch, I did some check, patch them all need a lot of effort. grep "any\[\]" -r ./types/p5/grep "any\[\]" -r ./types/p5/
./types/p5/global.d.ts: function clip(callback: (...args: any[]) => any, options?: object): void;
./types/p5/global.d.ts: function applyMatrix(arr: any[]): p5;
./types/p5/global.d.ts: function storeItem(key: string, value: string | number | boolean | object | any[]): void;
./types/p5/global.d.ts: function getItem(key: string): string | number | boolean | object | any[];
./types/p5/global.d.ts: function changed(fxn: ((...args: any[]) => any) | boolean): p5;
./types/p5/global.d.ts: function input(fxn: ((...args: any[]) => any) | boolean): p5;
./types/p5/global.d.ts: successCallback?: (...args: any[]) => any
./types/p5/global.d.ts: function createFileInput(callback: (...args: any[]) => any, multiple?: boolean): p5.File;
./types/p5/global.d.ts: function createVideo(src: string | string[], callback?: (...args: any[]) => any): p5.MediaElement;
./types/p5/global.d.ts: function createAudio(src?: string | string[], callback?: (...args: any[]) => any): p5.MediaElement;
./types/p5/global.d.ts: callback?: (...args: any[]) => any
./types/p5/global.d.ts: callback?: (p1: any[]) => any
./types/p5/global.d.ts: successCallback?: (...args: any[]) => any,
./types/p5/global.d.ts: errorCallback?: (...args: any[]) => any
./types/p5/global.d.ts: successCallback?: (...args: any[]) => any,
./types/p5/global.d.ts: errorCallback?: (...args: any[]) => any
./types/p5/global.d.ts: callback?: (...args: any[]) => any,
./types/p5/global.d.ts: errorCallback?: (...args: any[]) => any
./types/p5/global.d.ts: successCallback?: (...args: any[]) => any,
./types/p5/global.d.ts: errorCallback?: (...args: any[]) => any
./types/p5/global.d.ts: callback?: (...args: any[]) => any,
./types/p5/global.d.ts: errorCallback?: (...args: any[]) => any
./types/p5/global.d.ts: callback?: (...args: any[]) => any,
./types/p5/global.d.ts: errorCallback?: (...args: any[]) => any
./types/p5/global.d.ts: callback?: (...args: any[]) => any,
./types/p5/global.d.ts: errorCallback?: (...args: any[]) => any
./types/p5/global.d.ts: callback: (...args: any[]) => any,
./types/p5/global.d.ts: errorCallback?: (...args: any[]) => any
./types/p5/global.d.ts: callback?: (...args: any[]) => any,
./types/p5/global.d.ts: errorCallback?: (...args: any[]) => any
./types/p5/global.d.ts: callback?: (...args: any[]) => any,
./types/p5/global.d.ts: errorCallback?: (...args: any[]) => any
./types/p5/global.d.ts: callback: (...args: any[]) => any,
./types/p5/global.d.ts: errorCallback?: (...args: any[]) => any
./types/p5/global.d.ts: callback?: (...args: any[]) => any,
./types/p5/global.d.ts: errorCallback?: (...args: any[]) => any
./types/p5/global.d.ts: callback?: (...args: any[]) => any,
./types/p5/global.d.ts: errorCallback?: (...args: any[]) => any
./types/p5/global.d.ts: function saveJSON(json: any[] | object, filename: string, optimize?: boolean): void;
./types/p5/global.d.ts: function random(choices: any[]): any;
./types/p5/global.d.ts: successCallback?: (...args: any[]) => any,
./types/p5/global.d.ts: failureCallback?: (...args: any[]) => any
./types/p5/global.d.ts: str: string | object | any[] | number | boolean,
./types/p5/global.d.ts: function append(array: any[], value: any): any[];
./types/p5/global.d.ts: function arrayCopy(src: any[], srcPosition: number, dst: any[], dstPosition: number, length: number): void;
./types/p5/global.d.ts: function arrayCopy(src: any[], dst: any[], length?: number): void;
./types/p5/global.d.ts: function concat(a: any[], b: any[]): any[];
./types/p5/global.d.ts: function reverse(list: any[]): any[];
./types/p5/global.d.ts: function shorten(list: any[]): any[];
./types/p5/global.d.ts: function shuffle(array: any[], bool?: boolean): any[];
./types/p5/global.d.ts: function sort(list: any[], count?: number): any[];
./types/p5/global.d.ts: function splice(list: any[], value: any, position: number): any[];
./types/p5/global.d.ts: function subset(list: any[], start: number, count?: number): any[];
./types/p5/global.d.ts: function int(ns: any[]): number[];
./types/p5/global.d.ts: function boolean(ns: any[]): boolean[];
./types/p5/global.d.ts: function byte(ns: any[]): number[];
./types/p5/global.d.ts: function char(ns: any[]): string[];
./types/p5/global.d.ts: function join(list: any[], separator: string): string;
./types/p5/global.d.ts: function nfs(nums: any[], left?: number, right?: number): string[];
./types/p5/global.d.ts: function buildGeometry(callback: (...args: any[]) => any): p5.Geometry;
./types/p5/global.d.ts: successCallback?: (...args: any[]) => any,
./types/p5/global.d.ts: failureCallback?: (...args: any[]) => any
./types/p5/global.d.ts: function userStartAudio(elements?: Element | any[], callback?: (...args: any[]) => any): Promise<any>;
./types/p5/global.d.ts: path: string | any[],
./types/p5/global.d.ts: successCallback?: (...args: any[]) => any,
./types/p5/global.d.ts: errorCallback?: (...args: any[]) => any,
./types/p5/global.d.ts: whileLoading?: (...args: any[]) => any
./types/p5/global.d.ts: callback?: (...args: any[]) => any,
./types/p5/global.d.ts: errorCallback?: (...args: any[]) => any
./types/p5/src/webgl/p5.Geometry.d.ts: constructor(detailX?: number, detailY?: number, callback?: (...args: any[]) => any);
./types/p5/src/webgl/material.d.ts: successCallback?: (...args: any[]) => any,
./types/p5/src/webgl/material.d.ts: failureCallback?: (...args: any[]) => any
./types/p5/src/webgl/3d_primitives.d.ts: buildGeometry(callback: (...args: any[]) => any): Geometry;
./types/p5/src/webgl/p5.Framebuffer.d.ts: draw(callback: (...args: any[]) => any): void;
./types/p5/src/FFT.d.ts: analyze(): any[];
./types/p5/src/FFT.d.ts: waveform(): any[];
./types/p5/src/typography/p5.Font.d.ts: textToPoints(str: string, x: number, y: number, fontSize?: number, options?: object): any[];
./types/p5/src/typography/loading_displaying.d.ts: successCallback?: (...args: any[]) => any,
./types/p5/src/typography/loading_displaying.d.ts: failureCallback?: (...args: any[]) => any
./types/p5/src/typography/loading_displaying.d.ts: str: string | object | any[] | number | boolean,
./types/p5/src/io/files.d.ts: write(data: string | number | any[]): void;
./types/p5/src/io/files.d.ts: print(data: string | number | any[]): void;
./types/p5/src/io/files.d.ts: successCallback?: (...args: any[]) => any,
./types/p5/src/io/files.d.ts: errorCallback?: (...args: any[]) => any
./types/p5/src/io/files.d.ts: successCallback?: (...args: any[]) => any,
./types/p5/src/io/files.d.ts: errorCallback?: (...args: any[]) => any
./types/p5/src/io/files.d.ts: callback?: (...args: any[]) => any,
./types/p5/src/io/files.d.ts: errorCallback?: (...args: any[]) => any
./types/p5/src/io/files.d.ts: loadXML(path: string, successCallback?: (...args: any[]) => any, errorCallback?: (...args: any[]) => any): XML;
./types/p5/src/io/files.d.ts: loadBytes(file: string, callback?: (...args: any[]) => any, errorCallback?: (...args: any[]) => any): object;
./types/p5/src/io/files.d.ts: callback?: (...args: any[]) => any,
./types/p5/src/io/files.d.ts: errorCallback?: (...args: any[]) => any
./types/p5/src/io/files.d.ts: callback?: (...args: any[]) => any,
./types/p5/src/io/files.d.ts: errorCallback?: (...args: any[]) => any
./types/p5/src/io/files.d.ts: httpGet(path: string, callback: (...args: any[]) => any, errorCallback?: (...args: any[]) => any): Promise<any>;
./types/p5/src/io/files.d.ts: callback?: (...args: any[]) => any,
./types/p5/src/io/files.d.ts: errorCallback?: (...args: any[]) => any
./types/p5/src/io/files.d.ts: callback?: (...args: any[]) => any,
./types/p5/src/io/files.d.ts: errorCallback?: (...args: any[]) => any
./types/p5/src/io/files.d.ts: callback: (...args: any[]) => any,
./types/p5/src/io/files.d.ts: errorCallback?: (...args: any[]) => any
./types/p5/src/io/files.d.ts: callback?: (...args: any[]) => any,
./types/p5/src/io/files.d.ts: errorCallback?: (...args: any[]) => any
./types/p5/src/io/files.d.ts: callback?: (...args: any[]) => any,
./types/p5/src/io/files.d.ts: errorCallback?: (...args: any[]) => any
./types/p5/src/io/files.d.ts: saveJSON(json: any[] | object, filename: string, optimize?: boolean): void;
./types/p5/src/io/p5.Table.d.ts: getColumn(column: string | number): any[];
./types/p5/src/io/p5.Table.d.ts: getArray(): any[];
./types/p5/src/core/p5.Element.d.ts: mousePressed(fxn: ((...args: any[]) => any) | boolean): Element;
./types/p5/src/core/p5.Element.d.ts: doubleClicked(fxn: ((...args: any[]) => any) | boolean): Element;
./types/p5/src/core/p5.Element.d.ts: mouseWheel(fxn: ((...args: any[]) => any) | boolean): Element;
./types/p5/src/core/p5.Element.d.ts: mouseReleased(fxn: ((...args: any[]) => any) | boolean): Element;
./types/p5/src/core/p5.Element.d.ts: mouseClicked(fxn: ((...args: any[]) => any) | boolean): Element;
./types/p5/src/core/p5.Element.d.ts: mouseMoved(fxn: ((...args: any[]) => any) | boolean): Element;
./types/p5/src/core/p5.Element.d.ts: mouseOver(fxn: ((...args: any[]) => any) | boolean): Element;
./types/p5/src/core/p5.Element.d.ts: mouseOut(fxn: ((...args: any[]) => any) | boolean): Element;
./types/p5/src/core/p5.Element.d.ts: touchStarted(fxn: ((...args: any[]) => any) | boolean): Element;
./types/p5/src/core/p5.Element.d.ts: touchMoved(fxn: ((...args: any[]) => any) | boolean): Element;
./types/p5/src/core/p5.Element.d.ts: touchEnded(fxn: ((...args: any[]) => any) | boolean): Element;
./types/p5/src/core/p5.Element.d.ts: dragOver(fxn: ((...args: any[]) => any) | boolean): Element;
./types/p5/src/core/p5.Element.d.ts: dragLeave(fxn: ((...args: any[]) => any) | boolean): Element;
./types/p5/src/core/transform.d.ts: applyMatrix(arr: any[]): p5;
./types/p5/src/SoundFile.d.ts: setPath(path: string, successCallback?: (...args: any[]) => any): void;
./types/p5/src/SoundFile.d.ts: onended(callback: (...args: any[]) => any): void;
./types/p5/src/utilities/array_functions.d.ts: append(array: any[], value: any): any[];
./types/p5/src/utilities/array_functions.d.ts: arrayCopy(src: any[], srcPosition: number, dst: any[], dstPosition: number, length: number): void;
./types/p5/src/utilities/array_functions.d.ts: arrayCopy(src: any[], dst: any[], length?: number): void;
./types/p5/src/utilities/array_functions.d.ts: concat(a: any[], b: any[]): any[];
./types/p5/src/utilities/array_functions.d.ts: reverse(list: any[]): any[];
./types/p5/src/utilities/array_functions.d.ts: shorten(list: any[]): any[];
./types/p5/src/utilities/array_functions.d.ts: shuffle(array: any[], bool?: boolean): any[];
./types/p5/src/utilities/array_functions.d.ts: sort(list: any[], count?: number): any[];
./types/p5/src/utilities/array_functions.d.ts: splice(list: any[], value: any, position: number): any[];
./types/p5/src/utilities/array_functions.d.ts: subset(list: any[], start: number, count?: number): any[];
./types/p5/src/utilities/string_functions.d.ts: join(list: any[], separator: string): string;
./types/p5/src/utilities/string_functions.d.ts: nfs(nums: any[], left?: number, right?: number): string[];
./types/p5/src/utilities/conversion.d.ts: int(ns: any[]): number[];
./types/p5/src/utilities/conversion.d.ts: boolean(ns: any[]): boolean[];
./types/p5/src/utilities/conversion.d.ts: byte(ns: any[]): number[];
./types/p5/src/utilities/conversion.d.ts: char(ns: any[]): string[];
./types/p5/src/image/image.d.ts: callback?: (p1: any[]) => any
./types/p5/src/dom/dom.d.ts: onended(callback: (...args: any[]) => any): MediaElement;
./types/p5/src/dom/dom.d.ts: addCue(time: number, callback: (...args: any[]) => any, value?: object): number;
./types/p5/src/dom/dom.d.ts: changed(fxn: ((...args: any[]) => any) | boolean): p5;
./types/p5/src/dom/dom.d.ts: input(fxn: ((...args: any[]) => any) | boolean): p5;
./types/p5/src/dom/dom.d.ts: createImg(src: string, alt: string, crossOrigin?: string, successCallback?: (...args: any[]) => any): Element;
./types/p5/src/dom/dom.d.ts: createFileInput(callback: (...args: any[]) => any, multiple?: boolean): File;
./types/p5/src/dom/dom.d.ts: createVideo(src: string | string[], callback?: (...args: any[]) => any): MediaElement;
./types/p5/src/dom/dom.d.ts: createAudio(src?: string | string[], callback?: (...args: any[]) => any): MediaElement;
./types/p5/src/dom/dom.d.ts: callback?: (...args: any[]) => any
./types/p5/src/dom/dom.d.ts: drop(callback: (...args: any[]) => any, fxn?: (...args: any[]) => any): Element;
./types/p5/src/color/setting.d.ts: clip(callback: (...args: any[]) => any, options?: object): void;
./types/p5/src/math/p5.Vector.d.ts: static equals(v1: Vector | any[], v2: Vector | any[]): boolean;
./types/p5/src/math/p5.Vector.d.ts: equals(value: Vector | any[]): boolean;
./types/p5/src/math/random.d.ts: random(choices: any[]): any;
./types/p5/src/data/local_storage.d.ts: storeItem(key: string, value: string | number | boolean | object | any[]): void;
./types/p5/src/data/local_storage.d.ts: getItem(key: string): string | number | boolean | object | any[];
./types/p5/lib/addons/p5.sound.d.ts: bands: any[];
./types/p5/lib/addons/p5.sound.d.ts: constructor(path: string, callback?: (...args: any[]) => any, errorCallback?: (...args: any[]) => any);
./types/p5/lib/addons/p5.sound.d.ts: addImpulse(path: string, callback: (...args: any[]) => any, errorCallback: (...args: any[]) => any): void;
./types/p5/lib/addons/p5.sound.d.ts: resetImpulse(path: string, callback: (...args: any[]) => any, errorCallback: (...args: any[]) => any): void;
./types/p5/lib/addons/p5.sound.d.ts: impulses: any[];
./types/p5/lib/addons/p5.sound.d.ts: constructor(name: string, callback: (...args: any[]) => any, sequence: any[]);
./types/p5/lib/addons/p5.sound.d.ts: sequence: any[];
./types/p5/lib/addons/p5.sound.d.ts: replaceSequence(phraseName: string, sequence: any[]): void;
./types/p5/lib/addons/p5.sound.d.ts: onStep(callback: (...args: any[]) => any): void;
./types/p5/lib/addons/p5.sound.d.ts: constructor(callback: (...args: any[]) => any, interval?: number | string);
./types/p5/lib/addons/p5.sound.d.ts: onPeak(callback: (...args: any[]) => any, val?: object): void;
./types/p5/lib/addons/p5.sound.d.ts: record(soundFile: SoundFile, duration?: number, callback?: (...args: any[]) => any): void;
./types/p5/lib/addons/p5.sound.d.ts: constructor(freqLow: number, freqHigh: number, threshold: number, callback: (...args: any[]) => any);
./types/p5/lib/addons/p5.sound.d.ts: userStartAudio(elements?: Element | any[], callback?: (...args: any[]) => any): Promise<any>;
./types/p5/lib/addons/p5.sound.d.ts: path: string | any[],
./types/p5/lib/addons/p5.sound.d.ts: successCallback?: (...args: any[]) => any,
./types/p5/lib/addons/p5.sound.d.ts: errorCallback?: (...args: any[]) => any,
./types/p5/lib/addons/p5.sound.d.ts: whileLoading?: (...args: any[]) => any
./types/p5/lib/addons/p5.sound.d.ts: callback?: (...args: any[]) => any,
./types/p5/lib/addons/p5.sound.d.ts: errorCallback?: (...args: any[]) => any
./types/p5/lib/addons/p5.sound.d.ts: onended(callback: (...args: any[]) => any): void;
./types/p5/lib/addons/p5.sound.d.ts: setPath(path: string, callback: (...args: any[]) => any): void;
./types/p5/lib/addons/p5.sound.d.ts: setBuffer(buf: any[]): void;
./types/p5/lib/addons/p5.sound.d.ts: addCue(time: number, callback: (...args: any[]) => any, value?: object): number;
./types/p5/lib/addons/p5.sound.d.ts: waveform(bins?: number, precision?: string): any[];
./types/p5/lib/addons/p5.sound.d.ts: analyze(bins?: number, scale?: number): any[];
./types/p5/lib/addons/p5.sound.d.ts: linAverages(N: number): any[];
./types/p5/lib/addons/p5.sound.d.ts: logAverages(octaveBands: any[]): any[];
./types/p5/lib/addons/p5.sound.d.ts: getOctaveBands(N: number, fCtr0: number): any[];
./types/p5/lib/addons/p5.sound.d.ts: start(successCallback?: (...args: any[]) => any, errorCallback?: (...args: any[]) => any): void;
./types/p5/lib/addons/p5.sound.d.ts: getSources(successCallback?: (...args: any[]) => any, errorCallback?: (...args: any[]) => any): Promise<any>;
./types/p5/lib/addons/p5.sound.d.ts: set(xVal: number, yVal: number, zVal: number, time: number): any[];
./types/p5/lib/addons/p5.sound.d.ts: orient(xVal: number, yVal: number, zVal: number, time: number): any[];
w@archlinux ~/g/p5.ts (master)> |
I think it's ok if we don't hit all of these immediately -- some are array functions that we are removing in 2.0, and others are in the old p5.sound library that has been rewritten and will live separately in https://github.com/processing/p5.sound.js (which itself will need type info, but we will likely tackle this separately), and others I think are supposed to be there (e.g. the return value of We're having some documentation writers come to help out soon in preparation for 2.0, so having this list is great, and we can tell them to add types to things like callback arguments that currently are just |
This is based off of #6777 and #6972. WIP.
Maybe an AST based approach could be added to the current script. This still needs to handle a few cases, such as static methods.
Changes:
Screenshots of the change:
PR Checklist
npm run lint
passes