Skip to content

Commit c0f4494

Browse files
Atec-Nickbrettz9
authored andcommitted
fix(require-jsdoc): allow additional contexts to block preceeding export and find jsdoc
Adds fixes for TSTypeAliasDeclaration and TSEnumDeclaration similar to the recent changes to support TSInterfaceDeclaration. Adding these additional types to the switch statement pushed function complexity over 20, so getJSDocComment was split into two functions.
1 parent eb8edca commit c0f4494

File tree

3 files changed

+193
-39
lines changed

3 files changed

+193
-39
lines changed

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5890,6 +5890,50 @@ interface Example {
58905890
test: string
58915891
}
58925892
// Options: [{"contexts":["TSInterfaceDeclaration"]}]
5893+
5894+
/**
5895+
* This example type is great!
5896+
*/
5897+
export type Example = {
5898+
/**
5899+
* My super test string!
5900+
*/
5901+
test: string
5902+
};
5903+
// Options: [{"contexts":["TSTypeAliasDeclaration"]}]
5904+
5905+
/**
5906+
* This example type is great!
5907+
*/
5908+
type Example = {
5909+
/**
5910+
* My super test string!
5911+
*/
5912+
test: string
5913+
};
5914+
// Options: [{"contexts":["TSTypeAliasDeclaration"]}]
5915+
5916+
/**
5917+
* This example enum is great!
5918+
*/
5919+
export enum Example {
5920+
/**
5921+
* My super test enum!
5922+
*/
5923+
test = 123
5924+
}
5925+
// Options: [{"contexts":["TSEnumDeclaration"]}]
5926+
5927+
/**
5928+
* This example enum is great!
5929+
*/
5930+
enum Example {
5931+
/**
5932+
* My super test enum!
5933+
*/
5934+
test = 123
5935+
}
5936+
// Options: [{"contexts":["TSEnumDeclaration"]}]
58935937
````
58945938

58955939

src/eslint/getJSDocComment.js

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,48 +27,23 @@ const looksLikeExport = function (astNode) {
2727
};
2828

