microsoft / microsoft/TypeScript

Expose JsDoc default value for optional parameters in AST

Aperta
#29,521 3 commenti 6 reazioni 1 assegnatario Vedi su GitHub

@sandersn ci sta già lavorando.

Dal 23/1/2019.

Domain: API Domain: JSDoc In Discussion Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.