forked from OKEAMAH/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloc.js
More file actions
241 lines (202 loc) · 6.09 KB
/
loc.js
File metadata and controls
241 lines (202 loc) · 6.09 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import isNonEmptyArray from "../utils/is-non-empty-array.js";
import lineColumnToIndex from "../utils/line-column-to-index.js";
import { skipEverythingButNewLine } from "../utils/skip.js";
function calculateLocStart(node, text) {
// `postcss>=8`
if (typeof node.source?.start?.offset === "number") {
return node.source.start.offset;
}
// value-* nodes have this
if (typeof node.sourceIndex === "number") {
return node.sourceIndex;
}
if (node.source?.start) {
return lineColumnToIndex(node.source.start, text);
}
/* c8 ignore next */
throw Object.assign(new Error("Can not locate node."), { node });
}
function calculateLocEnd(node, text) {
if (node.type === "css-comment" && node.inline) {
return skipEverythingButNewLine(text, node.source.startOffset);
}
// `postcss>=8`
if (typeof node.source?.end?.offset === "number") {
return node.source.end.offset;
}
if (node.source) {
if (node.source.end) {
return lineColumnToIndex(node.source.end, text);
}
if (isNonEmptyArray(node.nodes)) {
return calculateLocEnd(node.nodes.at(-1), text);
}
}
return null;
}
function calculateLoc(node, text) {
if (node.source) {
node.source.startOffset = calculateLocStart(node, text);
node.source.endOffset = calculateLocEnd(node, text);
}
for (const key in node) {
const child = node[key];
if (key === "source" || !child || typeof child !== "object") {
continue;
}
if (child.type === "value-root" || child.type === "value-unknown") {
calculateValueNodeLoc(
child,
getValueRootOffset(node),
child.text || child.value,
);
} else {
calculateLoc(child, text);
}
}
}
function calculateValueNodeLoc(node, rootOffset, text) {
if (node.source) {
node.source.startOffset = calculateLocStart(node, text) + rootOffset;
node.source.endOffset = calculateLocEnd(node, text) + rootOffset;
}
for (const key in node) {
const child = node[key];
if (key === "source" || !child || typeof child !== "object") {
continue;
}
calculateValueNodeLoc(child, rootOffset, text);
}
}
function getValueRootOffset(node) {
let result = node.source.startOffset;
if (typeof node.prop === "string") {
result += node.prop.length;
}
if (node.type === "css-atrule" && typeof node.name === "string") {
result +=
1 + node.name.length + node.raws.afterName.match(/^\s*:?\s*/u)[0].length;
}
if (node.type !== "css-atrule" && typeof node.raws?.between === "string") {
result += node.raws.between.length;
}
return result;
}
/**
* Workaround for a bug: quotes and asterisks in inline comments corrupt loc data of subsequent nodes.
* This function replaces the quotes and asterisks with spaces. Later, when the comments are printed,
* their content is extracted from the original text.
* - https://github.com/prettier/prettier/issues/7780
* - https://github.com/shellscape/postcss-less/issues/145
* - https://github.com/prettier/prettier/issues/8130
* @param text {string}
*/
function replaceQuotesInInlineComments(text) {
/** @typedef { 'initial' | 'single-quotes' | 'double-quotes' | 'url' | 'comment-block' | 'comment-inline' } State */
/** @type {State} */
let state = "initial";
/** @type {State} */
let stateToReturnFromQuotes = "initial";
let inlineCommentStartIndex;
let inlineCommentContainsQuotes = false;
const inlineCommentsToReplace = [];
for (let i = 0; i < text.length; i++) {
const c = text[i];
switch (state) {
case "initial":
if (c === "'") {
state = "single-quotes";
continue;
}
if (c === '"') {
state = "double-quotes";
continue;
}
if (
(c === "u" || c === "U") &&
text.slice(i, i + 4).toLowerCase() === "url("
) {
state = "url";
i += 3;
continue;
}
if (c === "*" && text[i - 1] === "/") {
state = "comment-block";
continue;
}
if (c === "/" && text[i - 1] === "/") {
state = "comment-inline";
inlineCommentStartIndex = i - 1;
continue;
}
continue;
case "single-quotes":
if (c === "'" && text[i - 1] !== "\\") {
state = stateToReturnFromQuotes;
stateToReturnFromQuotes = "initial";
}
if (c === "\n" || c === "\r") {
return text; // invalid input
}
continue;
case "double-quotes":
if (c === '"' && text[i - 1] !== "\\") {
state = stateToReturnFromQuotes;
stateToReturnFromQuotes = "initial";
}
if (c === "\n" || c === "\r") {
return text; // invalid input
}
continue;
case "url":
if (c === ")") {
state = "initial";
}
if (c === "\n" || c === "\r") {
return text; // invalid input
}
if (c === "'") {
state = "single-quotes";
stateToReturnFromQuotes = "url";
continue;
}
if (c === '"') {
state = "double-quotes";
stateToReturnFromQuotes = "url";
continue;
}
continue;
case "comment-block":
if (c === "/" && text[i - 1] === "*") {
state = "initial";
}
continue;
case "comment-inline":
if (c === '"' || c === "'" || c === "*") {
inlineCommentContainsQuotes = true;
}
if (c === "\n" || c === "\r") {
if (inlineCommentContainsQuotes) {
inlineCommentsToReplace.push([inlineCommentStartIndex, i]);
}
state = "initial";
inlineCommentContainsQuotes = false;
}
continue;
}
}
for (const [start, end] of inlineCommentsToReplace) {
text =
text.slice(0, start) +
text.slice(start, end).replaceAll(/["'*]/gu, " ") +
text.slice(end);
}
return text;
}
function locStart(node) {
return node.source?.startOffset;
}
function locEnd(node) {
return node.source?.endOffset;
}
export { calculateLoc, locEnd, locStart, replaceQuotesInInlineComments };