Skip to content

Commit 65eb993

Browse files
a-tarasyukbradzacher
authored andcommitted
feat(eslint-plugin): [expl-func-ret-type] make error loc smaller (typescript-eslint#919)
1 parent 6bd7f2d commit 65eb993

File tree

2 files changed

+166
-5
lines changed

2 files changed

+166
-5
lines changed

packages/eslint-plugin/src/rules/explicit-function-return-type.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
TSESTree,
33
AST_NODE_TYPES,
4+
AST_TOKEN_TYPES,
45
} from '@typescript-eslint/experimental-utils';
56
import * as util from '../util';
67

@@ -57,6 +58,53 @@ export default util.createRule<Options, MessageIds>({
5758
},
5859
],
5960
create(context, [options]) {
61+
const sourceCode = context.getSourceCode();
62+
63+
/**
64+
* Returns start column position
65+
* @param node
66+
*/
67+
function getLocStart(
68+
node:
69+
| TSESTree.ArrowFunctionExpression
70+
| TSESTree.FunctionDeclaration
71+
| TSESTree.FunctionExpression,
72+
): TSESTree.LineAndColumnData {
73+
/* highlight method name */
74+
const parent = node.parent;
75+
if (
76+
parent &&
77+
(parent.type === AST_NODE_TYPES.MethodDefinition ||
78+
(parent.type === AST_NODE_TYPES.Property && parent.method))
79+
) {
80+
return parent.loc.start;
81+
}
82+
83+
return node.loc.start;
84+
}
85+
86+
/**
87+
* Returns end column position
88+
* @param node
89+
*/
90+
function getLocEnd(
91+
node:
92+
| TSESTree.ArrowFunctionExpression
93+
| TSESTree.FunctionDeclaration
94+
| TSESTree.FunctionExpression,
95+
): TSESTree.LineAndColumnData {
96+
/* highlight `=>` */
97+
if (node.type === AST_NODE_TYPES.ArrowFunctionExpression) {
98+
return sourceCode.getTokenBefore(
99+
node.body,
100+
token =>
101+
token.type === AST_TOKEN_TYPES.Punctuator && token.value === '=>',
102+
)!.loc.end;
103+
}
104+
105+
return sourceCode.getTokenBefore(node.body!)!.loc.end;
106+
}
107+
60108
/**
61109
* Checks if a node is a constructor.
62110
* @param node The node to check
@@ -258,6 +306,7 @@ export default util.createRule<Options, MessageIds>({
258306

259307
context.report({
260308
node,
309+
loc: { start: getLocStart(node), end: getLocEnd(node) },
261310
messageId: 'missingReturnType',
262311
});
263312
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy