Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/etc/get-type-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,16 @@ export function getTypeServices<
|| ts.isMethodDeclaration(tsNode)
|| ts.isFunctionExpression(tsNode)
) {
tsTypeNode = tsNode.type ?? tsNode.body; // TODO(#57): this doesn't work for Block bodies.
if (tsNode.type) {
tsTypeNode = tsNode.type;
} else if (tsNode.body && ts.isBlock(tsNode.body)) {
const returnStatement = tsNode.body.statements.find(ts.isReturnStatement);
if (returnStatement?.expression) {
tsTypeNode = returnStatement.expression;
}
} else {
tsTypeNode = tsNode.body;
}
} else if (
ts.isCallSignatureDeclaration(tsNode)
|| ts.isMethodSignature(tsNode)
Expand Down
10 changes: 10 additions & 0 deletions tests/rules/finnish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ ruleTester({ types: true }).run('finnish', finnishRule, {
someArray.forEach((element$: Observable<any>) => {});

function someFunction$(someParam$: Observable<any>): Observable<any> { return someParam; }
function someImplicitReturnFunction$(someParam$: Observable<any>) { return someParam; }

class SomeClass {
someProperty$: Observable<any>;
constructor (someParam$: Observable<any>) {}
get someGetter$(): Observable<any> { throw new Error("Some error."); }
set someSetter$(someParam$: Observable<any>) {}
someMethod$(someParam$: Observable<any>): Observable<any> { return someParam; }
someImplicitReturnMethod$(someParam$: Observable<any>) { return someParam; }
}

interface SomeInterface {
Expand Down Expand Up @@ -131,6 +133,7 @@ ruleTester({ types: true }).run('finnish', finnishRule, {
const someObservable$ = of(0);
const someArray = [someObservable$];
function someFunction(someParam$: Observable<any>): Observable<any> { return someParam$; }
function someImplicitReturnFunction(someParam$: Observable<any>) { return someParam$; }
`,
options: [{ functions: false }],
},
Expand All @@ -141,6 +144,7 @@ ruleTester({ types: true }).run('finnish', finnishRule, {

class SomeClass {
someMethod(someParam$: Observable<any>): Observable<any> { return someParam$; }
someImplicitReturnMethod(someParam$: Observable<any>) { return someParam$; }
}

interface SomeInterface {
Expand Down Expand Up @@ -269,6 +273,8 @@ ruleTester({ types: true }).run('finnish', finnishRule, {
const someArray = [someObservable$];
function someFunction(someParam$: Observable<any>): Observable<any> { return someParam$; }
~~~~~~~~~~~~ [shouldBeFinnish]
function someImplicitReturnFunction(someParam$: Observable<any>) { return someParam$; }
~~~~~~~~~~~~~~~~~~~~~~~~~~ [shouldBeFinnish]
`,
),
fromFixture(
Expand All @@ -279,6 +285,8 @@ ruleTester({ types: true }).run('finnish', finnishRule, {
class SomeClass {
someMethod(someParam$: Observable<any>): Observable<any> { return someParam$; }
~~~~~~~~~~ [shouldBeFinnish]
someImplicitReturnMethod(someParam$: Observable<any>) { return someParam$; }
~~~~~~~~~~~~~~~~~~~~~~~~ [shouldBeFinnish]
}

interface SomeInterface {
Expand Down Expand Up @@ -413,6 +421,8 @@ ruleTester({ types: true }).run('finnish', finnishRule, {
class SomeClass {
someMethod(someValue: any): Observable<any> { return of(someValue); }
~~~~~~~~~~ [shouldBeFinnish]
someImplicitReturnMethod(someValue: any) { return of(someValue); }
~~~~~~~~~~~~~~~~~~~~~~~~ [shouldBeFinnish]
}

interface SomeInterface {
Expand Down
4 changes: 4 additions & 0 deletions tests/rules/no-finnish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ ruleTester({ types: true }).run('no-finnish', noFinnishRule, {
someMethod$(someParam$: Observable<any>): Observable<any> { return someParam; }
~~~~~~~~~~~ [forbidden]
~~~~~~~~~~ [forbidden]
someImplicitReturnMethod$(someParam: Observable<any>) {
~~~~~~~~~~~~~~~~~~~~~~~~~ [forbidden]
return someParam;
}
}
`,
),
Expand Down
15 changes: 9 additions & 6 deletions tests/rules/no-misused-observables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,6 @@ ruleTester({ types: true }).run('no-misused-observables', noMisusedObservablesRu
new Foo(() => 42, 0);
new Foo;
`,
stripIndent`
// couldReturnType is bugged for block body implicit return types (#57)
import { of } from "rxjs";

[1, 2, 3].forEach(i => { return of(i); });
`,
// #endregion valid; void return argument
// #region valid; void return attribute
{
Expand Down Expand Up @@ -300,6 +294,15 @@ ruleTester({ types: true }).run('no-misused-observables', noMisusedObservablesRu
~~~~~~~~~~ [forbiddenVoidReturnArgument]
`,
),
fromFixture(
stripIndent`
// void return argument; block body; implicit return type
import { of } from "rxjs";

[1, 2, 3].forEach(i => { return of(i); });
~~~~~~~~~~~~~~~~~~~~~~ [forbiddenVoidReturnArgument]
`,
),
fromFixture(
stripIndent`
// void return argument; block body; union return
Expand Down