Skip to content

Commit e13a03a

Browse files
committed
optimize: ensure with new fix that we avoid checking back too far
1 parent 6bda6b4 commit e13a03a

3 files changed

Lines changed: 69 additions & 1 deletion

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10565,6 +10565,13 @@ export class User {
1056510565
}
1056610566
// Options: [{"contexts":["ClassProperty:has(Decorator[expression.callee.name=\"Input\"])"]}]
1056710567
// Message: Missing JSDoc comment.
10568+
10569+
@Input()
10570+
export class UserSettingsState {
10571+
method () {}
10572+
}
10573+
// Options: [{"require":{"MethodDefinition":true}}]
10574+
// Message: Missing JSDoc comment.
1056810575
````
1056910576

1057010577
The following patterns are not considered problems:

src/eslint/getJSDocComment.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,29 @@ const isCommentToken = (token) => {
1616

1717
const decoratorMetaTokens = new Map([[')', '('], ['>', '<']]);
1818

19+
// eslint-disable-next-line complexity
1920
const getDecorator = (token, sourceCode) => {
2021
if (!token) {
2122
return false;
2223
}
24+
2325
if (token.type === 'Punctuator') {
2426
const tokenClose = token.value;
2527
const tokenOpen = decoratorMetaTokens.get(tokenClose);
2628
if (tokenOpen) {
2729
let nested = 0;
2830
let tokenBefore = token;
31+
let exitEarlyUnlessTyped = false;
2932
do {
3033
tokenBefore = sourceCode.getTokenBefore(tokenBefore, {includeComments: true});
3134
// istanbul ignore if
3235
if (tokenBefore && tokenBefore.type === 'Punctuator') {
3336
if (tokenBefore.value === tokenClose) {
3437
nested++;
3538
} else if (tokenBefore.value === tokenOpen) {
39+
if (tokenOpen === '(') {
40+
exitEarlyUnlessTyped = true;
41+
}
3642
if (nested) {
3743
nested--;
3844
} else {
@@ -42,7 +48,33 @@ const getDecorator = (token, sourceCode) => {
4248
}
4349
} while (tokenBefore);
4450

45-
return getDecorator(tokenBefore, sourceCode);
51+
// Because our token retrieval doesn't give us as much info as AST, and
52+
// because decorators can be nested and fairly complex, besides finding
53+
// any decorators, we also want to avoid checking backwards indefinitely
54+
// in a potentially large document, so we exit early if parentheses are
55+
// not preceded by a type where we have to keep checking backward for
56+
// now (such as a regular call expression) or if a decorator is found.
57+
if (exitEarlyUnlessTyped) {
58+
// Check for `@someDecorator(`
59+
const identifier = sourceCode.getTokenBefore(tokenBefore, {includeComments: true});
60+
if (identifier && identifier.type === 'Identifier') {
61+
const before = sourceCode.getTokenBefore(identifier, {includeComments: true});
62+
if (before && before.type === 'Punctuator' && before.value === '@') {
63+
// Decorator found
64+
return before;
65+
}
66+
}
67+
68+
// If decorators may have `:`, we might need to check for those as with typed.
69+
if (!identifier || identifier.type !== 'Punctuator' || identifier.value !== '>') {
70+
// Should not be a decorator
71+
return false;
72+
}
73+
74+
// Could be a typed decorator, so keep checking for one
75+
}
76+
77+
return getDecorator(tokenBefore, sourceCode, true);
4678
}
4779
if (token.value === '@') {
4880
return token;

test/rules/assertions/requireJsdoc.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2763,6 +2763,35 @@ function quux (foo) {
27632763
`,
27642764
parser: require.resolve('@typescript-eslint/parser'),
27652765
},
2766+
{
2767+
code: `
2768+
@Input()
2769+
export class UserSettingsState {
2770+
method () {}
2771+
}
2772+
`,
2773+
errors: [
2774+
{
2775+
line: 4,
2776+
message: 'Missing JSDoc comment.',
2777+
},
2778+
],
2779+
options: [{
2780+
require: {
2781+
MethodDefinition: true,
2782+
},
2783+
}],
2784+
output: `
2785+
/**
2786+
*
2787+
*/
2788+
@Input()
2789+
export class UserSettingsState {
2790+
method () {}
2791+
}
2792+
`,
2793+
parser: require.resolve('@typescript-eslint/parser'),
2794+
},
27662795
],
27672796
valid: [{
27682797
code: `

0 commit comments

Comments
 (0)