oxfmt 0.47.0 → 0.49.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/configuration_schema.json +68 -10
- package/dist/acorn-ay2Aj6GQ.js +4972 -0
- package/dist/angular-z1ZEe5V_.js +4004 -0
- package/dist/{apis-hnICGpKH.js → apis-By1xj7IP.js} +15 -4
- package/dist/babel-Cs312VeV.js +9885 -0
- package/dist/{bindings--Yo3ZB_Z.js → bindings-Cdg_nxGV.js} +26 -26
- package/dist/{chunk-DSjvVL_1-DvmJ8i3j.js → chunk-DSjvVL_1-B3jw0SMn.js} +1 -1
- package/dist/chunk-HkwdBwDg.js +37 -0
- package/dist/cli-worker.js +1 -1
- package/dist/cli.js +3 -3
- package/dist/{dist-DBVLDX4Z.js → dist-Bbs0xV9H.js} +22 -17
- package/dist/{estree-CvnLDdmw.js → estree-DZBnPYMx.js} +1914 -1911
- package/dist/flow-ChhAFZSt.js +52916 -0
- package/dist/glimmer-Cg8KQipT.js +7533 -0
- package/dist/graphql-DsP-ifm0.js +1944 -0
- package/dist/html-7pWx9LND.js +7139 -0
- package/dist/index.d.ts +60 -4
- package/dist/index.js +2 -2
- package/dist/{jiti-Bb1dT6Cw-DpVi6yB1.js → jiti-Bb1dT6Cw-DezGRPD7.js} +5 -7
- package/dist/markdown-CCrs3bBt.js +6880 -0
- package/dist/{meriyah-CIaAh_Jq.js → meriyah-TC7VDz8E.js} +4649 -4647
- package/dist/{migrate-prettier-mSf_N9fW.js → migrate-prettier-Bze9N_6F.js} +1 -1
- package/dist/plugin-CwlqDRt_.js +1870 -0
- package/dist/postcss-ZbUej7DN.js +6788 -0
- package/dist/{prettier-plugin-oxfmt-CLoiASgP.js → prettier-plugin-oxfmt-BjdgYnLg.js} +1 -0
- package/dist/prettier-s1WPCGVk.js +15601 -0
- package/dist/{resolve-pWjAK-4f-BnvQ49el.js → resolve-pWjAK-4f-BnquIxPb.js} +2 -2
- package/dist/{sorter-BZkvDMjt-CgLjPuXI.js → sorter-BZkvDMjt-y2u_e1ZW.js} +6 -6
- package/dist/{sorter-C3ZRVYzK.js → sorter-l2HyESZg.js} +2 -2
- package/dist/typescript-Cgpjcksz.js +22899 -0
- package/dist/{utils-D8dQkKEd-CoIwOuFZ.js → utils-D8dQkKEd-Dgy5UIHl.js} +1 -1
- package/dist/{v3-D-mr2VVh-DA8uo048.js → v3-D-mr2VVh-Bw8A9MXh.js} +4 -4
- package/dist/{v4-C-HWEQJm-olIGVWd7.js → v4-C-HWEQJm-B81QD_Ac.js} +4 -4
- package/dist/yaml-DLZBHI7f.js +5280 -0
- package/package.json +28 -20
- package/dist/acorn-K_XpABqm.js +0 -4968
- package/dist/angular-B2OHapHD.js +0 -4057
- package/dist/babel-DA0BK-9N.js +0 -9883
- package/dist/chunk-DrSxFLj_.js +0 -14
- package/dist/flow-BthqNux8.js +0 -52912
- package/dist/glimmer-0WQITj6v.js +0 -7541
- package/dist/graphql-CZVKp936.js +0 -1945
- package/dist/html-CC61hPUL.js +0 -7137
- package/dist/markdown-BkJWkmO_.js +0 -6876
- package/dist/postcss-C8WoCp9x.js +0 -6785
- package/dist/prettier-DpEoE-3g.js +0 -15596
- package/dist/typescript-Cg8onQ4r.js +0 -22936
- package/dist/yaml-CTxkSwZx.js +0 -5281
|
@@ -0,0 +1,1870 @@
|
|
|
1
|
+
import { a as __toCommonJS, i as __require, t as __commonJSMin } from "./chunk-HkwdBwDg.js";
|
|
2
|
+
import { n as init_babel, t as babel_exports } from "./babel-Cs312VeV.js";
|
|
3
|
+
import { n as prettier_exports, t as init_prettier } from "./prettier-s1WPCGVk.js";
|
|
4
|
+
//#region ../../node_modules/.pnpm/prettier-plugin-svelte@3.5.1_prettier@3.8.3_svelte@5.55.5_@typescript-eslint+types@8.59.2_/node_modules/prettier-plugin-svelte/plugin.js
|
|
5
|
+
var require_plugin = /* @__PURE__ */ __commonJSMin(((exports) => {
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
var prettierPluginBabel = (init_babel(), __toCommonJS(babel_exports));
|
|
8
|
+
var prettier = (init_prettier(), __toCommonJS(prettier_exports));
|
|
9
|
+
var compiler = __require("svelte/compiler");
|
|
10
|
+
function _interopNamespace(e) {
|
|
11
|
+
if (e && e.__esModule) return e;
|
|
12
|
+
var n = Object.create(null);
|
|
13
|
+
if (e) Object.keys(e).forEach(function(k) {
|
|
14
|
+
if (k !== "default") {
|
|
15
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
16
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
get: function() {
|
|
19
|
+
return e[k];
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
n["default"] = e;
|
|
25
|
+
return Object.freeze(n);
|
|
26
|
+
}
|
|
27
|
+
/******************************************************************************
|
|
28
|
+
Copyright (c) Microsoft Corporation.
|
|
29
|
+
|
|
30
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
31
|
+
purpose with or without fee is hereby granted.
|
|
32
|
+
|
|
33
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
34
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
35
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
36
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
37
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
38
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
39
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
40
|
+
***************************************************************************** */
|
|
41
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
42
|
+
function adopt(value) {
|
|
43
|
+
return value instanceof P ? value : new P(function(resolve) {
|
|
44
|
+
resolve(value);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
return new (P || (P = Promise))(function(resolve, reject) {
|
|
48
|
+
function fulfilled(value) {
|
|
49
|
+
try {
|
|
50
|
+
step(generator.next(value));
|
|
51
|
+
} catch (e) {
|
|
52
|
+
reject(e);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function rejected(value) {
|
|
56
|
+
try {
|
|
57
|
+
step(generator["throw"](value));
|
|
58
|
+
} catch (e) {
|
|
59
|
+
reject(e);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function step(result) {
|
|
63
|
+
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
|
|
64
|
+
}
|
|
65
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
const selfClosingTags = [
|
|
69
|
+
"area",
|
|
70
|
+
"base",
|
|
71
|
+
"br",
|
|
72
|
+
"col",
|
|
73
|
+
"embed",
|
|
74
|
+
"hr",
|
|
75
|
+
"img",
|
|
76
|
+
"input",
|
|
77
|
+
"link",
|
|
78
|
+
"meta",
|
|
79
|
+
"param",
|
|
80
|
+
"source",
|
|
81
|
+
"track",
|
|
82
|
+
"wbr"
|
|
83
|
+
];
|
|
84
|
+
const blockElements = [
|
|
85
|
+
"address",
|
|
86
|
+
"article",
|
|
87
|
+
"aside",
|
|
88
|
+
"blockquote",
|
|
89
|
+
"details",
|
|
90
|
+
"dialog",
|
|
91
|
+
"dd",
|
|
92
|
+
"div",
|
|
93
|
+
"dl",
|
|
94
|
+
"dt",
|
|
95
|
+
"fieldset",
|
|
96
|
+
"figcaption",
|
|
97
|
+
"figure",
|
|
98
|
+
"footer",
|
|
99
|
+
"form",
|
|
100
|
+
"h1",
|
|
101
|
+
"h2",
|
|
102
|
+
"h3",
|
|
103
|
+
"h4",
|
|
104
|
+
"h5",
|
|
105
|
+
"h6",
|
|
106
|
+
"header",
|
|
107
|
+
"hgroup",
|
|
108
|
+
"hr",
|
|
109
|
+
"li",
|
|
110
|
+
"main",
|
|
111
|
+
"nav",
|
|
112
|
+
"ol",
|
|
113
|
+
"p",
|
|
114
|
+
"pre",
|
|
115
|
+
"section",
|
|
116
|
+
"table",
|
|
117
|
+
"ul"
|
|
118
|
+
];
|
|
119
|
+
/**
|
|
120
|
+
* HTML attributes that we may safely reformat (trim whitespace, add or remove newlines)
|
|
121
|
+
*/
|
|
122
|
+
const formattableAttributes = [];
|
|
123
|
+
const stringToBase64 = typeof Buffer !== "undefined" ? (str) => Buffer.from(str).toString("base64") : (str) => btoa(new TextEncoder().encode(str).reduce((acc, byte) => acc + String.fromCharCode(byte), ""));
|
|
124
|
+
const base64ToString = typeof Buffer !== "undefined" ? (str) => Buffer.from(str, "base64").toString() : (str) => new TextDecoder().decode(Uint8Array.from(atob(str), (c) => c.charCodeAt(0)));
|
|
125
|
+
const snippedTagContentAttribute = "✂prettier:content✂";
|
|
126
|
+
const scriptRegex = /<!--[^]*?-->|<script((?:\s+[^=>'"\/\s]+=(?:"[^"]*"|'[^']*'|[^>\s]+)|\s+[^=>'"\/\s]+)*\s*)>([^]*?)<\/script>/g;
|
|
127
|
+
const styleRegex = /<!--[^]*?-->|<style((?:\s+[^=>'"\/\s]+=(?:"[^"]*"|'[^']*'|[^>\s]+)|\s+[^=>'"\/\s]+)*\s*)>([^]*?)<\/style>/g;
|
|
128
|
+
const langTsRegex = /\slang=["']?ts["']?/;
|
|
129
|
+
function snipScriptAndStyleTagContent(source) {
|
|
130
|
+
let scriptMatchSpans = getMatchIndexes("script");
|
|
131
|
+
let styleMatchSpans = getMatchIndexes("style");
|
|
132
|
+
let isTypescript = false;
|
|
133
|
+
return {
|
|
134
|
+
text: snipTagContent(snipTagContent(source, "script", "{}", styleMatchSpans), "style", "", scriptMatchSpans),
|
|
135
|
+
isTypescript
|
|
136
|
+
};
|
|
137
|
+
function getMatchIndexes(tagName) {
|
|
138
|
+
const regex = getRegexp(tagName);
|
|
139
|
+
const indexes = [];
|
|
140
|
+
let match = null;
|
|
141
|
+
while ((match = regex.exec(source)) != null) if (source.slice(match.index, match.index + 4) !== "<!--") indexes.push([match.index, regex.lastIndex]);
|
|
142
|
+
return indexes;
|
|
143
|
+
}
|
|
144
|
+
function snipTagContent(_source, tagName, placeholder, otherSpans) {
|
|
145
|
+
const regex = getRegexp(tagName);
|
|
146
|
+
let newScriptMatchSpans = scriptMatchSpans;
|
|
147
|
+
let newStyleMatchSpans = styleMatchSpans;
|
|
148
|
+
const newSource = _source.replace(regex, (match, attributes, content, index) => {
|
|
149
|
+
if (match.startsWith("<!--") || withinOtherSpan(index)) return match;
|
|
150
|
+
if (langTsRegex.test(attributes)) isTypescript = true;
|
|
151
|
+
const newContent = `<${tagName}${attributes} ${snippedTagContentAttribute}="${stringToBase64(content)}">${placeholder}</${tagName}>`;
|
|
152
|
+
const lengthDiff = match.length - newContent.length;
|
|
153
|
+
newScriptMatchSpans = adjustSpans(scriptMatchSpans, newScriptMatchSpans);
|
|
154
|
+
newStyleMatchSpans = adjustSpans(styleMatchSpans, newStyleMatchSpans);
|
|
155
|
+
function adjustSpans(oldSpans, newSpans) {
|
|
156
|
+
return oldSpans.map((oldSpan, idx) => {
|
|
157
|
+
const newSpan = newSpans[idx];
|
|
158
|
+
if (oldSpan[0] > index) return [newSpan[0] - lengthDiff, newSpan[1] - lengthDiff];
|
|
159
|
+
else if (oldSpan[0] === index) return [newSpan[0], newSpan[1] - lengthDiff];
|
|
160
|
+
else return newSpan;
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
return newContent;
|
|
164
|
+
});
|
|
165
|
+
scriptMatchSpans = newScriptMatchSpans;
|
|
166
|
+
styleMatchSpans = newStyleMatchSpans;
|
|
167
|
+
return newSource;
|
|
168
|
+
function withinOtherSpan(idx) {
|
|
169
|
+
return otherSpans.some((otherSpan) => idx > otherSpan[0] && idx < otherSpan[1]);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
function getRegexp(tagName) {
|
|
173
|
+
return tagName === "script" ? scriptRegex : styleRegex;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
function hasSnippedContent(text) {
|
|
177
|
+
return text.includes(snippedTagContentAttribute);
|
|
178
|
+
}
|
|
179
|
+
const regex = /(<\w+.*?)\s*✂prettier:content✂="(.*?)">.*?(?=<\/)/gi;
|
|
180
|
+
function unsnipContent(text) {
|
|
181
|
+
return text.replace(regex, (_, start, encodedContent) => {
|
|
182
|
+
return `${start}>${base64ToString(encodedContent)}`;
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
function makeChoice(choice) {
|
|
186
|
+
return {
|
|
187
|
+
value: choice,
|
|
188
|
+
description: choice
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
const options = {
|
|
192
|
+
svelte5CompilerPath: {
|
|
193
|
+
category: "Svelte",
|
|
194
|
+
type: "string",
|
|
195
|
+
default: "",
|
|
196
|
+
description: "Only set this when using Svelte 5! Path to the Svelte 5 compiler"
|
|
197
|
+
},
|
|
198
|
+
svelteSortOrder: {
|
|
199
|
+
category: "Svelte",
|
|
200
|
+
type: "choice",
|
|
201
|
+
default: "options-scripts-markup-styles",
|
|
202
|
+
description: "Sort order for scripts, markup, and styles",
|
|
203
|
+
choices: [
|
|
204
|
+
makeChoice("options-scripts-markup-styles"),
|
|
205
|
+
makeChoice("options-scripts-styles-markup"),
|
|
206
|
+
makeChoice("options-markup-styles-scripts"),
|
|
207
|
+
makeChoice("options-markup-scripts-styles"),
|
|
208
|
+
makeChoice("options-styles-markup-scripts"),
|
|
209
|
+
makeChoice("options-styles-scripts-markup"),
|
|
210
|
+
makeChoice("scripts-options-markup-styles"),
|
|
211
|
+
makeChoice("scripts-options-styles-markup"),
|
|
212
|
+
makeChoice("markup-options-styles-scripts"),
|
|
213
|
+
makeChoice("markup-options-scripts-styles"),
|
|
214
|
+
makeChoice("styles-options-markup-scripts"),
|
|
215
|
+
makeChoice("styles-options-scripts-markup"),
|
|
216
|
+
makeChoice("scripts-markup-options-styles"),
|
|
217
|
+
makeChoice("scripts-styles-options-markup"),
|
|
218
|
+
makeChoice("markup-styles-options-scripts"),
|
|
219
|
+
makeChoice("markup-scripts-options-styles"),
|
|
220
|
+
makeChoice("styles-markup-options-scripts"),
|
|
221
|
+
makeChoice("styles-scripts-options-markup"),
|
|
222
|
+
makeChoice("scripts-markup-styles-options"),
|
|
223
|
+
makeChoice("scripts-styles-markup-options"),
|
|
224
|
+
makeChoice("markup-styles-scripts-options"),
|
|
225
|
+
makeChoice("markup-scripts-styles-options"),
|
|
226
|
+
makeChoice("styles-markup-scripts-options"),
|
|
227
|
+
makeChoice("styles-scripts-markup-options"),
|
|
228
|
+
makeChoice("none")
|
|
229
|
+
]
|
|
230
|
+
},
|
|
231
|
+
svelteStrictMode: {
|
|
232
|
+
category: "Svelte",
|
|
233
|
+
type: "boolean",
|
|
234
|
+
default: false,
|
|
235
|
+
description: "More strict HTML syntax: Quotes in attributes, no self-closing DOM tags"
|
|
236
|
+
},
|
|
237
|
+
svelteBracketNewLine: {
|
|
238
|
+
category: "Svelte",
|
|
239
|
+
type: "boolean",
|
|
240
|
+
description: "Put the `>` of a multiline element on a new line",
|
|
241
|
+
deprecated: "2.5.0"
|
|
242
|
+
},
|
|
243
|
+
svelteAllowShorthand: {
|
|
244
|
+
category: "Svelte",
|
|
245
|
+
type: "boolean",
|
|
246
|
+
default: true,
|
|
247
|
+
description: "Option to enable/disable component attribute shorthand if attribute name and expressions are same"
|
|
248
|
+
},
|
|
249
|
+
svelteIndentScriptAndStyle: {
|
|
250
|
+
category: "Svelte",
|
|
251
|
+
type: "boolean",
|
|
252
|
+
default: true,
|
|
253
|
+
description: "Whether or not to indent the code inside <script> and <style> tags in Svelte files"
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
const sortOrderSeparator = "-";
|
|
257
|
+
function parseSortOrder(sortOrder = "options-scripts-markup-styles") {
|
|
258
|
+
if (sortOrder === "none") return [];
|
|
259
|
+
const order = sortOrder.split(sortOrderSeparator);
|
|
260
|
+
if (!order.includes("options")) throw new Error("svelteSortOrder is missing option `options`");
|
|
261
|
+
return order;
|
|
262
|
+
}
|
|
263
|
+
function isBracketSameLine(options) {
|
|
264
|
+
return options.svelteBracketNewLine != null ? !options.svelteBracketNewLine : options.bracketSameLine != null ? options.bracketSameLine : false;
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Determines whether or not given node
|
|
268
|
+
* is the root of the Svelte AST.
|
|
269
|
+
*/
|
|
270
|
+
function isASTNode(n) {
|
|
271
|
+
return n && n.__isRoot;
|
|
272
|
+
}
|
|
273
|
+
function isPreTagContent(path) {
|
|
274
|
+
return path.stack.some((node) => node.type === "Element" && node.name.toLowerCase() === "pre" || node.type === "Attribute" && !formattableAttributes.includes(node.name));
|
|
275
|
+
}
|
|
276
|
+
function flatten(arrays) {
|
|
277
|
+
return [].concat.apply([], arrays);
|
|
278
|
+
}
|
|
279
|
+
function findLastIndex(isMatch, items) {
|
|
280
|
+
for (let i = items.length - 1; i >= 0; i--) if (isMatch(items[i], i)) return i;
|
|
281
|
+
return -1;
|
|
282
|
+
}
|
|
283
|
+
function replaceEndOfLineWith(text, replacement) {
|
|
284
|
+
const parts = [];
|
|
285
|
+
for (const part of text.split("\n")) {
|
|
286
|
+
if (parts.length > 0) parts.push(replacement);
|
|
287
|
+
if (part.endsWith("\r")) parts.push(part.slice(0, -1));
|
|
288
|
+
else parts.push(part);
|
|
289
|
+
}
|
|
290
|
+
return parts;
|
|
291
|
+
}
|
|
292
|
+
function getAttributeLine(node, options) {
|
|
293
|
+
const { hardline, line } = prettier.doc.builders;
|
|
294
|
+
const hasThisBinding = node.type === "InlineComponent" && !!node.expression || node.type === "Element" && !!node.tag;
|
|
295
|
+
const attributes = node.attributes.filter((attribute) => attribute.name !== snippedTagContentAttribute);
|
|
296
|
+
return options.singleAttributePerLine && (attributes.length > 1 || attributes.length && hasThisBinding) ? hardline : line;
|
|
297
|
+
}
|
|
298
|
+
function printWithPrependedAttributeLine(node, options, print) {
|
|
299
|
+
return (path) => path.getNode().name !== snippedTagContentAttribute ? [getAttributeLine(node, options), path.call(print)] : "";
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Check if doc is a hardline.
|
|
303
|
+
* We can't just rely on a simple equality check because the doc could be created with another
|
|
304
|
+
* runtime version of prettier than what we import, making a reference check fail.
|
|
305
|
+
*/
|
|
306
|
+
function isHardline(docToCheck) {
|
|
307
|
+
return docToCheck === prettier.doc.builders.hardline || deepEqual(docToCheck, prettier.doc.builders.hardline);
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Simple deep equal function which suits our needs. Only works properly on POJOs without cyclic deps.
|
|
311
|
+
*/
|
|
312
|
+
function deepEqual(x, y) {
|
|
313
|
+
if (x === y) return true;
|
|
314
|
+
else if (typeof x == "object" && x != null && typeof y == "object" && y != null) {
|
|
315
|
+
if (Object.keys(x).length != Object.keys(y).length) return false;
|
|
316
|
+
for (var prop in x) if (y.hasOwnProperty(prop)) {
|
|
317
|
+
if (!deepEqual(x[prop], y[prop])) return false;
|
|
318
|
+
} else return false;
|
|
319
|
+
return true;
|
|
320
|
+
} else return false;
|
|
321
|
+
}
|
|
322
|
+
function isDocCommand(doc) {
|
|
323
|
+
return typeof doc === "object" && doc !== null;
|
|
324
|
+
}
|
|
325
|
+
function isLine(docToCheck) {
|
|
326
|
+
return isHardline(docToCheck) || isDocCommand(docToCheck) && docToCheck.type === "line" || Array.isArray(docToCheck) && docToCheck.every(isLine);
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Check if the doc is empty, i.e. consists of nothing more than empty strings (possibly nested).
|
|
330
|
+
*/
|
|
331
|
+
function isEmptyDoc(doc) {
|
|
332
|
+
if (typeof doc === "string") return doc.length === 0;
|
|
333
|
+
if (isDocCommand(doc) && doc.type === "line") return !doc.keepIfLonely;
|
|
334
|
+
if (Array.isArray(doc)) return doc.length === 0;
|
|
335
|
+
const { contents } = doc;
|
|
336
|
+
if (contents) return isEmptyDoc(contents);
|
|
337
|
+
const { parts } = doc;
|
|
338
|
+
if (parts) return isEmptyGroup(parts);
|
|
339
|
+
return false;
|
|
340
|
+
}
|
|
341
|
+
function isEmptyGroup(group) {
|
|
342
|
+
return !group.find((doc) => !isEmptyDoc(doc));
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Trims both leading and trailing nodes matching `isWhitespace` independent of nesting level
|
|
346
|
+
* (though all trimmed adjacent nodes need to be a the same level). Modifies the `docs` array.
|
|
347
|
+
*/
|
|
348
|
+
function trim(docs, isWhitespace) {
|
|
349
|
+
trimLeft(docs, isWhitespace);
|
|
350
|
+
trimRight(docs, isWhitespace);
|
|
351
|
+
return docs;
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* Trims the leading nodes matching `isWhitespace` independent of nesting level (though all nodes need to be a the same level).
|
|
355
|
+
* If there are empty docs before the first whitespace, they are removed, too.
|
|
356
|
+
*/
|
|
357
|
+
function trimLeft(group, isWhitespace) {
|
|
358
|
+
let firstNonWhitespace = group.findIndex((doc) => !isEmptyDoc(doc) && !isWhitespace(doc));
|
|
359
|
+
if (firstNonWhitespace < 0 && group.length) firstNonWhitespace = group.length;
|
|
360
|
+
if (firstNonWhitespace > 0) {
|
|
361
|
+
if (group.splice(0, firstNonWhitespace).every(isEmptyDoc)) return trimLeft(group, isWhitespace);
|
|
362
|
+
} else {
|
|
363
|
+
const parts = getParts(group[0]);
|
|
364
|
+
if (parts) return trimLeft(parts, isWhitespace);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Trims the trailing nodes matching `isWhitespace` independent of nesting level (though all nodes need to be a the same level).
|
|
369
|
+
* If there are empty docs after the last whitespace, they are removed, too.
|
|
370
|
+
*/
|
|
371
|
+
function trimRight(group, isWhitespace) {
|
|
372
|
+
let lastNonWhitespace = group.length ? findLastIndex((doc) => !isEmptyDoc(doc) && !isWhitespace(doc), group) : 0;
|
|
373
|
+
if (lastNonWhitespace < group.length - 1) {
|
|
374
|
+
if (group.splice(lastNonWhitespace + 1).every(isEmptyDoc)) return trimRight(group, isWhitespace);
|
|
375
|
+
} else {
|
|
376
|
+
const parts = getParts(group[group.length - 1]);
|
|
377
|
+
if (parts) return trimRight(parts, isWhitespace);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
function getParts(doc) {
|
|
381
|
+
if (typeof doc === "object") {
|
|
382
|
+
if (Array.isArray(doc)) return doc;
|
|
383
|
+
if (doc.type === "fill") return doc.parts;
|
|
384
|
+
if (doc.type === "group") return getParts(doc.contents);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* `(foo = bar)` => `foo = bar`
|
|
389
|
+
*/
|
|
390
|
+
function removeParentheses(doc) {
|
|
391
|
+
return trim([doc], (_doc) => _doc === "(" || _doc === ")")[0];
|
|
392
|
+
}
|
|
393
|
+
const unsupportedLanguages = [
|
|
394
|
+
"coffee",
|
|
395
|
+
"coffeescript",
|
|
396
|
+
"styl",
|
|
397
|
+
"stylus",
|
|
398
|
+
"sass"
|
|
399
|
+
];
|
|
400
|
+
function isInlineElement(path, options, node) {
|
|
401
|
+
return node && node.type === "Element" && !isBlockElement(node, options) && !isPreTagContent(path);
|
|
402
|
+
}
|
|
403
|
+
function isBlockElement(node, options) {
|
|
404
|
+
return node && node.type === "Element" && options.htmlWhitespaceSensitivity !== "strict" && (options.htmlWhitespaceSensitivity === "ignore" || blockElements.includes(node.name));
|
|
405
|
+
}
|
|
406
|
+
function isSvelteBlock(node) {
|
|
407
|
+
return [
|
|
408
|
+
"IfBlock",
|
|
409
|
+
"SnippetBlock",
|
|
410
|
+
"AwaitBlock",
|
|
411
|
+
"CatchBlock",
|
|
412
|
+
"EachBlock",
|
|
413
|
+
"ElseBlock",
|
|
414
|
+
"KeyBlock",
|
|
415
|
+
"PendingBlock",
|
|
416
|
+
"ThenBlock"
|
|
417
|
+
].includes(node.type);
|
|
418
|
+
}
|
|
419
|
+
function isNodeWithChildren(node) {
|
|
420
|
+
return node.children;
|
|
421
|
+
}
|
|
422
|
+
function getChildren(node) {
|
|
423
|
+
return isNodeWithChildren(node) ? node.children : [];
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* Returns siblings, that is, the children of the parent.
|
|
427
|
+
*/
|
|
428
|
+
function getSiblings(path) {
|
|
429
|
+
let parent = path.getParentNode();
|
|
430
|
+
if (isASTNode(parent)) parent = parent.html;
|
|
431
|
+
return getChildren(parent);
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* Returns the next sibling node.
|
|
435
|
+
*/
|
|
436
|
+
function getNextNode(path, node = path.getNode()) {
|
|
437
|
+
return getSiblings(path).find((child) => child.start === node.end);
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* Returns the comment that is above the current node.
|
|
441
|
+
*/
|
|
442
|
+
function getLeadingComment(path) {
|
|
443
|
+
const siblings = getSiblings(path);
|
|
444
|
+
let node = path.getNode();
|
|
445
|
+
let prev = siblings.find((child) => child.end === node.start);
|
|
446
|
+
while (prev) if (prev.type === "Comment" && !isIgnoreStartDirective(prev) && !isIgnoreEndDirective(prev)) return prev;
|
|
447
|
+
else if (isEmptyTextNode(prev)) {
|
|
448
|
+
node = prev;
|
|
449
|
+
prev = siblings.find((child) => child.end === node.start);
|
|
450
|
+
} else return;
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Did there use to be any embedded object (that has been snipped out of the AST to be moved)
|
|
454
|
+
* at the specified position?
|
|
455
|
+
*/
|
|
456
|
+
function doesEmbedStartAfterNode(node, path, siblings = getSiblings(path)) {
|
|
457
|
+
if (!isNodeTopLevelHTML(node, path)) return false;
|
|
458
|
+
const position = node.end;
|
|
459
|
+
const root = path.stack[0];
|
|
460
|
+
const embeds = [
|
|
461
|
+
root.css,
|
|
462
|
+
root.html,
|
|
463
|
+
root.instance,
|
|
464
|
+
root.js,
|
|
465
|
+
root.module
|
|
466
|
+
];
|
|
467
|
+
const nextNode = siblings[siblings.indexOf(node) + 1];
|
|
468
|
+
return embeds.find((n) => n && n.start >= position && (!nextNode || n.end <= nextNode.start));
|
|
469
|
+
}
|
|
470
|
+
function isNodeTopLevelHTML(node, path) {
|
|
471
|
+
const root = path.stack[0];
|
|
472
|
+
return !!root.html && !!root.html.children && root.html.children.includes(node);
|
|
473
|
+
}
|
|
474
|
+
function isEmptyTextNode(node) {
|
|
475
|
+
return !!node && node.type === "Text" && getUnencodedText(node).trim() === "";
|
|
476
|
+
}
|
|
477
|
+
function isIgnoreDirective(node) {
|
|
478
|
+
return !!node && node.type === "Comment" && node.data.trim() === "prettier-ignore";
|
|
479
|
+
}
|
|
480
|
+
function isIgnoreStartDirective(node) {
|
|
481
|
+
return !!node && node.type === "Comment" && node.data.trim() === "prettier-ignore-start";
|
|
482
|
+
}
|
|
483
|
+
function isIgnoreEndDirective(node) {
|
|
484
|
+
return !!node && node.type === "Comment" && node.data.trim() === "prettier-ignore-end";
|
|
485
|
+
}
|
|
486
|
+
function printRaw(node, originalText, stripLeadingAndTrailingNewline = false) {
|
|
487
|
+
if (node.children.length === 0) return "";
|
|
488
|
+
const firstChild = node.children[0];
|
|
489
|
+
const lastChild = node.children[node.children.length - 1];
|
|
490
|
+
let raw = originalText.substring(firstChild.start, lastChild.end);
|
|
491
|
+
if (!stripLeadingAndTrailingNewline) return raw;
|
|
492
|
+
if (startsWithLinebreak(raw)) raw = raw.substring(raw.indexOf("\n") + 1);
|
|
493
|
+
if (endsWithLinebreak(raw)) {
|
|
494
|
+
raw = raw.substring(0, raw.lastIndexOf("\n"));
|
|
495
|
+
if (raw.charAt(raw.length - 1) === "\r") raw = raw.substring(0, raw.length - 1);
|
|
496
|
+
}
|
|
497
|
+
return raw;
|
|
498
|
+
}
|
|
499
|
+
function isTextNode(node) {
|
|
500
|
+
return node.type === "Text";
|
|
501
|
+
}
|
|
502
|
+
function getAttributeValue(attributeName, node) {
|
|
503
|
+
var _a;
|
|
504
|
+
const langAttribute = ((_a = node.attributes) !== null && _a !== void 0 ? _a : []).find((attribute) => attribute.name === attributeName);
|
|
505
|
+
return langAttribute && langAttribute.value;
|
|
506
|
+
}
|
|
507
|
+
function getAttributeTextValue(attributeName, node) {
|
|
508
|
+
const value = getAttributeValue(attributeName, node);
|
|
509
|
+
if (value != null && typeof value === "object") {
|
|
510
|
+
const textValue = value.find(isTextNode);
|
|
511
|
+
if (textValue) return textValue.data;
|
|
512
|
+
}
|
|
513
|
+
return null;
|
|
514
|
+
}
|
|
515
|
+
function getLangAttribute(node) {
|
|
516
|
+
const value = getAttributeTextValue("lang", node) || getAttributeTextValue("type", node);
|
|
517
|
+
if (value != null) return value.replace(/^text\//, "");
|
|
518
|
+
else return null;
|
|
519
|
+
}
|
|
520
|
+
/**
|
|
521
|
+
* Checks whether the node contains a `lang` or `type` attribute with a value corresponding to
|
|
522
|
+
* a language we cannot format. This might for example be `<template lang="pug">`.
|
|
523
|
+
* If the node does not contain a `lang` attribute, the result is true.
|
|
524
|
+
*/
|
|
525
|
+
function isNodeSupportedLanguage(node) {
|
|
526
|
+
const lang = getLangAttribute(node);
|
|
527
|
+
return !(lang && unsupportedLanguages.includes(lang));
|
|
528
|
+
}
|
|
529
|
+
/**
|
|
530
|
+
* Checks whether the node contains a `lang` or `type` attribute which indicates that
|
|
531
|
+
* the script contents are written in TypeScript. Note that the absence of the tag
|
|
532
|
+
* does not mean it's not TypeScript, because the user could have set the default
|
|
533
|
+
* to TypeScript in his settings.
|
|
534
|
+
*/
|
|
535
|
+
function isTypeScript(node) {
|
|
536
|
+
const lang = getLangAttribute(node) || "";
|
|
537
|
+
return ["typescript", "ts"].includes(lang);
|
|
538
|
+
}
|
|
539
|
+
function isJSON(node) {
|
|
540
|
+
const lang = getLangAttribute(node) || "";
|
|
541
|
+
return lang.endsWith("json") || lang.endsWith("importmap");
|
|
542
|
+
}
|
|
543
|
+
function isLess(node) {
|
|
544
|
+
const lang = getLangAttribute(node) || "";
|
|
545
|
+
return ["less"].includes(lang);
|
|
546
|
+
}
|
|
547
|
+
function isScss(node) {
|
|
548
|
+
const lang = getLangAttribute(node) || "";
|
|
549
|
+
return ["sass", "scss"].includes(lang);
|
|
550
|
+
}
|
|
551
|
+
function isPugTemplate(node) {
|
|
552
|
+
return node.type === "Element" && node.name === "template" && getLangAttribute(node) === "pug";
|
|
553
|
+
}
|
|
554
|
+
function isLoneMustacheTag(node) {
|
|
555
|
+
return node !== true && node.length === 1 && node[0].type === "MustacheTag";
|
|
556
|
+
}
|
|
557
|
+
function isAttributeShorthand(node) {
|
|
558
|
+
return node !== true && node.length === 1 && node[0].type === "AttributeShorthand";
|
|
559
|
+
}
|
|
560
|
+
/**
|
|
561
|
+
* True if node is of type `{a}` or `a={a}`
|
|
562
|
+
*/
|
|
563
|
+
function isOrCanBeConvertedToShorthand(node) {
|
|
564
|
+
if (isAttributeShorthand(node.value)) return true;
|
|
565
|
+
if (isLoneMustacheTag(node.value)) {
|
|
566
|
+
const expression = node.value[0].expression;
|
|
567
|
+
return expression.type === "Identifier" && expression.name === node.name;
|
|
568
|
+
}
|
|
569
|
+
return false;
|
|
570
|
+
}
|
|
571
|
+
function getUnencodedText(node) {
|
|
572
|
+
return node.raw || node.data;
|
|
573
|
+
}
|
|
574
|
+
function isTextNodeStartingWithLinebreak(node, nrLines = 1) {
|
|
575
|
+
return node.type === "Text" && startsWithLinebreak(getUnencodedText(node), nrLines);
|
|
576
|
+
}
|
|
577
|
+
function startsWithLinebreak(text, nrLines = 1) {
|
|
578
|
+
return new RegExp(`^([\\t\\f\\r ]*\\n){${nrLines}}`).test(text);
|
|
579
|
+
}
|
|
580
|
+
function isTextNodeEndingWithLinebreak(node, nrLines = 1) {
|
|
581
|
+
return node.type === "Text" && endsWithLinebreak(getUnencodedText(node), nrLines);
|
|
582
|
+
}
|
|
583
|
+
function endsWithLinebreak(text, nrLines = 1) {
|
|
584
|
+
return new RegExp(`(\\n[\\t\\f\\r ]*){${nrLines}}$`).test(text);
|
|
585
|
+
}
|
|
586
|
+
function isTextNodeStartingWithWhitespace(node) {
|
|
587
|
+
return node.type === "Text" && /^\s/.test(getUnencodedText(node));
|
|
588
|
+
}
|
|
589
|
+
function isTextNodeEndingWithWhitespace(node) {
|
|
590
|
+
return node.type === "Text" && /\s$/.test(getUnencodedText(node));
|
|
591
|
+
}
|
|
592
|
+
function trimTextNodeRight(node) {
|
|
593
|
+
node.raw = node.raw && node.raw.trimRight();
|
|
594
|
+
node.data = node.data && node.data.trimRight();
|
|
595
|
+
}
|
|
596
|
+
function trimTextNodeLeft(node) {
|
|
597
|
+
node.raw = node.raw && node.raw.trimLeft();
|
|
598
|
+
node.data = node.data && node.data.trimLeft();
|
|
599
|
+
}
|
|
600
|
+
/**
|
|
601
|
+
* Remove all leading whitespace up until the first non-empty text node,
|
|
602
|
+
* and all trailing whitespace from the last non-empty text node onwards.
|
|
603
|
+
*/
|
|
604
|
+
function trimChildren(children, path) {
|
|
605
|
+
let firstNonEmptyNode = children.findIndex((n) => !isEmptyTextNode(n) && !doesEmbedStartAfterNode(n, path));
|
|
606
|
+
firstNonEmptyNode = firstNonEmptyNode === -1 ? children.length - 1 : firstNonEmptyNode;
|
|
607
|
+
let lastNonEmptyNode = findLastIndex((n, idx) => {
|
|
608
|
+
return !isEmptyTextNode(n) && (idx === children.length - 1 && n.type !== "Comment" || !doesEmbedStartAfterNode(n, path));
|
|
609
|
+
}, children);
|
|
610
|
+
lastNonEmptyNode = lastNonEmptyNode === -1 ? 0 : lastNonEmptyNode;
|
|
611
|
+
for (let i = 0; i <= firstNonEmptyNode; i++) {
|
|
612
|
+
const n = children[i];
|
|
613
|
+
if (n.type === "Text") trimTextNodeLeft(n);
|
|
614
|
+
}
|
|
615
|
+
for (let i = children.length - 1; i >= lastNonEmptyNode; i--) {
|
|
616
|
+
const n = children[i];
|
|
617
|
+
if (n.type === "Text") trimTextNodeRight(n);
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
/**
|
|
621
|
+
* Check if given node's start tag should hug its first child. This is the case for inline elements when there's
|
|
622
|
+
* no whitespace between the `>` and the first child.
|
|
623
|
+
*/
|
|
624
|
+
function shouldHugStart(node, isSupportedLanguage, options) {
|
|
625
|
+
if (!isSupportedLanguage) return true;
|
|
626
|
+
if (node.type === "SvelteBoundary") return false;
|
|
627
|
+
if (isBlockElement(node, options)) return false;
|
|
628
|
+
if (!isNodeWithChildren(node)) return false;
|
|
629
|
+
const children = node.children;
|
|
630
|
+
if (children.length === 0) return true;
|
|
631
|
+
if (options.htmlWhitespaceSensitivity === "ignore") return false;
|
|
632
|
+
const firstChild = children[0];
|
|
633
|
+
return !isTextNodeStartingWithWhitespace(firstChild);
|
|
634
|
+
}
|
|
635
|
+
/**
|
|
636
|
+
* Check if given node's end tag should hug its last child. This is the case for inline elements when there's
|
|
637
|
+
* no whitespace between the last child and the `</`.
|
|
638
|
+
*/
|
|
639
|
+
function shouldHugEnd(node, isSupportedLanguage, options) {
|
|
640
|
+
if (!isSupportedLanguage) return true;
|
|
641
|
+
if (node.type === "SvelteBoundary") return false;
|
|
642
|
+
if (isBlockElement(node, options)) return false;
|
|
643
|
+
if (!isNodeWithChildren(node)) return false;
|
|
644
|
+
const children = node.children;
|
|
645
|
+
if (children.length === 0) return true;
|
|
646
|
+
if (options.htmlWhitespaceSensitivity === "ignore") return false;
|
|
647
|
+
const lastChild = children[children.length - 1];
|
|
648
|
+
return !isTextNodeEndingWithWhitespace(lastChild);
|
|
649
|
+
}
|
|
650
|
+
/**
|
|
651
|
+
* Check for a svelte block if there's whitespace at the start and if it's a space or a line.
|
|
652
|
+
*/
|
|
653
|
+
function checkWhitespaceAtStartOfSvelteBlock(node, options) {
|
|
654
|
+
if (!isSvelteBlock(node) || !isNodeWithChildren(node)) return "none";
|
|
655
|
+
const children = node.children;
|
|
656
|
+
if (children.length === 0) return "none";
|
|
657
|
+
const firstChild = children[0];
|
|
658
|
+
if (isTextNodeStartingWithLinebreak(firstChild)) return "line";
|
|
659
|
+
else if (isTextNodeStartingWithWhitespace(firstChild)) return "space";
|
|
660
|
+
const parentOpeningEnd = options.originalText.lastIndexOf("}", firstChild.start);
|
|
661
|
+
if (parentOpeningEnd > 0 && firstChild.start > parentOpeningEnd + 1) {
|
|
662
|
+
const textBetween = options.originalText.substring(parentOpeningEnd + 1, firstChild.start);
|
|
663
|
+
if (textBetween.trim() === "") return startsWithLinebreak(textBetween) ? "line" : "space";
|
|
664
|
+
}
|
|
665
|
+
return "none";
|
|
666
|
+
}
|
|
667
|
+
/**
|
|
668
|
+
* Check for a svelte block if there's whitespace at the end and if it's a space or a line.
|
|
669
|
+
*/
|
|
670
|
+
function checkWhitespaceAtEndOfSvelteBlock(node, options) {
|
|
671
|
+
if (!isSvelteBlock(node) || !isNodeWithChildren(node)) return "none";
|
|
672
|
+
const children = node.children;
|
|
673
|
+
if (children.length === 0) return "none";
|
|
674
|
+
const lastChild = children[children.length - 1];
|
|
675
|
+
if (isTextNodeEndingWithLinebreak(lastChild)) return "line";
|
|
676
|
+
else if (isTextNodeEndingWithWhitespace(lastChild)) return "space";
|
|
677
|
+
const parentClosingStart = options.originalText.indexOf("{", lastChild.end);
|
|
678
|
+
if (parentClosingStart > 0 && lastChild.end < parentClosingStart) {
|
|
679
|
+
const textBetween = options.originalText.substring(lastChild.end, parentClosingStart);
|
|
680
|
+
if (textBetween.trim() === "") return endsWithLinebreak(textBetween) ? "line" : "space";
|
|
681
|
+
}
|
|
682
|
+
return "none";
|
|
683
|
+
}
|
|
684
|
+
function isInsideQuotedAttribute(path, options) {
|
|
685
|
+
return path.stack.some((node) => (node.type === "Attribute" || node.type === "StyleDirective") && (!isLoneMustacheTag(node.value) || options.svelteStrictMode && !options._svelte_is5Plus));
|
|
686
|
+
}
|
|
687
|
+
/**
|
|
688
|
+
* Returns true if the softline between `</tagName` and `>` can be omitted.
|
|
689
|
+
*/
|
|
690
|
+
function canOmitSoftlineBeforeClosingTag(node, path, options) {
|
|
691
|
+
return isBracketSameLine(options) && (!hugsStartOfNextNode(node, options) || isLastChildWithinParentBlockElement(path, options));
|
|
692
|
+
}
|
|
693
|
+
/**
|
|
694
|
+
* Return true if given node does not hug the next node, meaning there's whitespace
|
|
695
|
+
* or the end of the doc afterwards.
|
|
696
|
+
*/
|
|
697
|
+
function hugsStartOfNextNode(node, options) {
|
|
698
|
+
if (node.end === options.originalText.length) return false;
|
|
699
|
+
return !options.originalText.substring(node.end).match(/^\s/);
|
|
700
|
+
}
|
|
701
|
+
function isLastChildWithinParentBlockElement(path, options) {
|
|
702
|
+
const parent = path.getParentNode();
|
|
703
|
+
if (!parent || !isBlockElement(parent, options)) return false;
|
|
704
|
+
const children = getChildren(parent);
|
|
705
|
+
return children[children.length - 1] === path.getNode();
|
|
706
|
+
}
|
|
707
|
+
function assignCommentsToNodes(ast) {
|
|
708
|
+
if (ast.module) ast.module.comments = removeAndGetLeadingComments(ast, ast.module);
|
|
709
|
+
if (ast.instance) ast.instance.comments = removeAndGetLeadingComments(ast, ast.instance);
|
|
710
|
+
if (ast.css) ast.css.comments = removeAndGetLeadingComments(ast, ast.css);
|
|
711
|
+
}
|
|
712
|
+
/**
|
|
713
|
+
* Returns the comments that are above the current node and deletes them from the html ast.
|
|
714
|
+
*/
|
|
715
|
+
function removeAndGetLeadingComments(ast, current) {
|
|
716
|
+
const siblings = getChildren(ast.html);
|
|
717
|
+
const comments = [];
|
|
718
|
+
const newlines = [];
|
|
719
|
+
if (!siblings.length) return [];
|
|
720
|
+
let node = current;
|
|
721
|
+
let prev = siblings.find((child) => child.end === node.start);
|
|
722
|
+
while (prev) {
|
|
723
|
+
if (prev.type === "Comment" && !isIgnoreStartDirective(prev) && !isIgnoreEndDirective(prev)) {
|
|
724
|
+
comments.push(prev);
|
|
725
|
+
if (comments.length !== newlines.length) newlines.push({
|
|
726
|
+
type: "Text",
|
|
727
|
+
data: "",
|
|
728
|
+
raw: "",
|
|
729
|
+
start: -1,
|
|
730
|
+
end: -1
|
|
731
|
+
});
|
|
732
|
+
} else if (isEmptyTextNode(prev)) newlines.push(prev);
|
|
733
|
+
else break;
|
|
734
|
+
node = prev;
|
|
735
|
+
prev = siblings.find((child) => child.end === node.start);
|
|
736
|
+
}
|
|
737
|
+
newlines.length = comments.length;
|
|
738
|
+
for (const comment of comments) siblings.splice(siblings.indexOf(comment), 1);
|
|
739
|
+
for (const text of newlines) siblings.splice(siblings.indexOf(text), 1);
|
|
740
|
+
return comments.map((comment, i) => ({
|
|
741
|
+
comment,
|
|
742
|
+
emptyLineAfter: getUnencodedText(newlines[i]).split("\n").length > 2
|
|
743
|
+
})).reverse();
|
|
744
|
+
}
|
|
745
|
+
const { join, line, group, indent, dedent, softline, hardline, fill, breakParent, literalline } = prettier.doc.builders;
|
|
746
|
+
function hasPragma(text) {
|
|
747
|
+
return /^\s*<!--\s*@(format|prettier)\W/.test(text);
|
|
748
|
+
}
|
|
749
|
+
let ignoreNext = false;
|
|
750
|
+
let ignoreRange = false;
|
|
751
|
+
let svelteOptionsDoc;
|
|
752
|
+
function print(path, options, print) {
|
|
753
|
+
var _a, _b;
|
|
754
|
+
const bracketSameLine = isBracketSameLine(options);
|
|
755
|
+
const n = path.getValue();
|
|
756
|
+
if (!n) return "";
|
|
757
|
+
if (isASTNode(n)) return printTopLevelParts(n, options, path, print);
|
|
758
|
+
const [open, close] = options.svelteStrictMode && !options._svelte_is5Plus ? ["\"{", "}\""] : ["{", "}"];
|
|
759
|
+
const printJsExpression = () => [
|
|
760
|
+
open,
|
|
761
|
+
printJS(path, print, "expression"),
|
|
762
|
+
close
|
|
763
|
+
];
|
|
764
|
+
const node = n;
|
|
765
|
+
if ((ignoreNext || ignoreRange && !isIgnoreEndDirective(node)) && (node.type !== "Text" || !isEmptyTextNode(node))) {
|
|
766
|
+
if (ignoreNext) ignoreNext = false;
|
|
767
|
+
return flatten(options.originalText.slice(options.locStart(node), options.locEnd(node)).split("\n").map((o, i) => i == 0 ? [o] : [literalline, o]));
|
|
768
|
+
}
|
|
769
|
+
switch (node.type) {
|
|
770
|
+
case "Fragment":
|
|
771
|
+
const children = node.children;
|
|
772
|
+
if (children.length === 0 || children.every(isEmptyTextNode)) return "";
|
|
773
|
+
if (!isPreTagContent(path)) {
|
|
774
|
+
trimChildren(node.children, path);
|
|
775
|
+
const output = trim([printChildren(path, print, options)], (n) => isLine(n) || typeof n === "string" && n.trim() === "" || n === breakParent);
|
|
776
|
+
if (output.every((doc) => isEmptyDoc(doc))) return "";
|
|
777
|
+
return group([...output, hardline]);
|
|
778
|
+
} else return group(path.map(print, "children"));
|
|
779
|
+
case "Text": if (!isPreTagContent(path)) {
|
|
780
|
+
if (isEmptyTextNode(node)) {
|
|
781
|
+
const hasWhiteSpace = getUnencodedText(node).trim().length < getUnencodedText(node).length;
|
|
782
|
+
const hasOneOrMoreNewlines = /\n/.test(getUnencodedText(node));
|
|
783
|
+
if (/\n\r?\s*\n\r?/.test(getUnencodedText(node))) return [hardline, hardline];
|
|
784
|
+
if (hasOneOrMoreNewlines) return hardline;
|
|
785
|
+
if (hasWhiteSpace) return line;
|
|
786
|
+
return "";
|
|
787
|
+
}
|
|
788
|
+
/**
|
|
789
|
+
* For non-empty text nodes each sequence of non-whitespace characters (effectively,
|
|
790
|
+
* each "word") is joined by a single `line`, which will be rendered as a single space
|
|
791
|
+
* until this node's current line is out of room, at which `fill` will break at the
|
|
792
|
+
* most convenient instance of `line`.
|
|
793
|
+
*/
|
|
794
|
+
return fill(splitTextToDocs(node));
|
|
795
|
+
} else {
|
|
796
|
+
let rawText = getUnencodedText(node);
|
|
797
|
+
const parent = path.getParentNode();
|
|
798
|
+
if (parent.type === "Attribute") {
|
|
799
|
+
if (parent.name === "class" && path.getParentNode(1).type === "Element") {
|
|
800
|
+
rawText = rawText.replace(/([^ \t\n])(([ \t]+$)|([ \t]+(\r?\n))|[ \t]+)/g, (match, characterBeforeWhitespace, _, isEndOfString, isEndOfLine, endOfLine) => isEndOfString ? match : characterBeforeWhitespace + (isEndOfLine ? endOfLine : " "));
|
|
801
|
+
rawText = rawText.replace(/([^ \t\n])[ \t]+$/, parent.value.indexOf(node) === parent.value.length - 1 ? "$1" : "$1 ");
|
|
802
|
+
}
|
|
803
|
+
return replaceEndOfLineWith(rawText, literalline);
|
|
804
|
+
}
|
|
805
|
+
return rawText;
|
|
806
|
+
}
|
|
807
|
+
case "Element":
|
|
808
|
+
case "InlineComponent":
|
|
809
|
+
case "Slot":
|
|
810
|
+
case "SlotTemplate":
|
|
811
|
+
case "Window":
|
|
812
|
+
case "Head":
|
|
813
|
+
case "SvelteBoundary":
|
|
814
|
+
case "Title": {
|
|
815
|
+
const isSupportedLanguage = !(node.name === "template" && !isNodeSupportedLanguage(node));
|
|
816
|
+
const isEmpty = node.children.every((child) => isEmptyTextNode(child));
|
|
817
|
+
const isDoctypeTag = node.name.toUpperCase() === "!DOCTYPE";
|
|
818
|
+
const didSelfClose = options.originalText[node.end - 2] === "/";
|
|
819
|
+
const isSelfClosingTag = isEmpty && ((node.type === "Element" && !options.svelteStrictMode || node.type === "Head" || node.type === "InlineComponent" || node.type === "Slot" || node.type === "SlotTemplate" || node.type === "SvelteBoundary" || node.type === "Title") && didSelfClose || node.type === "Window" || selfClosingTags.indexOf(node.name) !== -1 || isDoctypeTag);
|
|
820
|
+
const attributes = path.map(printWithPrependedAttributeLine(node, options, print), "attributes");
|
|
821
|
+
const attributeLine = getAttributeLine(node, options);
|
|
822
|
+
const possibleThisBinding = node.type === "InlineComponent" && node.expression ? [
|
|
823
|
+
attributeLine,
|
|
824
|
+
"this=",
|
|
825
|
+
...printJsExpression()
|
|
826
|
+
] : node.type === "Element" && node.tag ? [
|
|
827
|
+
attributeLine,
|
|
828
|
+
"this=",
|
|
829
|
+
...typeof node.tag === "string" ? [`"${node.tag}"`] : [
|
|
830
|
+
open,
|
|
831
|
+
printJS(path, print, "tag"),
|
|
832
|
+
close
|
|
833
|
+
]
|
|
834
|
+
] : "";
|
|
835
|
+
if (isSelfClosingTag) return group([
|
|
836
|
+
"<",
|
|
837
|
+
node.name,
|
|
838
|
+
indent(group([
|
|
839
|
+
possibleThisBinding,
|
|
840
|
+
...attributes,
|
|
841
|
+
bracketSameLine || isDoctypeTag ? "" : dedent(line)
|
|
842
|
+
])),
|
|
843
|
+
...[bracketSameLine && !isDoctypeTag ? " " : "", `${isDoctypeTag ? "" : "/"}>`]
|
|
844
|
+
]);
|
|
845
|
+
const children = node.children;
|
|
846
|
+
const firstChild = children[0];
|
|
847
|
+
const lastChild = children[children.length - 1];
|
|
848
|
+
let body;
|
|
849
|
+
const hugStart = shouldHugStart(node, isSupportedLanguage, options);
|
|
850
|
+
const hugEnd = shouldHugEnd(node, isSupportedLanguage, options);
|
|
851
|
+
if (isEmpty) body = isInlineElement(path, options, node) && node.children.length && isTextNodeStartingWithWhitespace(node.children[0]) && !isPreTagContent(path) ? () => line : () => bracketSameLine ? softline : "";
|
|
852
|
+
else if (isPreTagContent(path)) body = () => printPre(node, options.originalText, path, print);
|
|
853
|
+
else if (!isSupportedLanguage) body = () => printRaw(node, options.originalText, true);
|
|
854
|
+
else if (isInlineElement(path, options, node) && !isPreTagContent(path)) body = () => printChildren(path, print, options);
|
|
855
|
+
else body = () => printChildren(path, print, options);
|
|
856
|
+
const openingTag = [
|
|
857
|
+
"<",
|
|
858
|
+
node.name,
|
|
859
|
+
indent(group([
|
|
860
|
+
possibleThisBinding,
|
|
861
|
+
...attributes,
|
|
862
|
+
hugStart && !isEmpty ? "" : !bracketSameLine && !isPreTagContent(path) ? dedent(softline) : ""
|
|
863
|
+
]))
|
|
864
|
+
];
|
|
865
|
+
if (!isSupportedLanguage && !isEmpty) return group([
|
|
866
|
+
...openingTag,
|
|
867
|
+
">",
|
|
868
|
+
group([
|
|
869
|
+
hardline,
|
|
870
|
+
body(),
|
|
871
|
+
hardline
|
|
872
|
+
]),
|
|
873
|
+
`</${node.name}>`
|
|
874
|
+
]);
|
|
875
|
+
if (hugStart && hugEnd) {
|
|
876
|
+
const huggedContent = [softline, group([
|
|
877
|
+
">",
|
|
878
|
+
body(),
|
|
879
|
+
`</${node.name}`
|
|
880
|
+
])];
|
|
881
|
+
const omitSoftlineBeforeClosingTag = isEmpty && !bracketSameLine || canOmitSoftlineBeforeClosingTag(node, path, options);
|
|
882
|
+
return group([
|
|
883
|
+
...openingTag,
|
|
884
|
+
isEmpty ? group(huggedContent) : group(indent(huggedContent)),
|
|
885
|
+
omitSoftlineBeforeClosingTag ? "" : softline,
|
|
886
|
+
">"
|
|
887
|
+
]);
|
|
888
|
+
}
|
|
889
|
+
let noHugSeparatorStart = softline;
|
|
890
|
+
let noHugSeparatorEnd = softline;
|
|
891
|
+
if (isPreTagContent(path)) {
|
|
892
|
+
noHugSeparatorStart = "";
|
|
893
|
+
noHugSeparatorEnd = "";
|
|
894
|
+
} else {
|
|
895
|
+
let didSetEndSeparator = false;
|
|
896
|
+
if (!hugStart && firstChild && firstChild.type === "Text") {
|
|
897
|
+
if (isTextNodeStartingWithLinebreak(firstChild) && firstChild !== lastChild && (!isInlineElement(path, options, node) || isTextNodeEndingWithWhitespace(lastChild))) {
|
|
898
|
+
noHugSeparatorStart = hardline;
|
|
899
|
+
noHugSeparatorEnd = hardline;
|
|
900
|
+
didSetEndSeparator = true;
|
|
901
|
+
} else if (isInlineElement(path, options, node)) noHugSeparatorStart = line;
|
|
902
|
+
trimTextNodeLeft(firstChild);
|
|
903
|
+
}
|
|
904
|
+
if (!hugEnd && lastChild && lastChild.type === "Text") {
|
|
905
|
+
if (isInlineElement(path, options, node) && !didSetEndSeparator) noHugSeparatorEnd = line;
|
|
906
|
+
trimTextNodeRight(lastChild);
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
if (hugStart) return group([
|
|
910
|
+
...openingTag,
|
|
911
|
+
indent([softline, group([">", body()])]),
|
|
912
|
+
noHugSeparatorEnd,
|
|
913
|
+
`</${node.name}>`
|
|
914
|
+
]);
|
|
915
|
+
if (hugEnd) return group([
|
|
916
|
+
...openingTag,
|
|
917
|
+
">",
|
|
918
|
+
indent([noHugSeparatorStart, group([body(), `</${node.name}`])]),
|
|
919
|
+
canOmitSoftlineBeforeClosingTag(node, path, options) ? "" : softline,
|
|
920
|
+
">"
|
|
921
|
+
]);
|
|
922
|
+
if (isEmpty) return group([
|
|
923
|
+
...openingTag,
|
|
924
|
+
">",
|
|
925
|
+
body(),
|
|
926
|
+
`</${node.name}>`
|
|
927
|
+
]);
|
|
928
|
+
return group([
|
|
929
|
+
...openingTag,
|
|
930
|
+
">",
|
|
931
|
+
indent([noHugSeparatorStart, body()]),
|
|
932
|
+
noHugSeparatorEnd,
|
|
933
|
+
`</${node.name}>`
|
|
934
|
+
]);
|
|
935
|
+
}
|
|
936
|
+
case "Options": if (options.svelteSortOrder !== "none") throw new Error("Options tags should have been handled by prepareChildren");
|
|
937
|
+
case "Body":
|
|
938
|
+
case "Document":
|
|
939
|
+
case "SvelteHTML": return group([
|
|
940
|
+
"<",
|
|
941
|
+
node.name,
|
|
942
|
+
indent(group([...path.map(printWithPrependedAttributeLine(node, options, print), "attributes"), bracketSameLine ? "" : dedent(line)])),
|
|
943
|
+
...[bracketSameLine ? " " : "", "/>"]
|
|
944
|
+
]);
|
|
945
|
+
case "Identifier": return node.name;
|
|
946
|
+
case "AttributeShorthand": return node.expression.name;
|
|
947
|
+
case "Attribute": if (isOrCanBeConvertedToShorthand(node)) if (options.svelteAllowShorthand) return [
|
|
948
|
+
"{",
|
|
949
|
+
node.name,
|
|
950
|
+
"}"
|
|
951
|
+
];
|
|
952
|
+
else return [
|
|
953
|
+
node.name,
|
|
954
|
+
`=${open}`,
|
|
955
|
+
node.name,
|
|
956
|
+
close
|
|
957
|
+
];
|
|
958
|
+
else {
|
|
959
|
+
if (node.value === true) return [node.name];
|
|
960
|
+
const quotes = !isLoneMustacheTag(node.value) || ((_a = options.svelteStrictMode && !options._svelte_is5Plus) !== null && _a !== void 0 ? _a : false);
|
|
961
|
+
const attrNodeValue = printAttributeNodeValue(path, print, quotes, node);
|
|
962
|
+
if (quotes) return [
|
|
963
|
+
node.name,
|
|
964
|
+
"=",
|
|
965
|
+
"\"",
|
|
966
|
+
attrNodeValue,
|
|
967
|
+
"\""
|
|
968
|
+
];
|
|
969
|
+
else return [
|
|
970
|
+
node.name,
|
|
971
|
+
"=",
|
|
972
|
+
attrNodeValue
|
|
973
|
+
];
|
|
974
|
+
}
|
|
975
|
+
case "MustacheTag": return [
|
|
976
|
+
"{",
|
|
977
|
+
printJS(path, print, "expression"),
|
|
978
|
+
"}"
|
|
979
|
+
];
|
|
980
|
+
case "IfBlock": {
|
|
981
|
+
const def = [
|
|
982
|
+
"{#if ",
|
|
983
|
+
printJS(path, print, "expression"),
|
|
984
|
+
"}",
|
|
985
|
+
printSvelteBlockChildren(path, print, options)
|
|
986
|
+
];
|
|
987
|
+
if (node.else) def.push(path.call(print, "else"));
|
|
988
|
+
def.push("{/if}");
|
|
989
|
+
return group([def, breakParent]);
|
|
990
|
+
}
|
|
991
|
+
case "ElseBlock": {
|
|
992
|
+
const parent = path.getParentNode();
|
|
993
|
+
if (node.children.length === 1 && node.children[0].type === "IfBlock" && parent.type !== "EachBlock") {
|
|
994
|
+
const ifNode = node.children[0];
|
|
995
|
+
const def = [
|
|
996
|
+
"{:else if ",
|
|
997
|
+
path.map((ifPath) => printJS(ifPath, print, "expression"), "children")[0],
|
|
998
|
+
"}",
|
|
999
|
+
path.map((ifPath) => printSvelteBlockChildren(ifPath, print, options), "children")[0]
|
|
1000
|
+
];
|
|
1001
|
+
if (ifNode.else) def.push(path.map((ifPath) => ifPath.call(print, "else"), "children")[0]);
|
|
1002
|
+
return def;
|
|
1003
|
+
}
|
|
1004
|
+
return ["{:else}", printSvelteBlockChildren(path, print, options)];
|
|
1005
|
+
}
|
|
1006
|
+
case "EachBlock": {
|
|
1007
|
+
const def = ["{#each ", printJS(path, print, "expression")];
|
|
1008
|
+
if (node.context) def.push(" as", expandNode(node.context, options.originalText));
|
|
1009
|
+
if (node.index) def.push(", ", node.index);
|
|
1010
|
+
if (node.key) def.push(" (", printJS(path, print, "key"), ")");
|
|
1011
|
+
def.push("}", printSvelteBlockChildren(path, print, options));
|
|
1012
|
+
if (node.else) def.push(path.call(print, "else"));
|
|
1013
|
+
def.push("{/each}");
|
|
1014
|
+
return group([def, breakParent]);
|
|
1015
|
+
}
|
|
1016
|
+
case "AwaitBlock": {
|
|
1017
|
+
const hasPendingBlock = node.pending.children.some((n) => !isEmptyTextNode(n));
|
|
1018
|
+
const hasThenBlock = node.then.children.some((n) => !isEmptyTextNode(n));
|
|
1019
|
+
const hasCatchBlock = node.catch.children.some((n) => !isEmptyTextNode(n));
|
|
1020
|
+
let block = [];
|
|
1021
|
+
if (!hasPendingBlock && hasThenBlock) block.push(group([
|
|
1022
|
+
"{#await ",
|
|
1023
|
+
printJS(path, print, "expression"),
|
|
1024
|
+
" then",
|
|
1025
|
+
expandNode(node.value, options.originalText),
|
|
1026
|
+
"}"
|
|
1027
|
+
]), path.call(print, "then"));
|
|
1028
|
+
else if (!hasPendingBlock && hasCatchBlock) block.push(group([
|
|
1029
|
+
"{#await ",
|
|
1030
|
+
printJS(path, print, "expression"),
|
|
1031
|
+
" catch",
|
|
1032
|
+
expandNode(node.error, options.originalText),
|
|
1033
|
+
"}"
|
|
1034
|
+
]), path.call(print, "catch"));
|
|
1035
|
+
else {
|
|
1036
|
+
block.push(group([
|
|
1037
|
+
"{#await ",
|
|
1038
|
+
printJS(path, print, "expression"),
|
|
1039
|
+
"}"
|
|
1040
|
+
]));
|
|
1041
|
+
if (hasPendingBlock) block.push(path.call(print, "pending"));
|
|
1042
|
+
if (hasThenBlock) block.push(group([
|
|
1043
|
+
"{:then",
|
|
1044
|
+
expandNode(node.value, options.originalText),
|
|
1045
|
+
"}"
|
|
1046
|
+
]), path.call(print, "then"));
|
|
1047
|
+
}
|
|
1048
|
+
if ((hasPendingBlock || hasThenBlock) && hasCatchBlock) block.push(group([
|
|
1049
|
+
"{:catch",
|
|
1050
|
+
expandNode(node.error, options.originalText),
|
|
1051
|
+
"}"
|
|
1052
|
+
]), path.call(print, "catch"));
|
|
1053
|
+
block.push("{/await}");
|
|
1054
|
+
return group(block);
|
|
1055
|
+
}
|
|
1056
|
+
case "KeyBlock": {
|
|
1057
|
+
const def = [
|
|
1058
|
+
"{#key ",
|
|
1059
|
+
printJS(path, print, "expression"),
|
|
1060
|
+
"}",
|
|
1061
|
+
printSvelteBlockChildren(path, print, options)
|
|
1062
|
+
];
|
|
1063
|
+
def.push("{/key}");
|
|
1064
|
+
return group([def, breakParent]);
|
|
1065
|
+
}
|
|
1066
|
+
case "ThenBlock":
|
|
1067
|
+
case "PendingBlock":
|
|
1068
|
+
case "CatchBlock": return printSvelteBlockChildren(path, print, options);
|
|
1069
|
+
case "SnippetBlock": {
|
|
1070
|
+
const snippet = ["{#snippet ", printJS(path, print, "expression")];
|
|
1071
|
+
snippet.push("}", printSvelteBlockChildren(path, print, options), "{/snippet}");
|
|
1072
|
+
return snippet;
|
|
1073
|
+
}
|
|
1074
|
+
case "EventHandler": return [
|
|
1075
|
+
"on:",
|
|
1076
|
+
node.name,
|
|
1077
|
+
node.modifiers && node.modifiers.length ? ["|", join("|", node.modifiers)] : "",
|
|
1078
|
+
node.expression ? ["=", ...printJsExpression()] : ""
|
|
1079
|
+
];
|
|
1080
|
+
case "Binding": return [
|
|
1081
|
+
"bind:",
|
|
1082
|
+
node.name,
|
|
1083
|
+
node.expression.type === "Identifier" && node.expression.name === node.name && options.svelteAllowShorthand ? "" : ["=", ...printJsExpression()]
|
|
1084
|
+
];
|
|
1085
|
+
case "Class": return [
|
|
1086
|
+
"class:",
|
|
1087
|
+
node.name,
|
|
1088
|
+
node.expression.type === "Identifier" && node.expression.name === node.name && options.svelteAllowShorthand ? "" : ["=", ...printJsExpression()]
|
|
1089
|
+
];
|
|
1090
|
+
case "StyleDirective":
|
|
1091
|
+
const prefix = [
|
|
1092
|
+
"style:",
|
|
1093
|
+
node.name,
|
|
1094
|
+
node.modifiers && node.modifiers.length ? ["|", join("|", node.modifiers)] : ""
|
|
1095
|
+
];
|
|
1096
|
+
if (isOrCanBeConvertedToShorthand(node) || node.value === true) if (options.svelteAllowShorthand) return [...prefix];
|
|
1097
|
+
else return [
|
|
1098
|
+
...prefix,
|
|
1099
|
+
`=${open}`,
|
|
1100
|
+
node.name,
|
|
1101
|
+
close
|
|
1102
|
+
];
|
|
1103
|
+
else {
|
|
1104
|
+
const quotes = !isLoneMustacheTag(node.value) || ((_b = options.svelteStrictMode && !options._svelte_is5Plus) !== null && _b !== void 0 ? _b : false);
|
|
1105
|
+
const attrNodeValue = printAttributeNodeValue(path, print, quotes, node);
|
|
1106
|
+
if (quotes) return [
|
|
1107
|
+
...prefix,
|
|
1108
|
+
"=",
|
|
1109
|
+
"\"",
|
|
1110
|
+
attrNodeValue,
|
|
1111
|
+
"\""
|
|
1112
|
+
];
|
|
1113
|
+
else return [
|
|
1114
|
+
...prefix,
|
|
1115
|
+
"=",
|
|
1116
|
+
attrNodeValue
|
|
1117
|
+
];
|
|
1118
|
+
}
|
|
1119
|
+
case "Let": return [
|
|
1120
|
+
"let:",
|
|
1121
|
+
node.name,
|
|
1122
|
+
!node.expression || node.expression.type === "Identifier" && node.expression.name === node.name ? "" : ["=", ...printJsExpression()]
|
|
1123
|
+
];
|
|
1124
|
+
case "DebugTag": return [
|
|
1125
|
+
"{@debug",
|
|
1126
|
+
node.identifiers.length > 0 ? [" ", join(", ", path.map(print, "identifiers"))] : "",
|
|
1127
|
+
"}"
|
|
1128
|
+
];
|
|
1129
|
+
case "Ref": return ["ref:", node.name];
|
|
1130
|
+
case "Comment": {
|
|
1131
|
+
const nodeAfterComment = getNextNode(path);
|
|
1132
|
+
if (isIgnoreStartDirective(node) && isNodeTopLevelHTML(node, path)) ignoreRange = true;
|
|
1133
|
+
else if (isIgnoreEndDirective(node) && isNodeTopLevelHTML(node, path)) ignoreRange = false;
|
|
1134
|
+
else if (doesEmbedStartAfterNode(node, path) || isEmptyTextNode(nodeAfterComment) && doesEmbedStartAfterNode(nodeAfterComment, path)) return "";
|
|
1135
|
+
else if (isIgnoreDirective(node)) ignoreNext = true;
|
|
1136
|
+
return printComment(node);
|
|
1137
|
+
}
|
|
1138
|
+
case "Transition": return [
|
|
1139
|
+
node.intro && node.outro ? "transition" : node.intro ? "in" : "out",
|
|
1140
|
+
":",
|
|
1141
|
+
node.name,
|
|
1142
|
+
node.modifiers && node.modifiers.length ? ["|", join("|", node.modifiers)] : "",
|
|
1143
|
+
node.expression ? ["=", ...printJsExpression()] : ""
|
|
1144
|
+
];
|
|
1145
|
+
case "Action": return [
|
|
1146
|
+
"use:",
|
|
1147
|
+
node.name,
|
|
1148
|
+
node.expression ? ["=", ...printJsExpression()] : ""
|
|
1149
|
+
];
|
|
1150
|
+
case "Animation": return [
|
|
1151
|
+
"animate:",
|
|
1152
|
+
node.name,
|
|
1153
|
+
node.expression ? ["=", ...printJsExpression()] : ""
|
|
1154
|
+
];
|
|
1155
|
+
case "RawMustacheTag": return [
|
|
1156
|
+
"{@html ",
|
|
1157
|
+
printJS(path, print, "expression"),
|
|
1158
|
+
"}"
|
|
1159
|
+
];
|
|
1160
|
+
case "RenderTag": return [
|
|
1161
|
+
"{@render ",
|
|
1162
|
+
printJS(path, print, "expression"),
|
|
1163
|
+
"}"
|
|
1164
|
+
];
|
|
1165
|
+
case "AttachTag": return [
|
|
1166
|
+
"{@attach ",
|
|
1167
|
+
printJS(path, print, "expression"),
|
|
1168
|
+
"}"
|
|
1169
|
+
];
|
|
1170
|
+
case "Spread": return [
|
|
1171
|
+
"{...",
|
|
1172
|
+
printJS(path, print, "expression"),
|
|
1173
|
+
"}"
|
|
1174
|
+
];
|
|
1175
|
+
case "ConstTag": return [
|
|
1176
|
+
"{@const ",
|
|
1177
|
+
printJS(path, print, "expression"),
|
|
1178
|
+
"}"
|
|
1179
|
+
];
|
|
1180
|
+
}
|
|
1181
|
+
console.error(JSON.stringify(node, null, 4));
|
|
1182
|
+
throw new Error("unknown node type: " + node.type);
|
|
1183
|
+
}
|
|
1184
|
+
function printTopLevelParts(n, options, path, print) {
|
|
1185
|
+
if (options.svelteSortOrder === "none") {
|
|
1186
|
+
const topLevelPartsByEnd = {};
|
|
1187
|
+
const topLevelPartsByStart = {};
|
|
1188
|
+
if (n.module) {
|
|
1189
|
+
topLevelPartsByEnd[n.module.end] = n.module;
|
|
1190
|
+
topLevelPartsByStart[n.module.start] = n.module;
|
|
1191
|
+
}
|
|
1192
|
+
if (n.instance) {
|
|
1193
|
+
topLevelPartsByEnd[n.instance.end] = n.instance;
|
|
1194
|
+
topLevelPartsByStart[n.instance.start] = n.instance;
|
|
1195
|
+
}
|
|
1196
|
+
if (n.css) {
|
|
1197
|
+
topLevelPartsByEnd[n.css.end] = n.css;
|
|
1198
|
+
topLevelPartsByStart[n.css.start] = n.css;
|
|
1199
|
+
}
|
|
1200
|
+
const children = getChildren(n.html);
|
|
1201
|
+
for (let i = 0; i < children.length; i++) {
|
|
1202
|
+
const node = children[i];
|
|
1203
|
+
if (topLevelPartsByEnd[node.start]) {
|
|
1204
|
+
children.splice(i, 0, topLevelPartsByEnd[node.start]);
|
|
1205
|
+
delete topLevelPartsByEnd[node.start];
|
|
1206
|
+
} else if (i === children.length - 1 && topLevelPartsByStart[node.end]) children.push(topLevelPartsByStart[node.end]);
|
|
1207
|
+
}
|
|
1208
|
+
const result = path.call(print, "html");
|
|
1209
|
+
if (options.insertPragma && !hasPragma(options.originalText)) return [
|
|
1210
|
+
`<!-- @format -->`,
|
|
1211
|
+
hardline,
|
|
1212
|
+
result
|
|
1213
|
+
];
|
|
1214
|
+
else return result;
|
|
1215
|
+
}
|
|
1216
|
+
const parts = {
|
|
1217
|
+
options: [],
|
|
1218
|
+
scripts: [],
|
|
1219
|
+
markup: [],
|
|
1220
|
+
styles: []
|
|
1221
|
+
};
|
|
1222
|
+
if (n.module) parts.scripts.push(path.call(print, "module"));
|
|
1223
|
+
if (n.instance) parts.scripts.push(path.call(print, "instance"));
|
|
1224
|
+
if (n.css) parts.styles.push(path.call(print, "css"));
|
|
1225
|
+
const htmlDoc = path.call(print, "html");
|
|
1226
|
+
if (htmlDoc) parts.markup.push(htmlDoc);
|
|
1227
|
+
if (svelteOptionsDoc) parts.options.push(svelteOptionsDoc);
|
|
1228
|
+
const docs = flatten(parseSortOrder(options.svelteSortOrder).map((p) => parts[p]));
|
|
1229
|
+
ignoreNext = false;
|
|
1230
|
+
ignoreRange = false;
|
|
1231
|
+
svelteOptionsDoc = void 0;
|
|
1232
|
+
if (options.parentParser === "markdown") {
|
|
1233
|
+
const lastDoc = docs[docs.length - 1];
|
|
1234
|
+
trimRight([lastDoc], isLine);
|
|
1235
|
+
}
|
|
1236
|
+
if (options.insertPragma && !hasPragma(options.originalText)) return [
|
|
1237
|
+
`<!-- @format -->`,
|
|
1238
|
+
hardline,
|
|
1239
|
+
group(docs)
|
|
1240
|
+
];
|
|
1241
|
+
else return group([join(hardline, docs)]);
|
|
1242
|
+
}
|
|
1243
|
+
function printAttributeNodeValue(path, print, quotes, node) {
|
|
1244
|
+
const valueDocs = path.map((childPath) => childPath.call(print), "value");
|
|
1245
|
+
if (!quotes || !formattableAttributes.includes(node.name)) return valueDocs;
|
|
1246
|
+
else return indent(group(trim(valueDocs, isLine)));
|
|
1247
|
+
}
|
|
1248
|
+
function printSvelteBlockChildren(path, print, options) {
|
|
1249
|
+
const node = path.getValue();
|
|
1250
|
+
const children = node.children;
|
|
1251
|
+
if (!children || children.length === 0) return "";
|
|
1252
|
+
const whitespaceAtStartOfBlock = checkWhitespaceAtStartOfSvelteBlock(node, options);
|
|
1253
|
+
const whitespaceAtEndOfBlock = checkWhitespaceAtEndOfSvelteBlock(node, options);
|
|
1254
|
+
const startline = whitespaceAtStartOfBlock === "none" ? "" : whitespaceAtEndOfBlock === "line" || whitespaceAtStartOfBlock === "line" ? hardline : line;
|
|
1255
|
+
const endline = whitespaceAtEndOfBlock === "none" ? "" : whitespaceAtEndOfBlock === "line" || whitespaceAtStartOfBlock === "line" ? hardline : line;
|
|
1256
|
+
const firstChild = children[0];
|
|
1257
|
+
const lastChild = children[children.length - 1];
|
|
1258
|
+
if (isTextNodeStartingWithWhitespace(firstChild)) trimTextNodeLeft(firstChild);
|
|
1259
|
+
if (isTextNodeEndingWithWhitespace(lastChild)) trimTextNodeRight(lastChild);
|
|
1260
|
+
return [indent([startline, group(printChildren(path, print, options))]), endline];
|
|
1261
|
+
}
|
|
1262
|
+
function printPre(node, originalText, path, print) {
|
|
1263
|
+
const result = [];
|
|
1264
|
+
const length = node.children.length;
|
|
1265
|
+
for (let i = 0; i < length; i++) {
|
|
1266
|
+
const child = node.children[i];
|
|
1267
|
+
if (child.type === "Text") originalText.substring(child.start, child.end).split(/\r?\n/).forEach((line, j) => {
|
|
1268
|
+
if (j > 0) result.push(literalline);
|
|
1269
|
+
result.push(line);
|
|
1270
|
+
});
|
|
1271
|
+
else result.push(path.call(print, "children", i));
|
|
1272
|
+
}
|
|
1273
|
+
return result;
|
|
1274
|
+
}
|
|
1275
|
+
function printChildren(path, print, options) {
|
|
1276
|
+
if (isPreTagContent(path)) return path.map(print, "children");
|
|
1277
|
+
const childNodes = prepareChildren(path.getValue().children, path, print, options);
|
|
1278
|
+
path.getValue().children = childNodes;
|
|
1279
|
+
if (childNodes.length === 0) return "";
|
|
1280
|
+
const childDocs = [];
|
|
1281
|
+
let handleWhitespaceOfPrevTextNode = false;
|
|
1282
|
+
for (let i = 0; i < childNodes.length; i++) {
|
|
1283
|
+
const childNode = childNodes[i];
|
|
1284
|
+
if (childNode.type === "Text") handleTextChild(i, childNode);
|
|
1285
|
+
else if (isBlockElement(childNode, options)) handleBlockChild(i);
|
|
1286
|
+
else if (isInlineElement(path, options, childNode)) handleInlineChild(i);
|
|
1287
|
+
else {
|
|
1288
|
+
childDocs.push(printChild(i));
|
|
1289
|
+
handleWhitespaceOfPrevTextNode = false;
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
if (childNodes.length > 1 && childNodes.some((child) => isBlockElement(child, options))) childDocs.push(breakParent);
|
|
1293
|
+
return childDocs;
|
|
1294
|
+
function printChild(idx) {
|
|
1295
|
+
return path.call(print, "children", idx);
|
|
1296
|
+
}
|
|
1297
|
+
/**
|
|
1298
|
+
* Print inline child. Hug whitespace of previous text child if there was one.
|
|
1299
|
+
*/
|
|
1300
|
+
function handleInlineChild(idx) {
|
|
1301
|
+
if (handleWhitespaceOfPrevTextNode) childDocs.push(group([line, printChild(idx)]));
|
|
1302
|
+
else childDocs.push(printChild(idx));
|
|
1303
|
+
handleWhitespaceOfPrevTextNode = false;
|
|
1304
|
+
}
|
|
1305
|
+
/**
|
|
1306
|
+
* Print block element. Add softlines around it if needed
|
|
1307
|
+
* so it breaks into a separate line if children are broken up.
|
|
1308
|
+
* Don't add lines at the start/end if it's the first/last child because this
|
|
1309
|
+
* kind of whitespace handling is done in the parent already.
|
|
1310
|
+
*/
|
|
1311
|
+
function handleBlockChild(idx) {
|
|
1312
|
+
const prevChild = childNodes[idx - 1];
|
|
1313
|
+
if (prevChild && !isBlockElement(prevChild, options) && (prevChild.type !== "Text" || handleWhitespaceOfPrevTextNode || !isTextNodeEndingWithWhitespace(prevChild))) childDocs.push(softline);
|
|
1314
|
+
childDocs.push(printChild(idx));
|
|
1315
|
+
const nextChild = childNodes[idx + 1];
|
|
1316
|
+
if (nextChild && (nextChild.type !== "Text" || (!isEmptyTextNode(nextChild) || childNodes[idx + 2] && isInlineElement(path, options, childNodes[idx + 2])) && !isTextNodeStartingWithLinebreak(nextChild))) childDocs.push(softline);
|
|
1317
|
+
handleWhitespaceOfPrevTextNode = false;
|
|
1318
|
+
}
|
|
1319
|
+
/**
|
|
1320
|
+
* Print text child. First/last child white space handling
|
|
1321
|
+
* is done in parent already. By definition of the Svelte AST,
|
|
1322
|
+
* a text node always is inbetween other tags. Add hardlines
|
|
1323
|
+
* if the users wants to have them inbetween.
|
|
1324
|
+
* If the text is trimmed right, toggle flag telling
|
|
1325
|
+
* subsequent (inline)block element to alter its printing logic
|
|
1326
|
+
* to check if they need to hug or print lines themselves.
|
|
1327
|
+
*/
|
|
1328
|
+
function handleTextChild(idx, childNode) {
|
|
1329
|
+
handleWhitespaceOfPrevTextNode = false;
|
|
1330
|
+
if (idx === 0 || idx === childNodes.length - 1) {
|
|
1331
|
+
childDocs.push(printChild(idx));
|
|
1332
|
+
return;
|
|
1333
|
+
}
|
|
1334
|
+
const prevNode = childNodes[idx - 1];
|
|
1335
|
+
const nextNode = childNodes[idx + 1];
|
|
1336
|
+
if (isTextNodeStartingWithWhitespace(childNode) && !isEmptyTextNode(childNode)) {
|
|
1337
|
+
if (isInlineElement(path, options, prevNode) && !isTextNodeStartingWithLinebreak(childNode)) {
|
|
1338
|
+
trimTextNodeLeft(childNode);
|
|
1339
|
+
const lastChildDoc = childDocs.pop();
|
|
1340
|
+
childDocs.push(group([lastChildDoc, line]));
|
|
1341
|
+
}
|
|
1342
|
+
if (isBlockElement(prevNode, options) && !isTextNodeStartingWithLinebreak(childNode)) trimTextNodeLeft(childNode);
|
|
1343
|
+
}
|
|
1344
|
+
if (isTextNodeEndingWithWhitespace(childNode)) {
|
|
1345
|
+
if (isInlineElement(path, options, nextNode) && !isTextNodeEndingWithLinebreak(childNode)) {
|
|
1346
|
+
handleWhitespaceOfPrevTextNode = !prevNode || !isBlockElement(prevNode, options);
|
|
1347
|
+
trimTextNodeRight(childNode);
|
|
1348
|
+
}
|
|
1349
|
+
if (isBlockElement(nextNode, options) && !isTextNodeEndingWithLinebreak(childNode, 2)) {
|
|
1350
|
+
handleWhitespaceOfPrevTextNode = !prevNode || !isBlockElement(prevNode, options);
|
|
1351
|
+
trimTextNodeRight(childNode);
|
|
1352
|
+
}
|
|
1353
|
+
}
|
|
1354
|
+
childDocs.push(printChild(idx));
|
|
1355
|
+
}
|
|
1356
|
+
}
|
|
1357
|
+
/**
|
|
1358
|
+
* `svelte:options` is part of the html part but needs to be snipped out and handled
|
|
1359
|
+
* separately to reorder it as configured. The comment above it should be moved with it.
|
|
1360
|
+
* Do that here.
|
|
1361
|
+
*/
|
|
1362
|
+
function prepareChildren(children, path, print, options) {
|
|
1363
|
+
let svelteOptionsComment;
|
|
1364
|
+
const childrenWithoutOptions = [];
|
|
1365
|
+
const bracketSameLine = isBracketSameLine(options);
|
|
1366
|
+
for (let idx = 0; idx < children.length; idx++) {
|
|
1367
|
+
const currentChild = children[idx];
|
|
1368
|
+
if (currentChild.type === "Text" && getUnencodedText(currentChild) === "") continue;
|
|
1369
|
+
if (isEmptyTextNode(currentChild) && doesEmbedStartAfterNode(currentChild, path)) continue;
|
|
1370
|
+
if (options.svelteSortOrder !== "none") {
|
|
1371
|
+
if (isCommentFollowedByOptions(currentChild, idx)) {
|
|
1372
|
+
svelteOptionsComment = printComment(currentChild);
|
|
1373
|
+
const nextChild = children[idx + 1];
|
|
1374
|
+
idx += nextChild && isEmptyTextNode(nextChild) ? 1 : 0;
|
|
1375
|
+
continue;
|
|
1376
|
+
}
|
|
1377
|
+
if (currentChild.type === "Options") {
|
|
1378
|
+
printSvelteOptions(currentChild, idx, path, print);
|
|
1379
|
+
continue;
|
|
1380
|
+
}
|
|
1381
|
+
}
|
|
1382
|
+
childrenWithoutOptions.push(currentChild);
|
|
1383
|
+
}
|
|
1384
|
+
const mergedChildrenWithoutOptions = [];
|
|
1385
|
+
for (let idx = 0; idx < childrenWithoutOptions.length; idx++) {
|
|
1386
|
+
const currentChild = childrenWithoutOptions[idx];
|
|
1387
|
+
const nextChild = childrenWithoutOptions[idx + 1];
|
|
1388
|
+
if (currentChild.type === "Text" && nextChild && nextChild.type === "Text") {
|
|
1389
|
+
currentChild.raw += nextChild.raw;
|
|
1390
|
+
currentChild.data += nextChild.data;
|
|
1391
|
+
idx++;
|
|
1392
|
+
}
|
|
1393
|
+
mergedChildrenWithoutOptions.push(currentChild);
|
|
1394
|
+
}
|
|
1395
|
+
return mergedChildrenWithoutOptions;
|
|
1396
|
+
function printSvelteOptions(node, idx, path, print) {
|
|
1397
|
+
svelteOptionsDoc = group([[
|
|
1398
|
+
"<",
|
|
1399
|
+
node.name,
|
|
1400
|
+
indent(group([...path.map(printWithPrependedAttributeLine(node, options, print), "children", idx, "attributes"), bracketSameLine ? "" : dedent(line)])),
|
|
1401
|
+
...[bracketSameLine ? " " : "", "/>"]
|
|
1402
|
+
], hardline]);
|
|
1403
|
+
if (svelteOptionsComment) svelteOptionsDoc = group([
|
|
1404
|
+
svelteOptionsComment,
|
|
1405
|
+
hardline,
|
|
1406
|
+
svelteOptionsDoc
|
|
1407
|
+
]);
|
|
1408
|
+
}
|
|
1409
|
+
function isCommentFollowedByOptions(node, idx) {
|
|
1410
|
+
if (node.type !== "Comment" || isIgnoreEndDirective(node) || isIgnoreStartDirective(node)) return false;
|
|
1411
|
+
const nextChild = children[idx + 1];
|
|
1412
|
+
if (nextChild) {
|
|
1413
|
+
if (isEmptyTextNode(nextChild)) {
|
|
1414
|
+
const afterNext = children[idx + 2];
|
|
1415
|
+
return afterNext && afterNext.type === "Options";
|
|
1416
|
+
}
|
|
1417
|
+
return nextChild.type === "Options";
|
|
1418
|
+
}
|
|
1419
|
+
return false;
|
|
1420
|
+
}
|
|
1421
|
+
}
|
|
1422
|
+
/**
|
|
1423
|
+
* Split the text into words separated by whitespace. Replace the whitespaces by lines,
|
|
1424
|
+
* collapsing multiple whitespaces into a single line.
|
|
1425
|
+
*
|
|
1426
|
+
* If the text starts or ends with multiple newlines, two of those should be kept.
|
|
1427
|
+
*/
|
|
1428
|
+
function splitTextToDocs(node) {
|
|
1429
|
+
const text = getUnencodedText(node);
|
|
1430
|
+
let docs = join(line, text.split(/[\t\n\f\r ]+/)).filter((doc) => doc !== "");
|
|
1431
|
+
if (startsWithLinebreak(text)) docs[0] = hardline;
|
|
1432
|
+
if (startsWithLinebreak(text, 2)) docs = [hardline, ...docs];
|
|
1433
|
+
if (endsWithLinebreak(text)) docs[docs.length - 1] = hardline;
|
|
1434
|
+
if (endsWithLinebreak(text, 2)) docs = [...docs, hardline];
|
|
1435
|
+
return docs;
|
|
1436
|
+
}
|
|
1437
|
+
function printJS(path, print, name) {
|
|
1438
|
+
return path.call(print, name);
|
|
1439
|
+
}
|
|
1440
|
+
function expandNode(node, original) {
|
|
1441
|
+
let str = _expandNode(node);
|
|
1442
|
+
if (node === null || node === void 0 ? void 0 : node.typeAnnotation) str += ": " + original.slice(node.typeAnnotation.typeAnnotation.start, node.typeAnnotation.typeAnnotation.end);
|
|
1443
|
+
return str;
|
|
1444
|
+
}
|
|
1445
|
+
function _expandNode(node, parent) {
|
|
1446
|
+
if (node === null) return "";
|
|
1447
|
+
if (typeof node === "string") return " " + node;
|
|
1448
|
+
switch (node.type) {
|
|
1449
|
+
case "ArrayExpression":
|
|
1450
|
+
case "ArrayPattern": return " [" + node.elements.map((el) => el === null ? " " : _expandNode(el)).join(",").slice(1) + "]";
|
|
1451
|
+
case "AssignmentPattern": return _expandNode(node.left) + " =" + _expandNode(node.right);
|
|
1452
|
+
case "Identifier": return " " + node.name;
|
|
1453
|
+
case "Literal": return " " + node.raw;
|
|
1454
|
+
case "ObjectExpression": return " {" + node.properties.map((p) => _expandNode(p, node)).join(",") + " }";
|
|
1455
|
+
case "ObjectPattern": return " {" + node.properties.map(_expandNode).join(",") + " }";
|
|
1456
|
+
case "Property": if (node.value.type === "ObjectPattern" || node.value.type === "ArrayPattern") return " " + node.key.name + ":" + _expandNode(node.value);
|
|
1457
|
+
else if (node.value.type === "Identifier" && node.key.name !== node.value.name || parent && parent.type === "ObjectExpression") return _expandNode(node.key) + ":" + _expandNode(node.value);
|
|
1458
|
+
else return _expandNode(node.value);
|
|
1459
|
+
case "RestElement": return " ..." + node.argument.name;
|
|
1460
|
+
}
|
|
1461
|
+
console.error(JSON.stringify(node, null, 4));
|
|
1462
|
+
throw new Error("unknown node type: " + node.type);
|
|
1463
|
+
}
|
|
1464
|
+
function printComment(node) {
|
|
1465
|
+
let text = node.data;
|
|
1466
|
+
if (hasSnippedContent(text)) text = unsnipContent(text);
|
|
1467
|
+
return group([
|
|
1468
|
+
"<!--",
|
|
1469
|
+
text,
|
|
1470
|
+
"-->"
|
|
1471
|
+
]);
|
|
1472
|
+
}
|
|
1473
|
+
function getText(node, options, unsnip = false) {
|
|
1474
|
+
const leadingComments = node.leadingComments;
|
|
1475
|
+
const text = options.originalText.slice(options.locStart(leadingComments && leadingComments[0] || node), options.locEnd(node));
|
|
1476
|
+
if (!unsnip || !hasSnippedContent(text)) return text;
|
|
1477
|
+
return unsnipContent(text);
|
|
1478
|
+
}
|
|
1479
|
+
const extractAttributesRegex = /<[a-z]+((?:\s+[^=>'"\/]+=(?:"[^"]*"|'[^']*'|[^>\s]+)|\s+[^=>'"\/]+)*\s*)>/im;
|
|
1480
|
+
const attributeRegex = /([^\s=]+)(?:=(?:(?:("|')([\s\S]*?)\2)|(?:([^>\s]+?)(?:\s|>|$))))?/gim;
|
|
1481
|
+
function extractAttributes(html) {
|
|
1482
|
+
const [, attributesString] = html.match(extractAttributesRegex);
|
|
1483
|
+
const attrs = [];
|
|
1484
|
+
let match;
|
|
1485
|
+
while (match = attributeRegex.exec(attributesString)) {
|
|
1486
|
+
const [all, name, quotes, valueQuoted, valueUnquoted] = match;
|
|
1487
|
+
const value = valueQuoted || valueUnquoted;
|
|
1488
|
+
const attrStart = match.index;
|
|
1489
|
+
let valueNode;
|
|
1490
|
+
if (!value) valueNode = true;
|
|
1491
|
+
else {
|
|
1492
|
+
let valueStart = attrStart + name.length;
|
|
1493
|
+
if (quotes) valueStart += 2;
|
|
1494
|
+
valueNode = [{
|
|
1495
|
+
type: "Text",
|
|
1496
|
+
data: value,
|
|
1497
|
+
start: valueStart,
|
|
1498
|
+
end: valueStart + value.length
|
|
1499
|
+
}];
|
|
1500
|
+
}
|
|
1501
|
+
attrs.push({
|
|
1502
|
+
type: "Attribute",
|
|
1503
|
+
name,
|
|
1504
|
+
value: valueNode,
|
|
1505
|
+
start: attrStart,
|
|
1506
|
+
end: attrStart + all.length
|
|
1507
|
+
});
|
|
1508
|
+
}
|
|
1509
|
+
return attrs;
|
|
1510
|
+
}
|
|
1511
|
+
const { builders: { group: group$1, hardline: hardline$1, softline: softline$1, indent: indent$1, dedent: dedent$1, literalline: literalline$1 }, utils: { removeLines } } = prettier.doc;
|
|
1512
|
+
const leaveAlone = new Set([
|
|
1513
|
+
"Script",
|
|
1514
|
+
"Style",
|
|
1515
|
+
"Identifier",
|
|
1516
|
+
"MemberExpression",
|
|
1517
|
+
"CallExpression",
|
|
1518
|
+
"ArrowFunctionExpression"
|
|
1519
|
+
]);
|
|
1520
|
+
const dontTraverse = new Set([
|
|
1521
|
+
"start",
|
|
1522
|
+
"end",
|
|
1523
|
+
"type"
|
|
1524
|
+
]);
|
|
1525
|
+
function getVisitorKeys(node, nonTraversableKeys) {
|
|
1526
|
+
return Object.keys(node).filter((key) => {
|
|
1527
|
+
return !nonTraversableKeys.has(key) && !leaveAlone.has(node.type) && !dontTraverse.has(key);
|
|
1528
|
+
});
|
|
1529
|
+
}
|
|
1530
|
+
function embed(path, _options) {
|
|
1531
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
|
|
1532
|
+
const node = path.getNode();
|
|
1533
|
+
const options = _options;
|
|
1534
|
+
if (!options.locStart || !options.locEnd || !options.originalText) throw new Error("Missing required options");
|
|
1535
|
+
if (isASTNode(node)) {
|
|
1536
|
+
assignCommentsToNodes(node);
|
|
1537
|
+
attachAttributeComments(node, options.originalText);
|
|
1538
|
+
if (node.module) {
|
|
1539
|
+
node.module.type = "Script";
|
|
1540
|
+
node.module.attributes = extractAttributes(getText(node.module, options));
|
|
1541
|
+
}
|
|
1542
|
+
if (node.instance) {
|
|
1543
|
+
node.instance.type = "Script";
|
|
1544
|
+
node.instance.attributes = extractAttributes(getText(node.instance, options));
|
|
1545
|
+
}
|
|
1546
|
+
if (node.css) {
|
|
1547
|
+
node.css.type = "Style";
|
|
1548
|
+
node.css.content.type = "StyleProgram";
|
|
1549
|
+
}
|
|
1550
|
+
return null;
|
|
1551
|
+
}
|
|
1552
|
+
const parent = path.getParentNode();
|
|
1553
|
+
const printJsExpression = () => {
|
|
1554
|
+
var _a;
|
|
1555
|
+
return parent.expression ? printJS$1(parent, "expression", { forceSingleQuote: (_a = options.svelteStrictMode && !options._svelte_is5Plus) !== null && _a !== void 0 ? _a : false }) : void 0;
|
|
1556
|
+
};
|
|
1557
|
+
const printSvelteBlockJS = (name) => printJS$1(parent, name, { forceSingleLine: true });
|
|
1558
|
+
switch (parent.type) {
|
|
1559
|
+
case "IfBlock":
|
|
1560
|
+
case "ElseBlock":
|
|
1561
|
+
case "AwaitBlock":
|
|
1562
|
+
case "KeyBlock":
|
|
1563
|
+
printSvelteBlockJS("expression");
|
|
1564
|
+
break;
|
|
1565
|
+
case "EachBlock":
|
|
1566
|
+
printSvelteBlockJS("expression");
|
|
1567
|
+
printSvelteBlockJS("key");
|
|
1568
|
+
break;
|
|
1569
|
+
case "SnippetBlock":
|
|
1570
|
+
if (node === parent.expression) {
|
|
1571
|
+
parent.expression.end = options.originalText.indexOf(")", (_g = (_d = (_c = (_b = (_a = parent.parameters) === null || _a === void 0 ? void 0 : _a[parent.parameters.length - 1]) === null || _b === void 0 ? void 0 : _b.typeAnnotation) === null || _c === void 0 ? void 0 : _c.end) !== null && _d !== void 0 ? _d : (_f = (_e = parent.parameters) === null || _e === void 0 ? void 0 : _e[parent.parameters.length - 1]) === null || _f === void 0 ? void 0 : _f.end) !== null && _g !== void 0 ? _g : parent.expression.end) + 1;
|
|
1572
|
+
parent.parameters = null;
|
|
1573
|
+
node.isJS = true;
|
|
1574
|
+
node.asFunction = true;
|
|
1575
|
+
}
|
|
1576
|
+
break;
|
|
1577
|
+
case "Element":
|
|
1578
|
+
printJS$1(parent, "tag", { forceSingleQuote: (_h = options.svelteStrictMode && !options._svelte_is5Plus) !== null && _h !== void 0 ? _h : false });
|
|
1579
|
+
break;
|
|
1580
|
+
case "MustacheTag":
|
|
1581
|
+
printJS$1(parent, "expression", { forceSingleQuote: isInsideQuotedAttribute(path, options) });
|
|
1582
|
+
break;
|
|
1583
|
+
case "RawMustacheTag":
|
|
1584
|
+
printJS$1(parent, "expression", {});
|
|
1585
|
+
break;
|
|
1586
|
+
case "Spread":
|
|
1587
|
+
printJS$1(parent, "expression", {});
|
|
1588
|
+
break;
|
|
1589
|
+
case "AttachTag":
|
|
1590
|
+
printJS$1(parent, "expression", {});
|
|
1591
|
+
break;
|
|
1592
|
+
case "ConstTag":
|
|
1593
|
+
printJS$1(parent, "expression", { removeParentheses: true });
|
|
1594
|
+
break;
|
|
1595
|
+
case "Binding":
|
|
1596
|
+
printJS$1(parent, "expression", {
|
|
1597
|
+
removeParentheses: parent.expression.type === "SequenceExpression",
|
|
1598
|
+
surroundWithSoftline: true
|
|
1599
|
+
});
|
|
1600
|
+
break;
|
|
1601
|
+
case "RenderTag":
|
|
1602
|
+
if (node === parent.expression) {
|
|
1603
|
+
if ("argument" in parent || "arguments" in parent) {
|
|
1604
|
+
parent.expression.end = options.originalText.indexOf(")", (_o = (_k = (_j = parent.argument) === null || _j === void 0 ? void 0 : _j.end) !== null && _k !== void 0 ? _k : (_m = (_l = parent.arguments) === null || _l === void 0 ? void 0 : _l[parent.arguments.length - 1]) === null || _m === void 0 ? void 0 : _m.end) !== null && _o !== void 0 ? _o : parent.expression.end) + 1;
|
|
1605
|
+
parent.argument = null;
|
|
1606
|
+
parent.arguments = null;
|
|
1607
|
+
}
|
|
1608
|
+
printJS$1(parent, "expression", {});
|
|
1609
|
+
}
|
|
1610
|
+
break;
|
|
1611
|
+
case "EventHandler":
|
|
1612
|
+
case "Binding":
|
|
1613
|
+
case "Class":
|
|
1614
|
+
case "Let":
|
|
1615
|
+
case "Transition":
|
|
1616
|
+
case "Action":
|
|
1617
|
+
case "Animation":
|
|
1618
|
+
case "InlineComponent":
|
|
1619
|
+
printJsExpression();
|
|
1620
|
+
break;
|
|
1621
|
+
}
|
|
1622
|
+
if (node.isJS) return (textToDoc) => __awaiter(this, void 0, void 0, function* () {
|
|
1623
|
+
try {
|
|
1624
|
+
const embeddedOptions = {
|
|
1625
|
+
parser: options._svelte_ts ? "svelteTSExpressionParser" : "svelteExpressionParser",
|
|
1626
|
+
singleQuote: node.forceSingleQuote ? true : options.singleQuote,
|
|
1627
|
+
_svelte_asFunction: node.asFunction
|
|
1628
|
+
};
|
|
1629
|
+
const text = getText(node, options, true);
|
|
1630
|
+
let docs = yield textToDoc(node.asFunction ? forceIntoFunction(text) : forceIntoExpression(text), embeddedOptions);
|
|
1631
|
+
if (node.forceSingleLine) docs = removeLines(docs);
|
|
1632
|
+
if (node.removeParentheses) docs = removeParentheses(docs);
|
|
1633
|
+
if (node.asFunction) if (Array.isArray(docs) && typeof docs[0] === "string") {
|
|
1634
|
+
docs[0] = docs[0].replace("function ", "");
|
|
1635
|
+
docs.splice(-1, 1);
|
|
1636
|
+
} else throw new Error("Prettier AST changed, asFunction logic needs to change");
|
|
1637
|
+
if (node.surroundWithSoftline) docs = group$1(indent$1([
|
|
1638
|
+
softline$1,
|
|
1639
|
+
group$1(docs),
|
|
1640
|
+
dedent$1(softline$1)
|
|
1641
|
+
]));
|
|
1642
|
+
return docs;
|
|
1643
|
+
} catch (e) {
|
|
1644
|
+
return getText(node, options, true);
|
|
1645
|
+
}
|
|
1646
|
+
});
|
|
1647
|
+
const embedType = (tag, parser, isTopLevel) => {
|
|
1648
|
+
return (textToDoc, print) => __awaiter(this, void 0, void 0, function* () {
|
|
1649
|
+
return embedTag(tag, options.originalText, path, (content) => formatBodyContent(content, parser, textToDoc, options), print, isTopLevel, options);
|
|
1650
|
+
});
|
|
1651
|
+
};
|
|
1652
|
+
const embedScript = (isTopLevel) => embedType("script", isTypeScript(node) ? "typescript" : isJSON(node) ? "json" : "babel-ts", isTopLevel);
|
|
1653
|
+
const embedStyle = (isTopLevel) => embedType("style", isLess(node) ? "less" : isScss(node) ? "scss" : "css", isTopLevel);
|
|
1654
|
+
const embedPug = () => embedType("template", "pug", false);
|
|
1655
|
+
switch (node.type) {
|
|
1656
|
+
case "Script": return embedScript(true);
|
|
1657
|
+
case "Style": return embedStyle(true);
|
|
1658
|
+
case "Element": if (node.name === "script") return embedScript(false);
|
|
1659
|
+
else if (node.name === "style") return embedStyle(false);
|
|
1660
|
+
else if (isPugTemplate(node)) return embedPug();
|
|
1661
|
+
}
|
|
1662
|
+
return null;
|
|
1663
|
+
}
|
|
1664
|
+
function forceIntoExpression(statement) {
|
|
1665
|
+
return `(${statement}\n)`;
|
|
1666
|
+
}
|
|
1667
|
+
function forceIntoFunction(statement) {
|
|
1668
|
+
return `function ${statement} {}`;
|
|
1669
|
+
}
|
|
1670
|
+
function preformattedBody(str) {
|
|
1671
|
+
if (!str) return "";
|
|
1672
|
+
return [
|
|
1673
|
+
literalline$1,
|
|
1674
|
+
str.replace(/^[\t\f\r ]*\n/, "").replace(/\n[\t\f\r ]*$/, ""),
|
|
1675
|
+
hardline$1
|
|
1676
|
+
];
|
|
1677
|
+
}
|
|
1678
|
+
function getSnippedContent(node) {
|
|
1679
|
+
const encodedContent = getAttributeTextValue(snippedTagContentAttribute, node);
|
|
1680
|
+
if (encodedContent) return base64ToString(encodedContent);
|
|
1681
|
+
else return "";
|
|
1682
|
+
}
|
|
1683
|
+
function formatBodyContent(content, parser, textToDoc, options) {
|
|
1684
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1685
|
+
try {
|
|
1686
|
+
const body = yield textToDoc(content, { parser });
|
|
1687
|
+
if (parser === "pug" && typeof body === "string") {
|
|
1688
|
+
const whitespace = options.useTabs ? " " : " ".repeat(options.pugTabWidth && options.pugTabWidth > 0 ? options.pugTabWidth : options.tabWidth);
|
|
1689
|
+
return [hardline$1, body.split("\n").map((line) => line ? whitespace + line : line).join("\n")];
|
|
1690
|
+
}
|
|
1691
|
+
const indentIfDesired = (doc) => options.svelteIndentScriptAndStyle ? indent$1(doc) : doc;
|
|
1692
|
+
trimRight([body], isLine);
|
|
1693
|
+
return [indentIfDesired([hardline$1, body]), hardline$1];
|
|
1694
|
+
} catch (error) {
|
|
1695
|
+
if (process.env.PRETTIER_DEBUG) throw error;
|
|
1696
|
+
console.error(error);
|
|
1697
|
+
return preformattedBody(content);
|
|
1698
|
+
}
|
|
1699
|
+
});
|
|
1700
|
+
}
|
|
1701
|
+
function embedTag(tag, text, path, formatBodyContent, print, isTopLevel, options) {
|
|
1702
|
+
var _a;
|
|
1703
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1704
|
+
const node = path.getNode();
|
|
1705
|
+
const content = tag === "template" ? printRaw(node, text) : getSnippedContent(node);
|
|
1706
|
+
const previousComments = node.type === "Script" || node.type === "Style" ? node.comments : [getLeadingComment(path)].filter(Boolean).map((comment) => ({
|
|
1707
|
+
comment,
|
|
1708
|
+
emptyLineAfter: false
|
|
1709
|
+
}));
|
|
1710
|
+
const body = isNodeSupportedLanguage(node) && !isIgnoreDirective((_a = previousComments[previousComments.length - 1]) === null || _a === void 0 ? void 0 : _a.comment) && (tag !== "template" || options.plugins.some((plugin) => typeof plugin !== "string" && plugin.parsers && plugin.parsers.pug)) ? content.trim() !== "" ? yield formatBodyContent(content) : content === "" ? "" : hardline$1 : preformattedBody(content);
|
|
1711
|
+
let result = group$1([
|
|
1712
|
+
group$1([
|
|
1713
|
+
"<",
|
|
1714
|
+
tag,
|
|
1715
|
+
indent$1(group$1([...path.map(printWithPrependedAttributeLine(node, options, print), "attributes"), isBracketSameLine(options) ? "" : dedent$1(softline$1)])),
|
|
1716
|
+
">"
|
|
1717
|
+
]),
|
|
1718
|
+
body,
|
|
1719
|
+
"</",
|
|
1720
|
+
tag,
|
|
1721
|
+
">"
|
|
1722
|
+
]);
|
|
1723
|
+
const comments = [];
|
|
1724
|
+
for (const comment of previousComments) {
|
|
1725
|
+
comments.push("<!--", comment.comment.data, "-->");
|
|
1726
|
+
comments.push(hardline$1);
|
|
1727
|
+
if (comment.emptyLineAfter) comments.push(hardline$1);
|
|
1728
|
+
}
|
|
1729
|
+
if (isTopLevel && options.svelteSortOrder !== "none") return [
|
|
1730
|
+
...comments,
|
|
1731
|
+
result,
|
|
1732
|
+
hardline$1
|
|
1733
|
+
];
|
|
1734
|
+
else return isTopLevel && comments.length ? [...comments, result] : result;
|
|
1735
|
+
});
|
|
1736
|
+
}
|
|
1737
|
+
function printJS$1(node, name, options) {
|
|
1738
|
+
const part = node[name];
|
|
1739
|
+
if (!part || typeof part !== "object") return;
|
|
1740
|
+
part.isJS = true;
|
|
1741
|
+
part.forceSingleQuote = options.forceSingleQuote;
|
|
1742
|
+
part.forceSingleLine = options.forceSingleLine;
|
|
1743
|
+
part.removeParentheses = options.removeParentheses;
|
|
1744
|
+
part.surroundWithSoftline = options.surroundWithSoftline;
|
|
1745
|
+
}
|
|
1746
|
+
/**
|
|
1747
|
+
* Walk the AST and use `_comments` (stashed by the parser) to attach
|
|
1748
|
+
* attribute-level comments to their neighbouring attribute nodes via
|
|
1749
|
+
* Prettier's `util.addLeadingComment` / `util.addTrailingComment`.
|
|
1750
|
+
*/
|
|
1751
|
+
function attachAttributeComments(ast, original_text) {
|
|
1752
|
+
const comments = ast._comments;
|
|
1753
|
+
if (!comments || comments.length === 0) return;
|
|
1754
|
+
const commentsByStart = /* @__PURE__ */ new Map();
|
|
1755
|
+
for (const c of comments) commentsByStart.set(c.start, c);
|
|
1756
|
+
walkAndAttach(ast.html, commentsByStart, original_text);
|
|
1757
|
+
}
|
|
1758
|
+
function walkAndAttach(node, commentsByStart, original_text) {
|
|
1759
|
+
if (!node || typeof node !== "object") return;
|
|
1760
|
+
if ("attributes" in node && Array.isArray(node.attributes) && node.attributes.length > 0) {
|
|
1761
|
+
const attrs = node.attributes;
|
|
1762
|
+
attachCommentsInRange(node.start + 2, attrs[0].start, null, attrs[0], commentsByStart);
|
|
1763
|
+
for (let i = 0; i < attrs.length - 1; i++) attachCommentsInRange(attrs[i].end, attrs[i + 1].start, attrs[i], attrs[i + 1], commentsByStart);
|
|
1764
|
+
const last_attr = attrs[attrs.length - 1];
|
|
1765
|
+
const opening_tag_end = original_text && typeof node.end === "number" ? original_text.indexOf(">", last_attr.end) : -1;
|
|
1766
|
+
if (opening_tag_end >= 0 && opening_tag_end <= node.end) attachCommentsInRange(last_attr.end, opening_tag_end, last_attr, null, commentsByStart);
|
|
1767
|
+
}
|
|
1768
|
+
for (const child of getChildren(node)) walkAndAttach(child, commentsByStart, original_text);
|
|
1769
|
+
if ((node.type === "IfBlock" || node.type === "EachBlock") && node.else) walkAndAttach(node.else, commentsByStart, original_text);
|
|
1770
|
+
if (node.type === "AwaitBlock") {
|
|
1771
|
+
if (node.pending) walkAndAttach(node.pending, commentsByStart, original_text);
|
|
1772
|
+
if (node.then) walkAndAttach(node.then, commentsByStart, original_text);
|
|
1773
|
+
if (node.catch) walkAndAttach(node.catch, commentsByStart, original_text);
|
|
1774
|
+
}
|
|
1775
|
+
}
|
|
1776
|
+
function attachCommentsInRange(rangeStart, rangeEnd, precedingAttr, followingAttr, commentsByStart) {
|
|
1777
|
+
for (const [start, comment] of commentsByStart) if (start >= rangeStart && comment.end <= rangeEnd) {
|
|
1778
|
+
if (followingAttr) prettier.util.addLeadingComment(followingAttr, comment);
|
|
1779
|
+
else if (precedingAttr) prettier.util.addTrailingComment(precedingAttr, comment);
|
|
1780
|
+
commentsByStart.delete(start);
|
|
1781
|
+
}
|
|
1782
|
+
}
|
|
1783
|
+
const babelParser = prettierPluginBabel.parsers.babel;
|
|
1784
|
+
const typescriptParser = prettierPluginBabel.parsers["babel-ts"];
|
|
1785
|
+
const isSvelte5Plus = Number(compiler.VERSION.split(".")[0]) >= 5;
|
|
1786
|
+
function locStart(node) {
|
|
1787
|
+
return node.start;
|
|
1788
|
+
}
|
|
1789
|
+
function locEnd(node) {
|
|
1790
|
+
return node.end;
|
|
1791
|
+
}
|
|
1792
|
+
const languages = [{
|
|
1793
|
+
name: "svelte",
|
|
1794
|
+
parsers: ["svelte"],
|
|
1795
|
+
extensions: [".svelte"],
|
|
1796
|
+
vscodeLanguageIds: ["svelte"]
|
|
1797
|
+
}];
|
|
1798
|
+
const parsers = {
|
|
1799
|
+
svelte: {
|
|
1800
|
+
hasPragma,
|
|
1801
|
+
parse: (text, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
1802
|
+
try {
|
|
1803
|
+
let _parse = compiler.parse;
|
|
1804
|
+
if (options.svelte5CompilerPath) try {
|
|
1805
|
+
_parse = (yield Promise.resolve().then(function() {
|
|
1806
|
+
return /* @__PURE__ */ _interopNamespace(__require(options.svelte5CompilerPath));
|
|
1807
|
+
})).parse;
|
|
1808
|
+
} catch (e) {
|
|
1809
|
+
console.warn(`Failed to load Svelte 5 compiler from ${options.svelte5CompilerPath}`);
|
|
1810
|
+
console.warn(e);
|
|
1811
|
+
options.svelte5CompilerPath = void 0;
|
|
1812
|
+
}
|
|
1813
|
+
const root = _parse(text);
|
|
1814
|
+
root.__isRoot = true;
|
|
1815
|
+
return root;
|
|
1816
|
+
} catch (err) {
|
|
1817
|
+
if (err.start != null && err.end != null) err.loc = {
|
|
1818
|
+
start: err.start,
|
|
1819
|
+
end: err.end
|
|
1820
|
+
};
|
|
1821
|
+
throw err;
|
|
1822
|
+
}
|
|
1823
|
+
}),
|
|
1824
|
+
preprocess: (text, options) => {
|
|
1825
|
+
const result = snipScriptAndStyleTagContent(text);
|
|
1826
|
+
text = result.text.trim();
|
|
1827
|
+
options.originalText = text;
|
|
1828
|
+
const is = !!options.svelte5CompilerPath || isSvelte5Plus;
|
|
1829
|
+
options._svelte_ts = is && result.isTypescript;
|
|
1830
|
+
options._svelte_is5Plus = is;
|
|
1831
|
+
return text;
|
|
1832
|
+
},
|
|
1833
|
+
locStart,
|
|
1834
|
+
locEnd,
|
|
1835
|
+
astFormat: "svelte-ast"
|
|
1836
|
+
},
|
|
1837
|
+
svelteExpressionParser: Object.assign(Object.assign({}, babelParser), { parse: (text, options) => {
|
|
1838
|
+
const ast = babelParser.parse(text, options);
|
|
1839
|
+
let program = ast.program.body[0];
|
|
1840
|
+
if (!options._svelte_asFunction) program = program.expression;
|
|
1841
|
+
return Object.assign(Object.assign({}, ast), { program });
|
|
1842
|
+
} }),
|
|
1843
|
+
svelteTSExpressionParser: Object.assign(Object.assign({}, typescriptParser), { parse: (text, options) => {
|
|
1844
|
+
const ast = typescriptParser.parse(text, options);
|
|
1845
|
+
let program = ast.program.body[0];
|
|
1846
|
+
if (!options._svelte_asFunction) program = program.expression;
|
|
1847
|
+
return Object.assign(Object.assign({}, ast), { program });
|
|
1848
|
+
} })
|
|
1849
|
+
};
|
|
1850
|
+
const printers = { "svelte-ast": {
|
|
1851
|
+
print,
|
|
1852
|
+
embed,
|
|
1853
|
+
getVisitorKeys,
|
|
1854
|
+
isBlockComment(comment) {
|
|
1855
|
+
return comment.type === "Block";
|
|
1856
|
+
},
|
|
1857
|
+
printComment(commentPath) {
|
|
1858
|
+
const comment = commentPath.getValue();
|
|
1859
|
+
if (comment.type === "Line") return "//" + comment.value.replace(/\r$/, "");
|
|
1860
|
+
return "/*" + comment.value + "*/";
|
|
1861
|
+
}
|
|
1862
|
+
} };
|
|
1863
|
+
exports.languages = languages;
|
|
1864
|
+
exports.options = options;
|
|
1865
|
+
exports.parsers = parsers;
|
|
1866
|
+
exports.printers = printers;
|
|
1867
|
+
}));
|
|
1868
|
+
//#endregion
|
|
1869
|
+
export default require_plugin();
|
|
1870
|
+
export {};
|