-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
misc.js
99 lines (86 loc) · 2.46 KB
/
misc.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
"use strict";
/** @type {import("assert")} */
const assert = require("assert");
const {
builders: { concat, group, indent, join, line, hardline },
} = require("../../document");
const {
hasNewlineBetweenOrAfterDecorators,
getParentExportDeclaration,
} = require("../utils");
function printOptionalToken(path) {
const node = path.getValue();
if (
!node.optional ||
// It's an optional computed method parsed by typescript-estree.
// "?" is printed in `printMethod`.
(node.type === "Identifier" && node === path.getParentNode().key)
) {
return "";
}
if (
node.type === "OptionalCallExpression" ||
(node.type === "OptionalMemberExpression" && node.computed)
) {
return "?.";
}
return "?";
}
function printFunctionTypeParameters(path, options, print) {
const fun = path.getValue();
if (fun.typeArguments) {
return path.call(print, "typeArguments");
}
if (fun.typeParameters) {
return path.call(print, "typeParameters");
}
return "";
}
function printBindExpressionCallee(path, options, print) {
return concat(["::", path.call(print, "callee")]);
}
function printTypeScriptModifiers(path, options, print) {
const n = path.getValue();
if (!n.modifiers || !n.modifiers.length) {
return "";
}
return concat([join(" ", path.map(print, "modifiers")), " "]);
}
function printDecorators(path, options, print) {
const node = path.getValue();
return group(
concat([
join(line, path.map(print, "decorators")),
hasNewlineBetweenOrAfterDecorators(node, options) ? hardline : line,
])
);
}
function printFlowDeclaration(path, printed) {
const parentExportDecl = getParentExportDeclaration(path);
if (parentExportDecl) {
assert.strictEqual(parentExportDecl.type, "DeclareExportDeclaration");
return printed;
}
// If the parent node has type DeclareExportDeclaration, then it
// will be responsible for printing the "declare" token. Otherwise
// it needs to be printed with this non-exported declaration node.
return concat(["declare ", printed]);
}
function adjustClause(node, clause, forceSpace) {
if (node.type === "EmptyStatement") {
return ";";
}
if (node.type === "BlockStatement" || forceSpace) {
return concat([" ", clause]);
}
return indent(concat([line, clause]));
}
module.exports = {
printOptionalToken,
printFunctionTypeParameters,
printBindExpressionCallee,
printTypeScriptModifiers,
printDecorators,
printFlowDeclaration,
adjustClause,
};