UNPKG

5.83 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.getImportModules = void 0;
7const tsserverlibrary_1 = __importDefault(require("typescript/lib/tsserverlibrary"));
8const deno_type_hint_1 = require("./deno_type_hint");
9function getImportModules(sourceFile) {
10 const moduleNodes = [];
11 function delint(SourceFile) {
12 function delintNode(node) {
13 let moduleNode = null;
14 // import('xxx')
15 if (tsserverlibrary_1.default.isCallExpression(node)) {
16 const expression = node.expression;
17 const args = node.arguments;
18 const isDynamicImport = expression.kind === tsserverlibrary_1.default.SyntaxKind.ImportKeyword;
19 /* istanbul ignore else */
20 if (isDynamicImport) {
21 const argv = args[0];
22 /* istanbul ignore else */
23 if (argv && tsserverlibrary_1.default.isStringLiteral(argv)) {
24 moduleNode = argv;
25 }
26 }
27 } // import ts = require('ts')
28 else if (tsserverlibrary_1.default.isImportEqualsDeclaration(node)) {
29 const ref = node.moduleReference;
30 /* istanbul ignore else */
31 if (tsserverlibrary_1.default.isExternalModuleReference(ref) &&
32 ref.expression &&
33 tsserverlibrary_1.default.isStringLiteral(ref.expression)) {
34 moduleNode = ref.expression;
35 }
36 } // import * as from 'xx'
37 // import 'xx'
38 // import xx from 'xx'
39 else if (tsserverlibrary_1.default.isImportDeclaration(node)) {
40 const spec = node.moduleSpecifier;
41 /* istanbul ignore else */
42 if (tsserverlibrary_1.default.isStringLiteral(spec)) {
43 moduleNode = spec;
44 }
45 } // export { window } from "xxx";
46 // export * from "xxx";
47 // export * as xxx from "xxx";
48 else if (tsserverlibrary_1.default.isExportDeclaration(node)) {
49 const exportSpec = node.moduleSpecifier;
50 /* istanbul ignore else */
51 if (exportSpec && tsserverlibrary_1.default.isStringLiteral(exportSpec)) {
52 moduleNode = exportSpec;
53 }
54 }
55 if (moduleNode) {
56 moduleNodes.push(moduleNode);
57 }
58 tsserverlibrary_1.default.forEachChild(node, delintNode);
59 }
60 delintNode(SourceFile);
61 }
62 // delint it
63 delint(sourceFile);
64 const text = sourceFile.getFullText();
65 const getComments = (node, isTrailing) => {
66 /* istanbul ignore else */
67 if (node.parent) {
68 const nodePos = isTrailing ? node.end : node.pos;
69 const parentPos = isTrailing ? node.parent.end : node.parent.pos;
70 if (node.parent.kind === tsserverlibrary_1.default.SyntaxKind.SourceFile ||
71 nodePos !== parentPos) {
72 const comments = isTrailing
73 ? tsserverlibrary_1.default.getTrailingCommentRanges(sourceFile.text, nodePos)
74 : tsserverlibrary_1.default.getLeadingCommentRanges(sourceFile.text, nodePos);
75 if (Array.isArray(comments)) {
76 return comments.map((v) => {
77 const target = Object.assign(Object.assign({}, v), { text: text.substring(v.pos, v.end) });
78 return target;
79 });
80 }
81 return undefined;
82 }
83 }
84 };
85 const modules = sourceFile.typeReferenceDirectives
86 .map((directive) => {
87 const start = sourceFile.getLineAndCharacterOfPosition(directive.pos);
88 const end = sourceFile.getLineAndCharacterOfPosition(directive.end);
89 const module = {
90 moduleName: directive.fileName,
91 location: { start, end },
92 start: directive.pos,
93 length: directive.end - directive.pos,
94 };
95 return module;
96 })
97 .concat(moduleNodes.map((node) => {
98 const numberOfSpaces = Math.abs(
99 // why plus 2?
100 // because `moduleNode.text` only contain the plaintext without two quotes
101 // eg `import "./test"`
102 node.end - node.pos - (node.text.length + 2));
103 const startPosition = node.pos + numberOfSpaces + 1; // +1 to remove quotes
104 const endPosition = startPosition + node.text.length;
105 const start = sourceFile.getLineAndCharacterOfPosition(startPosition);
106 const end = sourceFile.getLineAndCharacterOfPosition(endPosition);
107 const location = {
108 start,
109 end,
110 };
111 const leadingComments = getComments(node.parent, false);
112 const trailingComments = getComments(node.parent, true);
113 const module = {
114 moduleName: node.text,
115 location,
116 start: startPosition,
117 length: endPosition - startPosition,
118 };
119 if (trailingComments) {
120 module.trailingComments = trailingComments;
121 }
122 if (leadingComments) {
123 module.leadingComments = leadingComments;
124 // get the last comment
125 const comment = module.leadingComments[module.leadingComments.length - 1];
126 const hint = deno_type_hint_1.parseCompileHint(sourceFile, comment);
127 module.hint = hint;
128 }
129 return module;
130 }));
131 return modules;
132}
133exports.getImportModules = getImportModules;
134//# sourceMappingURL=deno_modules.js.map
\No newline at end of file