pi-dent-terms 0.1.0 → 0.1.2
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 +41 -38
- package/index.ts +3 -2
- package/lib/after-edit.ts +8 -8
- package/lib/changes.ts +19 -19
- package/lib/checks.ts +22 -22
- package/lib/config.ts +10 -7
- package/lib/import.ts +8 -7
- package/lib/match.ts +10 -10
- package/lib/operations.ts +30 -30
- package/lib/prose.ts +11 -11
- package/lib/terms-file.ts +22 -20
- package/lib/text.ts +5 -5
- package/lib/tool.ts +48 -40
- package/lib/workspace.ts +10 -9
- package/package.json +3 -2
- package/specs/AGENTS.md +28 -0
- package/specs/terms-tool.md +369 -0
- package/specs/terms.md +756 -0
package/lib/match.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Matching of entries in prose: variants, occurrences, and uses. See specs/terms.md
|
|
2
|
+
* Matching of entries in prose: variants, occurrences, and uses. See specs/terms.md#matching.
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
import type { ProseItem, ProseRun } from "./prose.ts";
|
|
@@ -7,7 +7,7 @@ import { compareUtf16, isWhiteSpace, isWordChar, normalise } from "./text.ts";
|
|
|
7
7
|
|
|
8
8
|
export type Reason = "merged" | "split" | "renamed" | "out-of-scope";
|
|
9
9
|
|
|
10
|
-
/** The fields of an entry that matching reads; the entries passed have passed
|
|
10
|
+
/** The fields of an entry that matching reads; the entries passed have passed specs/terms.md#terms-file. */
|
|
11
11
|
export interface MatchEntry {
|
|
12
12
|
readonly id: string;
|
|
13
13
|
readonly label: string;
|
|
@@ -20,7 +20,7 @@ export interface MatchEntry {
|
|
|
20
20
|
|
|
21
21
|
export type VariantKind = "label" | "form" | "deprecated" | "reject";
|
|
22
22
|
|
|
23
|
-
/** A normalised string an entry is found by (
|
|
23
|
+
/** A normalised string an entry is found by (specs/terms.md#phrases). */
|
|
24
24
|
export interface Variant {
|
|
25
25
|
readonly entry: MatchEntry;
|
|
26
26
|
readonly kind: VariantKind;
|
|
@@ -31,7 +31,7 @@ export interface Variant {
|
|
|
31
31
|
readonly capitalised: boolean;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
/** A place in a prose run where a variant is found (
|
|
34
|
+
/** A place in a prose run where a variant is found (specs/terms.md#occurrences). */
|
|
35
35
|
export interface Occurrence {
|
|
36
36
|
readonly variant: Variant;
|
|
37
37
|
/** UTF-16 offsets of the source text, in the document text. */
|
|
@@ -48,7 +48,7 @@ export interface Occurrence {
|
|
|
48
48
|
|
|
49
49
|
const KIND_ORDER: readonly VariantKind[] = ["label", "form", "deprecated", "reject"];
|
|
50
50
|
|
|
51
|
-
/** The plural of a phrase whose last character is a letter, else undefined (
|
|
51
|
+
/** The plural of a phrase whose last character is a letter, else undefined (specs/terms.md#phrases). */
|
|
52
52
|
export function pluralOf(phrase: string): string | undefined {
|
|
53
53
|
const chars = [...phrase];
|
|
54
54
|
const last = chars.at(-1);
|
|
@@ -80,7 +80,7 @@ export function capitalisedOf(s: string): string | undefined {
|
|
|
80
80
|
return `${first.toUpperCase()}${s.slice(first.length)}`;
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
-
/** The variants of the entries, per
|
|
83
|
+
/** The variants of the entries, per specs/terms.md#phrases. */
|
|
84
84
|
export function variantsOf(entries: readonly MatchEntry[]): Variant[] {
|
|
85
85
|
const out: Variant[] = [];
|
|
86
86
|
const add = (entry: MatchEntry, kind: VariantKind, phrase: string, withPlural: boolean): void => {
|
|
@@ -135,7 +135,7 @@ interface Found {
|
|
|
135
135
|
readonly spelled: string;
|
|
136
136
|
}
|
|
137
137
|
|
|
138
|
-
/** Finds the occurrences of the variants in the runs of a document, per
|
|
138
|
+
/** Finds the occurrences of the variants in the runs of a document, per specs/terms.md#occurrences. */
|
|
139
139
|
export function findOccurrences(
|
|
140
140
|
text: string,
|
|
141
141
|
runs: readonly ProseRun[],
|
|
@@ -186,7 +186,7 @@ export function findOccurrences(
|
|
|
186
186
|
return out;
|
|
187
187
|
}
|
|
188
188
|
|
|
189
|
-
/** The ids of the entries used by the occurrences, ascending (
|
|
189
|
+
/** The ids of the entries used by the occurrences, ascending (specs/terms.md#uses). */
|
|
190
190
|
export function usedEntryIds(occurrences: readonly Occurrence[]): string[] {
|
|
191
191
|
const ids = new Set<string>();
|
|
192
192
|
for (const occ of occurrences) {
|
|
@@ -197,7 +197,7 @@ export function usedEntryIds(occurrences: readonly Occurrence[]): string[] {
|
|
|
197
197
|
return [...ids].toSorted(compareUtf16);
|
|
198
198
|
}
|
|
199
199
|
|
|
200
|
-
/** The variant found at position i that wins the order of
|
|
200
|
+
/** The variant found at position i that wins the order of specs/terms.md#occurrences, if any. */
|
|
201
201
|
function bestAt(
|
|
202
202
|
items: readonly ProseItem[],
|
|
203
203
|
i: number,
|
|
@@ -217,7 +217,7 @@ function bestAt(
|
|
|
217
217
|
return best;
|
|
218
218
|
}
|
|
219
219
|
|
|
220
|
-
/** Whether `a` comes before `b` in the order of
|
|
220
|
+
/** Whether `a` comes before `b` in the order of specs/terms.md#occurrences. */
|
|
221
221
|
function precedes(a: Found, b: Found): boolean {
|
|
222
222
|
if (a.spelled.length !== b.spelled.length) {
|
|
223
223
|
return a.spelled.length > b.spelled.length;
|
package/lib/operations.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Operations on the terms file (specs/terms.md
|
|
2
|
+
* Operations on the terms file (specs/terms.md#operations): each takes the terms file's bytes, the
|
|
3
3
|
* documents, the date, and its parameters, and gives a refusal or the new file text with its edit
|
|
4
4
|
* list. Pure: no I/O and no clock.
|
|
5
5
|
*/
|
|
@@ -26,7 +26,7 @@ import { compareUtf16, findingText, normalise, quote, trimWhiteSpace } from "./t
|
|
|
26
26
|
|
|
27
27
|
export type { RejectCandidate } from "./import.ts";
|
|
28
28
|
|
|
29
|
-
/** The parameters of each operation (
|
|
29
|
+
/** The parameters of each operation (specs/terms.md#operations). Optional lists may be omitted. */
|
|
30
30
|
export interface AddParams {
|
|
31
31
|
readonly op: "add";
|
|
32
32
|
readonly id: string;
|
|
@@ -34,7 +34,7 @@ export interface AddParams {
|
|
|
34
34
|
readonly meaning: string;
|
|
35
35
|
readonly owner: string;
|
|
36
36
|
readonly group: string;
|
|
37
|
-
/** One of `shared` and `confusable`, normalised (
|
|
37
|
+
/** One of `shared` and `confusable`, normalised (specs/terms.md#common-rules). */
|
|
38
38
|
readonly key: string;
|
|
39
39
|
readonly reject?: readonly string[] | undefined;
|
|
40
40
|
readonly distinguish?: readonly string[] | undefined;
|
|
@@ -44,7 +44,7 @@ export interface AddParams {
|
|
|
44
44
|
export interface UpdateParams {
|
|
45
45
|
readonly op: "update";
|
|
46
46
|
readonly id: string;
|
|
47
|
-
/** The one value `active`, normalised (
|
|
47
|
+
/** The one value `active`, normalised (specs/terms.md#common-rules); returns a deprecated entry to active. */
|
|
48
48
|
readonly status?: string | undefined;
|
|
49
49
|
readonly key?: string | undefined;
|
|
50
50
|
readonly meaning?: string | undefined;
|
|
@@ -76,7 +76,7 @@ export interface SplitParams {
|
|
|
76
76
|
export interface DeprecateParams {
|
|
77
77
|
readonly op: "deprecate";
|
|
78
78
|
readonly id: string;
|
|
79
|
-
/** One of `renamed` and `out-of-scope`, normalised (
|
|
79
|
+
/** One of `renamed` and `out-of-scope`, normalised (specs/terms.md#common-rules). */
|
|
80
80
|
readonly reason: string;
|
|
81
81
|
readonly replacedBy?: readonly string[] | undefined;
|
|
82
82
|
}
|
|
@@ -112,7 +112,7 @@ export interface OperationInput {
|
|
|
112
112
|
readonly op: OperationParams;
|
|
113
113
|
}
|
|
114
114
|
|
|
115
|
-
/** One suggested change of an occurrence (
|
|
115
|
+
/** One suggested change of an occurrence (specs/terms.md#common-rules); `new` is null when none is suggested. */
|
|
116
116
|
export interface Edit {
|
|
117
117
|
readonly path: string;
|
|
118
118
|
readonly line: number;
|
|
@@ -132,7 +132,7 @@ export type OperationResult =
|
|
|
132
132
|
| { readonly ok: false; readonly refusal: string }
|
|
133
133
|
| {
|
|
134
134
|
readonly ok: true;
|
|
135
|
-
/** The new terms file text in canonical form (
|
|
135
|
+
/** The new terms file text in canonical form (specs/terms.md#canonical-form). */
|
|
136
136
|
readonly text: string;
|
|
137
137
|
readonly edits: readonly Edit[];
|
|
138
138
|
/** The reject candidates of an `import`; empty for other operations. */
|
|
@@ -140,7 +140,7 @@ export type OperationResult =
|
|
|
140
140
|
readonly summary: OperationSummary;
|
|
141
141
|
};
|
|
142
142
|
|
|
143
|
-
/** The planned change of an operation that passed its refusals (
|
|
143
|
+
/** The planned change of an operation that passed its refusals (specs/terms.md#operations). */
|
|
144
144
|
interface Plan {
|
|
145
145
|
/** The new model; its entries are in ascending `id` order. */
|
|
146
146
|
readonly model: TermsModel;
|
|
@@ -171,7 +171,7 @@ interface Context {
|
|
|
171
171
|
|
|
172
172
|
type Update = readonly [id: string, change: (entry: Entry) => Entry];
|
|
173
173
|
|
|
174
|
-
/** Runs an operation (
|
|
174
|
+
/** Runs an operation (specs/terms.md#common-rules): the refusals, the new file, and its edit list. */
|
|
175
175
|
export function runOperation(input: OperationInput): OperationResult {
|
|
176
176
|
const { op } = input;
|
|
177
177
|
let model: TermsModel;
|
|
@@ -234,9 +234,9 @@ function plan(model: TermsModel, ctx: Context, op: OperationParams): Step {
|
|
|
234
234
|
}
|
|
235
235
|
|
|
236
236
|
// ---------------------------------------------------------------------------------------------
|
|
237
|
-
// Operations (
|
|
237
|
+
// Operations (specs/terms.md#operations)
|
|
238
238
|
|
|
239
|
-
/**
|
|
239
|
+
/** specs/terms.md#add. */
|
|
240
240
|
function planAdd(model: TermsModel, ctx: Context, p: AddParams): Step {
|
|
241
241
|
const id = normalise(p.id);
|
|
242
242
|
if (findEntry(model, id) !== undefined) {
|
|
@@ -266,7 +266,7 @@ function planAdd(model: TermsModel, ctx: Context, p: AddParams): Step {
|
|
|
266
266
|
};
|
|
267
267
|
}
|
|
268
268
|
|
|
269
|
-
/**
|
|
269
|
+
/** specs/terms.md#update. */
|
|
270
270
|
function planUpdate(model: TermsModel, ctx: Context, p: UpdateParams): Step {
|
|
271
271
|
const id = normalise(p.id);
|
|
272
272
|
const status = p.status === undefined ? undefined : statusOf(p.status);
|
|
@@ -393,7 +393,7 @@ function planUpdate(model: TermsModel, ctx: Context, p: UpdateParams): Step {
|
|
|
393
393
|
};
|
|
394
394
|
}
|
|
395
395
|
|
|
396
|
-
/**
|
|
396
|
+
/** specs/terms.md#rename. */
|
|
397
397
|
function planRename(model: TermsModel, ctx: Context, p: RenameParams): Step {
|
|
398
398
|
const id = normalise(p.id);
|
|
399
399
|
const blocked = activeRefusal(model, [id]);
|
|
@@ -402,7 +402,7 @@ function planRename(model: TermsModel, ctx: Context, p: RenameParams): Step {
|
|
|
402
402
|
}
|
|
403
403
|
const entry = entryOf(model, id);
|
|
404
404
|
const label = normalise(p.label);
|
|
405
|
-
// The old label is the entry's label as written (
|
|
405
|
+
// The old label is the entry's label as written (specs/terms.md#rename).
|
|
406
406
|
const oldLabel = entry.label;
|
|
407
407
|
if (normalise(oldLabel) === label) {
|
|
408
408
|
return refuse(`${id} is already labelled ${quote(label)}`);
|
|
@@ -449,7 +449,7 @@ function planRename(model: TermsModel, ctx: Context, p: RenameParams): Step {
|
|
|
449
449
|
};
|
|
450
450
|
}
|
|
451
451
|
|
|
452
|
-
/**
|
|
452
|
+
/** specs/terms.md#merge. */
|
|
453
453
|
function planMerge(model: TermsModel, ctx: Context, p: MergeParams): Step {
|
|
454
454
|
const id = normalise(p.id);
|
|
455
455
|
const into = normalise(p.into);
|
|
@@ -500,7 +500,7 @@ function planMerge(model: TermsModel, ctx: Context, p: MergeParams): Step {
|
|
|
500
500
|
};
|
|
501
501
|
}
|
|
502
502
|
|
|
503
|
-
/**
|
|
503
|
+
/** specs/terms.md#split. */
|
|
504
504
|
function planSplit(model: TermsModel, ctx: Context, p: SplitParams): Step {
|
|
505
505
|
const id = normalise(p.id);
|
|
506
506
|
const into = listOf(p.into);
|
|
@@ -535,7 +535,7 @@ function planSplit(model: TermsModel, ctx: Context, p: SplitParams): Step {
|
|
|
535
535
|
};
|
|
536
536
|
}
|
|
537
537
|
|
|
538
|
-
/**
|
|
538
|
+
/** specs/terms.md#deprecate. */
|
|
539
539
|
function planDeprecate(model: TermsModel, ctx: Context, p: DeprecateParams): Step {
|
|
540
540
|
const id = normalise(p.id);
|
|
541
541
|
const replacedBy = listOf(p.replacedBy);
|
|
@@ -602,7 +602,7 @@ function planDeprecate(model: TermsModel, ctx: Context, p: DeprecateParams): Ste
|
|
|
602
602
|
};
|
|
603
603
|
}
|
|
604
604
|
|
|
605
|
-
/**
|
|
605
|
+
/** specs/terms.md#reown. */
|
|
606
606
|
function planReown(model: TermsModel, ctx: Context, p: ReownParams): Step {
|
|
607
607
|
const id = normalise(p.id);
|
|
608
608
|
const owner = trimWhiteSpace(p.owner);
|
|
@@ -635,7 +635,7 @@ function planReown(model: TermsModel, ctx: Context, p: ReownParams): Step {
|
|
|
635
635
|
};
|
|
636
636
|
}
|
|
637
637
|
|
|
638
|
-
/**
|
|
638
|
+
/** specs/terms.md#import. */
|
|
639
639
|
function planImport(model: TermsModel, ctx: Context, p: ImportParams): Step {
|
|
640
640
|
if (model.entries.length > 0) {
|
|
641
641
|
return refuse(
|
|
@@ -689,7 +689,7 @@ function entryOf(model: TermsModel, id: string): Entry {
|
|
|
689
689
|
}
|
|
690
690
|
|
|
691
691
|
/**
|
|
692
|
-
* The refusal 3 of
|
|
692
|
+
* The refusal 3 of specs/terms.md#common-rules for the ids a parameter list names, in order: `no entry <x>`, or
|
|
693
693
|
* `<x> is deprecated`; undefined when every id names an active entry.
|
|
694
694
|
*/
|
|
695
695
|
function activeRefusal(model: TermsModel, ids: readonly string[]): string | undefined {
|
|
@@ -706,7 +706,7 @@ function activeRefusal(model: TermsModel, ids: readonly string[]): string | unde
|
|
|
706
706
|
}
|
|
707
707
|
|
|
708
708
|
/**
|
|
709
|
-
* Refusal 3 of
|
|
709
|
+
* Refusal 3 of specs/terms.md#common-rules for the id of `update` (specs/terms.md#update): a deprecated entry is refused only
|
|
710
710
|
* when `status` is absent.
|
|
711
711
|
*/
|
|
712
712
|
function updateIdRefusal(model: TermsModel, id: string, statusGiven: boolean): string | undefined {
|
|
@@ -720,35 +720,35 @@ function updateIdRefusal(model: TermsModel, id: string, statusGiven: boolean): s
|
|
|
720
720
|
return;
|
|
721
721
|
}
|
|
722
722
|
|
|
723
|
-
/** The entry returned to active: `reason` and `replacedBy` removed (
|
|
723
|
+
/** The entry returned to active: `reason` and `replacedBy` removed (specs/terms.md#update). */
|
|
724
724
|
function returnedToActive(entry: Entry): Entry {
|
|
725
725
|
const next: Entry = { ...entry, status: "active", replacedBy: [] };
|
|
726
726
|
delete next.reason;
|
|
727
727
|
return next;
|
|
728
728
|
}
|
|
729
729
|
|
|
730
|
-
/** The `status` parameter of `update`, normalised (
|
|
730
|
+
/** The `status` parameter of `update`, normalised (specs/terms.md#common-rules); the schema admits one value. */
|
|
731
731
|
function statusOf(value: string): "active" {
|
|
732
732
|
const status = normalise(value);
|
|
733
733
|
if (status === "active") return status;
|
|
734
734
|
throw new Error(`internal: no status ${status}`);
|
|
735
735
|
}
|
|
736
736
|
|
|
737
|
-
/** The `key` parameter, normalised (
|
|
737
|
+
/** The `key` parameter, normalised (specs/terms.md#common-rules); the tool's schema admits its three values. */
|
|
738
738
|
function keyOf(value: string): Key {
|
|
739
739
|
const key = normalise(value);
|
|
740
740
|
if (key === "shared" || key === "confusable" || key === "local") return key;
|
|
741
741
|
throw new Error(`internal: no key ${key}`);
|
|
742
742
|
}
|
|
743
743
|
|
|
744
|
-
/** The `reason` parameter of `deprecate`, normalised (
|
|
744
|
+
/** The `reason` parameter of `deprecate`, normalised (specs/terms.md#common-rules). */
|
|
745
745
|
function reasonOf(value: string): "renamed" | "out-of-scope" {
|
|
746
746
|
const reason = normalise(value);
|
|
747
747
|
if (reason === "renamed" || reason === "out-of-scope") return reason;
|
|
748
748
|
throw new Error(`internal: no reason ${reason}`);
|
|
749
749
|
}
|
|
750
750
|
|
|
751
|
-
/** The normalised strings of an optional list parameter (
|
|
751
|
+
/** The normalised strings of an optional list parameter (specs/terms.md#common-rules). */
|
|
752
752
|
function listOf(values: readonly string[] | undefined): string[] {
|
|
753
753
|
return (values ?? []).map((value) => normalise(value));
|
|
754
754
|
}
|
|
@@ -764,7 +764,7 @@ function updateEntries(model: TermsModel, updates: readonly Update[]): TermsMode
|
|
|
764
764
|
|
|
765
765
|
/**
|
|
766
766
|
* The phrases of `from` that `to` does not have, appended to the reject list of `to`
|
|
767
|
-
* (
|
|
767
|
+
* (specs/terms.md#merge and specs/terms.md#deprecate), with the phrases appended.
|
|
768
768
|
*/
|
|
769
769
|
function movePhrases(from: Entry, to: Entry): { moved: string[]; reject: string[] } {
|
|
770
770
|
const have = new Set(to.reject.map((phrase) => normalise(phrase)));
|
|
@@ -783,7 +783,7 @@ function isKind(occurrence: Occurrence, kind: Occurrence["variant"]["kind"], id:
|
|
|
783
783
|
}
|
|
784
784
|
|
|
785
785
|
/**
|
|
786
|
-
* The suggested text of an occurrence for a label (
|
|
786
|
+
* The suggested text of an occurrence for a label (specs/terms.md#common-rules): its plural when the variant is
|
|
787
787
|
* plural, none when the label has no plural, then its capitalised variant when the variant is
|
|
788
788
|
* capitalised and the label has one.
|
|
789
789
|
*/
|
|
@@ -798,7 +798,7 @@ function suggest(label: string, occurrence: Occurrence): string | null {
|
|
|
798
798
|
return occurrence.variant.capitalised ? (capitalisedOf(plural) ?? plural) : plural;
|
|
799
799
|
}
|
|
800
800
|
|
|
801
|
-
/** Whether the variant of an occurrence came from a `forms` phrase rather than the label (
|
|
801
|
+
/** Whether the variant of an occurrence came from a `forms` phrase rather than the label (specs/terms.md#common-rules). */
|
|
802
802
|
function fromForm(occurrence: Occurrence): boolean {
|
|
803
803
|
const { entry, phrase } = occurrence.variant;
|
|
804
804
|
return (
|
|
@@ -832,7 +832,7 @@ function editsOf(ws: Workspace, pick: Plan["pick"]): Edit[] {
|
|
|
832
832
|
}
|
|
833
833
|
|
|
834
834
|
/**
|
|
835
|
-
* The first finding of refusal 4 of
|
|
835
|
+
* The first finding of refusal 4 of specs/terms.md#common-rules: a T001, T002, T003, or T004 finding that concerns
|
|
836
836
|
* a changed entry.
|
|
837
837
|
*/
|
|
838
838
|
function refusalFour(planned: Plan, ws: Workspace): FileFinding | undefined {
|
package/lib/prose.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Prose of a document: runs of characters with source positions, suppression comments, tables,
|
|
3
|
-
* and headings. See specs/terms.md,
|
|
3
|
+
* and headings. See specs/terms.md#documents, specs/terms.md#prose, and specs/terms.md#import.
|
|
4
4
|
*
|
|
5
5
|
* Approach: the document is parsed with mdast (CommonMark plus the GFM table extension). mdast
|
|
6
6
|
* gives the block and inline structure and exact UTF-16 source offsets of every node, but merges
|
|
@@ -49,7 +49,7 @@ export interface ProseRun {
|
|
|
49
49
|
export interface ProseCell {
|
|
50
50
|
readonly line: number;
|
|
51
51
|
readonly column: number;
|
|
52
|
-
/** The cell text (
|
|
52
|
+
/** The cell text (specs/terms.md#import): normalised, code span content and image descriptions included. */
|
|
53
53
|
readonly text: string;
|
|
54
54
|
/** Destinations of the links in the cell, in document order. */
|
|
55
55
|
readonly links: readonly string[];
|
|
@@ -62,7 +62,7 @@ export interface ProseRow {
|
|
|
62
62
|
|
|
63
63
|
export interface ProseTable {
|
|
64
64
|
readonly line: number;
|
|
65
|
-
/** Whether the header cell texts are `Term`, `Meaning`, `Owner`, `Avoid or distinguish` (
|
|
65
|
+
/** Whether the header cell texts are `Term`, `Meaning`, `Owner`, `Avoid or distinguish` (specs/terms.md#import). */
|
|
66
66
|
readonly thesaurus: boolean;
|
|
67
67
|
readonly header: ProseRow;
|
|
68
68
|
readonly body: readonly ProseRow[];
|
|
@@ -71,14 +71,14 @@ export interface ProseTable {
|
|
|
71
71
|
export interface ProseHeading {
|
|
72
72
|
readonly line: number;
|
|
73
73
|
readonly column: number;
|
|
74
|
-
/** The heading text, as the cell text of
|
|
74
|
+
/** The heading text, as the cell text of specs/terms.md#import. */
|
|
75
75
|
readonly text: string;
|
|
76
76
|
}
|
|
77
77
|
|
|
78
78
|
export interface ProseSuppression {
|
|
79
79
|
readonly line: number;
|
|
80
80
|
readonly column: number;
|
|
81
|
-
/** The target line; absent when no nonblank line follows the comment's block (
|
|
81
|
+
/** The target line; absent when no nonblank line follows the comment's block (specs/terms.md#suppression-comments). */
|
|
82
82
|
readonly targetLine?: number;
|
|
83
83
|
readonly valid: boolean;
|
|
84
84
|
readonly code?: "P001" | "P002";
|
|
@@ -89,7 +89,7 @@ export interface ProseDocument {
|
|
|
89
89
|
readonly headings: readonly ProseHeading[];
|
|
90
90
|
readonly tables: readonly ProseTable[];
|
|
91
91
|
readonly suppressions: readonly ProseSuppression[];
|
|
92
|
-
/** The raw values of the code spans of the document, in document order (
|
|
92
|
+
/** The raw values of the code spans of the document, in document order (specs/terms.md#uses). */
|
|
93
93
|
readonly codeSpans: readonly string[];
|
|
94
94
|
}
|
|
95
95
|
|
|
@@ -139,7 +139,7 @@ interface Ctx {
|
|
|
139
139
|
}
|
|
140
140
|
|
|
141
141
|
/**
|
|
142
|
-
* A container enclosing a prose block (
|
|
142
|
+
* A container enclosing a prose block (specs/terms.md#prose-runs): its prefix is stripped from the start of a
|
|
143
143
|
* continuation line before the prose resumes.
|
|
144
144
|
*/
|
|
145
145
|
type Container =
|
|
@@ -227,7 +227,7 @@ function codeSpansOf(node: Nodes, out: string[]): void {
|
|
|
227
227
|
}
|
|
228
228
|
}
|
|
229
229
|
|
|
230
|
-
/** The first nonblank line after the block, ignoring its enclosing quote markers (
|
|
230
|
+
/** The first nonblank line after the block, ignoring its enclosing quote markers (specs/terms.md#suppression-comments). */
|
|
231
231
|
function nextNonblankLine(
|
|
232
232
|
text: string,
|
|
233
233
|
after: number,
|
|
@@ -319,7 +319,7 @@ function containerCtx(ctx: Ctx, node: Nodes): Ctx {
|
|
|
319
319
|
|
|
320
320
|
/**
|
|
321
321
|
* The indentation of a list item's content from its enclosing container's content (CommonMark
|
|
322
|
-
* 0.31.2,
|
|
322
|
+
* 0.31.2, specs/terms.md#file-checks): the marker's width plus the columns that follow it, one when there are none
|
|
323
323
|
* or more than four, the rest being content indentation.
|
|
324
324
|
*/
|
|
325
325
|
function listItemIndent(t: string, offset: number, column: number): number {
|
|
@@ -366,7 +366,7 @@ function visitTable(ctx: Ctx, node: Table, doc: Document): void {
|
|
|
366
366
|
const { line, column } = ctx.locate(offsetOf(cell.position?.start));
|
|
367
367
|
const links: string[] = [];
|
|
368
368
|
linkDestinations(ctx, cell.children, links);
|
|
369
|
-
//
|
|
369
|
+
// specs/terms.md#prose-runs: the Term and Avoid or distinguish cells of a thesaurus table hold no prose.
|
|
370
370
|
const excluded = thesaurus && (c === 0 || c === 3);
|
|
371
371
|
const items: ProseItem[] = [];
|
|
372
372
|
inline(ctx, cell.children, "prose", items, excluded ? undefined : doc.suppressions);
|
|
@@ -673,7 +673,7 @@ function commentText(raw: string): string | undefined {
|
|
|
673
673
|
return trimWhite(t.slice(4, -3));
|
|
674
674
|
}
|
|
675
675
|
|
|
676
|
-
/** Classifies a comment text: undefined when it is not a suppression comment (
|
|
676
|
+
/** Classifies a comment text: undefined when it is not a suppression comment (specs/terms.md#suppression-comments). */
|
|
677
677
|
function suppressionOf(text: string): { valid: boolean; code?: "P001" | "P002" } | undefined {
|
|
678
678
|
if (!TERMS_IGNORE.test(text)) {
|
|
679
679
|
return undefined;
|
package/lib/terms-file.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The terms file (specs/terms.md): parsing of its bytes into a typed model with YAML node
|
|
3
|
-
* positions (
|
|
3
|
+
* positions (specs/terms.md#terms-file, and the T001 part of specs/terms.md#file-checks), and the
|
|
4
|
+
* canonical writer (specs/terms.md#canonical-form).
|
|
4
5
|
*/
|
|
5
6
|
|
|
6
7
|
import {
|
|
@@ -18,10 +19,10 @@ import {
|
|
|
18
19
|
} from "yaml";
|
|
19
20
|
import { compareFindings, compareUtf16, normalise, quote, type Finding } from "./text.ts";
|
|
20
21
|
|
|
21
|
-
/** The workspace path of the terms file (
|
|
22
|
+
/** The workspace path of the terms file (specs/terms.md#location-and-syntax). */
|
|
22
23
|
export const TERMS_PATH = ".pi/pi-dent/terms.yaml";
|
|
23
24
|
|
|
24
|
-
/** A line and a column, both counted from 1; the column in UTF-16 code units (
|
|
25
|
+
/** A line and a column, both counted from 1; the column in UTF-16 code units (specs/terms.md#documents). */
|
|
25
26
|
export interface Position {
|
|
26
27
|
readonly line: number;
|
|
27
28
|
readonly column: number;
|
|
@@ -32,7 +33,7 @@ export type Status = "active" | "deprecated";
|
|
|
32
33
|
export type Reason = "merged" | "split" | "renamed" | "out-of-scope";
|
|
33
34
|
export type Op = "add" | "update" | "import" | "rename" | "merge" | "split" | "deprecate" | "reown";
|
|
34
35
|
|
|
35
|
-
/** A history item (
|
|
36
|
+
/** A history item (specs/terms.md#history-items), one variant per operand set. */
|
|
36
37
|
export type HistoryItem =
|
|
37
38
|
| { date: string; op: "add" }
|
|
38
39
|
| { date: string; op: "update" }
|
|
@@ -49,12 +50,12 @@ export type HistoryItem =
|
|
|
49
50
|
| { date: string; op: "deprecate"; from: string }
|
|
50
51
|
| { date: string; op: "reown"; from: string; to: string };
|
|
51
52
|
|
|
52
|
-
/** The file-level settings (
|
|
53
|
+
/** The file-level settings (specs/terms.md#root). */
|
|
53
54
|
export interface Settings {
|
|
54
55
|
checkAfterEdit?: boolean;
|
|
55
56
|
}
|
|
56
57
|
|
|
57
|
-
/** An entry (
|
|
58
|
+
/** An entry (specs/terms.md#entries). An absent list is an empty list. */
|
|
58
59
|
export interface Entry {
|
|
59
60
|
id: string;
|
|
60
61
|
label: string;
|
|
@@ -71,7 +72,7 @@ export interface Entry {
|
|
|
71
72
|
history: HistoryItem[];
|
|
72
73
|
}
|
|
73
74
|
|
|
74
|
-
/** An entry that is not left out (
|
|
75
|
+
/** An entry that is not left out (specs/terms.md#entries), with its place in the `terms` sequence. */
|
|
75
76
|
export interface EntryRecord {
|
|
76
77
|
readonly index: number;
|
|
77
78
|
/** The value path of the entry, `terms[<index>]`. */
|
|
@@ -79,7 +80,7 @@ export interface EntryRecord {
|
|
|
79
80
|
readonly entry: Entry;
|
|
80
81
|
}
|
|
81
82
|
|
|
82
|
-
/** A T001 finding; `valuePath` is its `<value path>` as
|
|
83
|
+
/** A T001 finding; `valuePath` is its `<value path>` as specs/terms.md#file-checks writes it. */
|
|
83
84
|
export interface T001Finding extends Finding {
|
|
84
85
|
readonly valuePath: string;
|
|
85
86
|
}
|
|
@@ -115,7 +116,7 @@ export interface TermsModel {
|
|
|
115
116
|
|
|
116
117
|
const KEY_VALUES: readonly Key[] = ["shared", "confusable", "local"];
|
|
117
118
|
const STATUS_VALUES: readonly Status[] = ["active", "deprecated"];
|
|
118
|
-
/** The one value of the `status` operand of an `update` history item (
|
|
119
|
+
/** The one value of the `status` operand of an `update` history item (specs/terms.md#history-items). */
|
|
119
120
|
const OLD_STATUS_VALUES: readonly "deprecated"[] = ["deprecated"];
|
|
120
121
|
const REASON_VALUES: readonly Reason[] = ["merged", "split", "renamed", "out-of-scope"];
|
|
121
122
|
const OP_VALUES: readonly Op[] = [
|
|
@@ -258,14 +259,14 @@ function escapeUnprintable(s: string): string {
|
|
|
258
259
|
return out;
|
|
259
260
|
}
|
|
260
261
|
|
|
261
|
-
/** Whether the escaped text holds `\"` and no other backslash (
|
|
262
|
+
/** Whether the escaped text holds `\"` and no other backslash (specs/terms.md#canonical-form, rule 6). */
|
|
262
263
|
function onlyQuoteEscapes(escaped: string): boolean {
|
|
263
264
|
const backslashes = escaped.split("\\").length - 1;
|
|
264
265
|
const quotes = escaped.split('\\"').length - 1;
|
|
265
266
|
return quotes > 0 && backslashes === quotes;
|
|
266
267
|
}
|
|
267
268
|
|
|
268
|
-
/** A string as the canonical form writes it (
|
|
269
|
+
/** A string as the canonical form writes it (specs/terms.md#canonical-form, rule 6). */
|
|
269
270
|
function scalar(s: string): string {
|
|
270
271
|
if (PLAIN_RE.test(s) && typeof parse(s, { schema: "core" }) === "string") {
|
|
271
272
|
return s;
|
|
@@ -284,7 +285,7 @@ function listLines(out: string[], field: string, items: readonly string[]): void
|
|
|
284
285
|
}
|
|
285
286
|
}
|
|
286
287
|
|
|
287
|
-
/** A history item as a block mapping (
|
|
288
|
+
/** A history item as a block mapping (specs/terms.md#canonical-form, rule 5); no flow collection is written. */
|
|
288
289
|
function historyLines(item: HistoryItem): string[] {
|
|
289
290
|
const out = [` - date: ${scalar(item.date)}`, ` op: ${scalar(item.op)}`];
|
|
290
291
|
const add = (name: string, value: string): void => {
|
|
@@ -371,7 +372,7 @@ function entryLines(entry: Entry): string[] {
|
|
|
371
372
|
}
|
|
372
373
|
|
|
373
374
|
/**
|
|
374
|
-
* The canonical form of the terms file (
|
|
375
|
+
* The canonical form of the terms file (specs/terms.md#canonical-form): entries are written in the order given.
|
|
375
376
|
*/
|
|
376
377
|
export function writeTermsFile(model: TermsModel): string {
|
|
377
378
|
const lines: string[] = [];
|
|
@@ -400,7 +401,7 @@ interface Problem {
|
|
|
400
401
|
/** The value path, `""` for the root, or `null` for `(file)`. */
|
|
401
402
|
readonly path: string | null;
|
|
402
403
|
readonly reason: string;
|
|
403
|
-
/** Unknown-key problems do not leave their entry out (
|
|
404
|
+
/** Unknown-key problems do not leave their entry out (specs/terms.md#entries). */
|
|
404
405
|
readonly unknownKeys: boolean;
|
|
405
406
|
readonly pos: Position | undefined;
|
|
406
407
|
}
|
|
@@ -462,7 +463,7 @@ function propsStart(src: string, start: number): number {
|
|
|
462
463
|
}
|
|
463
464
|
|
|
464
465
|
/**
|
|
465
|
-
* The offset a value's finding is placed at (
|
|
466
|
+
* The offset a value's finding is placed at (specs/terms.md#file-checks): an alias at its own `*`, a node with an
|
|
466
467
|
* anchor or a tag at the start of its properties, and any other node at its start.
|
|
467
468
|
*/
|
|
468
469
|
function positionOffset(ctx: Ctx, node: unknown): number | undefined {
|
|
@@ -570,14 +571,14 @@ function isUnder(path: string, entryPath: string): boolean {
|
|
|
570
571
|
return path === entryPath || path.startsWith(`${entryPath}.`) || path.startsWith(`${entryPath}[`);
|
|
571
572
|
}
|
|
572
573
|
|
|
573
|
-
/** True when a T001 finding that is not for unknown keys is at or under the entry (
|
|
574
|
+
/** True when a T001 finding that is not for unknown keys is at or under the entry (specs/terms.md#entries). */
|
|
574
575
|
function isLeftOut(ctx: Ctx, entryPath: string): boolean {
|
|
575
576
|
return ctx.problems.some(
|
|
576
577
|
(problem) => !problem.unknownKeys && problem.path !== null && isUnder(problem.path, entryPath),
|
|
577
578
|
);
|
|
578
579
|
}
|
|
579
580
|
|
|
580
|
-
/** The `update` history item with the operands it holds (
|
|
581
|
+
/** The `update` history item with the operands it holds (specs/terms.md#history-items). */
|
|
581
582
|
function updateHistoryItem(
|
|
582
583
|
date: string,
|
|
583
584
|
meaning: string | undefined,
|
|
@@ -835,7 +836,7 @@ function readEntry(ctx: Ctx, path: string, map: YAMLMap): Entry | undefined {
|
|
|
835
836
|
};
|
|
836
837
|
}
|
|
837
838
|
|
|
838
|
-
/** The string `id` of an entry mapping, or undefined when it has none (
|
|
839
|
+
/** The string `id` of an entry mapping, or undefined when it has none (specs/terms.md#entries). */
|
|
839
840
|
function stringIdOf(ctx: Ctx, map: YAMLMap): string | undefined {
|
|
840
841
|
const idField = fieldsOf(map).find((field) => field.name === "id");
|
|
841
842
|
const value = idField === undefined ? undefined : scalarValue(ctx, idField.value);
|
|
@@ -902,7 +903,7 @@ function finish(
|
|
|
902
903
|
};
|
|
903
904
|
}
|
|
904
905
|
|
|
905
|
-
/** The result of a failure that leaves no settings and no entries (
|
|
906
|
+
/** The result of a failure that leaves no settings and no entries (specs/terms.md#location-and-syntax). */
|
|
906
907
|
function fileFailure(reason: string, pos?: Position): TermsFile {
|
|
907
908
|
const problem: Problem = { path: null, reason, unknownKeys: false, pos };
|
|
908
909
|
return {
|
|
@@ -922,7 +923,8 @@ function parserMessage(message: string): string {
|
|
|
922
923
|
}
|
|
923
924
|
|
|
924
925
|
/**
|
|
925
|
-
* Parses the terms file bytes (specs/terms.md
|
|
926
|
+
* Parses the terms file bytes (specs/terms.md#terms-file and the T001 part of
|
|
927
|
+
* specs/terms.md#file-checks).
|
|
926
928
|
*/
|
|
927
929
|
export function parseTermsFile(bytes: Uint8Array): TermsFile {
|
|
928
930
|
let text: string;
|
package/lib/text.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Shared text helpers of specs/terms.md: character classes (
|
|
3
|
-
* (
|
|
2
|
+
* Shared text helpers of specs/terms.md: character classes (specs/terms.md#phrases), normalisation
|
|
3
|
+
* (specs/terms.md#phrases), code-unit comparison, quoting in messages, and findings (specs/terms.md#findings).
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
/** One reported problem of a check (specs/terms.md
|
|
6
|
+
/** One reported problem of a check (specs/terms.md#findings). */
|
|
7
7
|
export interface Finding {
|
|
8
8
|
readonly path: string;
|
|
9
9
|
readonly line: number;
|
|
@@ -33,7 +33,7 @@ export function normalise(s: string): string {
|
|
|
33
33
|
.replace(/ $/, "");
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
/** `s` with leading and trailing white space (
|
|
36
|
+
/** `s` with leading and trailing white space (specs/terms.md#phrases) removed, inner characters kept. */
|
|
37
37
|
export function trimWhiteSpace(s: string): string {
|
|
38
38
|
return s.replace(/^\p{White_Space}+|\p{White_Space}+$/gu, "");
|
|
39
39
|
}
|
|
@@ -56,7 +56,7 @@ export function findingText(f: Finding): string {
|
|
|
56
56
|
return `${f.path}:${f.line}:${f.column} ${f.code} ${f.message}`;
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
/** Orders findings by path, line, column, code, then message (
|
|
59
|
+
/** Orders findings by path, line, column, code, then message (specs/terms.md#findings). */
|
|
60
60
|
export function compareFindings(a: Finding, b: Finding): number {
|
|
61
61
|
return (
|
|
62
62
|
compareUtf16(a.path, b.path) ||
|