UNPKG

4.73 kBJavaScriptView Raw
1"use strict";
2var babylon_1 = require("babylon");
3var babel_types_1 = require("babel-types");
4var babel_traverse_1 = require("babel-traverse");
5// TODO: Mocha currently doesn't pick up the declaration in index.d.ts
6var findLineColumn = require("find-line-column");
7function parseImports(code) {
8 var parsed = babylon_1.parse(code, {
9 sourceType: "module",
10 plugins: [
11 "jsx",
12 "flow",
13 "doExpressions",
14 "objectRestSpread",
15 "decorators",
16 "classProperties",
17 "exportExtensions",
18 "asyncGenerators",
19 "functionBind",
20 "functionSent",
21 ],
22 });
23 var imports = [];
24 babel_traverse_1.default(parsed, {
25 ImportDeclaration: function (path) {
26 var node = path.node;
27 var start = node.start;
28 var end = node.end;
29 if (node.leadingComments) {
30 var comments = node.leadingComments;
31 var current = node.leadingComments.length - 1;
32 var previous = void 0;
33 while (comments[current] && comments[current].end + 1 === start) {
34 if (code.substring(comments[current].start, comments[current].end).indexOf("#!") === 0) {
35 break;
36 }
37 // TODO: Improve this so that comments with leading whitespace are allowed
38 if (findLineColumn(code, comments[current].start).col !== 0) {
39 break;
40 }
41 previous = current;
42 start = comments[previous].start;
43 current--;
44 }
45 }
46 if (node.trailingComments) {
47 var comments = node.trailingComments;
48 var current = 0;
49 var previous = void 0;
50 while (comments[current] && comments[current].start - 1 === end) {
51 if (comments[current].loc.start.line !== node.loc.start.line) {
52 break;
53 }
54 previous = current;
55 end = comments[previous].end;
56 current++;
57 }
58 }
59 var imported = {
60 start: start,
61 end: end,
62 moduleName: node.source.value,
63 type: "import",
64 namedMembers: [],
65 };
66 if (node.specifiers) {
67 node.specifiers.forEach(function (specifier) {
68 if (babel_types_1.isImportSpecifier(specifier)) {
69 imported.namedMembers.push({
70 name: specifier.imported.name,
71 alias: specifier.local.name,
72 });
73 }
74 else if (babel_types_1.isImportDefaultSpecifier(specifier)) {
75 imported.defaultMember = specifier.local.name;
76 }
77 else if (babel_types_1.isImportNamespaceSpecifier) {
78 imported.namespaceMember = specifier.local.name;
79 }
80 });
81 }
82 imports.push(imported);
83 },
84 });
85 return imports;
86}
87exports.parseImports = parseImports;
88function formatImport(code, imported) {
89 var originalImportCode = code.substring(imported.start, imported.end);
90 var namedMembers = imported.namedMembers;
91 if (namedMembers.length === 0) {
92 return originalImportCode;
93 }
94 return originalImportCode.replace(/\{[\s\S]*\}/g, function (namedMembersString) {
95 var useMultipleLines = namedMembersString.indexOf("\n") !== -1;
96 if (useMultipleLines) {
97 var prefix = namedMembersString.split("\n")[1].match(/^\s*/)[0];
98 return formatNamedMembers(namedMembers, useMultipleLines, prefix);
99 }
100 return formatNamedMembers(namedMembers, useMultipleLines);
101 });
102}
103exports.formatImport = formatImport;
104function formatNamedMembers(namedMembers, useMultipleLines, prefix) {
105 if (useMultipleLines) {
106 return "{\n" + namedMembers.map(function (_a) {
107 var name = _a.name, alias = _a.alias;
108 if (name === alias) {
109 return "" + prefix + name + ",\n";
110 }
111 return "" + prefix + name + " as " + alias + ",\n";
112 }).join("") + "}";
113 }
114 else {
115 return "{" + namedMembers.map(function (_a) {
116 var name = _a.name, alias = _a.alias;
117 if (name === alias) {
118 return "" + name;
119 }
120 return name + " as " + alias;
121 }).join(", ") + "}";
122 }
123}