forked from prettier/plugin-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
157 lines (126 loc) Β· 3.46 KB
/
utils.js
File metadata and controls
157 lines (126 loc) Β· 3.46 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const {
breakParent,
concat,
hardline,
lineSuffix,
literalline
} = require("./prettier");
const concatBody = (path, opts, print) => concat(path.map(print, "body"));
// If the node is a type of assignment or if the node is a paren and nested
// inside that paren is a node that is a type of assignment.
const containsAssignment = (node) =>
["assign", "massign"].includes(node.type) ||
(node.type === "paren" && node.body[0].body.some(containsAssignment));
const docLength = (doc) => {
if (doc.length) {
return doc.length;
}
if (doc.parts) {
return doc.parts.reduce((sum, child) => sum + docLength(child), 0);
}
if (doc.contents) {
return docLength(doc.contents);
}
return 0;
};
const empty = () => "";
const first = (path, opts, print) => path.call(print, "body", 0);
const hasAncestor = (path, types) => {
let parent = 0;
let parentNode = path.getParentNode();
while (parentNode) {
if (types.includes(parentNode.type)) {
return true;
}
parent += 1;
parentNode = path.getParentNode(parent);
}
return false;
};
const literal = (value) => () => value;
const makeArgs = (path, opts, print, argsIndex) => {
let argNodes = path.getValue().body[argsIndex];
const argPattern = [print, "body", argsIndex, "body"];
if (argNodes.type === "args_add_block") {
[argNodes] = argNodes.body;
argPattern.push(0, "body");
}
const args = path.call(print, "body", argsIndex);
const heredocs = [];
argNodes.body.forEach((argNode, index) => {
let pattern;
let heredoc;
if (argNode.type === "heredoc") {
pattern = [index, "body"];
heredoc = argNode;
} else if (
argNode.type === "string_literal" &&
argNode.body[0].type === "heredoc"
) {
pattern = [index, "body", 0, "body"];
[heredoc] = argNode.body;
} else {
return;
}
const content = path.map.apply(path, argPattern.slice().concat(pattern));
heredocs.push(
concat([literalline].concat(content).concat([heredoc.ending]))
);
args[index] = heredoc.beging;
});
return { args, heredocs };
};
const makeCall = (path, opts, print) => {
const operation = path.getValue().body[1];
if ([".", "&."].includes(operation)) {
return operation;
}
return operation === "::" ? "." : path.call(print, "body", 1);
};
const makeList = (path, opts, print) => path.map(print, "body");
const prefix = (value) => (path, opts, print) =>
concat([value, path.call(print, "body", 0)]);
const printComments = (printed, start, comments) => {
let node = printed;
comments.forEach((comment) => {
if (comment.start < start) {
node = concat([
comment.break ? breakParent : "",
comment.body,
hardline,
node
]);
} else {
node = concat([
node,
comment.break ? breakParent : "",
lineSuffix(` ${comment.body}`)
]);
}
});
return node;
};
const skipAssignIndent = (node) =>
["array", "hash", "heredoc", "lambda", "regexp_literal"].includes(
node.type
) ||
(node.type === "call" && skipAssignIndent(node.body[0])) ||
(node.type === "string_literal" && node.body[0].type === "heredoc");
const surround = (left, right) => (path, opts, print) =>
concat([left, path.call(print, "body", 0), right]);
module.exports = {
concatBody,
containsAssignment,
docLength,
empty,
first,
hasAncestor,
literal,
makeArgs,
makeCall,
makeList,
prefix,
printComments,
skipAssignIndent,
surround
};