Skip to content
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

JSDoc support for @yields #23857

Open
SergioMorchon opened this issue May 3, 2018 · 6 comments
Open

JSDoc support for @yields #23857

SergioMorchon opened this issue May 3, 2018 · 6 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Domain: JSDoc Relates to JSDoc parsing and type generation Suggestion An idea for TypeScript

Comments

@SergioMorchon
Copy link

SergioMorchon commented May 3, 2018

It would be nice to be able to use the @yields JSDoc tag.

We work with JS and JSDoc, but taking advantage of TSC and VSCode.
We also use the valid-jsdoc eslint rule.

We have:

/** @typedef Thing (...) */

function* walkThings() {
//... some yield here and there
}

We want to

/**
 * @yields {Thing}
 */
function* walkThings() {
//... some yield here and there
}

We can't do

/**
 * @returns {IterableIterator<Something>}
 */
function* walkThings() {
//... some yield here and there
}

Because it doesn't return, but yields, and eslint complains about it. We can disable the rule here, but it is not the desirable scenario.

@SergioMorchon SergioMorchon changed the title JSDoc support for @yields JSDoc support for @yields May 3, 2018
@mhegazy mhegazy added Domain: JSDoc Relates to JSDoc parsing and type generation Suggestion An idea for TypeScript labels May 3, 2018
@DanielRosenwasser
Copy link
Member

Just to weigh in here, sounds like a bug on the ESLint side as well.

@SergioMorchon
Copy link
Author

SergioMorchon commented May 3, 2018

Just to weigh in here, sounds like a bug on the ESLint side as well.
I'm not really confident about that @DanielRosenwasser :

  • return means something different from yield at a syntactic level.
  • The IterableIterator<T> is an implementation detail, the code does not return an iterable iterator, nor the developer wrote that. Just multiple yields.
  • I couldn't find any standard documentation about what typescript defined as IterableIterator, so I wouldn't expect from eslint to know about it. Just curious about it: where does it comes from? Is it an internal definition for typescript generators?

@weswigham
Copy link
Member

weswigham commented May 3, 2018

Is it an internal definition for typescript generators?

It's an interface (well, suite of them) defining the shape of a generator or IterableIterator spec object:

interface IteratorResult<T> {
    done: boolean;
    value: T;
}

interface Iterator<T> {
    next(value?: any): IteratorResult<T>;
    return?(value?: any): IteratorResult<T>;
    throw?(e?: any): IteratorResult<T>;
}

interface Iterable<T> {
    [Symbol.iterator](): Iterator<T>;
}

interface IterableIterator<T> extends Iterator<T> {
    [Symbol.iterator](): IterableIterator<T>;
}

@SergioMorchon
Copy link
Author

Thank you @weswigham for the response. I already noticed that (that's the reason why I used it in the JSDoc part). I wanted to know if there were such definition in a standard way, just like HTMLElement does, or if it was a definition made by the typescript developers to model the iterable iterator instances (thinking about PromiseLike<T>).

You solved my question anyway, thanks again!

@weswigham weswigham added the Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature label Nov 6, 2018
achingbrain pushed a commit to ipfs/js-ipfs-utils that referenced this issue Apr 19, 2022
Motivation:
* Fix ipfs/js-ipfs#4080
* tl;dr
  * fix `import globSourceImport from 'ipfs-utils/src/files/glob-source.js'` from typescript
  * fix importing ipfs-core from typescript

Context:
* microsoft/TypeScript#23857

Strategy
* Use jsdoc `@return` instead of `@yields` because tsc doesn't understand that `@yields` implies a `@return` type

Test Case
* gobengo/ipfs-core-esm-test#3
* I tested locally with `npm link` and this seemed to fix it
@clshortfuse
Copy link

clshortfuse commented Nov 25, 2022

The eslint rule in the original comment is now deprecated, but https://github.com/gajus/eslint-plugin-jsdoc can be satisfied with @yield (at least on the recommended ruleset). Typescript needs @return. Both can be used, but it's not really ideal:

/**
 * @template {boolean} [E=null]
 * @template {boolean} [A=null]
 * @template {boolean} [T=null]
 * @template {boolean} [C=null]
 * @param {Node} root Root node to walk
 * @param {Object} filter Tree walking options
 * @param {E} [filter.element] Yield elements
 * @param {A} [filter.attribute] Yield attributes
 * @param {T} [filter.text] Yield Text node
 * @param {C} [filter.comment] Yield Comment nodes
 * @yields {
 *  (E extends null ? null : Element) |
 *  (A extends null ? null : Attr) |
 *  (T extends null ? null : Text) |
 *  (C extends null ? null : Comment)
 * }
 * @return {Iterable<
 *  (E extends null ? null : Element) |
 *  (A extends null ? null : Attr) |
 *  (T extends null ? null : Text) |
 *  (C extends null ? null : Comment)
 * >} Iterable
 */
export function* iterateNodes(root, filter = {}) {

I have TS syntax in my JSDoc, so I have disabled "jsdoc/valid-types". Other than that, eslint is happy.

Edit: Intellisense (TS) parses it fine as well.

image

Tooltip documentation bug is filed here: microsoft/vscode#164888

Edit2: I think I'm supposed to use Generator<T> instead of Iterator<T>.

@AnrDaemon
Copy link

I think I'm supposed to use Generator<T> instead of Iterator<T>.

Seems so. Works on my side like that.

/** Runs `forEach()` callback over warehouses cache
 *
 * @returns {Generator<EqShowItem>}
 */
export function* forEachWarehouse() {
    // …
}

And then [...module.forEachWarehouse()].filter((wh) => …) correctly infer type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Domain: JSDoc Relates to JSDoc parsing and type generation Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants