microsoft / microsoft/TypeScript

Expose JsDoc default value for optional parameters in AST

Open
#29,521 3 comments 6 reactions 1 assignee View on GitHub

@sandersn is already working on this.

Since Jan 23, 2019.

Domain: API Domain: JSDoc In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Copied from my SO question:

I expected to be able to extract the default value of a JsDoc param but I'm actually unable to find the default value on the JsDoc node. For example, give this:

/** * @param [bar=42] Bar comment. */

I'd like to extract 42 from the node. Yet I find no default value field on the ts node:

image

It exposes isBracketed but I cannot find the 42. Typescript does claim to support default JsDoc arguments:

https://github.com/Microsoft/TypeScript/wiki/JSDoc-support-in-JavaScript#param-and-returns

Yet I cannot seem to find the exposed in the AST... And that's because, it's not currently in the AST! The parser currently skips over the expression without storing the return value anywhere.

// May have an optional default, e.g. '[foo = 42]'
if (parseOptionalToken(SyntaxKind.EqualsToken)) {
    parseExpression();
}

Source

Use Cases

I'm automating harvesting JsDoc comments for inclusion in my NPM README.md files. I'm coding in pure JS but using the TypeScript parser to dig out the JsDoc. (I would use TS except I cannot express that a key must be a symbol! So you guys owe me! jk)

Examples

I'd like to expose via the TS AST that isFunction is default false:

/**
 * @description Creates a descriptor that is an alias of another accessor or function.
 * 
 * @param name The name of the accessor or function being aliased.
 * @param [isFunction=false] True, if the alias target is a function. 
 * 
 * @returns A descriptor which will access or invoke a member of the specified name.
 */

Then my glorious @kingjs\expand-readme npm package can harvest that from the AST here. And then my equally glorious documentation can include the default value:

## API
```ts
alias(name[, isFunction=false])
```
### Parameters
- `name`: The name of the accessor or function being aliased.
- `isFunction`: True, if the alias target is a function.
### Returns
A descriptor which will access or invoke a member of the specified name.

I'd guess this would be fairly trivial to expose on the AST. Just have to decide on a name. Maybe defaultValue? Or rawDefaultValue just so I can return the string without concern for its type? Or maybe just a node that I can pull the raw text out of? Maybe you guys could make it an "up for grabs".

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.