Skip to content

Commit

Permalink
Automatic types generation from jsdoc comments (#662)
Browse files Browse the repository at this point in the history
Generates typescript types from jsdoc comments. Adjustments have been made  to ensure the api docs page uses the ICAL. prefix on the types, as this is the intended way to use the library.
  • Loading branch information
jannikac authored Jun 13, 2024
1 parent eddffb1 commit e82ff54
Show file tree
Hide file tree
Showing 28 changed files with 596 additions and 500 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ docs/recur-tester.html
tools/vzic/
tools/tzdb/
tools/libical/
tools/jsdoc-symbols-temp.json
coverage/
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default [
"!test/**/*.{js,cjs}",
"!tools/scriptutils.js",
"!tools/ICALTester/**/*.js",
"!tools/jsdoc-ical.cjs",
"!eslint.config.js",
"!rollup.config.js",
"!karma.conf.cjs"
Expand Down
10 changes: 10 additions & 0 deletions jsdoc-prepare.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"source": {
"include": "lib/ical",
"includePattern": ".js$"
},
"plugins": ["tools/jsdoc-collect-types.cjs", "node_modules/jsdoc-tsimport-plugin/index.js"],
"opts": {
"destination": "docs/api/"
}
}
2 changes: 1 addition & 1 deletion jsdoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"include": "lib/ical",
"includePattern": ".js$"
},
"plugins": ["plugins/markdown"],
"plugins": ["plugins/markdown", "tools/jsdoc-ical.cjs", "node_modules/jsdoc-tsimport-plugin/index.js"],
"opts": {
"encoding": "utf8",
"readme": "README.md",
Expand Down
5 changes: 2 additions & 3 deletions lib/ical/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
/**
* Represents the BINARY value type, which contains extra methods for encoding and decoding.
*
* @class
* @alias ICAL.Binary
* @memberof ICAL
*/
class Binary {
/**
* Creates a binary value from the given string.
*
* @param {String} aString The binary value string
* @return {ICAL.Binary} The binary value instance
* @return {Binary} The binary value instance
*/
static fromString(aString) {
return new Binary(aString);
Expand Down
60 changes: 43 additions & 17 deletions lib/ical/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ const COMPONENT_INDEX = 2;
* Wraps a jCal component, adding convenience methods to add, remove and update subcomponents and
* properties.
*
* @class
* @alias ICAL.Component
* @memberof ICAL
*/
class Component {
/**
Expand All @@ -31,11 +30,11 @@ class Component {
}

/**
* Creates a new ICAL.Component instance.
* Creates a new Component instance.
*
* @param {Array|String} jCal Raw jCal component data OR name of new
* component
* @param {ICAL.Component} parent Parent component to associate
* @param {Component=} parent Parent component to associate
*/
constructor(jCal, parent) {
if (typeof(jCal) === 'string') {
Expand Down Expand Up @@ -82,8 +81,20 @@ class Component {
*/
_timezoneCache = null;

/**
* @private
*/
_components = null;

/**
* @private
*/
_properties = null;

/**
* The name of this component
*
* @type {String}
* @readonly
*/
get name() {
Expand All @@ -101,6 +112,9 @@ class Component {
return parentDesign || design.getDesignSet(this.name);
}

/**
* @private
*/
_hydrateComponent(index) {
if (!this._components) {
this._components = [];
Expand All @@ -120,6 +134,9 @@ class Component {
return (this._components[index] = comp);
}

/**
* @private
*/
_hydrateProperty(index) {
if (!this._properties) {
this._properties = [];
Expand All @@ -143,7 +160,7 @@ class Component {
* Finds first sub component, optionally filtered by name.
*
* @param {String=} name Optional name to filter by
* @return {?ICAL.Component} The found subcomponent
* @return {?Component} The found subcomponent
*/
getFirstSubcomponent(name) {
if (name) {
Expand Down Expand Up @@ -171,7 +188,7 @@ class Component {
* Finds all sub components, optionally filtering by name.
*
* @param {String=} name Optional name to filter by
* @return {ICAL.Component[]} The found sub components
* @return {Component[]} The found sub components
*/
getAllSubcomponents(name) {
let jCalLen = this.jCal[COMPONENT_INDEX].length;
Expand Down Expand Up @@ -226,7 +243,7 @@ class Component {
* Finds the first property, optionally with the given name.
*
* @param {String=} name Lowercase property name
* @return {?ICAL.Property} The found property
* @return {?Property} The found property
*/
getFirstProperty(name) {
if (name) {
Expand Down Expand Up @@ -268,7 +285,7 @@ class Component {
* Get all properties in the component, optionally filtered by name.
*
* @param {String=} name Lowercase property name
* @return {ICAL.Property[]} List of properties
* @return {Property[]} List of properties
*/
getAllProperties(name) {
let jCalLen = this.jCal[PROPERTY_INDEX].length;
Expand Down Expand Up @@ -298,6 +315,9 @@ class Component {
}
}

/**
* @private
*/
_removeObjectByIndex(jCalIndex, cache, index) {
cache = cache || [];
// remove cached version
Expand All @@ -314,6 +334,9 @@ class Component {
this.jCal[jCalIndex].splice(index, 1);
}

/**
* @private
*/
_removeObject(jCalIndex, cache, nameOrObject) {
let i = 0;
let objects = this.jCal[jCalIndex];
Expand All @@ -339,6 +362,9 @@ class Component {
return false;
}

/**
* @private
*/
_removeAllObjects(jCalIndex, cache, name) {
let cached = this[cache];

Expand All @@ -359,8 +385,8 @@ class Component {
/**
* Adds a single sub component.
*
* @param {ICAL.Component} component The component to add
* @return {ICAL.Component} The passed in component
* @param {Component} component The component to add
* @return {Component} The passed in component
*/
addSubcomponent(component) {
if (!this._components) {
Expand All @@ -383,7 +409,7 @@ class Component {
* Removes a single component by name or the instance of a specific
* component.
*
* @param {ICAL.Component|String} nameOrComp Name of component, or component
* @param {Component|String} nameOrComp Name of component, or component
* @return {Boolean} True when comp is removed
*/
removeSubcomponent(nameOrComp) {
Expand All @@ -409,8 +435,8 @@ class Component {
/**
* Adds an {@link ICAL.Property} to the component.
*
* @param {ICAL.Property} property The property to add
* @return {ICAL.Property} The passed in property
* @param {Property} property The property to add
* @return {Property} The passed in property
*/
addProperty(property) {
if (!(property instanceof Property)) {
Expand Down Expand Up @@ -438,7 +464,7 @@ class Component {
*
* @param {String} name Property name to add
* @param {String|Number|Object} value Property value
* @return {ICAL.Property} The created property
* @return {Property} The created property
*/
addPropertyWithValue(name, value) {
let prop = new Property(name);
Expand All @@ -456,7 +482,7 @@ class Component {
*
* @param {String} name Property name to update
* @param {String|Number|Object} value Property value
* @return {ICAL.Property} The created property
* @return {Property} The created property
*/
updatePropertyWithValue(name, value) {
let prop = this.getFirstProperty(name);
Expand All @@ -474,7 +500,7 @@ class Component {
* Removes a single property by name or the instance of the specific
* property.
*
* @param {String|ICAL.Property} nameOrProp Property name or instance to remove
* @param {String|Property} nameOrProp Property name or instance to remove
* @return {Boolean} True, when deleted
*/
removeProperty(nameOrProp) {
Expand Down Expand Up @@ -523,7 +549,7 @@ class Component {
* matched, returns null.
*
* @param {String} tzid The ID of the time zone to retrieve
* @return {ICAL.Timezone} The time zone corresponding to the ID, or null
* @return {Timezone} The time zone corresponding to the ID, or null
*/
getTimeZoneByID(tzid) {
// VTIMEZONE components can only appear as a child of the VCALENDAR
Expand Down
9 changes: 4 additions & 5 deletions lib/ical/component_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ import Timezone from "./timezone.js";
*
* parser.process(stringOrComponent);
*
* @class
* @alias ICAL.ComponentParser
* @memberof ICAL
*/
class ComponentParser {
/**
Expand Down Expand Up @@ -89,15 +88,15 @@ class ComponentParser {
* Fired when a top level component (VTIMEZONE) is found
*
* @callback
* @param {ICAL.Timezone} component Timezone object
* @param {Timezone} component Timezone object
*/
ontimezone = /* c8 ignore next */ function(component) {};

/**
* Fired when a top level component (VEVENT) is found.
*
* @callback
* @param {ICAL.Event} component Top level component
* @param {Event} component Top level component
*/
onevent = /* c8 ignore next */ function(component) {};

Expand All @@ -107,7 +106,7 @@ class ComponentParser {
*
* Events must be registered prior to calling this method.
*
* @param {ICAL.Component|String|Object} ical The component to process,
* @param {Component|String|Object} ical The component to process,
* either in its final form, as a jCal Object, or string representation
*/
process(ical) {
Expand Down
15 changes: 7 additions & 8 deletions lib/ical/duration.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ const DATA_PROPS_TO_COPY = ["weeks", "days", "hours", "minutes", "seconds", "isN
* This class represents the "duration" value type, with various calculation
* and manipulation methods.
*
* @class
* @alias ICAL.Duration
* @memberof ICAL
*/
class Duration {
/**
* Returns a new ICAL.Duration instance from the passed seconds value.
*
* @param {Number} aSeconds The seconds to create the instance from
* @return {ICAL.Duration} The newly created duration instance
* @return {Duration} The newly created duration instance
*/
static fromSeconds(aSeconds) {
return (new Duration()).fromSeconds(aSeconds);
Expand All @@ -41,7 +40,7 @@ class Duration {
* Creates a new {@link ICAL.Duration} instance from the passed string.
*
* @param {String} aStr The string to parse
* @return {ICAL.Duration} The created duration instance
* @return {Duration} The created duration instance
*/
static fromString(aStr) {
let pos = 0;
Expand Down Expand Up @@ -76,7 +75,7 @@ class Duration {
* @param {Number} aData.minutes Duration in minutes
* @param {Number} aData.seconds Duration in seconds
* @param {Boolean} aData.isNegative If true, the duration is negative
* @return {ICAL.Duration} The createad duration instance
* @return {Duration} The createad duration instance
*/
static fromData(aData) {
return new Duration(aData);
Expand Down Expand Up @@ -159,7 +158,7 @@ class Duration {
/**
* Returns a clone of the duration object.
*
* @return {ICAL.Duration} The cloned object
* @return {Duration} The cloned object
*/
clone() {
return Duration.fromData(this);
Expand All @@ -182,7 +181,7 @@ class Duration {
* accordingly.
*
* @param {Number} aSeconds The duration value in seconds
* @return {ICAL.Duration} Returns this instance
* @return {Duration} Returns this instance
*/
fromSeconds(aSeconds) {
let secs = Math.abs(aSeconds);
Expand Down Expand Up @@ -246,7 +245,7 @@ class Duration {
/**
* Compares the duration instance with another one.
*
* @param {ICAL.Duration} aOther The instance to compare with
* @param {Duration} aOther The instance to compare with
* @return {Number} -1, 0 or 1 for less/equal/greater
*/
compare(aOther) {
Expand Down
Loading

0 comments on commit e82ff54

Please sign in to comment.