2929
/**
30-
* Retrieves the JSDoc comment for a given node.
30+
* Reduces the provided node to the appropriate node for evaluating JSDoc comment status.
3131
*
32-
* @param {SourceCode} sourceCode The ESLint SourceCode
33-
* @param {ASTNode} node The AST node to get the comment for.
34-
* @param {object} settings The settings in context
35-
* @returns {Token|null} The Block comment token containing the JSDoc comment
36-
* for the given node or null if not found.
37-
* @public
38-
* @deprecated
32+
* @param {ASTNode} node An AST node.
33+
* @param {SourceCode} sourceCode The ESLint SourceCode.
34+
* @returns {ASTNode} The AST node that can be evaluated for appropriate JSDoc comments.
35+
* @private
3936
*/
40-
const getJSDocComment = function (sourceCode, node, settings) {
41-
/**
42-
* Checks for the presence of a JSDoc comment for the given node and returns it.
43-
*
44-
* @param {ASTNode} astNode The AST node to get the comment for.
45-
* @returns {Token|null} The Block comment token containing the JSDoc comment
46-
* for the given node or null if not found.
47-
* @private
48-
*/
49-
const findJSDocComment = (astNode) => {
50-
const tokenBefore = sourceCode.getTokenBefore(astNode, {includeComments: true});
51-
const {minLines, maxLines} = settings;
52-
if (
53-
tokenBefore &&
54-
isCommentToken(tokenBefore) &&
55-
tokenBefore.type === 'Block' &&
56-
tokenBefore.value.charAt(0) === '*' &&
57-
astNode.loc.start.line - tokenBefore.loc.end.line >= minLines &&
58-
astNode.loc.start.line - tokenBefore.loc.end.line <= maxLines
59-
) {
60-
return tokenBefore;
61-
}
62-
63-
return null;
64-
};
37+
const getReducedASTNode = function (node, sourceCode) {
6538
let {parent} = node;
6639

6740
switch (node.type) {
6841
case 'TSInterfaceDeclaration':
42+
case 'TSTypeAliasDeclaration':
43+
case 'TSEnumDeclaration':
6944
case 'ClassDeclaration':
7045
case 'FunctionDeclaration':
71-
return findJSDocComment(looksLikeExport(parent) ? parent : node);
46+
return looksLikeExport(parent) ? parent : node;
7247

7348
case 'ClassExpression':
7449
case 'ObjectExpression':
@@ -91,19 +66,60 @@ const getJSDocComment = function (sourceCode, node, settings) {
9166
}
9267

9368
if (parent && parent.type !== 'FunctionDeclaration' && parent.type !== 'Program') {
94-
return findJSDocComment(parent);
69+
return parent;
9570
}
9671
}
9772

98-
return findJSDocComment(node);
73+
return node;
9974

10075
default:
101-
if (!node) {
102-
return null;
76+
return node;
77+
}
78+
};
79+
80+
/**
81+
* Retrieves the JSDoc comment for a given node.
82+
*
83+
* @param {SourceCode} sourceCode The ESLint SourceCode
84+
* @param {ASTNode} node The AST node to get the comment for.
85+
* @param {object} settings The settings in context
86+
* @returns {Token|null} The Block comment token containing the JSDoc comment
87+
* for the given node or null if not found.
88+
* @public
89+
* @deprecated
90+
*/
91+
const getJSDocComment = function (sourceCode, node, settings) {
92+
/**
93+
* Checks for the presence of a JSDoc comment for the given node and returns it.
94+
*
95+
* @param {ASTNode} astNode The AST node to get the comment for.
96+
* @returns {Token|null} The Block comment token containing the JSDoc comment
97+
* for the given node or null if not found.
98+
* @private
99+
*/
100+
const findJSDocComment = (astNode) => {
101+
const tokenBefore = sourceCode.getTokenBefore(astNode, {includeComments: true});
102+
const {minLines, maxLines} = settings;
103+
if (
104+
tokenBefore &&
105+
isCommentToken(tokenBefore) &&
106+
tokenBefore.type === 'Block' &&
107+
tokenBefore.value.charAt(0) === '*' &&
108+
astNode.loc.start.line - tokenBefore.loc.end.line >= minLines &&
109+
astNode.loc.start.line - tokenBefore.loc.end.line <= maxLines
110+
) {
111+
return tokenBefore;
103112
}
104113

105-
return findJSDocComment(node);
114+
return null;
115+
};
116+
117+
const reducedNode = getReducedASTNode(node, sourceCode);
118+
if (!reducedNode) {
119+
return null;
106120
}
121+
122+
return findJSDocComment(reducedNode);
107123
};
108124

109125
export default getJSDocComment;

test/rules/assertions/requireJsdoc.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2230,6 +2230,100 @@ export default {
22302230
parserOptions: {
22312231
sourceType: 'module',
22322232
},
2233+
}, {
2234+
code: `
2235+
/**
2236+
* This example type is great!
2237+
*/
2238+
export type Example = {
2239+
/**
2240+
* My super test string!
2241+
*/
2242+
test: string
2243+
};
2244+
`,
2245+
options: [
2246+
{
2247+
contexts: [
2248+
'TSTypeAliasDeclaration',
2249+
],
2250+
},
2251+
],
2252+
parser: require.resolve('@typescript-eslint/parser'),
2253+
parserOptions: {
2254+
sourceType: 'module',
2255+
},
2256+
},
2257+
{
2258+
code: `
2259+
/**
2260+
* This example type is great!
2261+
*/
2262+
type Example = {
2263+
/**
2264+
* My super test string!
2265+
*/
2266+
test: string
2267+
};
2268+
`,
2269+
options: [
2270+
{
2271+
contexts: [
2272+
'TSTypeAliasDeclaration',
2273+
],
2274+
},
2275+
],
2276+
parser: require.resolve('@typescript-eslint/parser'),
2277+
parserOptions: {
2278+
sourceType: 'module',
2279+
},
2280+
}, {
2281+
code: `
2282+
/**
2283+
* This example enum is great!
2284+
*/
2285+
export enum Example {
2286+
/**
2287+
* My super test enum!
2288+
*/
2289+
test = 123
2290+
}
2291+
`,
2292+
options: [
2293+
{
2294+
contexts: [
2295+
'TSEnumDeclaration',
2296+
],
2297+
},
2298+
],
2299+
parser: require.resolve('@typescript-eslint/parser'),
2300+
parserOptions: {
2301+
sourceType: 'module',
2302+
},
2303+
},
2304+
{
2305+
code: `
2306+
/**
2307+
* This example enum is great!
2308+
*/
2309+
enum Example {
2310+
/**
2311+
* My super test enum!
2312+
*/
2313+
test = 123
2314+
}
2315+
`,
2316+
options: [
2317+
{
2318+
contexts: [
2319+
'TSEnumDeclaration',
2320+
],
2321+
},
2322+
],
2323+
parser: require.resolve('@typescript-eslint/parser'),
2324+
parserOptions: {
2325+
sourceType: 'module',
2326+
},
22332327
},
22342328
],
22352329
};

0 commit comments

Comments
 (0)