UNPKG

2.37 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};
21Object.defineProperty(exports, "__esModule", { value: true });
22exports.convertComments = void 0;
23const ts = __importStar(require("typescript"));
24const util_1 = require("tsutils/util/util");
25const node_utils_1 = require("./node-utils");
26const ts_estree_1 = require("./ts-estree");
27/**
28 * Convert all comments for the given AST.
29 * @param ast the AST object
30 * @param code the TypeScript code
31 * @returns the converted ESTreeComment
32 * @private
33 */
34function convertComments(ast, code) {
35 const comments = [];
36 (0, util_1.forEachComment)(ast, (_, comment) => {
37 const type = comment.kind == ts.SyntaxKind.SingleLineCommentTrivia
38 ? ts_estree_1.AST_TOKEN_TYPES.Line
39 : ts_estree_1.AST_TOKEN_TYPES.Block;
40 const range = [comment.pos, comment.end];
41 const loc = (0, node_utils_1.getLocFor)(range[0], range[1], ast);
42 // both comments start with 2 characters - /* or //
43 const textStart = range[0] + 2;
44 const textEnd = comment.kind === ts.SyntaxKind.SingleLineCommentTrivia
45 ? // single line comments end at the end
46 range[1] - textStart
47 : // multiline comments end 2 characters early
48 range[1] - textStart - 2;
49 comments.push({
50 type,
51 value: code.substr(textStart, textEnd),
52 range,
53 loc,
54 });
55 }, ast);
56 return comments;
57}
58exports.convertComments = convertComments;
59//# sourceMappingURL=convert-comments.js.map
\No newline at end of file