Skip to content

Commit bf22fe9

Browse files
authored
Merge pull request #195 from microsoft/octogonz/eliminate-internal-api
eslint-plugin-tsdoc: Avoid importing internal API
2 parents 322837c + a72f6aa commit bf22fe9

File tree

6 files changed

+78
-17
lines changed

6 files changed

+78
-17
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/tsdoc",
5+
"comment": "Add new API TSDocConfiguration.allTsdocMessageIds",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "@microsoft/tsdoc",
10+
"email": "4673363+octogonz@users.noreply.github.com"
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "eslint-plugin-tsdoc",
5+
"comment": "Improve lint rule to check every doc comment in a source file",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "eslint-plugin-tsdoc",
10+
"email": "4673363+octogonz@users.noreply.github.com"
11+
}

eslint-plugin/src/index.ts

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
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";
11-
import { allTsdocMessageIds } from "@microsoft/tsdoc/lib/parser/TSDocMessageId";
1211
import * as eslint from "eslint";
1312
import * as ESTree from "estree";
1413

1514
const messageIds: {[x: string]: string} = {};
1615

17-
allTsdocMessageIds.forEach((messageId: string) => {
16+
const defaultTSDocConfiguration: TSDocConfiguration = new TSDocConfiguration()
17+
defaultTSDocConfiguration.allTsdocMessageIds.forEach((messageId: string) => {
1818
messageIds[messageId] = `${messageId}: {{ unformattedText }}`;
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/configuration/TSDocConfiguration.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { TSDocTagDefinition } from './TSDocTagDefinition';
33
import { TSDocValidationConfiguration } from './TSDocValidationConfiguration';
44
import { DocNodeManager } from './DocNodeManager';
55
import { BuiltInDocNodes } from '../nodes/BuiltInDocNodes';
6-
import { TSDocMessageId, allTsdocMessageIds } from '../parser/TSDocMessageId';
6+
import { TSDocMessageId, allTsdocMessageIds, allTsdocMessageIdsSet } from '../parser/TSDocMessageId';
77

88
/**
99
* Configuration for the TSDocParser.
@@ -170,7 +170,19 @@ export class TSDocConfiguration {
170170
* of the TSDoc parser, we may provide a way to register custom message identifiers.
171171
*/
172172
public isKnownMessageId(messageId: TSDocMessageId | string): boolean {
173-
return allTsdocMessageIds.has(messageId);
173+
return allTsdocMessageIdsSet.has(messageId);
174+
}
175+
176+
/**
177+
* Returns the list of {@link TSDocMessageId} strings that are implemented by this release of the TSDoc parser.
178+
*
179+
* @privateRemarks
180+
*
181+
* Why this API is associated with TSDocConfiguration: In the future, if we enable support for custom extensions
182+
* of the TSDoc parser, we may provide a way to register custom message identifiers.
183+
*/
184+
public get allTsdocMessageIds(): ReadonlyArray<TSDocMessageId> {
185+
return allTsdocMessageIds as ReadonlyArray<TSDocMessageId>;
174186
}
175187

176188
private _requireTagToBeDefined(tagDefinition: TSDocTagDefinition): void {

tsdoc/src/parser/TSDocMessageId.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,8 @@ export const enum TSDocMessageId {
336336
CodeSpanMissingDelimiter = 'tsdoc-code-span-missing-delimiter'
337337
}
338338

339-
// Exposed via ParserMessage.isValidMessageId()
340-
export const allTsdocMessageIds: Set<string> = new Set<string>([
339+
// Exposed via TSDocConfiguration.allTsdocMessageIds()
340+
export const allTsdocMessageIds: string[] = [
341341
'tsdoc-comment-not-found',
342342
'tsdoc-comment-missing-opening-delimiter',
343343
'tsdoc-comment-missing-closing-delimiter',
@@ -398,4 +398,7 @@ export const allTsdocMessageIds: Set<string> = new Set<string>([
398398
'tsdoc-code-fence-closing-syntax',
399399
'tsdoc-code-span-empty',
400400
'tsdoc-code-span-missing-delimiter'
401-
]);
401+
];
402+
allTsdocMessageIds.sort();
403+
404+
export const allTsdocMessageIdsSet: ReadonlySet<string> = new Set<string>(allTsdocMessageIds);

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)