UNPKG

1.97 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.getFunctionHeadLoc = void 0;
4const utils_1 = require("@typescript-eslint/utils");
5/**
6 * Creates a report location for the given function.
7 * The location only encompasses the "start" of the function, and not the body
8 *
9 * eg.
10 *
11 * ```
12 * function foo(args) {}
13 * ^^^^^^^^^^^^^^^^^^
14 *
15 * get y(args) {}
16 * ^^^^^^^^^^^
17 *
18 * const x = (args) => {}
19 * ^^^^^^^^^
20 * ```
21 */
22function getFunctionHeadLoc(node, sourceCode) {
23 function getLocStart() {
24 if (node.parent && node.parent.type === utils_1.AST_NODE_TYPES.MethodDefinition) {
25 // return the start location for class method
26 if (node.parent.decorators && node.parent.decorators.length > 0) {
27 // exclude decorators
28 return sourceCode.getTokenAfter(node.parent.decorators[node.parent.decorators.length - 1]).loc.start;
29 }
30 return node.parent.loc.start;
31 }
32 if (node.parent &&
33 node.parent.type === utils_1.AST_NODE_TYPES.Property &&
34 node.parent.method) {
35 // return the start location for object method shorthand
36 return node.parent.loc.start;
37 }
38 // return the start location for a regular function
39 return node.loc.start;
40 }
41 function getLocEnd() {
42 if (node.type === utils_1.AST_NODE_TYPES.ArrowFunctionExpression) {
43 // find the end location for arrow function expression
44 return sourceCode.getTokenBefore(node.body, token => token.type === utils_1.AST_TOKEN_TYPES.Punctuator && token.value === '=>').loc.end;
45 }
46 // return the end location for a regular function
47 return sourceCode.getTokenBefore(node.body).loc.end;
48 }
49 return {
50 start: getLocStart(),
51 end: getLocEnd(),
52 };
53}
54exports.getFunctionHeadLoc = getFunctionHeadLoc;
55//# sourceMappingURL=getFunctionHeadLoc.js.map
\No newline at end of file