UNPKG

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