|
1 | 1 | import {
|
2 | 2 | TSESTree,
|
3 | 3 | AST_NODE_TYPES,
|
| 4 | + AST_TOKEN_TYPES, |
4 | 5 | } from '@typescript-eslint/experimental-utils';
|
5 | 6 | import * as util from '../util';
|
6 | 7 |
|
@@ -57,6 +58,53 @@ export default util.createRule<Options, MessageIds>({
|
57 | 58 | },
|
58 | 59 | ],
|
59 | 60 | 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 | + |
60 | 108 | /**
|
61 | 109 | * Checks if a node is a constructor.
|
62 | 110 | * @param node The node to check
|
@@ -258,6 +306,7 @@ export default util.createRule<Options, MessageIds>({
|
258 | 306 |
|
259 | 307 | context.report({
|
260 | 308 | node,
|
| 309 | + loc: { start: getLocStart(node), end: getLocEnd(node) }, |
261 | 310 | messageId: 'missingReturnType',
|
262 | 311 | });
|
263 | 312 | }
|
|
0 commit comments