dorian 2.4.0 → 2.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/dependabot.yml +15 -0
- data/.github/workflows/ci.yml +38 -0
- data/.gitignore +30 -0
- data/.node-version +1 -0
- data/.prettierignore +2 -0
- data/.rubocop.yml +129 -0
- data/.ruby-version +1 -0
- data/.tool-versions +3 -0
- data/Gemfile +20 -0
- data/Gemfile.lock +186 -0
- data/LICENSE +19 -0
- data/README.md +24 -0
- data/VERSION +1 -1
- data/bin/bundle +123 -0
- data/bin/bundle-audit +31 -0
- data/bin/bundler-audit +31 -0
- data/bin/rspec +31 -0
- data/bin/rubocop +31 -0
- data/dorian.gemspec +35 -0
- data/lib/dorian/bin.rb +739 -81
- data/package-lock.json +39 -0
- data/package.json +11 -0
- data/samples/books.json +33 -0
- data/samples/books.jsonl +3 -0
- data/samples/config.yml +27 -0
- data/samples/config_2.yml +22 -0
- data/samples/maths.js +1 -0
- data/samples/numbers.raw +10 -0
- data/samples/people.csv +6 -0
- data/samples/people.yml +28 -0
- data/samples/query.sql +7 -0
- data/samples/user.json +31 -0
- data/spec/dorian_spec.rb +276 -0
- data/spec/spec_helper.rb +3 -0
- data/vendor/groovy-beautify/LICENSE.MD +21 -0
- data/vendor/groovy-beautify/README.md +65 -0
- data/vendor/groovy-beautify/dist/__tests__/index.test.d.ts +1 -0
- data/vendor/groovy-beautify/dist/cjs/index.js +573 -0
- data/vendor/groovy-beautify/dist/cjs/package.json +3 -0
- data/vendor/groovy-beautify/dist/es/index.js +682 -0
- data/vendor/groovy-beautify/dist/es/package.json +3 -0
- data/vendor/groovy-beautify/dist/formatter/FormatRule.d.ts +14 -0
- data/vendor/groovy-beautify/dist/formatter/Formatter.d.ts +11 -0
- data/vendor/groovy-beautify/dist/formatter/GroovyFormatRules.d.ts +3 -0
- data/vendor/groovy-beautify/dist/formatter/__tests__/formatter.test.d.ts +1 -0
- data/vendor/groovy-beautify/dist/index.d.ts +5 -0
- data/vendor/groovy-beautify/dist/parser/GroovyParseRules.d.ts +3 -0
- data/vendor/groovy-beautify/dist/parser/ParseRule.d.ts +23 -0
- data/vendor/groovy-beautify/dist/parser/Parser.d.ts +29 -0
- data/vendor/groovy-beautify/dist/parser/__tests__/parser.test.d.ts +1 -0
- data/vendor/groovy-beautify/dist/parser/types.d.ts +6 -0
- data/vendor/groovy-beautify/dist/utils/text.d.ts +4 -0
- data/vendor/groovy-beautify/package.json +55 -0
- data/vendor/prettier/LICENSE +4379 -0
- data/vendor/prettier/README.md +109 -0
- data/vendor/prettier/bin/prettier.cjs +71 -0
- data/vendor/prettier/doc.d.ts +243 -0
- data/vendor/prettier/doc.js +1545 -0
- data/vendor/prettier/doc.mjs +1503 -0
- data/vendor/prettier/index.cjs +825 -0
- data/vendor/prettier/index.d.ts +941 -0
- data/vendor/prettier/index.mjs +25899 -0
- data/vendor/prettier/internal/cli.mjs +4366 -0
- data/vendor/prettier/package.json +198 -0
- data/vendor/prettier/plugins/acorn.d.ts +6 -0
- data/vendor/prettier/plugins/acorn.js +6618 -0
- data/vendor/prettier/plugins/acorn.mjs +6456 -0
- data/vendor/prettier/plugins/angular.d.ts +8 -0
- data/vendor/prettier/plugins/angular.js +2435 -0
- data/vendor/prettier/plugins/angular.mjs +2375 -0
- data/vendor/prettier/plugins/babel.d.ts +18 -0
- data/vendor/prettier/plugins/babel.js +14271 -0
- data/vendor/prettier/plugins/babel.mjs +13986 -0
- data/vendor/prettier/plugins/estree.d.ts +1 -0
- data/vendor/prettier/plugins/estree.js +9487 -0
- data/vendor/prettier/plugins/estree.mjs +9279 -0
- data/vendor/prettier/plugins/flow.d.ts +5 -0
- data/vendor/prettier/plugins/flow.js +51477 -0
- data/vendor/prettier/plugins/flow.mjs +51219 -0
- data/vendor/prettier/plugins/glimmer.d.ts +5 -0
- data/vendor/prettier/plugins/glimmer.js +8534 -0
- data/vendor/prettier/plugins/glimmer.mjs +8421 -0
- data/vendor/prettier/plugins/graphql.d.ts +5 -0
- data/vendor/prettier/plugins/graphql.js +2421 -0
- data/vendor/prettier/plugins/graphql.mjs +2347 -0
- data/vendor/prettier/plugins/html.d.ts +8 -0
- data/vendor/prettier/plugins/html.js +8182 -0
- data/vendor/prettier/plugins/html.mjs +8077 -0
- data/vendor/prettier/plugins/markdown.d.ts +7 -0
- data/vendor/prettier/plugins/markdown.js +9068 -0
- data/vendor/prettier/plugins/markdown.mjs +8977 -0
- data/vendor/prettier/plugins/meriyah.d.ts +5 -0
- data/vendor/prettier/plugins/meriyah.js +5953 -0
- data/vendor/prettier/plugins/meriyah.mjs +5800 -0
- data/vendor/prettier/plugins/postcss.d.ts +7 -0
- data/vendor/prettier/plugins/postcss.js +9244 -0
- data/vendor/prettier/plugins/postcss.mjs +9046 -0
- data/vendor/prettier/plugins/typescript.d.ts +5 -0
- data/vendor/prettier/plugins/typescript.js +38058 -0
- data/vendor/prettier/plugins/typescript.mjs +37732 -0
- data/vendor/prettier/plugins/yaml.d.ts +5 -0
- data/vendor/prettier/plugins/yaml.js +7425 -0
- data/vendor/prettier/plugins/yaml.mjs +7329 -0
- data/vendor/prettier/standalone.d.ts +33 -0
- data/vendor/prettier/standalone.js +3984 -0
- data/vendor/prettier/standalone.mjs +3938 -0
- data/vendor/sql-formatter.js +18762 -0
- metadata +205 -2
@@ -0,0 +1,682 @@
|
|
1
|
+
var __defProp = Object.defineProperty;
|
2
|
+
var __defNormalProp = (obj, key, value) =>
|
3
|
+
key in obj
|
4
|
+
? __defProp(obj, key, {
|
5
|
+
enumerable: true,
|
6
|
+
configurable: true,
|
7
|
+
writable: true,
|
8
|
+
value,
|
9
|
+
})
|
10
|
+
: (obj[key] = value);
|
11
|
+
var __publicField = (obj, key, value) => {
|
12
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
13
|
+
return value;
|
14
|
+
};
|
15
|
+
class Parser {
|
16
|
+
constructor(text, rules) {
|
17
|
+
__publicField(this, "text");
|
18
|
+
__publicField(this, "textLength");
|
19
|
+
__publicField(this, "textPosition");
|
20
|
+
__publicField(this, "rules");
|
21
|
+
this.text = text;
|
22
|
+
this.textLength = text ? text.length : 0;
|
23
|
+
this.textPosition = 0;
|
24
|
+
this.rules = rules || [];
|
25
|
+
}
|
26
|
+
scan() {
|
27
|
+
if (this.textPosition >= this.textLength) return null;
|
28
|
+
return this.text[this.textPosition++];
|
29
|
+
}
|
30
|
+
isDone() {
|
31
|
+
return this.textPosition >= this.textLength;
|
32
|
+
}
|
33
|
+
move(length) {
|
34
|
+
this.textPosition += length;
|
35
|
+
}
|
36
|
+
match(cb, expression, wholeword) {
|
37
|
+
var _a;
|
38
|
+
let result;
|
39
|
+
if (expression instanceof RegExp) {
|
40
|
+
const modifiedExpression = new RegExp(
|
41
|
+
"^(" + expression.source + ")",
|
42
|
+
expression.flags,
|
43
|
+
);
|
44
|
+
const text =
|
45
|
+
(_a = this.text
|
46
|
+
.substring(this.textPosition)
|
47
|
+
.match(modifiedExpression)) == null
|
48
|
+
? void 0
|
49
|
+
: _a[0];
|
50
|
+
if (text !== void 0) {
|
51
|
+
result = text;
|
52
|
+
}
|
53
|
+
} else if (expression instanceof Array) {
|
54
|
+
for (const exp of expression) {
|
55
|
+
const match = this.match(cb, exp);
|
56
|
+
if (match) {
|
57
|
+
result = match;
|
58
|
+
break;
|
59
|
+
}
|
60
|
+
}
|
61
|
+
} else if (typeof expression === "function") {
|
62
|
+
result = expression(cb, this.text.substring(this.textPosition));
|
63
|
+
} else if (typeof expression === "string") {
|
64
|
+
if (this.text.substring(this.textPosition).startsWith(expression)) {
|
65
|
+
result = expression;
|
66
|
+
}
|
67
|
+
}
|
68
|
+
if (result && wholeword) {
|
69
|
+
const prevSymbol = this.text.charAt(this.textPosition - 1);
|
70
|
+
const nextSymbol = this.text.charAt(this.textPosition + result.length);
|
71
|
+
if (
|
72
|
+
prevSymbol.match(/[A-Za-z0-9_]/) ||
|
73
|
+
nextSymbol.match(/[A-Za-z0-9_]/)
|
74
|
+
) {
|
75
|
+
result = void 0;
|
76
|
+
}
|
77
|
+
}
|
78
|
+
return result;
|
79
|
+
}
|
80
|
+
matchStart(cb, rule) {
|
81
|
+
return this.match(cb, rule.start, rule.wholeword);
|
82
|
+
}
|
83
|
+
matchEnd(cb, rule) {
|
84
|
+
return this.match(cb, rule.end, rule.wholeword);
|
85
|
+
}
|
86
|
+
matchSkip(cb, rule) {
|
87
|
+
return this.match(cb, rule.skip, rule.wholeword);
|
88
|
+
}
|
89
|
+
createDefaultTextObject(text) {
|
90
|
+
return {
|
91
|
+
type: "text",
|
92
|
+
start: text,
|
93
|
+
};
|
94
|
+
}
|
95
|
+
parse() {
|
96
|
+
var _a;
|
97
|
+
const root = { type: "root", start: "" };
|
98
|
+
const ruleStack = [];
|
99
|
+
const objStack = [root];
|
100
|
+
while (!this.isDone()) {
|
101
|
+
const activeRule = ruleStack[ruleStack.length - 1];
|
102
|
+
const activeObj = objStack[objStack.length - 1];
|
103
|
+
const activeObjLastChild =
|
104
|
+
(_a = activeObj.children) == null
|
105
|
+
? void 0
|
106
|
+
: _a[activeObj.children.length - 1];
|
107
|
+
if (activeRule) {
|
108
|
+
if (activeRule.skip) {
|
109
|
+
const activeRuleSkipText = this.matchSkip(activeObj, activeRule);
|
110
|
+
if (typeof activeRuleSkipText === "string") {
|
111
|
+
if (activeObjLastChild && activeObjLastChild.type === "text") {
|
112
|
+
activeObjLastChild.start += activeRuleSkipText;
|
113
|
+
} else {
|
114
|
+
if (!activeObj.children) {
|
115
|
+
activeObj.children = [];
|
116
|
+
}
|
117
|
+
activeObj.children.push(
|
118
|
+
this.createDefaultTextObject(activeRuleSkipText),
|
119
|
+
);
|
120
|
+
}
|
121
|
+
this.move(activeRuleSkipText.length);
|
122
|
+
continue;
|
123
|
+
}
|
124
|
+
}
|
125
|
+
if (activeRule.end) {
|
126
|
+
const activeRuleText = this.matchEnd(activeObj, activeRule);
|
127
|
+
if (typeof activeRuleText === "string") {
|
128
|
+
activeObj.end = activeRuleText;
|
129
|
+
ruleStack.pop();
|
130
|
+
objStack.pop();
|
131
|
+
this.move(activeRuleText.length);
|
132
|
+
continue;
|
133
|
+
}
|
134
|
+
}
|
135
|
+
}
|
136
|
+
if (!(activeRule == null ? void 0 : activeRule.exclusive)) {
|
137
|
+
const matchedRule = this.rules.find((r) =>
|
138
|
+
this.matchStart(activeObj, r),
|
139
|
+
);
|
140
|
+
if (matchedRule) {
|
141
|
+
const matchedRuleText = this.matchStart(activeObj, matchedRule);
|
142
|
+
if (typeof matchedRuleText === "string") {
|
143
|
+
const newObj = {
|
144
|
+
type: matchedRule.name,
|
145
|
+
start: matchedRuleText,
|
146
|
+
};
|
147
|
+
if (!activeObj.children) {
|
148
|
+
activeObj.children = [];
|
149
|
+
}
|
150
|
+
activeObj.children.push(newObj);
|
151
|
+
if (matchedRule.end) {
|
152
|
+
ruleStack.push(matchedRule);
|
153
|
+
objStack.push(newObj);
|
154
|
+
}
|
155
|
+
this.move(matchedRuleText.length);
|
156
|
+
continue;
|
157
|
+
}
|
158
|
+
}
|
159
|
+
}
|
160
|
+
const ch = this.scan();
|
161
|
+
if (ch) {
|
162
|
+
if (activeObjLastChild && activeObjLastChild.type === "text") {
|
163
|
+
activeObjLastChild.start += ch;
|
164
|
+
} else {
|
165
|
+
if (!activeObj.children) {
|
166
|
+
activeObj.children = [];
|
167
|
+
}
|
168
|
+
activeObj.children.push(this.createDefaultTextObject(ch));
|
169
|
+
}
|
170
|
+
}
|
171
|
+
}
|
172
|
+
return root;
|
173
|
+
}
|
174
|
+
}
|
175
|
+
class ParseRule {
|
176
|
+
constructor(name, options) {
|
177
|
+
__publicField(this, "name");
|
178
|
+
__publicField(this, "exclusive", false);
|
179
|
+
__publicField(this, "wholeword", false);
|
180
|
+
__publicField(this, "start");
|
181
|
+
__publicField(this, "skip");
|
182
|
+
__publicField(this, "end");
|
183
|
+
this.name = name;
|
184
|
+
this.exclusive = !!options.exclusive;
|
185
|
+
this.wholeword = !!options.wholeword;
|
186
|
+
this.start = options.start;
|
187
|
+
this.skip = options.skip;
|
188
|
+
this.end = options.end;
|
189
|
+
}
|
190
|
+
}
|
191
|
+
var GroovyParseRules = [
|
192
|
+
new ParseRule("keywordblock", {
|
193
|
+
start: (cb, text) => {
|
194
|
+
var _a;
|
195
|
+
const functionalChildren =
|
196
|
+
(_a = cb.children) == null
|
197
|
+
? void 0
|
198
|
+
: _a.filter((c) => c.type !== "whitespace");
|
199
|
+
const last1 =
|
200
|
+
functionalChildren == null ? void 0 : functionalChildren.at(-1);
|
201
|
+
const last2 =
|
202
|
+
functionalChildren == null ? void 0 : functionalChildren.at(-2);
|
203
|
+
const trimmedText = text.trimStart();
|
204
|
+
if (
|
205
|
+
(last1 == null ? void 0 : last1.type) === "round" &&
|
206
|
+
(last2 == null ? void 0 : last2.type) === "keywords" &&
|
207
|
+
!trimmedText.startsWith("{")
|
208
|
+
) {
|
209
|
+
return text.substring(0, text.length - trimmedText.length);
|
210
|
+
}
|
211
|
+
return void 0;
|
212
|
+
},
|
213
|
+
end: /\n|$|(?=})/,
|
214
|
+
}),
|
215
|
+
new ParseRule("blockcomment", { start: "/*", end: "*/", exclusive: true }),
|
216
|
+
new ParseRule("linecomment", { start: "//", end: /(?=\n)/, exclusive: true }),
|
217
|
+
new ParseRule("multilinestring", {
|
218
|
+
start: "'''",
|
219
|
+
end: "'''",
|
220
|
+
exclusive: true,
|
221
|
+
}),
|
222
|
+
new ParseRule("multilinestring", {
|
223
|
+
start: '"""',
|
224
|
+
end: '"""',
|
225
|
+
exclusive: true,
|
226
|
+
}),
|
227
|
+
new ParseRule("string", {
|
228
|
+
start: '"',
|
229
|
+
end: '"',
|
230
|
+
skip: '\\"',
|
231
|
+
exclusive: true,
|
232
|
+
}),
|
233
|
+
new ParseRule("string", {
|
234
|
+
start: "'",
|
235
|
+
end: "'",
|
236
|
+
skip: "\\'",
|
237
|
+
exclusive: true,
|
238
|
+
}),
|
239
|
+
new ParseRule("regexp", {
|
240
|
+
start: /\//g,
|
241
|
+
end: /\//g,
|
242
|
+
skip: /'/g,
|
243
|
+
exclusive: true,
|
244
|
+
}),
|
245
|
+
new ParseRule("numeric", { start: /0[xX][0-9a-fA-F]+\b/g }),
|
246
|
+
new ParseRule("numeric", {
|
247
|
+
start: /[+-]?\d+(?:(?:\.\d*)?(?:[eE][+-]?\d+)?)?\b/g,
|
248
|
+
}),
|
249
|
+
new ParseRule("boolean", { start: /(?:true|false)\b/g }),
|
250
|
+
new ParseRule("block", { start: "{", end: "}" }),
|
251
|
+
new ParseRule("square", { start: ["?[", "["], end: "]" }),
|
252
|
+
new ParseRule("round", { start: "(", end: ")" }),
|
253
|
+
new ParseRule("dot", { start: /((\?|\*|&)+)?\.(?=[^\d])(\*)?/g }),
|
254
|
+
new ParseRule("delimiters", { start: [",", ":", ";"] }),
|
255
|
+
new ParseRule("incdec", { start: ["--", "++", "**"] }),
|
256
|
+
new ParseRule("operators", {
|
257
|
+
start: [
|
258
|
+
"--",
|
259
|
+
"-=",
|
260
|
+
"->",
|
261
|
+
"-",
|
262
|
+
"::",
|
263
|
+
"!==",
|
264
|
+
"!=",
|
265
|
+
"?:",
|
266
|
+
"?=",
|
267
|
+
"?",
|
268
|
+
"..<",
|
269
|
+
"..",
|
270
|
+
"**=",
|
271
|
+
"**",
|
272
|
+
"*=",
|
273
|
+
"*",
|
274
|
+
"/=",
|
275
|
+
"/",
|
276
|
+
"&&",
|
277
|
+
"&=",
|
278
|
+
"&",
|
279
|
+
"%=",
|
280
|
+
"%",
|
281
|
+
"^=",
|
282
|
+
"^",
|
283
|
+
"++",
|
284
|
+
"+=",
|
285
|
+
"+",
|
286
|
+
"<..<",
|
287
|
+
"<..",
|
288
|
+
"<<=",
|
289
|
+
"<<",
|
290
|
+
"<=>",
|
291
|
+
"<=",
|
292
|
+
"<",
|
293
|
+
"===",
|
294
|
+
"==~",
|
295
|
+
"==",
|
296
|
+
"=~",
|
297
|
+
"=",
|
298
|
+
">=",
|
299
|
+
">>=",
|
300
|
+
">>>=",
|
301
|
+
">>",
|
302
|
+
">",
|
303
|
+
"|=",
|
304
|
+
"||",
|
305
|
+
"|",
|
306
|
+
"~",
|
307
|
+
],
|
308
|
+
}),
|
309
|
+
new ParseRule("negation", { start: "!" }),
|
310
|
+
new ParseRule("keywords", {
|
311
|
+
start: [
|
312
|
+
"abstract",
|
313
|
+
"assert",
|
314
|
+
"boolean",
|
315
|
+
"break",
|
316
|
+
"byte",
|
317
|
+
"case",
|
318
|
+
"catch",
|
319
|
+
"char",
|
320
|
+
"class",
|
321
|
+
"continue",
|
322
|
+
"default",
|
323
|
+
"def",
|
324
|
+
"double",
|
325
|
+
"do",
|
326
|
+
"else",
|
327
|
+
"enum",
|
328
|
+
"extends",
|
329
|
+
"finally",
|
330
|
+
"final",
|
331
|
+
"float",
|
332
|
+
"for",
|
333
|
+
"goto",
|
334
|
+
"if",
|
335
|
+
"implements",
|
336
|
+
"import",
|
337
|
+
"instanceof",
|
338
|
+
"interface",
|
339
|
+
"int",
|
340
|
+
"long",
|
341
|
+
"native",
|
342
|
+
"new",
|
343
|
+
"package",
|
344
|
+
"private",
|
345
|
+
"protected",
|
346
|
+
"public",
|
347
|
+
"return",
|
348
|
+
"short",
|
349
|
+
"static",
|
350
|
+
"strictfp",
|
351
|
+
"super",
|
352
|
+
"switch",
|
353
|
+
"synchronized",
|
354
|
+
"throws",
|
355
|
+
"throw",
|
356
|
+
"transient",
|
357
|
+
"try",
|
358
|
+
"void",
|
359
|
+
"volatile",
|
360
|
+
"while",
|
361
|
+
"with",
|
362
|
+
],
|
363
|
+
wholeword: true,
|
364
|
+
}),
|
365
|
+
new ParseRule("identifiers", {
|
366
|
+
start: /[a-zA-Z_$][a-zA-Z0-9_$]*\b/g,
|
367
|
+
wholeword: true,
|
368
|
+
}),
|
369
|
+
new ParseRule("whitespace", { start: /\s+/g }),
|
370
|
+
];
|
371
|
+
class Formatter {
|
372
|
+
constructor(rules, options) {
|
373
|
+
__publicField(this, "rules");
|
374
|
+
__publicField(this, "options");
|
375
|
+
this.rules = (rules == null ? void 0 : rules.map((r) => new r(this))) || [];
|
376
|
+
this.options = options || { width: 80 };
|
377
|
+
}
|
378
|
+
format(obj, indent = 0) {
|
379
|
+
let text = "";
|
380
|
+
if (obj) {
|
381
|
+
const formatRule = this.rules.find((r) => r.matches(obj));
|
382
|
+
if (obj.start !== void 0) {
|
383
|
+
if (formatRule) {
|
384
|
+
text += formatRule.formatStart(obj, indent);
|
385
|
+
}
|
386
|
+
}
|
387
|
+
if (formatRule) {
|
388
|
+
text += formatRule.formatChildren(obj, indent);
|
389
|
+
}
|
390
|
+
if (obj.end !== void 0) {
|
391
|
+
if (formatRule) {
|
392
|
+
text += formatRule.formatEnd(obj, indent);
|
393
|
+
}
|
394
|
+
}
|
395
|
+
}
|
396
|
+
return text;
|
397
|
+
}
|
398
|
+
}
|
399
|
+
function padLeft(text, indent) {
|
400
|
+
return "".padStart(indent * 4) + text;
|
401
|
+
}
|
402
|
+
function padRight(text, indent) {
|
403
|
+
return text + "".padStart(indent * 4);
|
404
|
+
}
|
405
|
+
function trimSpacesAndTabsLeft(text) {
|
406
|
+
return text.replace(/^( |\t)+/, "");
|
407
|
+
}
|
408
|
+
function trimSpacesAndTabsRight(text) {
|
409
|
+
return text.replace(/( |\t)+$/, "");
|
410
|
+
}
|
411
|
+
class FormatRule {
|
412
|
+
constructor(formatter) {
|
413
|
+
__publicField(this, "formatter");
|
414
|
+
this.formatter = formatter;
|
415
|
+
}
|
416
|
+
matches(cb, siblings) {
|
417
|
+
return true;
|
418
|
+
}
|
419
|
+
afterSelf(nextText, indent) {
|
420
|
+
return nextText;
|
421
|
+
}
|
422
|
+
beforeSelf(prevText, indent, newLine) {
|
423
|
+
return prevText;
|
424
|
+
}
|
425
|
+
beforeChild(childText, indent) {
|
426
|
+
return childText;
|
427
|
+
}
|
428
|
+
formatStart(cb, indent) {
|
429
|
+
var _a;
|
430
|
+
return (_a = cb.start) != null ? _a : "";
|
431
|
+
}
|
432
|
+
formatEnd(cb, indent) {
|
433
|
+
var _a;
|
434
|
+
return (_a = cb.end) != null ? _a : "";
|
435
|
+
}
|
436
|
+
allowBreak(cb) {
|
437
|
+
return false;
|
438
|
+
}
|
439
|
+
formatChildren(parent, indent) {
|
440
|
+
var _a;
|
441
|
+
return (
|
442
|
+
((_a = parent == null ? void 0 : parent.children) == null
|
443
|
+
? void 0
|
444
|
+
: _a.reduce((res, child, i, children) => {
|
445
|
+
var _a2, _b, _c, _d;
|
446
|
+
const childFormatRule = this.formatter.rules.find((r) =>
|
447
|
+
r.matches(child),
|
448
|
+
);
|
449
|
+
const parentFormatRule = this.formatter.rules.find((r) =>
|
450
|
+
r.matches(parent),
|
451
|
+
);
|
452
|
+
const prevFormatRule = this.formatter.rules.find((r) =>
|
453
|
+
r.matches(children[i - 1]),
|
454
|
+
);
|
455
|
+
const nextFormatRule = this.formatter.rules.find((r) =>
|
456
|
+
r.matches(children[i + 1]),
|
457
|
+
);
|
458
|
+
let childString = this.formatter.format(child, indent);
|
459
|
+
if (prevFormatRule) {
|
460
|
+
childString = prevFormatRule.afterSelf(childString, indent);
|
461
|
+
}
|
462
|
+
if (parentFormatRule) {
|
463
|
+
childString = parentFormatRule.beforeChild(childString, indent);
|
464
|
+
}
|
465
|
+
if (nextFormatRule) {
|
466
|
+
const trimmed = trimSpacesAndTabsRight(res + childString);
|
467
|
+
const newLine =
|
468
|
+
!trimmed.length ||
|
469
|
+
trimSpacesAndTabsRight(res + childString).endsWith("\n");
|
470
|
+
childString = nextFormatRule.beforeSelf(
|
471
|
+
childString,
|
472
|
+
indent,
|
473
|
+
newLine,
|
474
|
+
);
|
475
|
+
}
|
476
|
+
const lastLineLength =
|
477
|
+
(_b =
|
478
|
+
(_a2 = res.split("\n").at(-1)) == null ? void 0 : _a2.length) !=
|
479
|
+
null
|
480
|
+
? _b
|
481
|
+
: 0;
|
482
|
+
const childFirstLineLength =
|
483
|
+
(_d =
|
484
|
+
(_c = childString.split("\n").at(0)) == null
|
485
|
+
? void 0
|
486
|
+
: _c.length) != null
|
487
|
+
? _d
|
488
|
+
: 0;
|
489
|
+
if (
|
490
|
+
lastLineLength + childFirstLineLength >
|
491
|
+
this.formatter.options.width &&
|
492
|
+
(childFormatRule == null
|
493
|
+
? void 0
|
494
|
+
: childFormatRule.allowBreak(child))
|
495
|
+
) {
|
496
|
+
childString = "\n" + childString.trimStart();
|
497
|
+
if (parentFormatRule) {
|
498
|
+
childString = parentFormatRule.beforeChild(
|
499
|
+
childString,
|
500
|
+
indent + 1,
|
501
|
+
);
|
502
|
+
}
|
503
|
+
}
|
504
|
+
res += childString;
|
505
|
+
return res;
|
506
|
+
}, "")) || ""
|
507
|
+
);
|
508
|
+
}
|
509
|
+
}
|
510
|
+
class RootFormatRule extends FormatRule {
|
511
|
+
matches(cb) {
|
512
|
+
return (cb == null ? void 0 : cb.type) === "root";
|
513
|
+
}
|
514
|
+
beforeChild(childText) {
|
515
|
+
let text = childText;
|
516
|
+
const trimmedRight = trimSpacesAndTabsRight(text);
|
517
|
+
if (trimmedRight.endsWith("\n")) {
|
518
|
+
text = trimmedRight;
|
519
|
+
}
|
520
|
+
return text;
|
521
|
+
}
|
522
|
+
}
|
523
|
+
class BaseBlockRule extends FormatRule {
|
524
|
+
beforeChild(childText, indent) {
|
525
|
+
let text = childText;
|
526
|
+
const trimmedLeft = trimSpacesAndTabsLeft(text);
|
527
|
+
if (trimmedLeft.startsWith("\n")) {
|
528
|
+
text = "\n" + padLeft(trimmedLeft.trimStart(), indent);
|
529
|
+
}
|
530
|
+
const trimmedRight = trimSpacesAndTabsRight(text);
|
531
|
+
if (trimmedRight.endsWith("\n")) {
|
532
|
+
text = padRight(trimmedRight, indent);
|
533
|
+
}
|
534
|
+
return text;
|
535
|
+
}
|
536
|
+
}
|
537
|
+
class BlockFormatRule extends BaseBlockRule {
|
538
|
+
matches(cb) {
|
539
|
+
return (cb == null ? void 0 : cb.type) === "block";
|
540
|
+
}
|
541
|
+
beforeSelf(prevText, indent, newLine) {
|
542
|
+
if (!newLine) {
|
543
|
+
return prevText.trimEnd() + " ";
|
544
|
+
} else {
|
545
|
+
return prevText;
|
546
|
+
}
|
547
|
+
}
|
548
|
+
formatEnd(cb, indent) {
|
549
|
+
return cb.end ? padLeft(cb.end, indent) : "";
|
550
|
+
}
|
551
|
+
formatChildren(cb, indent) {
|
552
|
+
let blockText = super.formatChildren(cb, indent + 1);
|
553
|
+
blockText = blockText.trim();
|
554
|
+
return "\n" + padLeft(blockText, indent + 1) + "\n";
|
555
|
+
}
|
556
|
+
}
|
557
|
+
class KeywordBlockFormatRule extends BlockFormatRule {
|
558
|
+
matches(cb) {
|
559
|
+
return (cb == null ? void 0 : cb.type) === "keywordblock";
|
560
|
+
}
|
561
|
+
formatStart() {
|
562
|
+
return "{";
|
563
|
+
}
|
564
|
+
formatEnd(cb, indent) {
|
565
|
+
return padLeft("}\n", indent);
|
566
|
+
}
|
567
|
+
}
|
568
|
+
class InlineBlockFormatRule extends BaseBlockRule {
|
569
|
+
matches(cb) {
|
570
|
+
return (
|
571
|
+
(cb == null ? void 0 : cb.type) === "round" ||
|
572
|
+
(cb == null ? void 0 : cb.type) === "square"
|
573
|
+
);
|
574
|
+
}
|
575
|
+
isMultiline(cb) {
|
576
|
+
var _a;
|
577
|
+
return (_a = cb.children) == null
|
578
|
+
? void 0
|
579
|
+
: _a.some((child) => {
|
580
|
+
var _a2;
|
581
|
+
return (_a2 = child.start) == null ? void 0 : _a2.includes("\n");
|
582
|
+
});
|
583
|
+
}
|
584
|
+
beforeSelf(prevText, indent, newLine) {
|
585
|
+
if (newLine) {
|
586
|
+
return trimSpacesAndTabsRight(prevText);
|
587
|
+
} else {
|
588
|
+
return prevText;
|
589
|
+
}
|
590
|
+
}
|
591
|
+
formatEnd(cb, indent) {
|
592
|
+
var _a;
|
593
|
+
if (this.isMultiline(cb)) {
|
594
|
+
return cb.end ? padLeft(cb.end, indent) : "";
|
595
|
+
} else {
|
596
|
+
return (_a = cb.end) != null ? _a : "";
|
597
|
+
}
|
598
|
+
}
|
599
|
+
formatChildren(cb, indent) {
|
600
|
+
if (this.isMultiline(cb)) {
|
601
|
+
let blockText = super.formatChildren(cb, indent + 1);
|
602
|
+
blockText = blockText.trim();
|
603
|
+
return "\n" + padLeft(blockText, indent + 1) + "\n";
|
604
|
+
} else {
|
605
|
+
const blockText = super.formatChildren(cb, indent);
|
606
|
+
return blockText.trim();
|
607
|
+
}
|
608
|
+
}
|
609
|
+
}
|
610
|
+
class DotSyntaxFormatRule extends FormatRule {
|
611
|
+
matches(cb) {
|
612
|
+
return (cb == null ? void 0 : cb.type) === "dot";
|
613
|
+
}
|
614
|
+
beforeSelf(prevText, indent, newLine) {
|
615
|
+
if (newLine) {
|
616
|
+
return padRight(trimSpacesAndTabsRight(prevText), indent + 1);
|
617
|
+
} else {
|
618
|
+
return prevText;
|
619
|
+
}
|
620
|
+
}
|
621
|
+
}
|
622
|
+
class OperatorsRule extends FormatRule {
|
623
|
+
matches(cb) {
|
624
|
+
return (cb == null ? void 0 : cb.type) === "operators";
|
625
|
+
}
|
626
|
+
beforeSelf(prevText) {
|
627
|
+
return trimSpacesAndTabsRight(prevText) + " ";
|
628
|
+
}
|
629
|
+
afterSelf(nextText) {
|
630
|
+
return " " + nextText.trimStart();
|
631
|
+
}
|
632
|
+
allowBreak() {
|
633
|
+
return true;
|
634
|
+
}
|
635
|
+
}
|
636
|
+
class DelimitersRule extends FormatRule {
|
637
|
+
matches(cb) {
|
638
|
+
return (cb == null ? void 0 : cb.type) === "delimiters";
|
639
|
+
}
|
640
|
+
beforeSelf(prevText) {
|
641
|
+
return trimSpacesAndTabsRight(prevText);
|
642
|
+
}
|
643
|
+
afterSelf(nextText) {
|
644
|
+
return " " + trimSpacesAndTabsLeft(nextText);
|
645
|
+
}
|
646
|
+
}
|
647
|
+
class KeywordRule extends FormatRule {
|
648
|
+
matches(cb) {
|
649
|
+
return (cb == null ? void 0 : cb.type) === "keywords";
|
650
|
+
}
|
651
|
+
beforeSelf(prevText, indent, newLine) {
|
652
|
+
if (!newLine) {
|
653
|
+
return prevText.trimEnd() + " ";
|
654
|
+
} else {
|
655
|
+
return prevText;
|
656
|
+
}
|
657
|
+
}
|
658
|
+
afterSelf(nextText) {
|
659
|
+
return " " + nextText.trimStart();
|
660
|
+
}
|
661
|
+
allowBreak() {
|
662
|
+
return true;
|
663
|
+
}
|
664
|
+
}
|
665
|
+
var GroovyFormatRules = [
|
666
|
+
RootFormatRule,
|
667
|
+
BlockFormatRule,
|
668
|
+
KeywordBlockFormatRule,
|
669
|
+
InlineBlockFormatRule,
|
670
|
+
DotSyntaxFormatRule,
|
671
|
+
KeywordRule,
|
672
|
+
OperatorsRule,
|
673
|
+
DelimitersRule,
|
674
|
+
FormatRule,
|
675
|
+
];
|
676
|
+
function index(groovyCode, options) {
|
677
|
+
const parser = new Parser(groovyCode, GroovyParseRules);
|
678
|
+
const parsingResult = parser.parse();
|
679
|
+
const formatter = new Formatter(GroovyFormatRules, options);
|
680
|
+
return formatter.format(parsingResult);
|
681
|
+
}
|
682
|
+
export { index as default };
|
@@ -0,0 +1,14 @@
|
|
1
|
+
import { CodeBlock } from "../parser/types";
|
2
|
+
import { Formatter } from "./Formatter";
|
3
|
+
export default class FormatRule {
|
4
|
+
formatter: Formatter;
|
5
|
+
constructor(formatter: Formatter);
|
6
|
+
matches(cb: CodeBlock, siblings?: CodeBlock[]): boolean;
|
7
|
+
afterSelf(nextText: string, indent: number): string;
|
8
|
+
beforeSelf(prevText: string, indent: number, newLine: boolean): string;
|
9
|
+
beforeChild(childText: string, indent: number): string;
|
10
|
+
formatStart(cb: CodeBlock, indent: number): string;
|
11
|
+
formatEnd(cb: CodeBlock, indent: number): string;
|
12
|
+
allowBreak(cb: CodeBlock): boolean;
|
13
|
+
formatChildren(parent: CodeBlock, indent: number): string;
|
14
|
+
}
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import { CodeBlock } from "../parser/types";
|
2
|
+
import FormatRule from "./FormatRule";
|
3
|
+
export declare type FormatterOptions = {
|
4
|
+
width: number;
|
5
|
+
};
|
6
|
+
export declare class Formatter {
|
7
|
+
rules: FormatRule[];
|
8
|
+
options: FormatterOptions;
|
9
|
+
constructor(rules?: Array<typeof FormatRule>, options?: FormatterOptions);
|
10
|
+
format(obj: CodeBlock, indent?: number): string;
|
11
|
+
}
|