Skip to content

Commit 05107f3

Browse files
committed
Add comment syntax to API, test removeAnnotationContents
1 parent a41c559 commit 05107f3

File tree

6 files changed

+786
-250
lines changed

6 files changed

+786
-250
lines changed

packages/annotation-comments/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ type AnnotationComment = {
7575
contents: string[]
7676
commentRange: SourceRange
7777
commentInnerRange: SourceRange
78+
commentSyntax: {
79+
opening: string
80+
closing?: string | undefined
81+
continuationLineStart?: RegExp | undefined
82+
}
7883
annotationRange: SourceRange
7984
contentRanges: SourceRange[]
8085
targetRanges: SourceRange[]

packages/annotation-comments/src/core/clean.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { cloneRange, createSingleLineRange, excludeRangesFromOuterRange, mergeIntersectingOrAdjacentRanges, rangesAreEqual, splitRangeByLines } from '../internal/ranges'
2-
import { excludeWhitespaceRanges } from '../internal/text-content'
1+
import { cloneRange, excludeRangesFromOuterRange, mergeIntersectingOrAdjacentRanges, rangesAreEqual, splitRangeByLines } from '../internal/ranges'
2+
import { excludeWhitespaceRanges, getTextContentInLine } from '../internal/text-content'
33
import type { AnnotatedCode, AnnotationComment, SourceLocation, SourceRange } from './types'
44

55
export type CleanCodeOptions = AnnotatedCode & {
@@ -49,12 +49,30 @@ export function cleanCode(options: CleanCodeOptions) {
4949
// If we're removing an entire annotation, include up to one line of whitespace above it
5050
const lineAboveIndex = rangeToBeRemoved.start.line - 1
5151
if (removeEntireAnnotation && annotationComment.commentInnerRange.start.line < lineAboveIndex) {
52-
const contentInLineAbove = excludeWhitespaceRanges(codeLines, [createSingleLineRange(lineAboveIndex)])
53-
if (!contentInLineAbove.length) {
52+
const { content } = getTextContentInLine({
53+
codeLines,
54+
lineIndex: lineAboveIndex,
55+
continuationLineStart: annotationComment.commentSyntax.continuationLineStart,
56+
})
57+
if (!content) {
5458
rangeToBeRemoved.start = { line: lineAboveIndex }
5559
}
5660
}
5761

62+
// If we're only removing the tag, also remove whitespace between the tag and content
63+
if (!removeEntireAnnotation && hasContents) {
64+
const firstContentStart = annotationComment.contentRanges[0].start
65+
if (!annotationComment.commentSyntax.closing && firstContentStart.line > rangeToBeRemoved.start.line) {
66+
// If the comment uses a single-line syntax and the first content starts on a line
67+
// below the tag, remove the entire tag line including the comment syntax
68+
rangeToBeRemoved.start = { line: rangeToBeRemoved.start.line }
69+
} else {
70+
// Otherwise (multi-line syntax or content starting at the tag line),
71+
// just extend the range to the start of the first content
72+
rangeToBeRemoved.end = { line: firstContentStart.line, column: firstContentStart.column ?? 0 }
73+
}
74+
}
75+
5876
rangesToBeRemoved.push(rangeToBeRemoved)
5977
})
6078

packages/annotation-comments/src/core/types.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,28 @@ export type AnnotationComment = {
1414
* excluding the comment's opening and closing syntax.
1515
*/
1616
commentInnerRange: SourceRange
17+
/**
18+
* The syntax used by the parent comment that contains the annotation.
19+
*/
20+
commentSyntax: {
21+
/**
22+
* The opening syntax (e.g. `//` or `/*` for JS-style comments).
23+
*/
24+
opening: string
25+
/**
26+
* The closing syntax (e.g. `-->` for HTML-style comments).
27+
*
28+
* This is `undefined` for single-line comment syntaxes.
29+
*/
30+
closing?: string | undefined
31+
/**
32+
* The syntax used to start a new line inside the parent comment.
33+
* Used by JSDoc-style comments (e.g. `*`).
34+
*
35+
* This is `undefined` for single-line comment syntaxes.
36+
*/
37+
continuationLineStart?: RegExp | undefined
38+
}
1739
/**
1840
* The outer range of the annotation, covering both the annotation tag and
1941
* any optional content.

packages/annotation-comments/src/parsers/comment-types/multi-line.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@ function getCommentFromMatchingSyntaxPair(options: {
344344
contents,
345345
commentRange,
346346
commentInnerRange,
347+
commentSyntax: {
348+
...syntax,
349+
},
347350
annotationRange,
348351
contentRanges,
349352
targetRanges: [],

packages/annotation-comments/src/parsers/comment-types/single-line.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ export function parseSingleLineParentComment(options: ParseParentCommentOptions)
137137
contents,
138138
commentRange,
139139
commentInnerRange,
140+
commentSyntax: {
141+
opening: singleLineCommentSyntax.syntax,
142+
},
140143
annotationRange,
141144
contentRanges,
142145
targetRanges: [],

0 commit comments

Comments
 (0)