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 | exports.getDenoCompileHint = exports.parseCompileHint = exports.Range = exports.Position = void 0;
|
7 | const tsserverlibrary_1 = __importDefault(require("typescript/lib/tsserverlibrary"));
|
8 | exports.Position = {
|
9 | |
10 |
|
11 |
|
12 |
|
13 |
|
14 | create(line, character) {
|
15 | return { line, character };
|
16 | },
|
17 | };
|
18 | exports.Range = {
|
19 | create(start, end) {
|
20 | return { start, end };
|
21 | },
|
22 | };
|
23 | function 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 | }
|
41 | exports.parseCompileHint = parseCompileHint;
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | function 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 |
|
60 | if (compileHint) {
|
61 | denoTypesComments.push(compileHint);
|
62 | }
|
63 | }
|
64 | }
|
65 | }
|
66 | return denoTypesComments;
|
67 | }
|
68 | exports.getDenoCompileHint = getDenoCompileHint;
|
69 |
|
\ | No newline at end of file |