Skip to content

Commit 3e9743a

Browse files
authored
fix(knowledge): status phrases are matched across wrapped lines (#14101)
* test(knowledge): a status phrase that wraps across a line break warns Red: check:knowledge reads status phrases one physical line at a time, so "not" at the end of one line and "built." at the start of the next is missed. Two list items or two table rows that only touch must not join. * fix(knowledge): status phrases are matched across wrapped lines check:knowledge matched each status phrase within one physical line, so a phrase wrapped across a line break went unreported. It now matches each run of wrapped lines (a list item or table row starts its own run) and reports the hit at the line where the phrase starts. AST-057 gets the one newly found phrase reworded; the two in AST-103 are reworded by #14089. * test(knowledge): a table without outer pipes keeps its rows apart Red: a GitHub table may leave out the outer pipes, so a row need not start with one, and the wrapped-phrase match joins two such rows. * fix(knowledge): a table row without outer pipes is a run of its own In a paragraph with a table's delimiter row, every line with a pipe is a row, as GitHub tables may leave out the outer pipes.
1 parent 9dfe5ff commit 3e9743a

3 files changed

Lines changed: 116 additions & 11 deletions

File tree

‎docs/specs/AST-057-row-actions/spec.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ and its panels stay put, so no element is added to any row.
166166
- **Vertical swipes**, and a swipe whose only
167167
meaning is dismissal (Toast already has that in `useToastGesture`).
168168
- **Public out-of-flow slots on `Item`.** Deliberately not admitted: the
169-
panel positions are internal, rendered from `swipe`. A second
170-
consumer — an accent bar, a slide-in checkbox, a drag handle — is a
169+
panel positions are internal, rendered from `swipe`. Any other out-of-flow
170+
part — an accent bar, a slide-in checkbox, a drag handle — needs its own
171171
`component:Item` anatomy record with its own admission argument.
172172
- **Swipe actions on an element that is not an `Item`.** `createSwipe`
173173
configures `Item`; attaching its result to any other element is not

‎scripts/lib/knowledge-status-phrases.mjs‎

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
/**
44
* @file Find temporary project status in spec and architecture records.
55
* @input A record's Markdown text, or a repository path
6-
* @output Build-status and sequencing phrases by line; `pnpm check:knowledge`
6+
* @output Build-status and sequencing phrases, each at the line where it
7+
* starts, also when it wraps across lines; `pnpm check:knowledge`
78
* prints them as warnings and never fails on them
89
* @position Advisory record-writing check beside scripts/check-knowledge.mjs
910
*/
@@ -97,13 +98,60 @@ function proseLines(paragraph) {
9798
return text.split('\n');
9899
}
99100

100-
function matches(text, patterns) {
101-
const hits = new Set();
101+
const LIST_ITEM = /^\s*(?:[-*+]|\d+[.)])\s/;
102+
const TABLE_ROW = /^\s*\|/;
103+
// A table's delimiter row, with or without the outer pipes: `--- | :---:`.
104+
const TABLE_DELIMITER = /^\s*\|?\s*:?-+:?\s*(?:\|\s*:?-+:?\s*)*\|?\s*$/;
105+
106+
/**
107+
* A paragraph's prose lines in runs that a phrase may wrap across. A list item
108+
* starts a new run, and a table row is a run of its own, so a phrase never
109+
* joins two items or two rows. In a paragraph with a delimiter row, every line
110+
* with a pipe is a row, as a table may leave out its outer pipes.
111+
*/
112+
function wrapRuns(lines) {
113+
const isTable = lines.some(
114+
({text}) =>
115+
text.includes('-') && text.includes('|') && TABLE_DELIMITER.test(text),
116+
);
117+
const runs = [];
118+
let run = null;
119+
lines.forEach(({line, text}) => {
120+
const row = TABLE_ROW.test(text) || (isTable && text.includes('|'));
121+
if (!run || row || run.row || LIST_ITEM.test(text)) {
122+
run = {row, lines: []};
123+
runs.push(run);
124+
}
125+
run.lines.push({line, text});
126+
});
127+
return runs;
128+
}
129+
130+
/**
131+
* Status phrases in one run of lines, each at the line where it starts.
132+
* @returns {Map<number, Set<string>>}
133+
*/
134+
function runMatches(run, patterns) {
135+
const starts = [];
136+
let offset = 0;
137+
for (const {line, text} of run.lines) {
138+
starts.push([offset, line]);
139+
offset += text.length + 1;
140+
}
141+
const text = run.lines.map(entry => entry.text).join('\n');
142+
const hits = new Map();
102143
for (const [phrase, pattern] of patterns) {
103144
pattern.lastIndex = 0;
104-
if (pattern.test(text)) hits.add(phrase);
145+
for (const match of text.matchAll(pattern)) {
146+
let line = starts[0][1];
147+
for (const [start, startLine] of starts) {
148+
if (start <= match.index) line = startLine;
149+
}
150+
if (!hits.has(line)) hits.set(line, new Set());
151+
hits.get(line).add(phrase);
152+
}
105153
}
106-
return [...hits];
154+
return hits;
107155
}
108156

109157
function isStandaloneInProgress(text) {
@@ -130,13 +178,21 @@ export function findStatusPhrases(content) {
130178

131179
// Prose paragraphs: runs of lines outside fences, block quotes, and headings.
132180
const prose = new Map();
181+
const phraseHits = new Map();
133182
let paragraph = [];
134183
let paragraphStart = 0;
135184
const flush = () => {
136185
if (!paragraph.length) return;
137-
proseLines(paragraph).forEach((text, offset) =>
138-
prose.set(paragraphStart + offset, text),
139-
);
186+
const texts = proseLines(paragraph).map((text, offset) => ({
187+
line: paragraphStart + offset,
188+
text,
189+
}));
190+
for (const {line, text} of texts) prose.set(line, text);
191+
for (const run of wrapRuns(texts)) {
192+
for (const [line, found] of runMatches(run, STATUS_PATTERNS)) {
193+
phraseHits.set(line, found);
194+
}
195+
}
140196
paragraph = [];
141197
};
142198
let fence = null;
@@ -166,7 +222,10 @@ export function findStatusPhrases(content) {
166222
const hits = [];
167223
let statusLineSeen = false;
168224
for (const [i, text] of [...prose].sort(([a], [b]) => a - b)) {
169-
const found = matches(text, STATUS_PATTERNS);
225+
// In phrase-list order, as the patterns ran.
226+
const found = STATUS_PHRASES.filter(phrase =>
227+
phraseHits.get(i)?.has(phrase),
228+
);
170229
if (STATUS_LINE.test(text) || isStandaloneInProgress(text)) {
171230
if (statusLineSeen && IN_PROGRESS.test(text)) found.push('in progress');
172231
IN_PROGRESS.lastIndex = 0;

‎scripts/lib/knowledge-status-phrases.test.mjs‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,52 @@ describe('status phrases', () => {
5353
expect(phrases(record(line))).toEqual([phrase]);
5454
});
5555

56+
it('warns on a phrase that wraps across a line break, at the line where it starts', () => {
57+
const hits = findStatusPhrases(
58+
record(
59+
'The Selector record says the module is not',
60+
'built. Table has not',
61+
'started either.',
62+
),
63+
);
64+
expect(hits.map(hit => [hit.line, hit.phrase])).toEqual([
65+
[7, 'not built'],
66+
[8, 'has not started'],
67+
]);
68+
});
69+
70+
it('does not join two list items or two table rows into one phrase', () => {
71+
expect(
72+
phrases(
73+
record(
74+
'- The trigger is not',
75+
'- built into the field.',
76+
'',
77+
'| Part | Note |',
78+
'| --- | --- |',
79+
'| Label | not |',
80+
'| built | here |',
81+
),
82+
),
83+
).toEqual([]);
84+
});
85+
86+
it('does not join two rows of a table written without outer pipes', () => {
87+
expect(
88+
phrases(
89+
record('Part | Note', '--- | ---', 'Label | not', 'built | here'),
90+
),
91+
).toEqual([]);
92+
});
93+
94+
it('joins the wrapped lines of one list item', () => {
95+
expect(
96+
findStatusPhrases(
97+
record('- The Selector module is not', ' built.', '- Table is.'),
98+
).map(hit => [hit.line, hit.phrase]),
99+
).toEqual([[7, 'not built']]);
100+
});
101+
56102
it('matches whole words only', () => {
57103
expect(
58104
phrases(record('The firstconsumer field and preadoption order.')),

0 commit comments

Comments
 (0)