fread-ink / epub-cfi-resolver

A simple parser, resolver and generator for the EPUB-CFI format
GNU Affero General Public License v3.0
19 stars 3 forks source link

Adding JSDoc support #24

Open jangxyz opened 10 months ago

jangxyz commented 10 months ago

This is related to #6.

If there is a way to add this type information in some extra file that doesn't add any typescript to the existing code then that would be fine.

TypeScript supports JSDoc annotations, so you could actually achieve this.

Some thing like this:

JSDoc code sample ```javascript /** * Generate CFI string from node and offset. * * @param {Node | { node: Node, offset?: number }[]} node * @param {number} [offset] * @param {string} [extra] * @returns {string} */ static generate(node, offset, extra) { /** @type {string} */ let cfi; if (node instanceof Array) { let strs = []; for (let o of node) { strs.push(this.generatePart(o.node, o.offset)); } cfi = strs.join('!'); } else { cfi = this.generatePart(node, offset, extra); } if (extra) cfi += extra; return `epubcfi(${cfi})`; } ```

You can generate a .d.ts type generation file so users can have type completion and auto-suggestion benefits.

JSDoc is a bit painful to write than in TypeScript, but it's a perfect harmony for both parties; those who do not want to author in TypeScript & for people who want to have type definition support.

Actually, I have done most of the part, on pursuit of trying to understand the code, except for some confusions on this.from and this.to -- can't really understand which type they are supposed to be.

If you are interested, and help me understand the code a little bit, maybe I can submit a PR for it.