@gram-lang/format 1.4.1 → 1.5.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/README.md +13 -12
- package/dist/index.d.ts +1 -0
- package/dist/index.js +54 -10
- package/package.json +2 -2
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,6 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
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;
|
|
3
|
+
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
4
|
}
|
|
5
5
|
function summarizeChanges(changes) {
|
|
6
6
|
const parts = [];
|
|
@@ -13,6 +13,8 @@ function summarizeChanges(changes) {
|
|
|
13
13
|
parts.push(`${p(changes.spacesInsideBraces, "brace")} interior trimmed`);
|
|
14
14
|
if (changes.trailingZeros > 0)
|
|
15
15
|
parts.push(`${p(changes.trailingZeros, "trailing zero")} removed`);
|
|
16
|
+
if (changes.quantityOperatorSpacing > 0)
|
|
17
|
+
parts.push(`${p(changes.quantityOperatorSpacing, "sum")} spacing fixed`);
|
|
16
18
|
if (changes.temperatureSpacing > 0)
|
|
17
19
|
parts.push(`${p(changes.temperatureSpacing, "temperature")} spacing fixed`);
|
|
18
20
|
if (changes.compositeSeparatorSpacing > 0)
|
|
@@ -60,12 +62,46 @@ function splitFrontmatter(source) {
|
|
|
60
62
|
}
|
|
61
63
|
return { frontmatter: "", body: source };
|
|
62
64
|
}
|
|
65
|
+
var SUM_OPERATOR = / [+-] /;
|
|
66
|
+
var HAS_TARGET = /.(?:@&|&)/u;
|
|
67
|
+
var DECIMAL_WITH_ZEROS = /(?<![\p{L}\d.,_])(?<!\p{L}-)(\d+)([.,])(\d*?)(0+)(?![\d.,]\d)(?!\d)/gu;
|
|
68
|
+
function trimTrailingZeros(inner) {
|
|
69
|
+
const target = HAS_TARGET.exec(inner);
|
|
70
|
+
const end = target ? target.index + 1 : inner.length;
|
|
71
|
+
let count = 0;
|
|
72
|
+
const amount = inner.slice(0, end).replace(
|
|
73
|
+
DECIMAL_WITH_ZEROS,
|
|
74
|
+
(match, n, sep, digits, zeros) => {
|
|
75
|
+
if (sep === "," && digits.length + zeros.length === 3) return match;
|
|
76
|
+
count++;
|
|
77
|
+
return digits ? `${n}${sep}${digits}` : n;
|
|
78
|
+
}
|
|
79
|
+
);
|
|
80
|
+
return { text: amount + inner.slice(end), count };
|
|
81
|
+
}
|
|
82
|
+
function braceOwner(body, offset) {
|
|
83
|
+
if (body[offset - 1] === "=") return "checkpoint";
|
|
84
|
+
for (let i = offset - 1; i >= 0; i--) {
|
|
85
|
+
const c = body[i];
|
|
86
|
+
if (c === "\n" || c === "}" || c === "{") return "other";
|
|
87
|
+
if (c === "@" || c === "&") return "ingredient";
|
|
88
|
+
if (c === "~") return "timer";
|
|
89
|
+
if (c === "^") return "temperature";
|
|
90
|
+
if (c === "#") return "cookware";
|
|
91
|
+
}
|
|
92
|
+
return "other";
|
|
93
|
+
}
|
|
94
|
+
var isCheckpoint = (body, offset) => braceOwner(body, offset) === "checkpoint";
|
|
95
|
+
function spaceSumOperators(inner) {
|
|
96
|
+
return inner.replace(/(?<=[\p{L}\d%.])\s*\+\s*(?=[\d\p{No}])/gu, " + ").replace(/(?<=[\p{L}%])\s*-\s*(?=[\d\p{No}])/gu, " - ");
|
|
97
|
+
}
|
|
63
98
|
function formatGram(source) {
|
|
64
99
|
const changes = {
|
|
65
100
|
lowercasedIds: 0,
|
|
66
101
|
spacesBeforeBrace: 0,
|
|
67
102
|
spacesInsideBraces: 0,
|
|
68
103
|
trailingZeros: 0,
|
|
104
|
+
quantityOperatorSpacing: 0,
|
|
69
105
|
temperatureSpacing: 0,
|
|
70
106
|
compositeSeparatorSpacing: 0,
|
|
71
107
|
arrowDeclarationSpacing: 0,
|
|
@@ -91,21 +127,29 @@ function formatGram(source) {
|
|
|
91
127
|
if (trimmed !== inner) changes.spacesInsideBraces++;
|
|
92
128
|
return `{${trimmed}}`;
|
|
93
129
|
});
|
|
94
|
-
body = body.replace(/\{(
|
|
95
|
-
|
|
96
|
-
return `{${n}${u}}`;
|
|
97
|
-
});
|
|
98
|
-
body = body.replace(/\{(\d+\.\d*[1-9])0+([a-zA-Z°%]*)\}/g, (_, n, u) => {
|
|
99
|
-
changes.trailingZeros++;
|
|
100
|
-
return `{${n}${u}}`;
|
|
101
|
-
});
|
|
102
|
-
body = body.replace(/\{([^}]*)\}/g, (_, inner) => {
|
|
130
|
+
body = body.replace(/\{([^}]*)\}/g, (match, inner, offset) => {
|
|
131
|
+
if (isCheckpoint(body, offset)) return match;
|
|
103
132
|
const fixed = inner.replace(/(\d+)\s+°/g, (_m, n) => {
|
|
104
133
|
changes.temperatureSpacing++;
|
|
105
134
|
return `${n}\xB0`;
|
|
106
135
|
});
|
|
107
136
|
return `{${fixed}}`;
|
|
108
137
|
});
|
|
138
|
+
body = body.replace(/\{([^{}\n]*)\}/g, (match, inner, offset) => {
|
|
139
|
+
if (isCheckpoint(body, offset)) return match;
|
|
140
|
+
const { text, count } = trimTrailingZeros(inner);
|
|
141
|
+
if (count === 0) return match;
|
|
142
|
+
changes.trailingZeros += count;
|
|
143
|
+
return `{${text}}`;
|
|
144
|
+
});
|
|
145
|
+
body = body.replace(/\{([^{}\n]*)\}/g, (match, inner, offset) => {
|
|
146
|
+
if (braceOwner(body, offset) !== "ingredient") return match;
|
|
147
|
+
if (HAS_TARGET.test(inner)) return match;
|
|
148
|
+
const spaced = spaceSumOperators(inner);
|
|
149
|
+
if (!SUM_OPERATOR.test(spaced) || spaced === inner) return match;
|
|
150
|
+
changes.quantityOperatorSpacing++;
|
|
151
|
+
return `{${spaced}}`;
|
|
152
|
+
});
|
|
109
153
|
body = body.replace(/ *< *@/g, (match) => {
|
|
110
154
|
if (match !== "<@") changes.compositeSeparatorSpacing++;
|
|
111
155
|
return "<@";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gram-lang/format",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
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",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"prepublishOnly": "npm run build"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@gram-lang/parser": "1.
|
|
37
|
+
"@gram-lang/parser": "1.5.0"
|
|
38
38
|
},
|
|
39
39
|
"type": "module"
|
|
40
40
|
}
|