1
1
2
2
import {
3
- ParserMessageLog ,
4
3
TSDocParser ,
5
4
TextRange ,
6
5
TSDocConfiguration ,
7
6
StandardTags ,
8
7
TSDocTagDefinition ,
9
- TSDocTagSyntaxKind
8
+ TSDocTagSyntaxKind ,
9
+ ParserContext
10
10
} from "@microsoft/tsdoc" ;
11
11
import { allTsdocMessageIds } from "@microsoft/tsdoc/lib/parser/TSDocMessageId" ;
12
12
import * as eslint from "eslint" ;
@@ -19,7 +19,7 @@ allTsdocMessageIds.forEach((messageId: string) => {
19
19
} ) ;
20
20
21
21
interface IPlugin {
22
- rules : { [ x : string ] : eslint . Rule . RuleModule } ;
22
+ rules : { [ x : string ] : eslint . Rule . RuleModule } ;
23
23
}
24
24
25
25
const plugin : IPlugin = {
@@ -64,11 +64,27 @@ const plugin: IPlugin = {
64
64
65
65
const sourceCode : eslint . SourceCode = context . getSourceCode ( ) ;
66
66
const checkCommentBlocks : ( node : ESTree . Node ) => void = function ( node : ESTree . Node ) {
67
- const commentToken : eslint . AST . Token | null = sourceCode . getJSDocComment ( node ) ;
68
- if ( commentToken ) {
69
- const textRange : TextRange = TextRange . fromStringRange ( sourceCode . text , commentToken . range [ 0 ] , commentToken . range [ 1 ] ) ;
70
- const results : ParserMessageLog = tsdocParser . parseRange ( textRange ) . log ;
71
- for ( const message of results . messages ) {
67
+ for ( const comment of sourceCode . getAllComments ( ) ) {
68
+ if ( comment . type !== "Block" ) {
69
+ continue ;
70
+ }
71
+ if ( ! comment . range ) {
72
+ continue ;
73
+ }
74
+
75
+ const textRange : TextRange = TextRange . fromStringRange ( sourceCode . text , comment . range [ 0 ] , comment . range [ 1 ] ) ;
76
+
77
+ // Smallest comment is "/***/"
78
+ if ( textRange . length < 5 ) {
79
+ continue ;
80
+ }
81
+ // Make sure it starts with "/**"
82
+ if ( textRange . buffer [ textRange . pos + 2 ] !== '*' ) {
83
+ continue ;
84
+ }
85
+
86
+ const parserContext : ParserContext = tsdocParser . parseRange ( textRange ) ;
87
+ for ( const message of parserContext . log . messages ) {
72
88
context . report ( {
73
89
loc : {
74
90
start : sourceCode . getLocFromIndex ( message . textRange . pos ) ,
@@ -84,8 +100,7 @@ const plugin: IPlugin = {
84
100
}
85
101
86
102
return {
87
- ClassDeclaration : checkCommentBlocks ,
88
- FunctionDeclaration : checkCommentBlocks
103
+ Program : checkCommentBlocks
89
104
} ;
90
105
}
91
106
}
0 commit comments