-
-
Notifications
You must be signed in to change notification settings - Fork 153
/
getJSDocComment.js
266 lines (237 loc) · 7.02 KB
/
getJSDocComment.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
* Obtained originally from {@link https://github.com/eslint/eslint/blob/master/lib/util/source-code.js#L313}
*
* @license MIT
*/
/**
* Checks if the given token is a comment token or not.
*
* @param {Token} token - The token to check.
* @returns {boolean} `true` if the token is a comment token.
*/
const isCommentToken = (token) => {
return token.type === 'Line' || token.type === 'Block' || token.type === 'Shebang';
};
const getDecorator = (token, sourceCode) => {
if (!token) {
return false;
}
if (token.type === 'Punctuator' && token.value === ')') {
let nested = 0;
let tokenBefore = token;
do {
tokenBefore = sourceCode.getTokenBefore(tokenBefore, {includeComments: true});
// istanbul ignore if
if (tokenBefore && tokenBefore.type === 'Punctuator') {
if (tokenBefore.value === ')') {
nested++;
}
if (tokenBefore.value === '(') {
if (nested) {
nested--;
} else {
break;
}
}
}
} while (tokenBefore);
return tokenBefore;
}
if (token.type === 'Identifier') {
const tokenBefore = sourceCode.getTokenBefore(token, {includeComments: true});
if (tokenBefore && tokenBefore.type === 'Punctuator' && tokenBefore.value === '@') {
return tokenBefore;
}
}
return false;
};
/**
* Check to see if its a ES6 export declaration.
*
* @param {ASTNode} astNode An AST node.
* @returns {boolean} whether the given node represents an export declaration.
* @private
*/
const looksLikeExport = function (astNode) {
return astNode.type === 'ExportDefaultDeclaration' || astNode.type === 'ExportNamedDeclaration' ||
astNode.type === 'ExportAllDeclaration' || astNode.type === 'ExportSpecifier';
};
const getTSFunctionComment = function (astNode) {
const {parent} = astNode;
const grandparent = parent.parent;
const greatGrandparent = grandparent.parent;
const greatGreatGrandparent = greatGrandparent && greatGrandparent.parent;
// istanbul ignore if
if (parent.type !== 'TSTypeAnnotation') {
return astNode;
}
switch (grandparent.type) {
case 'ClassProperty':
case 'TSDeclareFunction':
case 'TSMethodSignature':
case 'TSPropertySignature':
return grandparent;
case 'ArrowFunctionExpression':
// istanbul ignore else
if (
greatGrandparent.type === 'VariableDeclarator'
// && greatGreatGrandparent.parent.type === 'VariableDeclaration'
) {
return greatGreatGrandparent.parent;
}
// istanbul ignore next
return astNode;
case 'FunctionExpression':
// istanbul ignore else
if (greatGrandparent.type === 'MethodDefinition') {
return greatGrandparent;
}
// Fallthrough
default:
// istanbul ignore if
if (grandparent.type !== 'Identifier') {
// istanbul ignore next
return astNode;
}
}
// istanbul ignore next
switch (greatGrandparent.type) {
case 'ArrowFunctionExpression':
// istanbul ignore else
if (
greatGreatGrandparent.type === 'VariableDeclarator' &&
greatGreatGrandparent.parent.type === 'VariableDeclaration'
) {
return greatGreatGrandparent.parent;
}
// istanbul ignore next
return astNode;
case 'FunctionDeclaration':
return greatGrandparent;
case 'VariableDeclarator':
// istanbul ignore else
if (greatGreatGrandparent.type === 'VariableDeclaration') {
return greatGreatGrandparent;
}
// Fallthrough
default:
// istanbul ignore next
return astNode;
}
};
const invokedExpression = new Set(['CallExpression', 'OptionalCallExpression', 'NewExpression']);
const allowableCommentNode = new Set([
'VariableDeclaration',
'ExpressionStatement',
'MethodDefinition',
'Property',
'ObjectProperty',
'ClassProperty',
]);
/* eslint-disable complexity */
/**
* Reduces the provided node to the appropriate node for evaluating JSDoc comment status.
*
* @param {ASTNode} node An AST node.
* @param {SourceCode} sourceCode The ESLint SourceCode.
* @returns {ASTNode} The AST node that can be evaluated for appropriate JSDoc comments.
* @private
*/
const getReducedASTNode = function (node, sourceCode) {
let {parent} = node;
switch (node.type) {
case 'TSFunctionType':
return getTSFunctionComment(node);
case 'TSInterfaceDeclaration':
case 'TSTypeAliasDeclaration':
case 'TSEnumDeclaration':
case 'ClassDeclaration':
case 'FunctionDeclaration':
return looksLikeExport(parent) ? parent : node;
case 'ClassExpression':
case 'ObjectExpression':
case 'ArrowFunctionExpression':
case 'TSEmptyBodyFunctionExpression':
case 'FunctionExpression':
if (
!invokedExpression.has(parent.type)
) {
while (
!sourceCode.getCommentsBefore(parent).length &&
!/Function/u.test(parent.type) &&
!allowableCommentNode.has(parent.type)
) {
parent = parent.parent;
if (!parent) {
break;
}
}
if (parent && parent.type !== 'FunctionDeclaration' && parent.type !== 'Program') {
if (parent.parent && parent.parent.type === 'ExportNamedDeclaration') {
return parent.parent;
}
return parent;
}
}
return node;
default:
return node;
}
};
/**
* Retrieves the JSDoc comment for a given node.
*
* @param {SourceCode} sourceCode The ESLint SourceCode
* @param {ASTNode} node The AST node to get the comment for.
* @param {object} settings The settings in context
* @returns {Token|null} The Block comment token containing the JSDoc comment
* for the given node or null if not found.
* @public
* @deprecated
*/
const getJSDocComment = function (sourceCode, node, settings) {
/**
* Checks for the presence of a JSDoc comment for the given node and returns it.
*
* @param {ASTNode} astNode The AST node to get the comment for.
* @returns {Token|null} The Block comment token containing the JSDoc comment
* for the given node or null if not found.
* @private
*/
const findJSDocComment = (astNode) => {
const {minLines, maxLines} = settings;
let currentNode = astNode;
let tokenBefore = null;
while (currentNode) {
tokenBefore = sourceCode.getTokenBefore(currentNode, {includeComments: true});
const decorator = getDecorator(tokenBefore, sourceCode);
if (decorator) {
currentNode = decorator;
continue;
}
if (!tokenBefore || !isCommentToken(tokenBefore)) {
return null;
}
if (tokenBefore.type === 'Line') {
currentNode = tokenBefore;
continue;
}
break;
}
if (
tokenBefore.type === 'Block' &&
tokenBefore.value.charAt(0) === '*' &&
currentNode.loc.start.line - tokenBefore.loc.end.line >= minLines &&
currentNode.loc.start.line - tokenBefore.loc.end.line <= maxLines
) {
return tokenBefore;
}
return null;
};
const reducedNode = getReducedASTNode(node, sourceCode);
return findJSDocComment(reducedNode);
};
export {
getReducedASTNode, getJSDocComment, getDecorator,
};
export default getJSDocComment;