Skip to content

Commit 8e06ed9

Browse files
feat(no-misused-observables): improve report loc for methods (#238)
See tseslint issue 10216. If a property is a function, instead of reporting the entire function, report either its return type or else use `getFunctionHeadLocation` to report a more narrow location. See tests for example.
1 parent c712a22 commit 8e06ed9

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/rules/no-misused-observables.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { AST_NODE_TYPES, TSESTree as es, TSESLint as eslint, ESLintUtils } from '@typescript-eslint/utils';
2+
import { getFunctionHeadLocation, isFunction } from '@typescript-eslint/utils/ast-utils';
23
import * as tsutils from 'ts-api-utils';
34
import ts from 'typescript';
45
import {
@@ -251,10 +252,25 @@ export const noMisusedObservablesRule = ruleCreator({
251252
return;
252253
}
253254

254-
context.report({
255-
messageId: 'forbiddenVoidReturnProperty',
256-
node: node.value,
257-
});
255+
if (isFunction(node.value)) {
256+
const functionNode = node.value;
257+
if (functionNode.returnType) {
258+
context.report({
259+
messageId: 'forbiddenVoidReturnProperty',
260+
node: functionNode.returnType.typeAnnotation,
261+
});
262+
} else {
263+
context.report({
264+
messageId: 'forbiddenVoidReturnProperty',
265+
loc: getFunctionHeadLocation(functionNode, context.sourceCode),
266+
});
267+
}
268+
} else {
269+
context.report({
270+
messageId: 'forbiddenVoidReturnProperty',
271+
node: node.value,
272+
});
273+
}
258274
}
259275

260276
function checkReturnStatement(node: es.ReturnStatement): void {
@@ -277,6 +293,7 @@ export const noMisusedObservablesRule = ruleCreator({
277293
return current;
278294
}
279295
const functionNode = getFunctionNode();
296+
280297
if (
281298
functionNode?.returnType
282299
&& !isPossiblyFunctionType(functionNode.returnType)

tests/rules/no-misused-observables.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ ruleTester({ types: true }).run('no-misused-observables', noMisusedObservablesRu
740740
type Foo = { bar: () => void };
741741
const foo: Foo = {
742742
bar: () => of(42),
743-
~~~~~~~~~~~~ [forbiddenVoidReturnProperty]
743+
~~ [forbiddenVoidReturnProperty]
744744
};
745745
`,
746746
),
@@ -752,7 +752,7 @@ ruleTester({ types: true }).run('no-misused-observables', noMisusedObservablesRu
752752
type Foo = { bar: () => void };
753753
const foo: Foo = {
754754
bar(): Observable<number> { return of(42); },
755-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [forbiddenVoidReturnProperty]
755+
~~~~~~~~~~~~~~~~~~ [forbiddenVoidReturnProperty]
756756
};
757757
`,
758758
),

0 commit comments

Comments
 (0)