Skip to content

Commit

Permalink
feat: preserve comment style
Browse files Browse the repository at this point in the history
  • Loading branch information
nikku committed Feb 26, 2024
1 parent b708ad4 commit 9a10b17
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 7 deletions.
29 changes: 22 additions & 7 deletions lib/post-transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,12 +531,24 @@ declare function p${
// remove entirely, if empty
if (!newDoc.replace(/[/*\s]/g, '')) {
return null;
} else {
return b.commentBlock.from({
leading: true,
value: newDoc
});
}

// leave comment unchanged
if (newDoc === comment.value) {
return comment;
}

const commentBuilder = {
'CommentLine': b.commentLine,
'CommentBlock': b.commentBlock
};

return commentBuilder[comment.type].from({
leading: comment.leading,
trailing: comment.trailing,
loc: comment.loc,
value: newDoc
});
}

/**
Expand All @@ -551,9 +563,12 @@ declare function p${
const comment = comments.get(key);

const newComment = cleanComment(comment.value);
if (newComment) {

if (newComment && newComment !== comment.value) {
replace(comment, newComment);
} else {
}

if (!newComment) {
replace(comment);
}
}
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/post/comments-source-map.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type Element = import('../model/Types').Element;
export type Closure = {
enclosedElements: Record<string, Element>;
};
//# sourceMappingURL=Elements.d.ts.map
6 changes: 6 additions & 0 deletions test/fixtures/post/comments-source-map.expected.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type Element = import('../model/Types').Element;

export type Closure = {
enclosedElements: Record<string, Element>;
};
//# sourceMappingURL=Elements.d.ts.map
17 changes: 17 additions & 0 deletions test/fixtures/post/comments.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// initial comment

/** some comment */

/* */
//


/** @type { number } */
declare const foo = 1;

/** some other comment */

/* */
//

// trailing
10 changes: 10 additions & 0 deletions test/fixtures/post/comments.expected.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// initial comment

/** some comment */

/** @type { number } */
declare const foo = 1;

/** some other comment */

// trailing
3 changes: 3 additions & 0 deletions test/transform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ describe('transform', function() {

testPostTransform('post/undocumented-param');

testPostTransform('post/comments');
testPostTransform('post/comments-source-map');


describe('error handling', function() {

Expand Down

0 comments on commit 9a10b17

Please sign in to comment.