Skip to content

Commit 4ff5210

Browse files
committed
Add JSDoc eslint rule
See the next commit for a more fleshed-out description.
1 parent 78456ea commit 4ff5210

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

Diff for: .eslintrc.json

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
"local/simple-indent": "error",
8888
"local/debug-assert": "error",
8989
"local/no-keywords": "error",
90+
"local/jsdoc-format": "error",
9091

9192
// eslint-plugin-import
9293
"import/no-extraneous-dependencies": ["error", { "optionalDependencies": false }],

Diff for: scripts/eslint/rules/jsdoc-format.cjs

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
const { TSESTree } = require("@typescript-eslint/utils");
2+
const { createRule } = require("./utils.cjs");
3+
4+
module.exports = createRule({
5+
name: "jsdoc-format",
6+
meta: {
7+
docs: {
8+
description: ``,
9+
recommended: "error",
10+
},
11+
messages: {
12+
internalCommentInNonJSDocError: `@internal should not appear in non-JSDoc comment for declaration.`,
13+
internalCommentNotLastError: `@internal should only appear in final JSDoc comment for declaration.`,
14+
multipleJSDocError: `Declaration has multiple JSDoc comments.`,
15+
internalCommentOnParameterProperty: `@internal cannot appear on a JSDoc comment; use a declared property and an assignment in the constructor instead.`,
16+
},
17+
schema: [],
18+
type: "problem",
19+
},
20+
defaultOptions: [],
21+
22+
create(context) {
23+
const sourceCode = context.getSourceCode();
24+
const atInternal = "@internal";
25+
const jsdocStart = "/**";
26+
27+
/** @type {(c: TSESTree.Comment, indexInComment: number) => TSESTree.SourceLocation} */
28+
const getAtInternalLoc = (c, indexInComment) => {
29+
const line = c.loc.start.line;
30+
return {
31+
start: {
32+
line,
33+
column: c.loc.start.column + indexInComment,
34+
},
35+
end: {
36+
line,
37+
column: c.loc.start.column + indexInComment + atInternal.length,
38+
},
39+
};
40+
};
41+
42+
/** @type {(c: TSESTree.Comment) => TSESTree.SourceLocation} */
43+
const getJSDocStartLoc = (c) => {
44+
return {
45+
start: c.loc.start,
46+
end: {
47+
line: c.loc.start.line,
48+
column: c.loc.start.column + jsdocStart.length,
49+
},
50+
};
51+
};
52+
53+
/** @type {(node: TSESTree.Node) => void} */
54+
const checkJSDocFormat = (node) => {
55+
const blockComments = sourceCode.getCommentsBefore(node).filter(c => c.type === "Block");
56+
if (blockComments.length === 0) {
57+
return;
58+
}
59+
60+
const last = blockComments.length - 1;
61+
let seenJSDoc = false;
62+
for (let i = 0; i < blockComments.length; i++) {
63+
const c = blockComments[i];
64+
const rawComment = sourceCode.getText(c);
65+
66+
const isJSDoc = rawComment.startsWith(jsdocStart);
67+
if (isJSDoc && seenJSDoc) {
68+
context.report({ messageId: "multipleJSDocError", node: c, loc: getJSDocStartLoc(c) });
69+
}
70+
seenJSDoc = seenJSDoc || isJSDoc;
71+
72+
const indexInComment = rawComment.indexOf(atInternal);
73+
if (indexInComment === -1) {
74+
continue;
75+
}
76+
77+
if (!isJSDoc) {
78+
context.report({ messageId: "internalCommentInNonJSDocError", node: c, loc: getAtInternalLoc(c, indexInComment) });
79+
}
80+
else if (i !== last) {
81+
context.report({ messageId: "internalCommentNotLastError", node: c, loc: getAtInternalLoc(c, indexInComment) });
82+
}
83+
else if (node.type === "TSParameterProperty") {
84+
context.report({ messageId: "internalCommentOnParameterProperty", node: c, loc: getAtInternalLoc(c, indexInComment) });
85+
}
86+
87+
// TODO: check duplicate @internal?
88+
}
89+
};
90+
91+
return {
92+
ClassDeclaration: checkJSDocFormat,
93+
FunctionDeclaration: checkJSDocFormat,
94+
TSEnumDeclaration: checkJSDocFormat,
95+
TSModuleDeclaration: checkJSDocFormat,
96+
VariableDeclaration: checkJSDocFormat,
97+
TSInterfaceDeclaration: checkJSDocFormat,
98+
TSTypeAliasDeclaration: checkJSDocFormat,
99+
TSCallSignatureDeclaration: checkJSDocFormat,
100+
ExportAllDeclaration: checkJSDocFormat,
101+
ExportNamedDeclaration: checkJSDocFormat,
102+
TSImportEqualsDeclaration: checkJSDocFormat,
103+
TSNamespaceExportDeclaration: checkJSDocFormat,
104+
TSConstructSignatureDeclaration: checkJSDocFormat,
105+
ExportDefaultDeclaration: checkJSDocFormat,
106+
TSPropertySignature: checkJSDocFormat,
107+
TSIndexSignature: checkJSDocFormat,
108+
TSMethodSignature: checkJSDocFormat,
109+
TSParameterProperty: checkJSDocFormat,
110+
};
111+
},
112+
});

0 commit comments

Comments
 (0)