Skip to content

Commit

Permalink
Update some JSDocs
Browse files Browse the repository at this point in the history
  • Loading branch information
ericyd committed Feb 6, 2024
1 parent 3ec0efb commit acd8212
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/components/circle.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Tag } from './tag.js'
* @property {number} x
* @property {number} y
* @property {number} radius
* @extends Tag
*/
export class Circle extends Tag {
/** @type {number} */
Expand Down
4 changes: 4 additions & 0 deletions lib/components/hexagon.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import { Polygon } from './polygon.js'
* @property {Radians} [rotation=0]
*/

/**
* @class Hexagon
* @extends Polygon
*/
export class Hexagon extends Polygon {
/** @type {number} */
#rotation = 0
Expand Down
4 changes: 4 additions & 0 deletions lib/components/linear-gradient.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import { Tag } from './tag.js'
* @property {number} [numericPrecision=Infinity]
*/

/**
* @class LinearGradient
* @extends Tag
*/
export class LinearGradient extends Tag {
/** @param {LinearGradientAttributes} [attributes] */
constructor({
Expand Down
7 changes: 5 additions & 2 deletions lib/components/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { toFixedPrecision } from '../math.js'

/**
* @class Path
* @extends Tag
*/
export class Path extends Tag {
/** @type {PathInstruction[]} */
Expand Down Expand Up @@ -217,6 +218,8 @@ export class Path extends Tag {
}
}

/** @typedef {PathAttributes | ((Path: Path) => void)} PathAttributesOrBuilder */

/**
* @overload
* @param {PathAttributes} attrsOrBuilder
Expand All @@ -230,11 +233,11 @@ export class Path extends Tag {
* @returns {Path}
*/
/**
* @param {PathAttributes | ((Path: Path) => void)} attrsOrBuilder
* @param {PathAttributesOrBuilder} attrsOrBuilder
* @param {PathAttributes} [attributes={}]
* @returns {Path}
*/
export function path(attrsOrBuilder, attributes = {}) {
export const path = (attrsOrBuilder, attributes = {}) => {
if (typeof attrsOrBuilder === 'function') {
const c = new Path(attributes)
attrsOrBuilder(c)
Expand Down
4 changes: 4 additions & 0 deletions lib/components/polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import { Tag } from './tag.js'
* @property {Vector2[]} [points=[]]
*/

/**
* @class Polygon
* @extends Tag
*/
export class Polygon extends Tag {
/** @type {Vector2[]} */
points = []
Expand Down
4 changes: 4 additions & 0 deletions lib/components/polyline.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import { Tag } from './tag.js'
* @property {Vector2[]} [points=[]]
*/

/**
* @class Polyline
* @extends Tag
*/
export class Polyline extends Tag {
/** @type {Vector2} */
cursor = new Vector2(0, 0)
Expand Down
1 change: 1 addition & 0 deletions lib/components/rectangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { Tag } from './tag.js'
* })
* @example
* const r = rect({ x: 1, y: 10, width: 100, height: 15, borderRadius: 1.4 })
* @extends Tag
*/
export class Rectangle extends Tag {
/**
Expand Down
28 changes: 21 additions & 7 deletions lib/components/svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { Vector2, vec2 } from '../vector2.js'
* Although you can construct this class manually, it's much nicer to the the `svg` builder function,
* or the `renderSvg` function if you're running this locally or on a server.
* @example
* ```js
* import { svg, vec2 } from '@salamivg/core'
*
* const document = svg({ width: 100, height: 100, scale: 5 }, (doc) => {
Expand All @@ -41,7 +40,7 @@ import { Vector2, vec2 } from '../vector2.js'
* })
*
* console.log(document.render())
* ```
* @extends Tag
*/
export class Svg extends Tag {
/** @type {LinearGradient[]} */
Expand Down Expand Up @@ -71,7 +70,6 @@ export class Svg extends Tag {
return vec2(this.width / 2, this.height / 2)
}

// TODO@v1: find a more generic way of expressing this "instance or builder" pattern
/**
* @param {Path | ((path: Path) => void)} instanceOrBuilder
*/
Expand Down Expand Up @@ -107,14 +105,30 @@ export class Svg extends Tag {
}

/**
* TODO@v1: "or builder" can be anything accepted by the "circle" helper
* TODO@v1: add overload type defs
* @param {Circle | ((circle: Circle) => void)} instanceOrBuilder
* @overload
* @param {Circle} instance
* @returns {Circle}
*/
/**
* @overload
* @param {import('./circle.js').CircleAttributes} attributes
* @returns {Circle}
*/
/**
* @overload
* @param {((circle: Circle) => void)} builder} builder
* @returns {Circle}
*/
/**
* @param {Circle | import('./circle.js').CircleAttributes | ((circle: Circle) => void)} instanceOrBuilder
* @returns {Circle}
*/
circle(instanceOrBuilder) {
// @ts-expect-error TS is complaining because the parent method returns a Tag, which isn't a Circle. What I mean to say is any subclass of Tag, not sure how to express that in JSDoc.
return instanceOrBuilder instanceof Circle
? this.addChild(instanceOrBuilder)
: this.addChild(circle(instanceOrBuilder))
: // @ts-expect-error I can't figure out how to make TS compile with overloads calling other overloads
this.addChild(circle(instanceOrBuilder))
}

/**
Expand Down

0 comments on commit acd8212

Please sign in to comment.