Skip to content

Commit 9d24b25

Browse files
James Whitneybrettz9
authored andcommitted
feat(getJSDocComment): ignore line comment
Often it is required to disable linting in-between the JSDoc and the code. This PR allows that to happen. Without the following change the `jsdoc/require-jsdoc` rule needs to be constantly disabled. Consider the following example: ```ts /** * A user. * @interface User */ interface User { /** * The users age. * @type {number} * @memberof User */ age: number; /** * The users name. * @type {string} * @memberof User */ name: string; } /** * Determine if the provided value is a `User`; * @param {*} user The value to check. * @returns {boolean} Whether or not the provided value is a `User`. */ // Allow the Function to check any input. // tslint:disable-next-line: no-any function isUser(user: any): user is User { if (user == null) { return false; } const { age, name } = user; if (typeof age !== `number`) { return false; } if (typeof name !== `string`) { return false; } return true; } ``` In the above example, using `unknown` wouldn't cut it. This is because `unknown` cannot be destructured. Eg: `const { age, name } = user;`. This PR also updates the checkExamples.js assertion so as not to require an absolute path to @typescript-eslint/parser. This absolute path would mean constant calls to npm run create-readme because of the different paths that developers would have the repo checked out at.
1 parent 6ce1127 commit 9d24b25

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ function f () {
790790
function quux () {
791791

792792
}
793-
// Options: [{"baseConfig":{"parser":"/Users/brett/eslint-plugin-jsdoc/node_modules/@typescript-eslint/parser/dist/parser.js","parserOptions":{"ecmaVersion":6},"rules":{"semi":["error","always"]}},"eslintrcForExamples":false}]
793+
// Options: [{"baseConfig":{"parser":"@typescript-eslint/parser","parserOptions":{"ecmaVersion":6},"rules":{"semi":["error","always"]}},"eslintrcForExamples":false}]
794794
// Message: @example error (semi): Missing semicolon.
795795
````
796796

@@ -885,7 +885,7 @@ function quux () {}
885885
function quux () {
886886

887887
}
888-
// Options: [{"baseConfig":{"parser":"/Users/brett/eslint-plugin-jsdoc/node_modules/@typescript-eslint/parser/dist/parser.js","parserOptions":{"ecmaVersion":6},"rules":{"semi":["error","always"]}},"eslintrcForExamples":false}]
888+
// Options: [{"baseConfig":{"parser":"@typescript-eslint/parser","parserOptions":{"ecmaVersion":6},"rules":{"semi":["error","always"]}},"eslintrcForExamples":false}]
889889
````
890890

891891

src/eslint/getJSDocComment.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,27 @@ const getJSDocComment = function (sourceCode, node, settings) {
9898
* @private
9999
*/
100100
const findJSDocComment = (astNode) => {
101-
const tokenBefore = sourceCode.getTokenBefore(astNode, {includeComments: true});
102101
const {minLines, maxLines} = settings;
102+
let currentNode = astNode;
103+
let tokenBefore = null;
104+
105+
while (currentNode) {
106+
tokenBefore = sourceCode.getTokenBefore(currentNode, {includeComments: true});
107+
if (!tokenBefore || !isCommentToken(tokenBefore)) {
108+
return null;
109+
}
110+
if (tokenBefore.type === 'Line') {
111+
currentNode = tokenBefore;
112+
continue;
113+
}
114+
break;
115+
}
116+
103117
if (
104-
tokenBefore &&
105-
isCommentToken(tokenBefore) &&
106118
tokenBefore.type === 'Block' &&
107119
tokenBefore.value.charAt(0) === '*' &&
108-
astNode.loc.start.line - tokenBefore.loc.end.line >= minLines &&
109-
astNode.loc.start.line - tokenBefore.loc.end.line <= maxLines
120+
currentNode.loc.start.line - tokenBefore.loc.end.line >= minLines &&
121+
currentNode.loc.start.line - tokenBefore.loc.end.line <= maxLines
110122
) {
111123
return tokenBefore;
112124
}

test/eslint/getJSDocComment.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,11 @@ ruleTester.run('getJSDocComment', rule, {
4343
/** Doc */
4444
var a = {};
4545
`,
46+
}, {
47+
code: `
48+
/** Doc */
49+
// eslint-disable-next-line no-var
50+
var a = {};
51+
`,
4652
}],
4753
});

test/rules/assertions/checkExamples.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ export default {
504504
],
505505
options: [{
506506
baseConfig: {
507-
parser: require.resolve('@typescript-eslint/parser'),
507+
parser: '@typescript-eslint/parser',
508508
parserOptions: {
509509
ecmaVersion: 6,
510510
},
@@ -695,7 +695,7 @@ export default {
695695
`,
696696
options: [{
697697
baseConfig: {
698-
parser: require.resolve('@typescript-eslint/parser'),
698+
parser: '@typescript-eslint/parser',
699699
parserOptions: {
700700
ecmaVersion: 6,
701701
},

0 commit comments

Comments
 (0)