Skip to content

Commit fa8d50c

Browse files
committed
Add best parent comment selection tests
1 parent 27aa323 commit fa8d50c

1 file changed

Lines changed: 104 additions & 0 deletions

File tree

packages/annotation-comments/test/parent-comment.test.ts

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,110 @@ describe('parseParentComment()', () => {
595595
])
596596
})
597597
})
598+
describe('Picks the best comment range if multiple options are present', () => {
599+
describe('In case of multiple syntax options, prefers the one starting closest to the tag', () => {
600+
test('Nested multi-line syntax on a single line', () => {
601+
const lines = [
602+
'{ /* ',
603+
'This is some content inside a JSX-like comment',
604+
// Nested multi-line annotation comment
605+
'<!-- [!note] Look, a note! -->',
606+
// Other content follows
607+
'More content inside the JSX-like comment',
608+
'*/ }',
609+
'someCode()',
610+
]
611+
const comment = getParentComment(getTestCode(lines.join('\n'))) as AnnotationComment
612+
expect(comment.contents).toEqual(['Look, a note!'])
613+
// Expect the space between the code and the comment to be included
614+
expect(comment.commentRange).toEqual({ start: { line: 4 }, end: { line: 4 } })
615+
expect(comment.contentRanges).toEqual([{ start: { line: 4, column: lines[2].indexOf('Look') }, end: { line: 4, column: lines[2].indexOf(' -->') } }])
616+
})
617+
test('Nested multi-line syntax on multiple lines', () => {
618+
const lines = [
619+
'{ /* ',
620+
'This is some content inside a JSX-like comment',
621+
// Nested multi-line annotation comment
622+
'<!--',
623+
'[!note] Look, a note!',
624+
'-->',
625+
// Other content follows
626+
'More content inside the JSX-like comment',
627+
'*/ }',
628+
'someCode()',
629+
]
630+
const comment = getParentComment(getTestCode(lines.join('\n'))) as AnnotationComment
631+
expect(comment.contents).toEqual(['Look, a note!'])
632+
// Expect the space between the code and the comment to be included
633+
expect(comment.commentRange).toEqual({ start: { line: 4 }, end: { line: 6 } })
634+
expect(comment.contentRanges).toEqual([{ start: { line: 5, column: lines[3].indexOf('Look') }, end: { line: 5 } }])
635+
})
636+
})
637+
describe('If opening/closing options overlap, prefers the longest one', () => {
638+
test('"<div> { /* [!ins] */ }"', () => {
639+
const lines = [
640+
// JSX annotation comment following an HTML tag
641+
'<div> { /* [!ins] */ }',
642+
'someCode()',
643+
]
644+
const comment = getParentComment(getTestCode(lines.join('\n'))) as AnnotationComment
645+
expect(comment.contents).toEqual([])
646+
// Expect the space between the code and the comment to be included
647+
expect(comment.commentRange).toEqual({ start: { line: 2, column: lines[0].indexOf(' { /*') }, end: { line: 2 } })
648+
expect(comment.contentRanges).toEqual([])
649+
})
650+
test('"{ /* [!ins] */ } <div>"', () => {
651+
const lines = [
652+
// JSX annotation comment following an HTML tag
653+
' { /* [!ins] */ } <div>',
654+
'someCode()',
655+
]
656+
const comment = getParentComment(getTestCode(lines.join('\n'))) as AnnotationComment
657+
expect(comment.contents).toEqual([])
658+
// Expect the space between the comment and the code to be included,
659+
// but not the indentation before the code
660+
expect(comment.commentRange).toEqual({ start: { line: 2, column: 2 }, end: { line: 2, column: lines[0].indexOf('<div') } })
661+
expect(comment.contentRanges).toEqual([])
662+
})
663+
test('"{ /* [!note] Annotation content */ }" on its own line', () => {
664+
const lines = [
665+
// Single-line variant
666+
'{ /* [!note] Annotation content */ }',
667+
'someCode()',
668+
]
669+
const comment = getParentComment(getTestCode(lines.join('\n'))) as AnnotationComment
670+
expect(comment.contents).toEqual(['Annotation content'])
671+
// Expect the closing syntax not to be included in the comment range
672+
// as the comment also contains non-annotation content
673+
expect(comment.commentRange).toEqual({ start: { line: 2 }, end: { line: 2 } })
674+
expect(comment.contentRanges).toEqual([
675+
{
676+
start: { line: 2, column: lines[0].indexOf('Annotation') },
677+
end: { line: 2, column: lines[0].indexOf(' */ }') },
678+
},
679+
])
680+
})
681+
test('Multi-line comment including "{ /*", content lines, and "*/ }"', () => {
682+
const lines = [
683+
// Multi-line variant
684+
'{ /*',
685+
' [!note] Annotation content',
686+
' that spans multiple lines',
687+
'*/ }',
688+
'someCode()',
689+
]
690+
const comment = getParentComment(getTestCode(lines.join('\n'))) as AnnotationComment
691+
expect(comment.contents).toEqual(['Annotation content', 'that spans multiple lines'])
692+
// Expect the closing syntax not to be included in the comment range
693+
// as the comment also contains non-annotation content
694+
expect(comment.commentRange).toEqual({ start: { line: 2 }, end: { line: 5 } })
695+
expect(comment.contentRanges).toEqual([
696+
{ start: { line: 3, column: lines[1].indexOf('Annotation') }, end: { line: 3 } },
697+
{ start: { line: 4, column: 2 }, end: { line: 4 } },
698+
])
699+
})
700+
})
701+
})
598702
})
599703

600704
/**

0 commit comments

Comments
 (0)