UNPKG

4.25 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10 Object.defineProperty(o, "default", { enumerable: true, value: v });
11}) : function(o, v) {
12 o["default"] = v;
13});
14var __importStar = (this && this.__importStar) || function (mod) {
15 if (mod && mod.__esModule) return mod;
16 var result = {};
17 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18 __setModuleDefault(result, mod);
19 return result;
20};
21var __exportStar = (this && this.__exportStar) || function(m, exports) {
22 for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
23};
24Object.defineProperty(exports, "__esModule", { value: true });
25exports.forEachReturnStatement = exports.getNameLocationInGlobalDirectiveComment = void 0;
26const escapeRegExp_1 = require("./escapeRegExp");
27const ts = __importStar(require("typescript"));
28// deeply re-export, for convenience
29__exportStar(require("@typescript-eslint/utils/dist/ast-utils"), exports);
30// The following is copied from `eslint`'s source code since it doesn't exist in eslint@5.
31// https://github.com/eslint/eslint/blob/145aec1ab9052fbca96a44d04927c595951b1536/lib/rules/utils/ast-utils.js#L1751-L1779
32// Could be export { getNameLocationInGlobalDirectiveComment } from 'eslint/lib/rules/utils/ast-utils'
33/**
34 * Get the `loc` object of a given name in a `/*globals` directive comment.
35 * @param {SourceCode} sourceCode The source code to convert index to loc.
36 * @param {Comment} comment The `/*globals` directive comment which include the name.
37 * @param {string} name The name to find.
38 * @returns {SourceLocation} The `loc` object.
39 */
40function getNameLocationInGlobalDirectiveComment(sourceCode, comment, name) {
41 const namePattern = new RegExp(`[\\s,]${(0, escapeRegExp_1.escapeRegExp)(name)}(?:$|[\\s,:])`, 'gu');
42 // To ignore the first text "global".
43 namePattern.lastIndex = comment.value.indexOf('global') + 6;
44 // Search a given variable name.
45 const match = namePattern.exec(comment.value);
46 // Convert the index to loc.
47 const start = sourceCode.getLocFromIndex(comment.range[0] + '/*'.length + (match ? match.index + 1 : 0));
48 const end = {
49 line: start.line,
50 column: start.column + (match ? name.length : 1),
51 };
52 return { start, end };
53}
54exports.getNameLocationInGlobalDirectiveComment = getNameLocationInGlobalDirectiveComment;
55// Copied from typescript https://github.com/microsoft/TypeScript/blob/42b0e3c4630c129ca39ce0df9fff5f0d1b4dd348/src/compiler/utilities.ts#L1335
56// Warning: This has the same semantics as the forEach family of functions,
57// in that traversal terminates in the event that 'visitor' supplies a truthy value.
58function forEachReturnStatement(body, visitor) {
59 return traverse(body);
60 function traverse(node) {
61 switch (node.kind) {
62 case ts.SyntaxKind.ReturnStatement:
63 return visitor(node);
64 case ts.SyntaxKind.CaseBlock:
65 case ts.SyntaxKind.Block:
66 case ts.SyntaxKind.IfStatement:
67 case ts.SyntaxKind.DoStatement:
68 case ts.SyntaxKind.WhileStatement:
69 case ts.SyntaxKind.ForStatement:
70 case ts.SyntaxKind.ForInStatement:
71 case ts.SyntaxKind.ForOfStatement:
72 case ts.SyntaxKind.WithStatement:
73 case ts.SyntaxKind.SwitchStatement:
74 case ts.SyntaxKind.CaseClause:
75 case ts.SyntaxKind.DefaultClause:
76 case ts.SyntaxKind.LabeledStatement:
77 case ts.SyntaxKind.TryStatement:
78 case ts.SyntaxKind.CatchClause:
79 return ts.forEachChild(node, traverse);
80 }
81 return undefined;
82 }
83}
84exports.forEachReturnStatement = forEachReturnStatement;
85//# sourceMappingURL=astUtils.js.map
\No newline at end of file