Skip to content

Commit

Permalink
fix(require-jsdoc): place comment block above any decorators; fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Jun 18, 2020
1 parent 75fd013 commit d5f8159
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -8767,6 +8767,11 @@ class Animal {
}
// Options: [{"contexts":["ClassProperty"]}]
// Message: Missing JSDoc comment.
@Entity('users')
export class User {}
// Options: [{"require":{"ClassDeclaration":true}}]
// Message: Missing JSDoc comment.
````
The following patterns are not considered problems:
Expand Down
2 changes: 1 addition & 1 deletion src/eslint/getJSDocComment.js
Expand Up @@ -253,5 +253,5 @@ const getJSDocComment = function (sourceCode, node, settings) {
return findJSDocComment(reducedNode);
};

export {getReducedASTNode, getJSDocComment};
export {getReducedASTNode, getJSDocComment, getDecorator};
export default getJSDocComment;
13 changes: 11 additions & 2 deletions src/rules/requireJsdoc.js
@@ -1,7 +1,7 @@
import _ from 'lodash';
import jsdocUtils from '../jsdocUtils';
import exportParser from '../exportParser';
import {getJSDocComment, getReducedASTNode} from '../eslint/getJSDocComment';
import {getJSDocComment, getReducedASTNode, getDecorator} from '../eslint/getJSDocComment';
import {getSettings} from '../iterateJsdoc';

const OPTIONS_SCHEMA = {
Expand Down Expand Up @@ -166,7 +166,16 @@ export default {
const fix = (fixer) => {
// Default to one line break if the `minLines`/`maxLines` settings allow
const lines = settings.minLines === 0 && settings.maxLines >= 1 ? 1 : settings.minLines;
const baseNode = getReducedASTNode(node, sourceCode);
let baseNode = getReducedASTNode(node, sourceCode);

let decorator;
do {
const tokenBefore = sourceCode.getTokenBefore(baseNode, {includeComments: true});
decorator = getDecorator(tokenBefore, sourceCode);
if (decorator) {
baseNode = decorator;
}
} while (decorator);

const indent = jsdocUtils.getIndent({
text: sourceCode.getText(
Expand Down
30 changes: 30 additions & 0 deletions test/rules/assertions/requireJsdoc.js
Expand Up @@ -2241,6 +2241,36 @@ export default {
sourceType: 'module',
},
},
{
code: `
@Entity('users')
export class User {}
`,
errors: [
{
line: 3,
message: 'Missing JSDoc comment.',
},
],
options: [
{
require: {
ClassDeclaration: true,
},
},
],
output: `
/**
*
*/
@Entity('users')
export class User {}
`,
parser: require.resolve('@typescript-eslint/parser'),
parserOptions: {
sourceType: 'module',
},
},
],
valid: [{
code: `
Expand Down

0 comments on commit d5f8159

Please sign in to comment.