UNPKG

2.5 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6const tsserverlibrary_1 = __importDefault(require("typescript/lib/tsserverlibrary"));
7exports.Position = {
8 /**
9 * Creates a new Position literal from the given line and character.
10 * @param line The position's line.
11 * @param character The position's character.
12 */
13 create(line, character) {
14 return { line, character };
15 },
16};
17exports.Range = {
18 create(start, end) {
19 return { start, end };
20 },
21};
22function parseCompileHint(sourceFile, comment) {
23 const text = sourceFile.getFullText().substring(comment.pos, comment.end);
24 const regexp = /@deno-types=['"]([^'"]+)['"]/;
25 const matchers = regexp.exec(text);
26 if (!matchers) {
27 return;
28 }
29 const start = sourceFile.getLineAndCharacterOfPosition(comment.pos);
30 const end = sourceFile.getLineAndCharacterOfPosition(comment.end);
31 const moduleNameStart = exports.Position.create(start.line, start.character + '// @deno-types="'.length);
32 const moduleNameEnd = exports.Position.create(end.line, end.character - '"'.length);
33 const moduleName = matchers[1];
34 return {
35 text: moduleName,
36 range: exports.Range.create(start, end),
37 contentRange: exports.Range.create(moduleNameStart, moduleNameEnd),
38 };
39}
40exports.parseCompileHint = parseCompileHint;
41/**
42 * Get Deno compile hint from a source file
43 * @param ts
44 */
45function getDenoCompileHint(sourceFile, pos = 0) {
46 const denoTypesComments = [];
47 const comments = tsserverlibrary_1.default.getLeadingCommentRanges(sourceFile.getFullText(), pos) ||
48 [];
49 for (const comment of comments) {
50 if (comment.hasTrailingNewLine) {
51 const text = sourceFile
52 .getFullText()
53 .substring(comment.pos, comment.end);
54 const regexp = /@deno-types=['"]([^'"]+)['"]/;
55 const matchers = regexp.exec(text);
56 if (matchers) {
57 const compileHint = parseCompileHint(sourceFile, comment);
58 /* istanbul ignore else */
59 if (compileHint) {
60 denoTypesComments.push(compileHint);
61 }
62 }
63 }
64 }
65 return denoTypesComments;
66}
67exports.getDenoCompileHint = getDenoCompileHint;
68//# sourceMappingURL=deno_type_hint.js.map
\No newline at end of file