MCPcopy Index your code
hub / github.com/microsoft/typescript-go / emitDetachedComments

Method emitDetachedComments

internal/printer/printer.go:5644–5721  ·  view source on GitHub ↗
(textRange core.TextRange)

Source from the content-addressed store, hash-verified

5642}
5643
5644func (p *Printer) emitDetachedComments(textRange core.TextRange) (result detachedCommentsInfo, hasResult bool) {
5645 if p.currentSourceFile == nil {
5646 return result, hasResult
5647 }
5648
5649 text := p.currentSourceFile.Text()
5650 lineMap := p.currentSourceFile.ECMALineMap()
5651
5652 var leadingComments []ast.CommentRange
5653 if p.commentsDisabled {
5654 // removeComments is true, only reserve pinned comment at the top of file
5655 // For example:
5656 // /*! Pinned Comment */
5657 //
5658 // var x = 10;
5659 if textRange.Pos() == 0 {
5660 for comment := range scanner.GetLeadingCommentRanges(p.emitContext.Factory.AsNodeFactory(), text, textRange.Pos()) {
5661 if IsPinnedComment(text, comment) {
5662 leadingComments = append(leadingComments, comment)
5663 }
5664 }
5665 }
5666 } else {
5667 // removeComments is false, just get detached as normal and bypass the process to filter comment
5668 leadingComments = slices.Collect(scanner.GetLeadingCommentRanges(p.emitContext.Factory.AsNodeFactory(), text, textRange.Pos()))
5669 }
5670
5671 if len(leadingComments) > 0 {
5672 var detachedComments []ast.CommentRange
5673 var lastComment ast.CommentRange
5674 for i, comment := range leadingComments {
5675 if i > 0 {
5676 lastCommentLine := scanner.ComputeLineOfPosition(lineMap, lastComment.End())
5677 commentLine := scanner.ComputeLineOfPosition(lineMap, comment.Pos())
5678
5679 if commentLine >= lastCommentLine+2 {
5680 // There was a blank line between the last comment and this comment. This
5681 // comment is not part of the copyright comments. Return what we have so
5682 // far.
5683 break
5684 }
5685 }
5686
5687 detachedComments = append(detachedComments, comment)
5688 lastComment = comment
5689 }
5690
5691 if len(detachedComments) > 0 {
5692 // All comments look like they could have been part of the copyright header. Make
5693 // sure there is at least one blank line between it and the node. If not, it's not
5694 // a copyright header.
5695 lastCommentLine := scanner.ComputeLineOfPosition(lineMap, core.LastOrNil(detachedComments).End())
5696 nodeLine := scanner.ComputeLineOfPosition(lineMap, scanner.SkipTrivia(text, textRange.Pos()))
5697 if nodeLine >= lastCommentLine+2 {
5698 // Valid detachedComments
5699
5700 // Filter to only comments that should be written (e.g., JSDoc-style in declaration emit)
5701 var commentsToEmit []ast.CommentRange

Calls 15

shouldWriteCommentMethod · 0.95
writeLineMethod · 0.95
emitCommentsMethod · 0.95
GetLeadingCommentRangesFunction · 0.92
ComputeLineOfPositionFunction · 0.92
LastOrNilFunction · 0.92
SkipTriviaFunction · 0.92
IsPinnedCommentFunction · 0.85
lenFunction · 0.85
TextMethod · 0.65
ECMALineMapMethod · 0.65

Tested by

no test coverage detected