UNPKG

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