Skip to content

Commit 8b5f8e1

Browse files
committed
Integrate ESLint-deprecated getJSDocComment
1 parent b79f380 commit 8b5f8e1

File tree

3 files changed

+117
-6
lines changed

3 files changed

+117
-6
lines changed

Diff for: src/eslint/LICENSE-MIT.txt

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright JS Foundation and other contributors, https://js.foundation
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

Diff for: src/eslint/getJSDocComment.js

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* Obtained from {@link https://github.com/eslint/eslint/blob/master/lib/util/source-code.js#L313}
3+
* @license MIT
4+
*/
5+
import astUtils from 'eslint/lib/util/ast-utils';
6+
7+
/**
8+
* Check to see if its a ES6 export declaration.
9+
*
10+
* @param {ASTNode} astNode An AST node.
11+
* @returns {boolean} whether the given node represents an export declaration.
12+
* @private
13+
*/
14+
const looksLikeExport = function (astNode) {
15+
return astNode.type === 'ExportDefaultDeclaration' || astNode.type === 'ExportNamedDeclaration' ||
16+
astNode.type === 'ExportAllDeclaration' || astNode.type === 'ExportSpecifier';
17+
};
18+
19+
/**
20+
* Retrieves the JSDoc comment for a given node.
21+
*
22+
* @param {SourceCode} sourceCode The ESLint SourceCode
23+
* @param {ASTNode} node The AST node to get the comment for.
24+
* @returns {Token|null} The Block comment token containing the JSDoc comment
25+
* for the given node or null if not found.
26+
* @public
27+
* @deprecated
28+
*/
29+
const getJSDocComment = function (sourceCode, node) {
30+
/**
31+
* Checks for the presence of a JSDoc comment for the given node and returns it.
32+
*
33+
* @param {ASTNode} astNode The AST node to get the comment for.
34+
* @returns {Token|null} The Block comment token containing the JSDoc comment
35+
* for the given node or null if not found.
36+
* @private
37+
*/
38+
const findJSDocComment = (astNode) => {
39+
const tokenBefore = sourceCode.getTokenBefore(astNode, {includeComments: true});
40+
41+
if (
42+
tokenBefore &&
43+
astUtils.isCommentToken(tokenBefore) &&
44+
tokenBefore.type === 'Block' &&
45+
tokenBefore.value.charAt(0) === '*' &&
46+
astNode.loc.start.line - tokenBefore.loc.end.line <= 1
47+
) {
48+
return tokenBefore;
49+
}
50+
51+
return null;
52+
};
53+
let parent = node.parent;
54+
55+
switch (node.type) {
56+
case 'ClassDeclaration':
57+
case 'FunctionDeclaration':
58+
return findJSDocComment(looksLikeExport(parent) ? parent : node);
59+
60+
case 'ClassExpression':
61+
return findJSDocComment(parent.parent);
62+
63+
case 'ArrowFunctionExpression':
64+
case 'FunctionExpression':
65+
if (parent.type !== 'CallExpression' && parent.type !== 'NewExpression') {
66+
while (
67+
!sourceCode.getCommentsBefore(parent).length &&
68+
!/Function/u.test(parent.type) &&
69+
parent.type !== 'MethodDefinition' &&
70+
parent.type !== 'Property'
71+
) {
72+
parent = parent.parent;
73+
74+
if (!parent) {
75+
break;
76+
}
77+
}
78+
79+
if (parent && parent.type !== 'FunctionDeclaration' && parent.type !== 'Program') {
80+
return findJSDocComment(parent);
81+
}
82+
}
83+
84+
return findJSDocComment(node);
85+
86+
// falls through
87+
default:
88+
return null;
89+
}
90+
};
91+
92+
export default getJSDocComment;

Diff for: src/iterateJsdoc.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import _ from 'lodash';
22
import commentParser from 'comment-parser';
33
import jsdocUtils from './jsdocUtils';
4+
import getJSDocComment from './eslint/getJSDocComment';
45

56
const parseComment = (commentNode, indent) => {
67
// Preserve JSDoc block start/end indentation.
@@ -143,21 +144,20 @@ const curryUtils = (
143144
], tag.tag);
144145
};
145146

146-
utils.getClassJsdocNode = () => {
147+
utils.getClassNode = () => {
147148
const greatGrandParent = ancestors.slice(-3)[0];
148149
const greatGrandParentValue = greatGrandParent && sourceCode.getFirstToken(greatGrandParent).value;
149150

150151
if (greatGrandParentValue === 'class') {
151-
const classJsdocNode = sourceCode.getJSDocComment(greatGrandParent);
152-
153-
return classJsdocNode;
152+
return greatGrandParent;
154153
}
155154

156155
return false;
157156
};
158157

159158
utils.classHasTag = (tagName) => {
160-
const classJsdocNode = utils.getClassJsdocNode();
159+
const classNode = utils.getClassNode();
160+
const classJsdocNode = getJSDocComment(sourceCode, classNode);
161161

162162
if (classJsdocNode) {
163163
const indent = _.repeat(' ', classJsdocNode.loc.start.column);
@@ -212,7 +212,7 @@ export default (iterator, options) => {
212212
const checkSeesForNamepaths = Boolean(_.get(context, 'settings.jsdoc.checkSeesForNamepaths'));
213213

214214
const checkJsdoc = (node) => {
215-
const jsdocNode = sourceCode.getJSDocComment(node);
215+
const jsdocNode = getJSDocComment(sourceCode, node);
216216

217217
if (!jsdocNode) {
218218
return;

0 commit comments

Comments
 (0)