Skip to content

Commit 4f8bd5e

Browse files
committed
Process every doc comment
1 parent 322837c commit 4f8bd5e

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

eslint-plugin/src/index.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11

22
import {
3-
ParserMessageLog,
43
TSDocParser,
54
TextRange,
65
TSDocConfiguration,
76
StandardTags,
87
TSDocTagDefinition,
9-
TSDocTagSyntaxKind
8+
TSDocTagSyntaxKind,
9+
ParserContext
1010
} from "@microsoft/tsdoc";
1111
import { allTsdocMessageIds } from "@microsoft/tsdoc/lib/parser/TSDocMessageId";
1212
import * as eslint from "eslint";
@@ -19,7 +19,7 @@ allTsdocMessageIds.forEach((messageId: string) => {
1919
});
2020

2121
interface IPlugin {
22-
rules: {[x: string]: eslint.Rule.RuleModule};
22+
rules: {[x: string]: eslint.Rule.RuleModule};
2323
}
2424

2525
const plugin: IPlugin = {
@@ -64,11 +64,27 @@ const plugin: IPlugin = {
6464

6565
const sourceCode: eslint.SourceCode = context.getSourceCode();
6666
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) {
7288
context.report({
7389
loc: {
7490
start: sourceCode.getLocFromIndex(message.textRange.pos),
@@ -84,8 +100,7 @@ const plugin: IPlugin = {
84100
}
85101

86102
return {
87-
ClassDeclaration: checkCommentBlocks,
88-
FunctionDeclaration: checkCommentBlocks
103+
Program: checkCommentBlocks
89104
};
90105
}
91106
}

tsdoc/src/parser/TextRange.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ export class TextRange {
6262
return new TextRange(buffer, pos, end);
6363
}
6464

65+
/**
66+
* Returns the length of the text range.
67+
* @remarks
68+
* This value is calculated as the `end` property minus the `pos` property.
69+
*/
70+
public get length(): number {
71+
return this.end - this.pos;
72+
}
73+
6574
/**
6675
* Constructs a TextRange that corresponds to a different range of an existing buffer.
6776
*/

0 commit comments

Comments
 (0)