Skip to content

Commit c898dec

Browse files
geraintwhitegajus
authored andcommitted
fix: require-return-type excludeMatching for class properties (#217) (#303)
1 parent 677e55c commit c898dec

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

src/rules/requireReturnType.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ const create = (context) => {
6666
const shouldFilterNode = (functionNode) => {
6767
const isArrow = functionNode.type === 'ArrowFunctionExpression';
6868
const isMethod = functionNode.parent && functionNode.parent.type === 'MethodDefinition';
69+
const isProperty = functionNode.parent && functionNode.parent.type === 'ClassProperty';
6970
let selector;
7071

71-
if (isMethod) {
72+
if (isMethod || isProperty) {
7273
selector = 'parent.key.name';
7374
} else if (isArrow) {
7475
selector = 'parent.id.name';

tests/rules/assertions/requireReturnType.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,30 @@ export default {
239239
}
240240
]
241241
},
242+
{
243+
code: 'class Test { foo() { return 42; } }',
244+
errors: [
245+
{
246+
message: 'Missing return type annotation.'
247+
}
248+
]
249+
},
250+
{
251+
code: 'class Test { foo = () => { return 42; } }',
252+
errors: [
253+
{
254+
message: 'Missing return type annotation.'
255+
}
256+
]
257+
},
258+
{
259+
code: 'class Test { foo = () => 42; }',
260+
errors: [
261+
{
262+
message: 'Missing return type annotation.'
263+
}
264+
]
265+
},
242266
{
243267
code: 'async () => { return; }',
244268
errors: [
@@ -645,6 +669,39 @@ export default {
645669
{
646670
code: 'class Test { constructor() { } }'
647671
},
672+
{
673+
code: 'class Test { foo() { return 42; } }',
674+
options: [
675+
'always',
676+
{
677+
excludeMatching: [ 'foo' ]
678+
}
679+
]
680+
},
681+
{
682+
code: 'class Test { foo = () => { return 42; } }',
683+
options: [
684+
'always',
685+
{
686+
excludeMatching: [ 'foo' ]
687+
}
688+
]
689+
},
690+
{
691+
code: 'class Test { foo = () => 42; }',
692+
options: [
693+
'always',
694+
{
695+
excludeMatching: [ 'foo' ]
696+
}
697+
]
698+
},
699+
{
700+
code: 'class Test { foo = (): number => { return 42; } }'
701+
},
702+
{
703+
code: 'class Test { foo = (): number => 42; }'
704+
},
648705
{
649706
code: 'async (foo): Promise<number> => { return 3; }'
650707
},

0 commit comments

Comments
 (0)