1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | const tsserverlibrary_1 = __importDefault(require("typescript/lib/tsserverlibrary"));
|
7 | exports.Position = {
|
8 | |
9 |
|
10 |
|
11 |
|
12 |
|
13 | create(line, character) {
|
14 | return { line, character };
|
15 | },
|
16 | };
|
17 | exports.Range = {
|
18 | create(start, end) {
|
19 | return { start, end };
|
20 | },
|
21 | };
|
22 | function 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 | }
|
40 | exports.parseCompileHint = parseCompileHint;
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | function 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 |
|
59 | if (compileHint) {
|
60 | denoTypesComments.push(compileHint);
|
61 | }
|
62 | }
|
63 | }
|
64 | }
|
65 | return denoTypesComments;
|
66 | }
|
67 | exports.getDenoCompileHint = getDenoCompileHint;
|
68 |
|
\ | No newline at end of file |