forked from prettier/prettier
/
misc.js
124 lines (104 loc) · 3.14 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"use strict";
const { isNonEmptyArray } = require("../../common/util.js");
const {
builders: { indent, join, line },
} = require("../../document/index.js");
const { isFlowAnnotationComment } = require("../utils/index.js");
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 printDefiniteToken(path) {
return path.getValue().definite ||
path.match(
undefined,
(node, name) =>
name === "id" && node.type === "VariableDeclarator" && node.definite
)
? "!"
: "";
}
function printFunctionTypeParameters(path, options, print) {
const fun = path.getValue();
if (fun.typeArguments) {
return print("typeArguments");
}
if (fun.typeParameters) {
return print("typeParameters");
}
return "";
}
function printTypeAnnotation(path, options, print) {
const node = path.getValue();
if (!node.typeAnnotation) {
return "";
}
const parentNode = path.getParentNode();
const isFunctionDeclarationIdentifier =
parentNode.type === "DeclareFunction" && parentNode.id === node;
if (isFlowAnnotationComment(options.originalText, node.typeAnnotation)) {
return [" /*: ", print("typeAnnotation"), " */"];
}
return [isFunctionDeclarationIdentifier ? "" : ": ", print("typeAnnotation")];
}
function printBindExpressionCallee(path, options, print) {
return ["::", print("callee")];
}
function printTypeScriptModifiers(path, options, print) {
const node = path.getValue();
if (!isNonEmptyArray(node.modifiers)) {
return "";
}
return [join(" ", path.map(print, "modifiers")), " "];
}
function adjustClause(node, clause, forceSpace) {
if (node.type === "EmptyStatement") {
return ";";
}
if (node.type === "BlockStatement" || forceSpace) {
return [" ", clause];
}
return indent([line, clause]);
}
function printRestSpread(path, options, print) {
return ["...", print("argument"), printTypeAnnotation(path, options, print)];
}
function printDirective(rawText, options) {
const rawContent = rawText.slice(1, -1);
// Check for the alternate quote, to determine if we're allowed to swap
// the quotes on a DirectiveLiteral.
if (rawContent.includes('"') || rawContent.includes("'")) {
return rawText;
}
const enclosingQuote = options.singleQuote ? "'" : '"';
// Directives are exact code unit sequences, which means that you can't
// change the escape sequences they use.
// See https://github.com/prettier/prettier/issues/1555
// and https://tc39.github.io/ecma262/#directive-prologue
return enclosingQuote + rawContent + enclosingQuote;
}
module.exports = {
printOptionalToken,
printDefiniteToken,
printFunctionTypeParameters,
printBindExpressionCallee,
printTypeScriptModifiers,
printTypeAnnotation,
printRestSpread,
adjustClause,
printDirective,
};