@driftsys/markspec 0.12.0 → 0.13.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/esm/ast/build.d.ts.map +1 -1
- package/esm/ast/build.js +19 -5
- package/esm/ast/equivalence.js +14 -2
- package/esm/ast/nodes.d.ts +15 -3
- package/esm/ast/nodes.d.ts.map +1 -1
- package/esm/compiler/inverses.d.ts.map +1 -1
- package/esm/compiler/inverses.js +18 -3
- package/esm/compiler/mod.d.ts +34 -1
- package/esm/compiler/mod.d.ts.map +1 -1
- package/esm/compiler/mod.js +112 -48
- package/esm/compiler/schema.d.ts +3 -2
- package/esm/compiler/schema.d.ts.map +1 -1
- package/esm/compiler/schema.js +6 -3
- package/esm/config/markspec.d.ts +2 -2
- package/esm/config/mod.d.ts.map +1 -1
- package/esm/config/mod.js +47 -5
- package/esm/criteria/scenarios.d.ts +55 -0
- package/esm/criteria/scenarios.d.ts.map +1 -0
- package/esm/criteria/scenarios.js +133 -0
- package/esm/discovery/mod.d.ts +20 -1
- package/esm/discovery/mod.d.ts.map +1 -1
- package/esm/discovery/mod.js +105 -17
- package/esm/discovery/projects.d.ts +6 -4
- package/esm/discovery/projects.d.ts.map +1 -1
- package/esm/discovery/projects.js +27 -17
- package/esm/formatter/entry_body.d.ts +13 -12
- package/esm/formatter/entry_body.d.ts.map +1 -1
- package/esm/formatter/entry_body.js +25 -17
- package/esm/formatter/feature_file.d.ts +40 -0
- package/esm/formatter/feature_file.d.ts.map +1 -0
- package/esm/formatter/feature_file.js +88 -0
- package/esm/formatter/md_equiv.d.ts +6 -5
- package/esm/formatter/md_equiv.d.ts.map +1 -1
- package/esm/formatter/md_equiv.js +6 -5
- package/esm/formatter/mod.d.ts +25 -3
- package/esm/formatter/mod.d.ts.map +1 -1
- package/esm/formatter/mod.js +267 -27
- package/esm/formatter/tab_markers.d.ts +15 -0
- package/esm/formatter/tab_markers.d.ts.map +1 -0
- package/esm/formatter/tab_markers.js +133 -0
- package/esm/gates/mod.d.ts +2 -2
- package/esm/gates/mod.d.ts.map +1 -1
- package/esm/gates/mod.js +18 -3
- package/esm/gherkin/load.d.ts +35 -0
- package/esm/gherkin/load.d.ts.map +1 -0
- package/esm/gherkin/load.js +39 -0
- package/esm/gherkin/parse.d.ts +11 -0
- package/esm/gherkin/parse.d.ts.map +1 -0
- package/esm/gherkin/parse.js +311 -0
- package/esm/gherkin/position.d.ts +23 -0
- package/esm/gherkin/position.d.ts.map +1 -0
- package/esm/gherkin/position.js +21 -0
- package/esm/gherkin/procedure.d.ts +76 -0
- package/esm/gherkin/procedure.d.ts.map +1 -0
- package/esm/gherkin/procedure.js +164 -0
- package/esm/gherkin/project.d.ts +35 -0
- package/esm/gherkin/project.d.ts.map +1 -0
- package/esm/gherkin/project.js +64 -0
- package/esm/gherkin/role.d.ts +23 -0
- package/esm/gherkin/role.d.ts.map +1 -0
- package/esm/gherkin/role.js +28 -0
- package/esm/gherkin/types.d.ts +118 -0
- package/esm/gherkin/types.d.ts.map +1 -0
- package/esm/gherkin/types.js +12 -0
- package/esm/lock/acquire_compile.d.ts +12 -4
- package/esm/lock/acquire_compile.d.ts.map +1 -1
- package/esm/lock/acquire_compile.js +15 -5
- package/esm/lock/canonical_edges.d.ts +6 -2
- package/esm/lock/canonical_edges.d.ts.map +1 -1
- package/esm/lock/canonical_edges.js +10 -3
- package/esm/lock/model.d.ts +5 -0
- package/esm/lock/model.d.ts.map +1 -1
- package/esm/lock/parser.d.ts.map +1 -1
- package/esm/lock/parser.js +2 -0
- package/esm/lock/resolve.d.ts +6 -2
- package/esm/lock/resolve.d.ts.map +1 -1
- package/esm/lock/resolve.js +36 -4
- package/esm/lock/serializer.d.ts.map +1 -1
- package/esm/lock/serializer.js +7 -1
- package/esm/lock/upstream_deps.js +1 -1
- package/esm/mod.d.ts +24 -6
- package/esm/mod.d.ts.map +1 -1
- package/esm/mod.js +19 -5
- package/esm/model/mod.d.ts +43 -2
- package/esm/model/mod.d.ts.map +1 -1
- package/esm/model/mod.js +1 -1
- package/esm/model/relations.d.ts +9 -0
- package/esm/model/relations.d.ts.map +1 -1
- package/esm/model/relations.js +16 -1
- package/esm/parser/attributes.d.ts +82 -3
- package/esm/parser/attributes.d.ts.map +1 -1
- package/esm/parser/attributes.js +199 -32
- package/esm/parser/body_tokens.d.ts +1 -1
- package/esm/parser/body_tokens.d.ts.map +1 -1
- package/esm/parser/body_tokens.js +49 -35
- package/esm/parser/feature_file.d.ts +116 -0
- package/esm/parser/feature_file.d.ts.map +1 -0
- package/esm/parser/feature_file.js +540 -0
- package/esm/parser/markdown.d.ts +7 -0
- package/esm/parser/markdown.d.ts.map +1 -1
- package/esm/parser/markdown.js +129 -40
- package/esm/parser/mod.d.ts +18 -0
- package/esm/parser/mod.d.ts.map +1 -1
- package/esm/parser/mod.js +51 -0
- package/esm/parser/translate.d.ts.map +1 -1
- package/esm/parser/translate.js +5 -0
- package/esm/refs/mod.d.ts +8 -3
- package/esm/refs/mod.d.ts.map +1 -1
- package/esm/refs/mod.js +20 -7
- package/esm/refs/split.d.ts +58 -0
- package/esm/refs/split.d.ts.map +1 -0
- package/esm/refs/split.js +137 -0
- package/esm/reporter/mod.d.ts +7 -0
- package/esm/reporter/mod.d.ts.map +1 -1
- package/esm/reporter/mod.js +113 -3
- package/esm/sync/mapping.d.ts +4 -3
- package/esm/sync/mapping.d.ts.map +1 -1
- package/esm/sync/mapping.js +52 -9
- package/esm/util/display_id.d.ts +20 -0
- package/esm/util/display_id.d.ts.map +1 -0
- package/esm/util/display_id.js +19 -0
- package/esm/validator/attributes.d.ts.map +1 -1
- package/esm/validator/attributes.js +4 -1
- package/esm/validator/block_role.d.ts +40 -0
- package/esm/validator/block_role.d.ts.map +1 -0
- package/esm/validator/block_role.js +109 -0
- package/esm/validator/feature_ac.d.ts.map +1 -1
- package/esm/validator/feature_ac.js +6 -1
- package/esm/validator/feature_parse.d.ts +35 -0
- package/esm/validator/feature_parse.d.ts.map +1 -0
- package/esm/validator/feature_parse.js +69 -0
- package/esm/validator/locators.d.ts +36 -0
- package/esm/validator/locators.d.ts.map +1 -0
- package/esm/validator/locators.js +184 -0
- package/esm/validator/mod.d.ts +10 -1
- package/esm/validator/mod.d.ts.map +1 -1
- package/esm/validator/mod.js +38 -14
- package/esm/validator/normalize.d.ts.map +1 -1
- package/esm/validator/normalize.js +8 -3
- package/esm/validator/per_type_attrs.d.ts +3 -3
- package/esm/validator/per_type_attrs.js +3 -3
- package/esm/validator/pipeline.d.ts +10 -16
- package/esm/validator/pipeline.d.ts.map +1 -1
- package/esm/validator/pipeline.js +37 -16
- package/esm/validator/procedure.d.ts +22 -0
- package/esm/validator/procedure.d.ts.map +1 -0
- package/esm/validator/procedure.js +58 -0
- package/esm/validator/trace_types.d.ts.map +1 -1
- package/esm/validator/trace_types.js +4 -3
- package/esm/validator/traceability.d.ts.map +1 -1
- package/esm/validator/traceability.js +4 -1
- package/esm/validator/type_resolution.d.ts +5 -3
- package/esm/validator/type_resolution.d.ts.map +1 -1
- package/esm/validator/type_resolution.js +14 -2
- package/esm/validator/types.d.ts +11 -0
- package/esm/validator/types.d.ts.map +1 -1
- package/esm/validator/types.js +26 -4
- package/esm/validator/uxil_family.d.ts +1 -1
- package/package.json +3 -1
- package/script/ast/build.d.ts.map +1 -1
- package/script/ast/build.js +19 -5
- package/script/ast/equivalence.js +14 -2
- package/script/ast/nodes.d.ts +15 -3
- package/script/ast/nodes.d.ts.map +1 -1
- package/script/compiler/inverses.d.ts.map +1 -1
- package/script/compiler/inverses.js +18 -3
- package/script/compiler/mod.d.ts +34 -1
- package/script/compiler/mod.d.ts.map +1 -1
- package/script/compiler/mod.js +110 -46
- package/script/compiler/schema.d.ts +3 -2
- package/script/compiler/schema.d.ts.map +1 -1
- package/script/compiler/schema.js +6 -3
- package/script/config/markspec.d.ts +2 -2
- package/script/config/mod.d.ts.map +1 -1
- package/script/config/mod.js +47 -5
- package/script/criteria/scenarios.d.ts +55 -0
- package/script/criteria/scenarios.d.ts.map +1 -0
- package/script/criteria/scenarios.js +139 -0
- package/script/discovery/mod.d.ts +20 -1
- package/script/discovery/mod.d.ts.map +1 -1
- package/script/discovery/mod.js +106 -17
- package/script/discovery/projects.d.ts +6 -4
- package/script/discovery/projects.d.ts.map +1 -1
- package/script/discovery/projects.js +27 -17
- package/script/formatter/entry_body.d.ts +13 -12
- package/script/formatter/entry_body.d.ts.map +1 -1
- package/script/formatter/entry_body.js +25 -17
- package/script/formatter/feature_file.d.ts +40 -0
- package/script/formatter/feature_file.d.ts.map +1 -0
- package/script/formatter/feature_file.js +92 -0
- package/script/formatter/md_equiv.d.ts +6 -5
- package/script/formatter/md_equiv.d.ts.map +1 -1
- package/script/formatter/md_equiv.js +6 -5
- package/script/formatter/mod.d.ts +25 -3
- package/script/formatter/mod.d.ts.map +1 -1
- package/script/formatter/mod.js +266 -26
- package/script/formatter/tab_markers.d.ts +15 -0
- package/script/formatter/tab_markers.d.ts.map +1 -0
- package/script/formatter/tab_markers.js +141 -0
- package/script/gates/mod.d.ts +2 -2
- package/script/gates/mod.d.ts.map +1 -1
- package/script/gates/mod.js +18 -3
- package/script/gherkin/load.d.ts +35 -0
- package/script/gherkin/load.d.ts.map +1 -0
- package/script/gherkin/load.js +76 -0
- package/script/gherkin/parse.d.ts +11 -0
- package/script/gherkin/parse.d.ts.map +1 -0
- package/script/gherkin/parse.js +314 -0
- package/script/gherkin/position.d.ts +23 -0
- package/script/gherkin/position.d.ts.map +1 -0
- package/script/gherkin/position.js +24 -0
- package/script/gherkin/procedure.d.ts +76 -0
- package/script/gherkin/procedure.d.ts.map +1 -0
- package/script/gherkin/procedure.js +169 -0
- package/script/gherkin/project.d.ts +35 -0
- package/script/gherkin/project.d.ts.map +1 -0
- package/script/gherkin/project.js +67 -0
- package/script/gherkin/role.d.ts +23 -0
- package/script/gherkin/role.d.ts.map +1 -0
- package/script/gherkin/role.js +31 -0
- package/script/gherkin/types.d.ts +118 -0
- package/script/gherkin/types.d.ts.map +1 -0
- package/script/gherkin/types.js +13 -0
- package/script/lock/acquire_compile.d.ts +12 -4
- package/script/lock/acquire_compile.d.ts.map +1 -1
- package/script/lock/acquire_compile.js +15 -5
- package/script/lock/canonical_edges.d.ts +6 -2
- package/script/lock/canonical_edges.d.ts.map +1 -1
- package/script/lock/canonical_edges.js +10 -3
- package/script/lock/model.d.ts +5 -0
- package/script/lock/model.d.ts.map +1 -1
- package/script/lock/parser.d.ts.map +1 -1
- package/script/lock/parser.js +2 -0
- package/script/lock/resolve.d.ts +6 -2
- package/script/lock/resolve.d.ts.map +1 -1
- package/script/lock/resolve.js +36 -4
- package/script/lock/serializer.d.ts.map +1 -1
- package/script/lock/serializer.js +7 -1
- package/script/lock/upstream_deps.js +1 -1
- package/script/mod.d.ts +24 -6
- package/script/mod.d.ts.map +1 -1
- package/script/mod.js +48 -6
- package/script/model/mod.d.ts +43 -2
- package/script/model/mod.d.ts.map +1 -1
- package/script/model/mod.js +2 -1
- package/script/model/relations.d.ts +9 -0
- package/script/model/relations.d.ts.map +1 -1
- package/script/model/relations.js +17 -1
- package/script/parser/attributes.d.ts +82 -3
- package/script/parser/attributes.d.ts.map +1 -1
- package/script/parser/attributes.js +205 -33
- package/script/parser/body_tokens.d.ts +1 -1
- package/script/parser/body_tokens.d.ts.map +1 -1
- package/script/parser/body_tokens.js +49 -35
- package/script/parser/feature_file.d.ts +116 -0
- package/script/parser/feature_file.d.ts.map +1 -0
- package/script/parser/feature_file.js +548 -0
- package/script/parser/markdown.d.ts +7 -0
- package/script/parser/markdown.d.ts.map +1 -1
- package/script/parser/markdown.js +128 -39
- package/script/parser/mod.d.ts +18 -0
- package/script/parser/mod.d.ts.map +1 -1
- package/script/parser/mod.js +52 -1
- package/script/parser/translate.d.ts.map +1 -1
- package/script/parser/translate.js +5 -0
- package/script/refs/mod.d.ts +8 -3
- package/script/refs/mod.d.ts.map +1 -1
- package/script/refs/mod.js +20 -7
- package/script/refs/split.d.ts +58 -0
- package/script/refs/split.d.ts.map +1 -0
- package/script/refs/split.js +145 -0
- package/script/reporter/mod.d.ts +7 -0
- package/script/reporter/mod.d.ts.map +1 -1
- package/script/reporter/mod.js +113 -3
- package/script/sync/mapping.d.ts +4 -3
- package/script/sync/mapping.d.ts.map +1 -1
- package/script/sync/mapping.js +52 -9
- package/script/util/display_id.d.ts +20 -0
- package/script/util/display_id.d.ts.map +1 -0
- package/script/util/display_id.js +22 -0
- package/script/validator/attributes.d.ts.map +1 -1
- package/script/validator/attributes.js +4 -1
- package/script/validator/block_role.d.ts +40 -0
- package/script/validator/block_role.d.ts.map +1 -0
- package/script/validator/block_role.js +112 -0
- package/script/validator/feature_ac.d.ts.map +1 -1
- package/script/validator/feature_ac.js +6 -1
- package/script/validator/feature_parse.d.ts +35 -0
- package/script/validator/feature_parse.d.ts.map +1 -0
- package/script/validator/feature_parse.js +73 -0
- package/script/validator/locators.d.ts +36 -0
- package/script/validator/locators.d.ts.map +1 -0
- package/script/validator/locators.js +188 -0
- package/script/validator/mod.d.ts +10 -1
- package/script/validator/mod.d.ts.map +1 -1
- package/script/validator/mod.js +38 -14
- package/script/validator/normalize.d.ts.map +1 -1
- package/script/validator/normalize.js +8 -3
- package/script/validator/per_type_attrs.d.ts +3 -3
- package/script/validator/per_type_attrs.js +3 -3
- package/script/validator/pipeline.d.ts +10 -16
- package/script/validator/pipeline.d.ts.map +1 -1
- package/script/validator/pipeline.js +36 -16
- package/script/validator/procedure.d.ts +22 -0
- package/script/validator/procedure.d.ts.map +1 -0
- package/script/validator/procedure.js +61 -0
- package/script/validator/trace_types.d.ts.map +1 -1
- package/script/validator/trace_types.js +4 -3
- package/script/validator/traceability.d.ts.map +1 -1
- package/script/validator/traceability.js +4 -1
- package/script/validator/type_resolution.d.ts +5 -3
- package/script/validator/type_resolution.d.ts.map +1 -1
- package/script/validator/type_resolution.js +14 -2
- package/script/validator/types.d.ts +11 -0
- package/script/validator/types.d.ts.map +1 -1
- package/script/validator/types.js +27 -4
- package/script/validator/uxil_family.d.ts +1 -1
package/esm/formatter/mod.js
CHANGED
|
@@ -10,10 +10,11 @@ import { ulid as defaultUlid } from "@std/ulid";
|
|
|
10
10
|
import { stringify as stringifyYaml } from "@std/yaml";
|
|
11
11
|
import { attributeSpec, CSV_SPLITTABLE_TYPES, IDENTITY_KEY, } from "../model/mod.js";
|
|
12
12
|
import { MARKDOWN_EXTENSIONS } from "../discovery/mod.js";
|
|
13
|
-
import { ATTR_LINE_RE } from "../parser/attributes.js";
|
|
13
|
+
import { ATTR_LINE_RE, findTrailerStart, parseAttributes, } from "../parser/attributes.js";
|
|
14
14
|
import { extractFrontMatter } from "../parser/frontmatter.js";
|
|
15
15
|
import { parseMarkdown } from "../parser/markdown.js";
|
|
16
16
|
import { fencedCodeLines } from "../util/fence.js";
|
|
17
|
+
import { splitTraceValues } from "../refs/split.js";
|
|
17
18
|
import { applyLineEnding, detectLineEnding, normalizeLineEndings, } from "../util/line_endings.js";
|
|
18
19
|
import { isSentenceInitial as _isSentenceInitial, normalizeProseModals, } from "../util/modals.js";
|
|
19
20
|
import { synthesizedUlid } from "./synth_ulid.js";
|
|
@@ -23,6 +24,7 @@ import { normalizeBodyAst } from "../ast/normalize.js";
|
|
|
23
24
|
import { astEquivalent } from "../ast/equivalence.js";
|
|
24
25
|
import { formatProseSegments } from "./prose.js";
|
|
25
26
|
import { contentColumn } from "./entry_body.js";
|
|
27
|
+
import { entryListItems, protectedTabRanges, tabMarkers, } from "./tab_markers.js";
|
|
26
28
|
import { MARKSPEC_MARKDOWN_GLOBAL_CONFIG, } from "./dprint.js";
|
|
27
29
|
import { markdownSemanticallyEquivalent } from "./md_equiv.js";
|
|
28
30
|
/**
|
|
@@ -209,10 +211,60 @@ function emitBodyViaAst(lines, file, diagnostics, cachedEntries, proseFormat) {
|
|
|
209
211
|
// Process bottom-to-top so splices don't shift earlier entry positions.
|
|
210
212
|
const sorted = [...entries].sort((a, b) => b.location.line - a.location.line);
|
|
211
213
|
for (const entry of sorted) {
|
|
214
|
+
const protectedRanges = protectedTabRanges(lines);
|
|
215
|
+
const start = entry.location.line - 1;
|
|
216
|
+
if (protectedRanges.some((range) => start >= range.start && start < range.end))
|
|
217
|
+
continue;
|
|
218
|
+
const children = protectedRanges.filter((range, index) => range.start > start && range.start < (entry.endLine ?? start) &&
|
|
219
|
+
!protectedRanges.some((outer, other) => other !== index &&
|
|
220
|
+
outer.start < range.start && outer.end >= range.end));
|
|
221
|
+
if (children.length > 0) {
|
|
222
|
+
// Format the parent's prose with each refused child represented by a
|
|
223
|
+
// verbatim block, then restore the original child lines.
|
|
224
|
+
const end = entry.endLine;
|
|
225
|
+
const fragment = lines.slice(start, end);
|
|
226
|
+
const indent = contentColumn(lines[start], entry.location.column - 1);
|
|
227
|
+
const replacements = [];
|
|
228
|
+
for (const child of children.toReversed()) {
|
|
229
|
+
const childEnd = Math.min(child.end, end);
|
|
230
|
+
let token = "<!-- markspec-preserved-child -->";
|
|
231
|
+
while (lines.some((line) => line.includes(token)) ||
|
|
232
|
+
replacements.some((r) => r.token === token))
|
|
233
|
+
token = token.replace(" -->", "- -->");
|
|
234
|
+
replacements.push({
|
|
235
|
+
token,
|
|
236
|
+
original: lines.slice(child.start, childEnd),
|
|
237
|
+
});
|
|
238
|
+
fragment.splice(child.start - start, childEnd - child.start, " ".repeat(indent) + token, "");
|
|
239
|
+
}
|
|
240
|
+
const nestedDiagnostics = [];
|
|
241
|
+
emitBodyViaAst(fragment, file, nestedDiagnostics, undefined, proseFormat);
|
|
242
|
+
for (const { token, original } of replacements) {
|
|
243
|
+
const at = fragment.findIndex((line) => line.trim() === token);
|
|
244
|
+
if (at < 0)
|
|
245
|
+
throw new Error("Preserved child placeholder was lost");
|
|
246
|
+
let after = at + 1;
|
|
247
|
+
while (after < fragment.length && fragment[after].trim() === "") {
|
|
248
|
+
after++;
|
|
249
|
+
}
|
|
250
|
+
fragment.splice(at, after - at, ...original);
|
|
251
|
+
}
|
|
252
|
+
diagnostics.push(...nestedDiagnostics.map((d) => ({
|
|
253
|
+
...d,
|
|
254
|
+
location: d.location
|
|
255
|
+
? { ...d.location, line: d.location.line + start }
|
|
256
|
+
: undefined,
|
|
257
|
+
})));
|
|
258
|
+
if (fragment.join("\n") !== lines.slice(start, end).join("\n")) {
|
|
259
|
+
lines.splice(start, end - start, ...fragment);
|
|
260
|
+
bodyChanged = true;
|
|
261
|
+
}
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
212
264
|
if (!entry.body.trim())
|
|
213
265
|
continue; // empty or whitespace-only body
|
|
214
266
|
// List-item continuation indent: the item's content column, which
|
|
215
|
-
//
|
|
267
|
+
// counts every space and tab after the marker (#865).
|
|
216
268
|
const indent = contentColumn(lines[entry.location.line - 1] ?? "", entry.location.column - 1);
|
|
217
269
|
const indentStr = " ".repeat(indent);
|
|
218
270
|
const titleLineIdx = entry.location.line - 1; // 0-based
|
|
@@ -354,6 +406,97 @@ function stampDisciplineFrozen(value, today) {
|
|
|
354
406
|
}
|
|
355
407
|
return value;
|
|
356
408
|
}
|
|
409
|
+
/**
|
|
410
|
+
* Normalize tab markers before any pass can stamp or splice their entries.
|
|
411
|
+
* Compare whole documents so a neighbouring list cannot become a child.
|
|
412
|
+
*/
|
|
413
|
+
function normalizeTabMarkers(markdown, options, diagnostics) {
|
|
414
|
+
const lines = markdown.split("\n");
|
|
415
|
+
const original = [...lines];
|
|
416
|
+
const markers = tabMarkers(lines).sort((a, b) => b.start - a.start);
|
|
417
|
+
if (markers.length === 0)
|
|
418
|
+
return markdown;
|
|
419
|
+
const accepted = new Set();
|
|
420
|
+
const refused = new Set();
|
|
421
|
+
for (const marker of markers) {
|
|
422
|
+
const before = lines.join("\n");
|
|
423
|
+
const candidate = [...lines];
|
|
424
|
+
const title = lines[marker.start];
|
|
425
|
+
const indent = contentColumn(title, marker.column);
|
|
426
|
+
const delta = indent - (marker.column + 2);
|
|
427
|
+
candidate[marker.start] = title.slice(0, marker.column) + "- " +
|
|
428
|
+
title.slice(marker.column + 1 + marker.whitespace.length);
|
|
429
|
+
for (let i = marker.start + 1; i < marker.end; i++) {
|
|
430
|
+
if (candidate[i].startsWith(" ".repeat(indent))) {
|
|
431
|
+
candidate[i] = candidate[i].slice(delta);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
const after = candidate.join("\n");
|
|
435
|
+
const beforeEntries = parseMarkdown(before).entries;
|
|
436
|
+
const afterEntries = parseMarkdown(after).entries;
|
|
437
|
+
const identitiesKept = beforeEntries.length === afterEntries.length &&
|
|
438
|
+
beforeEntries.every((entry, i) => entry.displayId === afterEntries[i].displayId &&
|
|
439
|
+
entry.id === afterEntries[i].id);
|
|
440
|
+
const containsRefused = [...refused].some((start) => start > marker.start && start < marker.end);
|
|
441
|
+
if (containsRefused || !markdownSemanticallyEquivalent(before, after) ||
|
|
442
|
+
!identitiesKept) {
|
|
443
|
+
// Refusing an enclosing entry must also undo admitted changes to its
|
|
444
|
+
// descendants. No identities have been stamped at this point.
|
|
445
|
+
lines.splice(marker.start, marker.end - marker.start, ...original.slice(marker.start, marker.end));
|
|
446
|
+
for (const start of accepted) {
|
|
447
|
+
if (start >= marker.start && start < marker.end)
|
|
448
|
+
accepted.delete(start);
|
|
449
|
+
}
|
|
450
|
+
refused.add(marker.start);
|
|
451
|
+
diagnostics.push({
|
|
452
|
+
code: "MSL-F013",
|
|
453
|
+
severity: "warning",
|
|
454
|
+
message: "Tab-separated entry marker cannot be normalized without " +
|
|
455
|
+
"changing CommonMark structure; kept the entry unchanged",
|
|
456
|
+
location: {
|
|
457
|
+
file: options?.file ?? "<unknown>",
|
|
458
|
+
line: marker.start + 1,
|
|
459
|
+
column: marker.column + 1,
|
|
460
|
+
},
|
|
461
|
+
});
|
|
462
|
+
continue;
|
|
463
|
+
}
|
|
464
|
+
lines.splice(0, lines.length, ...candidate);
|
|
465
|
+
accepted.add(marker.start);
|
|
466
|
+
}
|
|
467
|
+
const indexedStarts = new Set(parseMarkdown(lines.join("\n")).entries.map((entry) => entry.location.line - 1));
|
|
468
|
+
for (const start of [...accepted].sort((a, b) => b - a)) {
|
|
469
|
+
// Re-read extents after each nested splice. Admission itself never
|
|
470
|
+
// changes line counts, so earlier title positions remain valid.
|
|
471
|
+
const marker = entryListItems(lines).find((item) => item.start === start);
|
|
472
|
+
// Nested entries are outside the parser's top-level entry index. Format
|
|
473
|
+
// their normalized item separately, then restore the enclosing indent.
|
|
474
|
+
if (marker.container.start !== marker.start &&
|
|
475
|
+
!indexedStarts.has(marker.container.start)) {
|
|
476
|
+
const prefix = " ".repeat(marker.column);
|
|
477
|
+
const fragment = lines.slice(marker.start, marker.end)
|
|
478
|
+
.map((line) => line.startsWith(prefix) ? line.slice(prefix.length) : line)
|
|
479
|
+
.join("\n");
|
|
480
|
+
const nested = formatEntries(fragment, options);
|
|
481
|
+
const replacement = nested.output.split("\n")
|
|
482
|
+
.map((line) => line === "" ? "" : prefix + line);
|
|
483
|
+
lines.splice(marker.start, marker.end - marker.start, ...replacement);
|
|
484
|
+
diagnostics.push(...nested.diagnostics.map((diagnostic) => ({
|
|
485
|
+
...diagnostic,
|
|
486
|
+
...(diagnostic.location
|
|
487
|
+
? {
|
|
488
|
+
location: {
|
|
489
|
+
...diagnostic.location,
|
|
490
|
+
line: diagnostic.location.line + marker.start,
|
|
491
|
+
column: diagnostic.location.column + marker.column,
|
|
492
|
+
},
|
|
493
|
+
}
|
|
494
|
+
: {}),
|
|
495
|
+
})));
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
return lines.join("\n");
|
|
499
|
+
}
|
|
357
500
|
/**
|
|
358
501
|
* Format a Markdown string — normalize attribute blocks,
|
|
359
502
|
* fix indentation, enforce canonical ordering.
|
|
@@ -363,6 +506,25 @@ function stampDisciplineFrozen(value, today) {
|
|
|
363
506
|
* @returns Format result with output text and diagnostics
|
|
364
507
|
*/
|
|
365
508
|
export function format(markdown, options) {
|
|
509
|
+
// A `.feature` file is Gherkin, not Markdown: neither the entry formatter
|
|
510
|
+
// nor the prose pass may touch it. Its Id stamping is `formatFeatureText`.
|
|
511
|
+
if (extname(options?.file ?? "").toLowerCase() === ".feature") {
|
|
512
|
+
return { output: markdown, diagnostics: [], changed: false };
|
|
513
|
+
}
|
|
514
|
+
const diagnostics = [];
|
|
515
|
+
const ending = detectLineEnding(markdown);
|
|
516
|
+
const prepared = normalizeTabMarkers(normalizeLineEndings(markdown), options, diagnostics);
|
|
517
|
+
const result = formatEntries(prepared, options);
|
|
518
|
+
const output = applyLineEnding(result.output, ending);
|
|
519
|
+
return {
|
|
520
|
+
output,
|
|
521
|
+
diagnostics: [...diagnostics, ...result.diagnostics],
|
|
522
|
+
changed: output !== markdown,
|
|
523
|
+
preservedRanges: protectedTabRanges(normalizeLineEndings(output).split("\n")),
|
|
524
|
+
};
|
|
525
|
+
}
|
|
526
|
+
/** Run the entry and prose passes after tab-marker admission. */
|
|
527
|
+
function formatEntries(markdown, options) {
|
|
366
528
|
const file = options?.file ?? "<unknown>";
|
|
367
529
|
// Detect the source file's line-ending convention up front so we can
|
|
368
530
|
// restore it on write-back. The rest of the formatter operates on a
|
|
@@ -407,7 +569,11 @@ export function format(markdown, options) {
|
|
|
407
569
|
// Process bottom-to-top so line splicing doesn't shift earlier entries.
|
|
408
570
|
const sorted = [...entries].sort((a, b) => b.location.line - a.location.line);
|
|
409
571
|
const genUlid = options?.generateUlid ?? defaultUlid;
|
|
572
|
+
const protectedRanges = protectedTabRanges(lines);
|
|
410
573
|
for (const entry of sorted) {
|
|
574
|
+
if (protectedRanges.some((range) => entry.location.line - 1 >= range.start &&
|
|
575
|
+
entry.location.line - 1 < range.end))
|
|
576
|
+
continue;
|
|
411
577
|
const indent = contentColumn(lines[entry.location.line - 1] ?? "", entry.location.column - 1);
|
|
412
578
|
// `entry.endLine` comes from the parse of the unedited buffer. Entries
|
|
413
579
|
// are processed bottom to top, so only this entry's own edits move its
|
|
@@ -416,7 +582,14 @@ export function format(markdown, options) {
|
|
|
416
582
|
const itemEndFloor = () => entry.endLine === undefined
|
|
417
583
|
? 0
|
|
418
584
|
: entry.endLine + (lines.length - lengthAtStart);
|
|
419
|
-
|
|
585
|
+
const existingTrailer = protectedRanges.length > 0
|
|
586
|
+
? findAttributeBlockRange(lines, entry.location.line, indent, itemEndFloor(), protectedRanges)
|
|
587
|
+
: undefined;
|
|
588
|
+
let attrs = protectedRanges.length > 0
|
|
589
|
+
? existingTrailer
|
|
590
|
+
? parseAttributes(lines.slice(existingTrailer.start, existingTrailer.end))
|
|
591
|
+
: []
|
|
592
|
+
: [...entry.rawAttributes];
|
|
420
593
|
const titleLineIdx = entry.location.line - 1;
|
|
421
594
|
// #686: a title that soft-wraps onto extra physical lines is a single
|
|
422
595
|
// Markdown paragraph, but the line-based passes here and in
|
|
@@ -469,6 +642,14 @@ export function format(markdown, options) {
|
|
|
469
642
|
// input reproduces the same identity. Falls back to fresh random
|
|
470
643
|
// when `Source:` is missing — synthesizing without a source pointer
|
|
471
644
|
// makes no sense, but the formatter never fails a stamp.
|
|
645
|
+
// A trailer holding a line the attribute parser cannot read (a
|
|
646
|
+
// continuation of a value) is left exactly as it is: rewriting it
|
|
647
|
+
// would drop that line, and stamping an Id would replace the
|
|
648
|
+
// identity the author wrote. The parser reports the line as MSL-P021.
|
|
649
|
+
const trailerRange = findAttributeBlockRange(lines, entry.location.line, indent, itemEndFloor(), protectedRanges);
|
|
650
|
+
if (trailerRange &&
|
|
651
|
+
lines.slice(trailerRange.start, trailerRange.end).some((l) => l.trim() !== "" && !ATTR_LINE_RE.test(l.trim())))
|
|
652
|
+
continue;
|
|
472
653
|
const hasIdentity = attrs.some((a) => a.key === IDENTITY_KEY);
|
|
473
654
|
if (!hasIdentity && entry.shape === "Authored") {
|
|
474
655
|
const origin = attrs.find((a) => a.key === "Origin")?.value.trim();
|
|
@@ -512,13 +693,22 @@ export function format(markdown, options) {
|
|
|
512
693
|
if (attrs.length === 0)
|
|
513
694
|
continue;
|
|
514
695
|
const normalized = sortAttributes(expandCsvValues(attrs));
|
|
515
|
-
const range = findAttributeBlockRange(lines, entry.location.line, indent, itemEndFloor());
|
|
696
|
+
const range = findAttributeBlockRange(lines, entry.location.line, indent, itemEndFloor(), protectedRanges);
|
|
516
697
|
if (range) {
|
|
517
698
|
// Replace existing attribute block.
|
|
518
699
|
const newBlock = renderAttributeBlock(normalized, indent);
|
|
519
700
|
const oldBlock = lines.slice(range.start, range.end).join("\n");
|
|
520
|
-
|
|
521
|
-
|
|
701
|
+
// A block directly under body prose (no blank line), indented or
|
|
702
|
+
// not, is not a code block in CommonMark: it continues the
|
|
703
|
+
// paragraph. The canonical layout separates it (#916). A line above
|
|
704
|
+
// that belongs to a preserved range (a refused child entry) is left
|
|
705
|
+
// as it is.
|
|
706
|
+
const above = range.start - 1;
|
|
707
|
+
const needsBlank = above > titleLineIdx &&
|
|
708
|
+
lines[above].trim() !== "" &&
|
|
709
|
+
!protectedRanges.some((r) => above >= r.start && above < r.end);
|
|
710
|
+
if (newBlock !== oldBlock || needsBlank) {
|
|
711
|
+
lines.splice(range.start, range.end - range.start, ...(needsBlank ? [""] : []), ...newBlock.split("\n"));
|
|
522
712
|
changed = true;
|
|
523
713
|
}
|
|
524
714
|
}
|
|
@@ -571,7 +761,26 @@ export function format(markdown, options) {
|
|
|
571
761
|
end: findItemEnd(collapsedLines, start, entryIndent, e.endLine ?? 0),
|
|
572
762
|
};
|
|
573
763
|
});
|
|
574
|
-
|
|
764
|
+
if (tabMarkers(collapsedLines).length > 0) {
|
|
765
|
+
// Title-only entry items are outside the entry index. Keep their list
|
|
766
|
+
// context too: isolated prose formatting can move adjacent entries.
|
|
767
|
+
const indexedStarts = new Set(proseEntries.map((entry) => entry.location.line - 1));
|
|
768
|
+
extents.push(...entryListItems(collapsedLines)
|
|
769
|
+
.filter((item) => !indexedStarts.has(item.start))
|
|
770
|
+
.map((item) => item.listContext));
|
|
771
|
+
extents.push(...protectedTabRanges(collapsedLines, true));
|
|
772
|
+
}
|
|
773
|
+
extents.sort((a, b) => a.start - b.start);
|
|
774
|
+
const merged = [];
|
|
775
|
+
for (const extent of extents) {
|
|
776
|
+
const previous = merged.at(-1);
|
|
777
|
+
if (previous && extent.start <= previous.end) {
|
|
778
|
+
previous.end = Math.max(previous.end, extent.end);
|
|
779
|
+
}
|
|
780
|
+
else
|
|
781
|
+
merged.push({ ...extent });
|
|
782
|
+
}
|
|
783
|
+
const prose = formatProseSegments(collapsedLines, merged, proseFormat);
|
|
575
784
|
if (prose.changed)
|
|
576
785
|
changed = true;
|
|
577
786
|
for (const fallback of prose.fallbacks) {
|
|
@@ -635,9 +844,11 @@ function renderFrontMatter(attrs) {
|
|
|
635
844
|
function collapseBlankLines(lines) {
|
|
636
845
|
const out = [];
|
|
637
846
|
const fencedLines = fencedCodeLines(lines.join("\n"));
|
|
847
|
+
const protectedRanges = protectedTabRanges(lines, true);
|
|
638
848
|
let prevBlank = false;
|
|
639
849
|
for (const [i, line] of lines.entries()) {
|
|
640
|
-
if (fencedLines.has(i)
|
|
850
|
+
if (fencedLines.has(i) ||
|
|
851
|
+
protectedRanges.some((range) => i >= range.start && i < range.end)) {
|
|
641
852
|
out.push(line);
|
|
642
853
|
prevBlank = false;
|
|
643
854
|
continue;
|
|
@@ -654,17 +865,23 @@ function collapseBlankLines(lines) {
|
|
|
654
865
|
* Expand CSV values on repeatable-type attributes into one entry per value
|
|
655
866
|
* per ADR-002 §2.6. `id-list` / `tag-list` / `external-id` accept CSV input
|
|
656
867
|
* but must round-trip as multi-line output; `citation` is left alone
|
|
657
|
-
* because locators may contain commas.
|
|
868
|
+
* because locators may contain commas. `id-list` values split with
|
|
869
|
+
* {@linkcode splitTraceValues}, so a comma inside a scenario locator does
|
|
870
|
+
* not split.
|
|
658
871
|
*/
|
|
659
872
|
export function expandCsvValues(attrs) {
|
|
660
873
|
const result = [];
|
|
661
874
|
for (const attr of attrs) {
|
|
662
875
|
const spec = attributeSpec(attr.key);
|
|
663
876
|
if (spec && CSV_SPLITTABLE_TYPES.has(spec.type) && attr.value.includes(",")) {
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
877
|
+
// An id-list value keeps commas inside a bracket locator
|
|
878
|
+
// (`A [x, y], B`, #915); the locator text is kept as authored.
|
|
879
|
+
const values = spec.type === "id-list"
|
|
880
|
+
? splitTraceValues(attr.value)
|
|
881
|
+
: attr.value
|
|
882
|
+
.split(",")
|
|
883
|
+
.map((s) => s.trim())
|
|
884
|
+
.filter((s) => s.length > 0);
|
|
668
885
|
for (const value of values) {
|
|
669
886
|
result.push({ key: attr.key, value });
|
|
670
887
|
}
|
|
@@ -798,19 +1015,26 @@ function removeEmptyTrailerLines(body, entries, diagnostics) {
|
|
|
798
1015
|
const lines = body.split("\n");
|
|
799
1016
|
let removedAny = false;
|
|
800
1017
|
const sorted = [...entries].sort((a, b) => b.location.line - a.location.line);
|
|
1018
|
+
const protectedRanges = protectedTabRanges(lines);
|
|
801
1019
|
for (const entry of sorted) {
|
|
1020
|
+
if (protectedRanges.some((range) => entry.location.line - 1 >= range.start &&
|
|
1021
|
+
entry.location.line - 1 < range.end))
|
|
1022
|
+
continue;
|
|
802
1023
|
const indent = contentColumn(lines[entry.location.line - 1] ?? "", entry.location.column - 1);
|
|
803
1024
|
const startIdx = entry.location.line - 1;
|
|
804
1025
|
let end = findItemEnd(lines, startIdx, indent, entry.endLine ?? 0);
|
|
805
1026
|
while (end > startIdx && lines[end - 1].trim() === "")
|
|
806
1027
|
end--;
|
|
807
1028
|
let start = end;
|
|
808
|
-
while (start - 1 > startIdx && lines[start - 1].trim() !== ""
|
|
1029
|
+
while (start - 1 > startIdx && lines[start - 1].trim() !== "" &&
|
|
1030
|
+
!protectedRanges.some((range) => start - 1 >= range.start && start - 1 < range.end))
|
|
809
1031
|
start--;
|
|
810
1032
|
if (start >= end || start - 1 <= startIdx)
|
|
811
1033
|
continue;
|
|
812
1034
|
const trailerIndent = " ".repeat(indent + 4);
|
|
813
1035
|
const block = lines.slice(start, end);
|
|
1036
|
+
if (protectedRanges.some((range) => range.start > startIdx && start < range.end && end > range.start))
|
|
1037
|
+
continue;
|
|
814
1038
|
const isTrailerBlock = block.every((l) => l.startsWith(trailerIndent) && TRAILER_SHAPED_RE.test(l.trim()));
|
|
815
1039
|
if (!isTrailerBlock)
|
|
816
1040
|
continue;
|
|
@@ -845,9 +1069,26 @@ function removeEmptyTrailerLines(body, entries, diagnostics) {
|
|
|
845
1069
|
* for an entry starting at the given line.
|
|
846
1070
|
*
|
|
847
1071
|
* Scans forward from the entry start to find the list item boundary,
|
|
848
|
-
* then
|
|
1072
|
+
* then passes the entry content below the last preserved range, with the
|
|
1073
|
+
* list-item indent removed, to the parser's {@linkcode findTrailerStart}.
|
|
1074
|
+
* The formatter and the parser therefore agree on which lines are the
|
|
1075
|
+
* trailer, so `markspec fmt` never rewrites a line that the parser read as
|
|
1076
|
+
* body, and never drops an attribute line that the parser read (#916).
|
|
1077
|
+
* The LSP MSL-P023 quick fix uses it too.
|
|
1078
|
+
*
|
|
1079
|
+
* @param lines - Raw lines of the whole file.
|
|
1080
|
+
* @param entryStartLine - 1-based line of the entry's title (`- [ID] ...`).
|
|
1081
|
+
* @param indent - Content column of the list item: the 0-based column of
|
|
1082
|
+
* the `[` that opens the display ID.
|
|
1083
|
+
* @param itemEndFloor - Pass the parser's `Entry.endLine` (0 when unknown):
|
|
1084
|
+
* the item end scan starts no earlier than this line index, so a lazy
|
|
1085
|
+
* continuation line does not end the item early (see `findItemEnd`).
|
|
1086
|
+
* @param preservedRanges - Ranges inside the entry that are kept verbatim
|
|
1087
|
+
* (nested entries); the trailer is searched below the last one.
|
|
1088
|
+
* @returns The 0-based, half-open line range `[start, end)` of the trailer
|
|
1089
|
+
* over `lines`, or `undefined` when the entry has no trailer.
|
|
849
1090
|
*/
|
|
850
|
-
export function findAttributeBlockRange(lines, entryStartLine, indent, itemEndFloor = 0) {
|
|
1091
|
+
export function findAttributeBlockRange(lines, entryStartLine, indent, itemEndFloor = 0, preservedRanges = []) {
|
|
851
1092
|
const startIdx = entryStartLine - 1;
|
|
852
1093
|
const itemEnd = findItemEnd(lines, startIdx, indent, itemEndFloor);
|
|
853
1094
|
// Walk backwards from itemEnd, skip trailing blank lines.
|
|
@@ -857,19 +1098,18 @@ export function findAttributeBlockRange(lines, entryStartLine, indent, itemEndFl
|
|
|
857
1098
|
}
|
|
858
1099
|
if (scanEnd <= startIdx)
|
|
859
1100
|
return undefined;
|
|
860
|
-
//
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
attrStart = i;
|
|
868
|
-
}
|
|
869
|
-
else {
|
|
870
|
-
break;
|
|
1101
|
+
// Content lines of the entry body, below the last preserved range, with
|
|
1102
|
+
// the list-item indent removed as the parser does. The shared trailer
|
|
1103
|
+
// detection decides where the attribute block starts (#916).
|
|
1104
|
+
let floor = startIdx + 1;
|
|
1105
|
+
for (let i = startIdx + 1; i < scanEnd; i++) {
|
|
1106
|
+
if (preservedRanges.some((range) => i >= range.start && i < range.end)) {
|
|
1107
|
+
floor = i + 1;
|
|
871
1108
|
}
|
|
872
1109
|
}
|
|
1110
|
+
const indentStr = " ".repeat(indent);
|
|
1111
|
+
const content = lines.slice(floor, scanEnd).map((l) => l.startsWith(indentStr) ? l.slice(indent) : l);
|
|
1112
|
+
const attrStart = floor + findTrailerStart(content);
|
|
873
1113
|
if (attrStart >= scanEnd)
|
|
874
1114
|
return undefined;
|
|
875
1115
|
return { start: attrStart, end: scanEnd };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { EntryExtent } from "./prose.js";
|
|
2
|
+
/** An entry title and the list context that controls its indentation. */
|
|
3
|
+
export interface EntryListItem extends EntryExtent {
|
|
4
|
+
readonly column: number;
|
|
5
|
+
readonly whitespace: string;
|
|
6
|
+
readonly container: EntryExtent;
|
|
7
|
+
readonly listContext: EntryExtent;
|
|
8
|
+
}
|
|
9
|
+
/** Find actual unordered list items, excluding code, HTML, and task lists. */
|
|
10
|
+
export declare function entryListItems(lines: readonly string[]): EntryListItem[];
|
|
11
|
+
/** Find entry list items with a tab in the whitespace after the marker. */
|
|
12
|
+
export declare function tabMarkers(lines: readonly string[]): EntryListItem[];
|
|
13
|
+
/** Ranges kept out of every formatter pass after a tab rewrite is refused. */
|
|
14
|
+
export declare function protectedTabRanges(lines: readonly string[], preserveListContext?: boolean): EntryExtent[];
|
|
15
|
+
//# sourceMappingURL=tab_markers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tab_markers.d.ts","sourceRoot":"","sources":["../../src/formatter/tab_markers.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAI9C,yEAAyE;AACzE,MAAM,WAAW,aAAc,SAAQ,WAAW;IAChD,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,SAAS,EAAE,WAAW,CAAC;IAChC,QAAQ,CAAC,WAAW,EAAE,WAAW,CAAC;CACnC;AAED,8EAA8E;AAC9E,wBAAgB,cAAc,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,aAAa,EAAE,CAoFxE;AAED,2EAA2E;AAC3E,wBAAgB,UAAU,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,aAAa,EAAE,CAKpE;AAED,8EAA8E;AAC9E,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,SAAS,MAAM,EAAE,EACxB,mBAAmB,UAAQ,GAC1B,WAAW,EAAE,CA4Cf"}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { unified } from "unified";
|
|
2
|
+
import remarkParse from "remark-parse";
|
|
3
|
+
import remarkGfm from "remark-gfm";
|
|
4
|
+
import { contentColumn } from "./entry_body.js";
|
|
5
|
+
const parser = unified().use(remarkParse).use(remarkGfm);
|
|
6
|
+
/** Find actual unordered list items, excluding code, HTML, and task lists. */
|
|
7
|
+
export function entryListItems(lines) {
|
|
8
|
+
const markers = [];
|
|
9
|
+
const visit = (node, container, enclosingList) => {
|
|
10
|
+
if (node.type === "list") {
|
|
11
|
+
for (const item of node.children) {
|
|
12
|
+
const outer = container ?? item;
|
|
13
|
+
const start = (item.position?.start.line ?? 1) - 1;
|
|
14
|
+
const column = (item.position?.start.column ?? 1) - 1;
|
|
15
|
+
const match = /^[-*+]([ \t]+)\[[^\]\s]+\](?:\s|$)/.exec((lines[start] ?? "").slice(column));
|
|
16
|
+
if (!node.ordered && item.checked == null && match &&
|
|
17
|
+
item.children[0]?.type === "paragraph") {
|
|
18
|
+
markers.push({
|
|
19
|
+
start,
|
|
20
|
+
end: item.position?.end.line ?? start + 1,
|
|
21
|
+
column,
|
|
22
|
+
whitespace: match[1],
|
|
23
|
+
container: {
|
|
24
|
+
start: (outer.position?.start.line ?? 1) - 1,
|
|
25
|
+
end: outer.position?.end.line ?? lines.length,
|
|
26
|
+
},
|
|
27
|
+
listContext: enclosingList ?? {
|
|
28
|
+
start: (node.position?.start.line ?? 1) - 1,
|
|
29
|
+
end: node.position?.end.line ?? lines.length,
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
for (const child of item.children) {
|
|
34
|
+
visit(child, outer, enclosingList ?? {
|
|
35
|
+
start: (node.position?.start.line ?? 1) - 1,
|
|
36
|
+
end: node.position?.end.line ?? lines.length,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
else if ("children" in node) {
|
|
42
|
+
for (const [index, child] of node.children.entries()) {
|
|
43
|
+
let context = enclosingList;
|
|
44
|
+
if (!context && child.type === "list") {
|
|
45
|
+
// A marker change (for example `*` to `-`) splits adjacent
|
|
46
|
+
// items into separate mdast lists. Their indentation still
|
|
47
|
+
// affects admission of a tab rewrite across that boundary.
|
|
48
|
+
let first = index;
|
|
49
|
+
let last = index;
|
|
50
|
+
while (first > 0 && node.children[first - 1].type === "list")
|
|
51
|
+
first--;
|
|
52
|
+
while (node.children[last + 1]?.type === "list")
|
|
53
|
+
last++;
|
|
54
|
+
// Indented blocks after the list can move into an entry when its
|
|
55
|
+
// content column shrinks. Formatting them in isolation can remove
|
|
56
|
+
// that indentation and admit a refused rewrite on the next run.
|
|
57
|
+
// Keep complete blocks, including unindented lazy continuations.
|
|
58
|
+
const contentColumn = Math.min(...node.children.slice(first, last + 1).flatMap((sibling) => sibling.type === "list"
|
|
59
|
+
? sibling.children.map((item) => (item.position?.start.column ?? 1) + 1)
|
|
60
|
+
: []));
|
|
61
|
+
while ((node.children[last + 1]?.position?.start.column ?? 0) >=
|
|
62
|
+
contentColumn)
|
|
63
|
+
last++;
|
|
64
|
+
context = {
|
|
65
|
+
start: (node.children[first].position?.start.line ?? 1) - 1,
|
|
66
|
+
end: node.children[last].position?.end.line ?? lines.length,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
visit(child, container, context);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
visit(parser.parse(lines.join("\n")));
|
|
74
|
+
return markers;
|
|
75
|
+
}
|
|
76
|
+
/** Find entry list items with a tab in the whitespace after the marker. */
|
|
77
|
+
export function tabMarkers(lines) {
|
|
78
|
+
if (!lines.some((line) => /[-*+][ \t]*\t[ \t]*\[/.test(line)))
|
|
79
|
+
return [];
|
|
80
|
+
return entryListItems(lines).filter((marker) => marker.whitespace.includes("\t"));
|
|
81
|
+
}
|
|
82
|
+
/** Ranges kept out of every formatter pass after a tab rewrite is refused. */
|
|
83
|
+
export function protectedTabRanges(lines, preserveListContext = false) {
|
|
84
|
+
const markers = tabMarkers(lines);
|
|
85
|
+
const items = markers.length > 0 ? entryListItems(lines) : [];
|
|
86
|
+
const ranges = markers.map((marker) => {
|
|
87
|
+
// A prose rewrite of a neighbouring item can make a refused marker
|
|
88
|
+
// eligible on the next run. Keep the containing list and the following
|
|
89
|
+
// indented blocks stable, even when CommonMark puts them outside it.
|
|
90
|
+
const range = preserveListContext ? marker.listContext : marker;
|
|
91
|
+
let start = range.start;
|
|
92
|
+
if (!preserveListContext && marker.column > 0) {
|
|
93
|
+
for (let i = start - 1; i >= marker.container.start; i--) {
|
|
94
|
+
if (lines[i].trim() === "")
|
|
95
|
+
continue;
|
|
96
|
+
if (!lines[i].startsWith(" ".repeat(marker.column)))
|
|
97
|
+
break;
|
|
98
|
+
if (/^[-*+][ \t]/.test(lines[i].slice(marker.column)))
|
|
99
|
+
start = i;
|
|
100
|
+
else if (!lines[i].startsWith(" ".repeat(marker.column + 2)))
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
let end = range.end;
|
|
105
|
+
const nextEntry = items.find((item) => item.start > marker.start)?.start ??
|
|
106
|
+
lines.length;
|
|
107
|
+
const trailerIndent = " ".repeat(contentColumn(lines[marker.start], marker.column) + 4);
|
|
108
|
+
for (let i = end; i < nextEntry; i++) {
|
|
109
|
+
if (lines[i].trim() === "")
|
|
110
|
+
continue;
|
|
111
|
+
if (!lines[i].startsWith(" ".repeat(marker.column + 2)))
|
|
112
|
+
break;
|
|
113
|
+
// A less-indented trailer belongs to an enclosing space-marker entry.
|
|
114
|
+
if (/^[A-Z][A-Za-z-]*:(?:\s|$)/.test(lines[i].trim()) &&
|
|
115
|
+
!lines[i].startsWith(trailerIndent))
|
|
116
|
+
break;
|
|
117
|
+
end = i + 1;
|
|
118
|
+
}
|
|
119
|
+
while (end < lines.length && lines[end].trim() === "")
|
|
120
|
+
end++;
|
|
121
|
+
return { start, end };
|
|
122
|
+
});
|
|
123
|
+
const merged = [];
|
|
124
|
+
for (const range of ranges.sort((a, b) => a.start - b.start)) {
|
|
125
|
+
const previous = merged.at(-1);
|
|
126
|
+
if (previous && range.start <= previous.end) {
|
|
127
|
+
previous.end = Math.max(previous.end, range.end);
|
|
128
|
+
}
|
|
129
|
+
else
|
|
130
|
+
merged.push({ ...range });
|
|
131
|
+
}
|
|
132
|
+
return merged;
|
|
133
|
+
}
|
package/esm/gates/mod.d.ts
CHANGED
|
@@ -29,8 +29,8 @@ import { type LockEdge, type ParseLockfileResult, type ReadFile as LockReadFile
|
|
|
29
29
|
* `fmt --check` never disagree on what "formatted" means. `MSL-F010` is pure
|
|
30
30
|
* formatter drift; `MSL-F011` is reference-canonicalization drift (a ULID or
|
|
31
31
|
* stale display ID `fmt` would rewrite) — kept distinct so the author knows
|
|
32
|
-
* which fmt concern fired. Markdown only — `markspec fmt`
|
|
33
|
-
* files, so the caller passes only
|
|
32
|
+
* which fmt concern fired. Markdown and `.feature` files only — `markspec fmt`
|
|
33
|
+
* never rewrites source files, so the caller passes only those file contents.
|
|
34
34
|
*
|
|
35
35
|
* @param mdContents Markdown file path → current on-disk content.
|
|
36
36
|
* @param entries The full parsed entry set (project + corpus). Only
|
package/esm/gates/mod.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../src/gates/mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,gBAAgB,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../src/gates/mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,gBAAgB,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAK3E,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAG7D,OAAO,EAGL,KAAK,QAAQ,EACb,KAAK,mBAAmB,EACxB,KAAK,QAAQ,IAAI,YAAY,EAE9B,MAAM,gBAAgB,CAAC;AAGxB;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAsB,YAAY,CAChC,UAAU,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,EACvC,OAAO,EAAE,SAAS,KAAK,EAAE,EACzB,MAAM,EAAE,SAAS,QAAQ,EAAE,EAC3B,mBAAmB,EAAE,cAAc,GAAG,SAAS,GAC9C,OAAO,CAAC,UAAU,EAAE,CAAC,CAsDvB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAsB,iBAAiB,CACrC,SAAS,EAAE,mBAAmB,EAC9B,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,SAAS,KAAK,EAAE,EACzB,KAAK,CAAC,EAAE;IAAE,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,QAAQ,EAAE,YAAY,CAAA;CAAE,GACtE,OAAO,CAAC,UAAU,EAAE,CAAC,CAuCvB;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,aAAa,CACjC,OAAO,EAAE,SAAS,KAAK,EAAE,EACzB,OAAO,EAAE,gBAAgB,GAAG,SAAS,GACpC,OAAO,CAAC,UAAU,EAAE,CAAC,CAQvB"}
|
package/esm/gates/mod.js
CHANGED
|
@@ -18,7 +18,9 @@
|
|
|
18
18
|
* it — no cycle.
|
|
19
19
|
*/
|
|
20
20
|
import { isCorpusEntry } from "../model/mod.js";
|
|
21
|
+
import { extname } from "@std/path";
|
|
21
22
|
import { format } from "../formatter/mod.js";
|
|
23
|
+
import { formatFeatureText } from "../formatter/feature_file.js";
|
|
22
24
|
import { parseFile } from "../parser/mod.js";
|
|
23
25
|
import { buildRefIndex, canonicalizeRefs } from "../refs/mod.js";
|
|
24
26
|
import { detectOfflineEdgeDrift, formatEdgeDriftMessage, verifyUpstreamCache, } from "../lock/mod.js";
|
|
@@ -32,8 +34,8 @@ import { runLint } from "../lint/mod.js";
|
|
|
32
34
|
* `fmt --check` never disagree on what "formatted" means. `MSL-F010` is pure
|
|
33
35
|
* formatter drift; `MSL-F011` is reference-canonicalization drift (a ULID or
|
|
34
36
|
* stale display ID `fmt` would rewrite) — kept distinct so the author knows
|
|
35
|
-
* which fmt concern fired. Markdown only — `markspec fmt`
|
|
36
|
-
* files, so the caller passes only
|
|
37
|
+
* which fmt concern fired. Markdown and `.feature` files only — `markspec fmt`
|
|
38
|
+
* never rewrites source files, so the caller passes only those file contents.
|
|
37
39
|
*
|
|
38
40
|
* @param mdContents Markdown file path → current on-disk content.
|
|
39
41
|
* @param entries The full parsed entry set (project + corpus). Only
|
|
@@ -55,6 +57,19 @@ export async function fmtDriftGate(mdContents, entries, ledger, formatMarkdownPr
|
|
|
55
57
|
// `emittableEntries` (upstream-only, #771); do not "unify" it.
|
|
56
58
|
const refIndex = buildRefIndex(entries.filter((e) => !isCorpusEntry(e)));
|
|
57
59
|
for (const [filePath, content] of mdContents) {
|
|
60
|
+
// A `.feature` file only drifts when a headed entry lacks its Id. It has
|
|
61
|
+
// no prose pass and no reference canonicalisation.
|
|
62
|
+
if (extname(filePath).toLowerCase() === ".feature") {
|
|
63
|
+
if ((await formatFeatureText(content, filePath)).changed) {
|
|
64
|
+
diagnostics.push({
|
|
65
|
+
code: "MSL-F010",
|
|
66
|
+
severity: "error",
|
|
67
|
+
message: "file is not formatted (run `markspec fmt`)",
|
|
68
|
+
location: { file: filePath, line: 1, column: 1 },
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
58
73
|
const formatted = format(content, {
|
|
59
74
|
file: filePath,
|
|
60
75
|
formatMarkdownProse,
|
|
@@ -68,7 +83,7 @@ export async function fmtDriftGate(mdContents, entries, ledger, formatMarkdownPr
|
|
|
68
83
|
});
|
|
69
84
|
}
|
|
70
85
|
const parsed = await parseFile(formatted.output, { file: filePath });
|
|
71
|
-
const refResult = canonicalizeRefs(formatted.output, parsed.entries, refIndex, ledger);
|
|
86
|
+
const refResult = canonicalizeRefs(formatted.output, parsed.entries, refIndex, ledger, formatted.preservedRanges);
|
|
72
87
|
if (refResult.changed) {
|
|
73
88
|
diagnostics.push({
|
|
74
89
|
code: "MSL-F011",
|