microsoft / microsoft/TypeScript
Expose JsDoc default value for optional parameters in AST
@sandersn arbeitet bereits daran.
Seit 23.1.2019.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
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:

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();
}
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Bewertung
Dieses Issue wurde noch nicht bewertet.