@gram-lang/format 1.4.2 → 1.5.1
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/README.md +13 -12
- package/dist/index.d.ts +1 -0
- package/dist/index.js +328 -67
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @gram-lang/format
|
|
2
2
|
|
|
3
|
-
[](https://www.npmjs.com/package/@gram-lang/format)
|
|
4
4
|
[](https://www.gnu.org/licenses/gpl-3.0)
|
|
5
5
|
|
|
6
6
|
*Part of the [Gram monorepo](https://git.gram-lang.org/gram-lang/gram).*
|
|
@@ -11,7 +11,7 @@ Canonical `.gram` source code formatter, shared by the CLI (`gram format`) and L
|
|
|
11
11
|
|
|
12
12
|
## 📚 General Documentation
|
|
13
13
|
|
|
14
|
-
For full syntax specifications, command reference, and best practices, please refer to the **[Gram Documentation](https://
|
|
14
|
+
For full syntax specifications, command reference, and best practices, please refer to the **[Gram Documentation](https://gram-lang.org/docs/)**.
|
|
15
15
|
|
|
16
16
|
---
|
|
17
17
|
|
|
@@ -52,19 +52,20 @@ if (hasChanges(changes)) {
|
|
|
52
52
|
|
|
53
53
|
* `src/index.ts`: Exports `formatGram`, `hasChanges`, and `summarizeChanges`.
|
|
54
54
|
|
|
55
|
-
The package enforces canonical formatting rules across `.gram` files (preserving frontmatter boundary integrity):
|
|
55
|
+
The package enforces canonical formatting rules across `.gram` files (preserving frontmatter boundary integrity and never altering target state text inside `={...}`):
|
|
56
56
|
1. Lowercase ingredient IDs (`@Farine` → `@farine`)
|
|
57
57
|
2. Remove space between `@id` and `{` (`@ing {10g}` → `@ing{10g}`)
|
|
58
58
|
3. Trim spaces inside braces (`{ 10g }` → `{10g}`)
|
|
59
|
-
4. Remove trailing decimal zeros (`{500.0g}` → `{500g}`)
|
|
60
|
-
5. Normalize
|
|
61
|
-
6. Normalize
|
|
62
|
-
7. Normalize
|
|
63
|
-
8. Normalize
|
|
64
|
-
9.
|
|
65
|
-
10.
|
|
66
|
-
11.
|
|
67
|
-
12.
|
|
59
|
+
4. Remove trailing decimal zeros after a point or comma (`{500.0g}` → `{500g}`, `{1,50}` → `{1,5}`, leaving `{1,000}` intact)
|
|
60
|
+
5. Normalize spacing around `+`/`-` in compound ingredient quantities (`{1 cup+2 tbsp}` → `{1 cup + 2 tbsp}`)
|
|
61
|
+
6. Normalize temperature unit spacing (`{180 °C}` → `{180°C}`)
|
|
62
|
+
7. Normalize composite ingredient separator spacing (`<@`)
|
|
63
|
+
8. Normalize arrow declaration spacing (`->&name{}`)
|
|
64
|
+
9. Normalize section header spacing (`## Header`)
|
|
65
|
+
10. Convert tabs to 4 spaces
|
|
66
|
+
11. Trim trailing whitespace line by line
|
|
67
|
+
12. Collapse consecutive blank lines to single blank lines
|
|
68
|
+
13. Ensure single newline at end of file
|
|
68
69
|
|
|
69
70
|
---
|
|
70
71
|
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
+
import { getAST } from "@gram-lang/parser";
|
|
2
3
|
function hasChanges(changes) {
|
|
3
|
-
return changes.lowercasedIds > 0 || changes.spacesBeforeBrace > 0 || changes.spacesInsideBraces > 0 || changes.trailingZeros > 0 || changes.temperatureSpacing > 0 || changes.compositeSeparatorSpacing > 0 || changes.arrowDeclarationSpacing > 0 || changes.headerSpacing > 0 || changes.tabsToSpaces > 0 || changes.consecutiveBlankLines > 0 || changes.trailingWhitespace > 0 || changes.eofNewline;
|
|
4
|
+
return changes.lowercasedIds > 0 || changes.spacesBeforeBrace > 0 || changes.spacesInsideBraces > 0 || changes.trailingZeros > 0 || changes.quantityOperatorSpacing > 0 || changes.temperatureSpacing > 0 || changes.compositeSeparatorSpacing > 0 || changes.arrowDeclarationSpacing > 0 || changes.headerSpacing > 0 || changes.tabsToSpaces > 0 || changes.consecutiveBlankLines > 0 || changes.trailingWhitespace > 0 || changes.eofNewline;
|
|
4
5
|
}
|
|
5
6
|
function summarizeChanges(changes) {
|
|
6
7
|
const parts = [];
|
|
@@ -13,6 +14,8 @@ function summarizeChanges(changes) {
|
|
|
13
14
|
parts.push(`${p(changes.spacesInsideBraces, "brace")} interior trimmed`);
|
|
14
15
|
if (changes.trailingZeros > 0)
|
|
15
16
|
parts.push(`${p(changes.trailingZeros, "trailing zero")} removed`);
|
|
17
|
+
if (changes.quantityOperatorSpacing > 0)
|
|
18
|
+
parts.push(`${p(changes.quantityOperatorSpacing, "sum")} spacing fixed`);
|
|
16
19
|
if (changes.temperatureSpacing > 0)
|
|
17
20
|
parts.push(`${p(changes.temperatureSpacing, "temperature")} spacing fixed`);
|
|
18
21
|
if (changes.compositeSeparatorSpacing > 0)
|
|
@@ -38,27 +41,271 @@ function summarizeChanges(changes) {
|
|
|
38
41
|
if (changes.eofNewline) parts.push("EOF newline added");
|
|
39
42
|
return parts.join(" \xB7 ");
|
|
40
43
|
}
|
|
44
|
+
var TRAILING_BLANKS = /[ \t]+$/;
|
|
45
|
+
var LEADING_BLANKS = /^[ \t]+/;
|
|
46
|
+
function trimBlanks(text) {
|
|
47
|
+
return text.replace(LEADING_BLANKS, "").replace(TRAILING_BLANKS, "");
|
|
48
|
+
}
|
|
49
|
+
var LINE_BREAK = /\r\n|\r|\n/g;
|
|
41
50
|
function splitFrontmatter(source) {
|
|
42
|
-
const
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
if (line.
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
51
|
+
const none = { frontmatter: "", body: source };
|
|
52
|
+
const lineBreak = new RegExp(LINE_BREAK);
|
|
53
|
+
let start = 0;
|
|
54
|
+
let first = true;
|
|
55
|
+
for (; ; ) {
|
|
56
|
+
const match = lineBreak.exec(source);
|
|
57
|
+
const end = match ? match.index : source.length;
|
|
58
|
+
const next = match ? end + match[0].length : source.length;
|
|
59
|
+
let line = source.slice(start, end).replace(TRAILING_BLANKS, "");
|
|
60
|
+
if (first) line = line.replace(/^\uFEFF/, "");
|
|
61
|
+
const isFence = line === "---";
|
|
62
|
+
if (first) {
|
|
63
|
+
if (!match || !isFence) return none;
|
|
64
|
+
first = false;
|
|
65
|
+
} else if (isFence) {
|
|
66
|
+
return { frontmatter: source.slice(0, next), body: source.slice(next) };
|
|
67
|
+
}
|
|
68
|
+
if (!match) return none;
|
|
69
|
+
start = next;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
var SUM_OPERATOR = / [+-] /;
|
|
73
|
+
var HAS_TARGET = /.(?:@&|&)/u;
|
|
74
|
+
var DECIMAL_WITH_ZEROS = /(?<![\p{L}\d.,_])(?<!\p{L}-)(\d+)([.,])(\d*?)(0+)(?![\d.,]\d)(?!\d)/gu;
|
|
75
|
+
function trimTrailingZeros(inner) {
|
|
76
|
+
const target = HAS_TARGET.exec(inner);
|
|
77
|
+
const end = target ? target.index + 1 : inner.length;
|
|
78
|
+
let count = 0;
|
|
79
|
+
const amount = inner.slice(0, end).replace(
|
|
80
|
+
DECIMAL_WITH_ZEROS,
|
|
81
|
+
(match, n, sep, digits, zeros) => {
|
|
82
|
+
if (sep === "," && digits.length + zeros.length === 3) return match;
|
|
83
|
+
count++;
|
|
84
|
+
return digits ? `${n}${sep}${digits}` : n;
|
|
85
|
+
}
|
|
86
|
+
);
|
|
87
|
+
return { text: amount + inner.slice(end), count };
|
|
88
|
+
}
|
|
89
|
+
var KIND_OF = {
|
|
90
|
+
Ingredient: "ingredient",
|
|
91
|
+
Composite: "ingredient",
|
|
92
|
+
Reference: "ingredient",
|
|
93
|
+
Timer: "timer",
|
|
94
|
+
Temperature: "temperature",
|
|
95
|
+
Cookware: "cookware",
|
|
96
|
+
Checkpoint: "checkpoint"
|
|
97
|
+
};
|
|
98
|
+
var VERBATIM = /* @__PURE__ */ new Set(["Comment", "IntermediateDecl"]);
|
|
99
|
+
function locate(source, text, at, end) {
|
|
100
|
+
if (!text) return null;
|
|
101
|
+
if (source.startsWith(text, at)) return { start: at, end: at + text.length };
|
|
102
|
+
const brace = source.indexOf("{", at);
|
|
103
|
+
if (brace === -1 || brace >= end) return null;
|
|
104
|
+
const start = skipBlanks(source, brace + 1);
|
|
105
|
+
return source.startsWith(text, start) ? { start, end: start + text.length } : null;
|
|
106
|
+
}
|
|
107
|
+
function skipBlanks(source, at) {
|
|
108
|
+
let p = at;
|
|
109
|
+
while (source[p] === " " || source[p] === " ") p++;
|
|
110
|
+
return p;
|
|
111
|
+
}
|
|
112
|
+
function locateAfterSlash(source, text, from) {
|
|
113
|
+
if (!text) return null;
|
|
114
|
+
let p = skipBlanks(source, from);
|
|
115
|
+
if (source[p] !== "/") return null;
|
|
116
|
+
p = skipBlanks(source, p + 1);
|
|
117
|
+
return source.startsWith(text, p) ? { start: p, end: p + text.length } : null;
|
|
118
|
+
}
|
|
119
|
+
function quantityNumbers(q, source) {
|
|
120
|
+
if (!q?.loc) return [];
|
|
121
|
+
const { start, end } = q.loc;
|
|
122
|
+
const found = [];
|
|
123
|
+
switch (q.type) {
|
|
124
|
+
case "Quantity":
|
|
125
|
+
found.push(
|
|
126
|
+
locate(source, q.value?.text, start, end)
|
|
127
|
+
);
|
|
128
|
+
break;
|
|
129
|
+
case "CompoundQuantity":
|
|
130
|
+
for (const term of q.terms) {
|
|
131
|
+
if (term.loc)
|
|
132
|
+
found.push(locate(source, term.value?.text, term.loc.start, end));
|
|
133
|
+
}
|
|
134
|
+
break;
|
|
135
|
+
case "RelativeQuantity":
|
|
136
|
+
found.push(locate(source, q.text, start, end));
|
|
137
|
+
break;
|
|
138
|
+
case "RatioQuantity": {
|
|
139
|
+
const numerator = q.numerator;
|
|
140
|
+
const denominator = q.denominator;
|
|
141
|
+
const duration = q.durationRaw;
|
|
142
|
+
const first = locate(
|
|
143
|
+
source,
|
|
144
|
+
duration ?? numerator.value?.text,
|
|
145
|
+
start,
|
|
146
|
+
end
|
|
147
|
+
);
|
|
148
|
+
found.push(first);
|
|
149
|
+
const unitEnd = first ? source.indexOf("/", first.end) : -1;
|
|
150
|
+
if (unitEnd !== -1 && unitEnd < end) {
|
|
151
|
+
found.push(locateAfterSlash(source, denominator.value?.text, unitEnd));
|
|
152
|
+
}
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return found.filter((r) => r !== null && r.end <= end);
|
|
157
|
+
}
|
|
158
|
+
function elementNumbers(n, source) {
|
|
159
|
+
if (n.type === "Temperature") {
|
|
160
|
+
const value = n.value;
|
|
161
|
+
if (n.text || !value || !n.loc) return [];
|
|
162
|
+
const at = locate(source, value.text, n.loc.start, n.loc.end);
|
|
163
|
+
return at ? [at] : [];
|
|
164
|
+
}
|
|
165
|
+
return quantityNumbers(n.quantity, source);
|
|
166
|
+
}
|
|
167
|
+
function anchorNumbers(retro, source) {
|
|
168
|
+
if (retro.value === null || retro.value === void 0 || !retro.loc)
|
|
169
|
+
return [];
|
|
170
|
+
const raw = retro.raw;
|
|
171
|
+
const unit = retro.unit;
|
|
172
|
+
const number = trimBlanks(
|
|
173
|
+
unit && raw.endsWith(unit) ? raw.slice(0, raw.length - unit.length) : raw
|
|
174
|
+
);
|
|
175
|
+
const at = locate(source, number, retro.loc.start, retro.loc.end);
|
|
176
|
+
return at ? [at] : [];
|
|
177
|
+
}
|
|
178
|
+
function collectSpans(node, source, spans = []) {
|
|
179
|
+
if (Array.isArray(node)) {
|
|
180
|
+
for (const child of node) collectSpans(child, source, spans);
|
|
181
|
+
return spans;
|
|
182
|
+
}
|
|
183
|
+
if (!node || typeof node !== "object") return spans;
|
|
184
|
+
const n = node;
|
|
185
|
+
const verbatim = (start, end) => spans.push({ start, end, numbers: [], compound: false });
|
|
186
|
+
const kind = n.type ? KIND_OF[n.type] : void 0;
|
|
187
|
+
if (kind && n.loc) {
|
|
188
|
+
const composite = n.composite;
|
|
189
|
+
const end = composite?.loc ? composite.loc.start : n.loc.end;
|
|
190
|
+
const quantity = n.quantity;
|
|
191
|
+
spans.push({
|
|
192
|
+
kind,
|
|
193
|
+
start: n.loc.start,
|
|
194
|
+
end,
|
|
195
|
+
numbers: elementNumbers(n, source).filter((r) => r.end <= end),
|
|
196
|
+
compound: quantity?.type === "CompoundQuantity"
|
|
197
|
+
});
|
|
198
|
+
if (composite) collectSpans(composite, source, spans);
|
|
199
|
+
return spans;
|
|
200
|
+
}
|
|
201
|
+
if (n.type && VERBATIM.has(n.type) && n.loc) {
|
|
202
|
+
verbatim(n.loc.start, n.loc.end);
|
|
203
|
+
return spans;
|
|
204
|
+
}
|
|
205
|
+
if (n.type === "Step" && n.loc && typeof n.action === "string") {
|
|
206
|
+
const open = n.loc.start;
|
|
207
|
+
const at = source[open] === "[" ? source.indexOf(n.action, open + 1) : -1;
|
|
208
|
+
const close = at === -1 ? -1 : source.indexOf("]", at + n.action.length);
|
|
209
|
+
if (close !== -1) verbatim(open, close + 1);
|
|
210
|
+
}
|
|
211
|
+
if (n.type === "Section" && n.loc && typeof n.title === "string" && n.title) {
|
|
212
|
+
const at = source.startsWith("##", n.loc.start) ? source.indexOf(n.title, n.loc.start + 2) : -1;
|
|
213
|
+
if (at !== -1) verbatim(at, at + n.title.length);
|
|
214
|
+
}
|
|
215
|
+
const retro = n.retroPlanning;
|
|
216
|
+
if (n.type === "ImportDecl" && n.loc) {
|
|
217
|
+
const line = source.slice(n.loc.start, n.loc.end);
|
|
218
|
+
const open = line.indexOf('"');
|
|
219
|
+
const quote = line.indexOf('"', open + 1);
|
|
220
|
+
if (open !== -1 && quote !== -1) {
|
|
221
|
+
verbatim(n.loc.start + open, n.loc.start + quote + 1);
|
|
222
|
+
}
|
|
223
|
+
spans.push({
|
|
224
|
+
kind: "bindings",
|
|
225
|
+
start: n.loc.start + quote + 1,
|
|
226
|
+
end: retro?.loc ? retro.loc.start : n.loc.end,
|
|
227
|
+
numbers: [],
|
|
228
|
+
compound: false
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
if (retro?.loc) {
|
|
232
|
+
spans.push({
|
|
233
|
+
kind: "timer",
|
|
234
|
+
start: retro.loc.start,
|
|
235
|
+
end: retro.loc.end,
|
|
236
|
+
numbers: anchorNumbers(retro, source),
|
|
237
|
+
compound: false
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
for (const [key, value] of Object.entries(n)) {
|
|
241
|
+
if (key !== "retroPlanning" && key !== "loc") {
|
|
242
|
+
collectSpans(value, source, spans);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return spans;
|
|
246
|
+
}
|
|
247
|
+
function parseSpans(source) {
|
|
248
|
+
try {
|
|
249
|
+
return collectSpans(getAST(source), source).sort(
|
|
250
|
+
(a, b) => a.start - b.start
|
|
251
|
+
);
|
|
252
|
+
} catch {
|
|
253
|
+
return null;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
function spaceSumOperators(inner) {
|
|
257
|
+
return inner.replace(/(?<=[\p{L}\d%.])[ \t]*\+[ \t]*(?=[\d\p{No}])/gu, " + ").replace(/(?<=[\p{L}%])[ \t]*-[ \t]*(?=[\d\p{No}])/gu, " - ");
|
|
258
|
+
}
|
|
259
|
+
function formatNumbers(span, numbers, changes) {
|
|
260
|
+
let text = span;
|
|
261
|
+
for (const { start, end } of [...numbers].sort((a, b) => b.start - a.start)) {
|
|
262
|
+
const zeros = trimTrailingZeros(text.slice(start, end));
|
|
263
|
+
changes.trailingZeros += zeros.count;
|
|
264
|
+
let after = text.slice(end);
|
|
265
|
+
const gap = /^[ \t]+(?=°)/.exec(after);
|
|
266
|
+
if (gap) {
|
|
267
|
+
changes.temperatureSpacing++;
|
|
268
|
+
after = after.slice(gap[0].length);
|
|
57
269
|
}
|
|
58
|
-
|
|
59
|
-
cursor = nextLineEnd + 1;
|
|
270
|
+
text = text.slice(0, start) + zeros.text + after;
|
|
60
271
|
}
|
|
61
|
-
return
|
|
272
|
+
return text;
|
|
273
|
+
}
|
|
274
|
+
function formatElement(original, kind, numbers, compound, changes) {
|
|
275
|
+
const span = formatNumbers(original, numbers, changes);
|
|
276
|
+
const open = span.search(/[{(]/);
|
|
277
|
+
const hasBraces = open !== -1 && span[open] === "{";
|
|
278
|
+
let head = open === -1 ? span : span.slice(0, open);
|
|
279
|
+
const rest = open === -1 ? "" : span.slice(open);
|
|
280
|
+
if (kind === "ingredient") {
|
|
281
|
+
head = head.replace(/(?<=@)([a-zA-Z][a-zA-Z0-9_-]*)/g, (match) => {
|
|
282
|
+
const lower = match.toLowerCase();
|
|
283
|
+
if (lower !== match) changes.lowercasedIds++;
|
|
284
|
+
return lower;
|
|
285
|
+
});
|
|
286
|
+
if (hasBraces) {
|
|
287
|
+
head = head.replace(/(@[a-zA-Z][a-zA-Z0-9_-]*)[ \t]+$/, (_, id) => {
|
|
288
|
+
changes.spacesBeforeBrace++;
|
|
289
|
+
return id;
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
if (!hasBraces) return head + rest;
|
|
294
|
+
const braces = /^\{([^{}\r\n]*)\}/.exec(rest);
|
|
295
|
+
if (!braces) return head + rest;
|
|
296
|
+
return `${head}{${formatBraces(braces[1] ?? "", kind, compound, changes)}}${rest.slice(braces[0].length)}`;
|
|
297
|
+
}
|
|
298
|
+
function formatBraces(original, kind, compound, changes) {
|
|
299
|
+
const inner = trimBlanks(original);
|
|
300
|
+
if (inner !== original) changes.spacesInsideBraces++;
|
|
301
|
+
if (kind === "ingredient" && compound && !HAS_TARGET.test(inner)) {
|
|
302
|
+
const spaced = spaceSumOperators(inner);
|
|
303
|
+
if (SUM_OPERATOR.test(spaced) && spaced !== inner) {
|
|
304
|
+
changes.quantityOperatorSpacing++;
|
|
305
|
+
return spaced;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
return inner;
|
|
62
309
|
}
|
|
63
310
|
function formatGram(source) {
|
|
64
311
|
const changes = {
|
|
@@ -66,6 +313,7 @@ function formatGram(source) {
|
|
|
66
313
|
spacesBeforeBrace: 0,
|
|
67
314
|
spacesInsideBraces: 0,
|
|
68
315
|
trailingZeros: 0,
|
|
316
|
+
quantityOperatorSpacing: 0,
|
|
69
317
|
temperatureSpacing: 0,
|
|
70
318
|
compositeSeparatorSpacing: 0,
|
|
71
319
|
arrowDeclarationSpacing: 0,
|
|
@@ -77,63 +325,76 @@ function formatGram(source) {
|
|
|
77
325
|
};
|
|
78
326
|
const { frontmatter, body: originalBody } = splitFrontmatter(source);
|
|
79
327
|
let body = originalBody;
|
|
80
|
-
body = body.replace(/
|
|
81
|
-
const lower = match.toLowerCase();
|
|
82
|
-
if (lower !== match) changes.lowercasedIds++;
|
|
83
|
-
return lower;
|
|
84
|
-
});
|
|
85
|
-
body = body.replace(/@([a-zA-Z][a-zA-Z0-9_-]*)\s+\{/g, (_, id) => {
|
|
86
|
-
changes.spacesBeforeBrace++;
|
|
87
|
-
return `@${id}{`;
|
|
88
|
-
});
|
|
89
|
-
body = body.replace(/\{([^}]*)\}/g, (_, inner) => {
|
|
90
|
-
const trimmed = inner.trim();
|
|
91
|
-
if (trimmed !== inner) changes.spacesInsideBraces++;
|
|
92
|
-
return `{${trimmed}}`;
|
|
93
|
-
});
|
|
94
|
-
body = body.replace(/\{([^}]*)\}/g, (_, inner) => {
|
|
95
|
-
const fixed = inner.replace(/(\d+)\s+°/g, (_m, n) => {
|
|
96
|
-
changes.temperatureSpacing++;
|
|
97
|
-
return `${n}\xB0`;
|
|
98
|
-
});
|
|
99
|
-
return `{${fixed}}`;
|
|
100
|
-
});
|
|
101
|
-
body = body.replace(/\{(\d+)\.0+([a-zA-Z°%]*)\}/g, (_, n, u) => {
|
|
102
|
-
changes.trailingZeros++;
|
|
103
|
-
return `{${n}${u}}`;
|
|
104
|
-
});
|
|
105
|
-
body = body.replace(/\{(\d+\.\d*[1-9])0+([a-zA-Z°%]*)\}/g, (_, n, u) => {
|
|
106
|
-
changes.trailingZeros++;
|
|
107
|
-
return `{${n}${u}}`;
|
|
108
|
-
});
|
|
109
|
-
body = body.replace(/ *< *@/g, (match) => {
|
|
328
|
+
body = body.replace(/[ \t]*<[ \t]*@/g, (match) => {
|
|
110
329
|
if (match !== "<@") changes.compositeSeparatorSpacing++;
|
|
111
330
|
return "<@";
|
|
112
331
|
});
|
|
113
|
-
body = body.replace(/->&([^{\n]+?)\
|
|
332
|
+
body = body.replace(/->&([^{\r\n]+?)[ \t]+\{\}/g, (_, name) => {
|
|
114
333
|
changes.arrowDeclarationSpacing++;
|
|
115
334
|
return `->&${name}{}`;
|
|
116
335
|
});
|
|
117
336
|
body = body.replace(/^(##(?!#))[ \t]*(?=\S)/gm, (match, hashes) => {
|
|
118
|
-
const
|
|
119
|
-
if (match !==
|
|
120
|
-
return
|
|
121
|
-
});
|
|
122
|
-
body = body.replace(/\t/g, () => {
|
|
123
|
-
changes.tabsToSpaces++;
|
|
124
|
-
return " ";
|
|
337
|
+
const normalized2 = `${hashes} `;
|
|
338
|
+
if (match !== normalized2) changes.headerSpacing++;
|
|
339
|
+
return normalized2;
|
|
125
340
|
});
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
341
|
+
const repaired = frontmatter + body;
|
|
342
|
+
const segments = [
|
|
343
|
+
{ text: frontmatter, verbatim: false }
|
|
344
|
+
];
|
|
345
|
+
let cursor = frontmatter.length;
|
|
346
|
+
for (const span of parseSpans(repaired) ?? []) {
|
|
347
|
+
if (span.start < cursor) continue;
|
|
348
|
+
segments.push({
|
|
349
|
+
text: repaired.slice(cursor, span.start),
|
|
350
|
+
verbatim: false
|
|
351
|
+
});
|
|
352
|
+
const text = repaired.slice(span.start, span.end);
|
|
353
|
+
segments.push({
|
|
354
|
+
text: span.kind ? formatElement(
|
|
355
|
+
text,
|
|
356
|
+
span.kind,
|
|
357
|
+
span.numbers.map(({ start, end }) => ({
|
|
358
|
+
start: start - span.start,
|
|
359
|
+
end: end - span.start
|
|
360
|
+
})),
|
|
361
|
+
span.compound,
|
|
362
|
+
changes
|
|
363
|
+
) : text,
|
|
364
|
+
verbatim: true
|
|
365
|
+
});
|
|
366
|
+
cursor = span.end;
|
|
367
|
+
}
|
|
368
|
+
segments.push({ text: repaired.slice(cursor), verbatim: false });
|
|
369
|
+
const last = segments.length - 1;
|
|
370
|
+
const normalized = segments.map(({ text, verbatim }, i) => {
|
|
371
|
+
if (verbatim) {
|
|
372
|
+
return text.replace(/\r\n?/g, () => {
|
|
373
|
+
changes.trailingWhitespace++;
|
|
374
|
+
return "\n";
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
let out = text;
|
|
378
|
+
if (i > 0) {
|
|
379
|
+
out = out.replace(/\t/g, () => {
|
|
380
|
+
changes.tabsToSpaces++;
|
|
381
|
+
return " ";
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
out = out.replace(
|
|
385
|
+
i === last ? /[ \t]*(?:\r\n|\r|\n)|[ \t]+$/g : /[ \t]*(?:\r\n|\r|\n)/g,
|
|
386
|
+
(match) => {
|
|
387
|
+
if (match !== "\n") changes.trailingWhitespace++;
|
|
388
|
+
return /[\r\n]$/.test(match) ? "\n" : "";
|
|
389
|
+
}
|
|
390
|
+
);
|
|
391
|
+
return out.replace(/\n{3,}/g, () => {
|
|
392
|
+
changes.consecutiveBlankLines++;
|
|
393
|
+
return "\n\n";
|
|
394
|
+
});
|
|
135
395
|
});
|
|
136
|
-
|
|
396
|
+
let content = normalized.join("");
|
|
397
|
+
const trimmedEnd = content.replace(/\n+$/, "");
|
|
137
398
|
const withEof = `${trimmedEnd}
|
|
138
399
|
`;
|
|
139
400
|
if (withEof !== content) changes.eofNewline = true;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gram-lang/format",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.1",
|
|
4
4
|
"description": "Canonical .gram source formatter, shared by the CLI and language server",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -33,8 +33,8 @@
|
|
|
33
33
|
"test": "bun test --only-failures",
|
|
34
34
|
"prepublishOnly": "npm run build"
|
|
35
35
|
},
|
|
36
|
-
"
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
36
|
+
"type": "module",
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@gram-lang/parser": "1.5.1"
|
|
39
|
+
}
|
|
40
40
|
}
|