-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
module.js
115 lines (101 loc) · 2.97 KB
/
module.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"use strict";
const {
builders: { concat, softline, group, indent, join, line, ifBreak },
} = require("../../document");
const { shouldPrintComma } = require("../utils");
/**
* @typedef {import("../../document").Doc} Doc
*/
function printModuleSource(path, options, print) {
const node = path.getValue();
return node.source ? concat([" from ", path.call(print, "source")]) : "";
}
function printModuleSpecifiers(path, options, print) {
const node = path.getValue();
/** @type{Doc[]} */
const parts = [node.type === "ImportDeclaration" ? " " : ""];
if (node.specifiers && node.specifiers.length > 0) {
const standalonesSpecifiers = [];
const groupedSpecifiers = [];
path.each((specifierPath) => {
const specifierType = path.getValue().type;
if (
specifierType === "ExportNamespaceSpecifier" ||
specifierType === "ExportDefaultSpecifier" ||
specifierType === "ImportNamespaceSpecifier" ||
specifierType === "ImportDefaultSpecifier"
) {
standalonesSpecifiers.push(print(specifierPath));
} else if (
specifierType === "ExportSpecifier" ||
specifierType === "ImportSpecifier"
) {
groupedSpecifiers.push(print(specifierPath));
} else {
/* istanbul ignore next */
throw new Error(
`Unknown specifier type ${JSON.stringify(specifierType)}`
);
}
}, "specifiers");
parts.push(join(", ", standalonesSpecifiers));
if (groupedSpecifiers.length !== 0) {
if (standalonesSpecifiers.length !== 0) {
parts.push(", ");
}
const canBreak =
groupedSpecifiers.length > 1 ||
standalonesSpecifiers.length > 0 ||
node.specifiers.some((node) => node.comments);
if (canBreak) {
parts.push(
group(
concat([
"{",
indent(
concat([
options.bracketSpacing ? line : softline,
join(concat([",", line]), groupedSpecifiers),
])
),
ifBreak(shouldPrintComma(options) ? "," : ""),
options.bracketSpacing ? line : softline,
"}",
])
)
);
} else {
parts.push(
concat([
"{",
options.bracketSpacing ? " " : "",
concat(groupedSpecifiers),
options.bracketSpacing ? " " : "",
"}",
])
);
}
}
} else {
parts.push("{}");
}
return concat(parts);
}
function printImportAssertions(path, options, print) {
const node = path.getNode();
if (Array.isArray(node.assertions) && node.assertions.length !== 0) {
return concat([
" assert {",
options.bracketSpacing ? " " : "",
join(", ", path.map(print, "assertions")),
options.bracketSpacing ? " " : "",
"}",
]);
}
return "";
}
module.exports = {
printModuleSource,
printModuleSpecifiers,
printImportAssertions,
};