Skip to content

Commit

Permalink
feat(require-jsdoc): allow simple decorators to intervene between c…
Browse files Browse the repository at this point in the history
…omment block and context object; fixes part of #455

This does not yet support intervening parenthesized decorators
  • Loading branch information
brettz9 committed Dec 27, 2019
1 parent 4532e66 commit 641479b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -7233,6 +7233,15 @@ enum Example {
const foo = {...{}};
function bar() {}
// Options: [{"exemptEmptyFunctions":false,"publicOnly":true,"require":{"ArrowFunctionExpression":true,"ClassDeclaration":true,"ClassExpression":true,"FunctionDeclaration":true,"FunctionExpression":false,"MethodDefinition":true}}]
/**
* Class documentation
*/
@logged
export default class Foo {
// ....
}
// Options: [{"exemptEmptyFunctions":false,"require":{"ClassDeclaration":true}}]
````
Expand Down
16 changes: 16 additions & 0 deletions src/eslint/getJSDocComment.js
Expand Up @@ -14,6 +14,17 @@ const isCommentToken = (token) => {
return token.type === 'Line' || token.type === 'Block' || token.type === 'Shebang';
};

const getDecorator = (token, sourceCode) => {
if (token && 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.
*
Expand Down Expand Up @@ -104,6 +115,11 @@ const getJSDocComment = function (sourceCode, node, settings) {

while (currentNode) {
tokenBefore = sourceCode.getTokenBefore(currentNode, {includeComments: true});
const decorator = getDecorator(tokenBefore, sourceCode);
if (decorator) {
currentNode = decorator;
continue;
}
if (!tokenBefore || !isCommentToken(tokenBefore)) {
return null;
}
Expand Down
23 changes: 23 additions & 0 deletions test/rules/assertions/requireJsdoc.js
Expand Up @@ -2354,5 +2354,28 @@ export default {
sourceType: 'module',
},
},
{
code: `
/**
* Class documentation
*/
@logged
export default class Foo {
// ....
}
`,
options: [
{
exemptEmptyFunctions: false,
require: {
ClassDeclaration: true,
},
},
],
parser: require.resolve('@typescript-eslint/parser'),
parserOptions: {
sourceType: 'module',
},
},
],
};

0 comments on commit 641479b

Please sign in to comment.