Skip to content

Commit

Permalink
Merge pull request #69 from Rich-Harris/preserve-type-expressions
Browse files Browse the repository at this point in the history
preserve type expressions in JSDoc annotations for tags other than `param` and `returns`
  • Loading branch information
Rich-Harris authored Dec 10, 2023
2 parents 7a7edfa + c821f7f commit 1e0cb03
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,20 @@ export function clean_jsdoc(node, code) {
if (tag.comment) {
should_keep = true;

const typeExpression = /** @type {ts.JSDocTypeExpression | undefined} */ (
// @ts-ignore
tag.typeExpression
);

if (typeExpression) {
// turn `@param {string} foo description` into `@param foo description`
let a = typeExpression.pos;
let b = typeExpression.end;

while (code.original[b] === ' ') b += 1;
code.remove(a, b);
if (type === 'param' || type === 'returns') {
const typeExpression = /** @type {ts.JSDocTypeExpression | undefined} */ (
// @ts-ignore
tag.typeExpression
);

if (typeExpression) {
// turn `@param {string} foo description` into `@param foo description`
let a = typeExpression.pos;
let b = typeExpression.end;

while (code.original[b] === ' ') b += 1;
code.remove(a, b);
}
}
} else if (preserved_jsdoc_tags.has(type)) {
should_keep = true;
Expand Down
7 changes: 7 additions & 0 deletions test/samples/throws/input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @throws {Error} nope
* @returns {never}
*/
export function nope() {
throw new Error('nope');
}
8 changes: 8 additions & 0 deletions test/samples/throws/output/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
declare module 'throws' {
/**
* @throws {Error} nope
* */
export function nope(): never;
}

//# sourceMappingURL=index.d.ts.map
14 changes: 14 additions & 0 deletions test/samples/throws/output/index.d.ts.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": 3,
"file": "index.d.ts",
"names": [
"nope"
],
"sources": [
"../input/index.js"
],
"sourcesContent": [
null
],
"mappings": ";;;;iBAIgBA,IAAIA"
}

0 comments on commit 1e0cb03

Please sign in to comment.