-
Notifications
You must be signed in to change notification settings - Fork 2.9k
refactor: improve matrix parsing validation and performance #3129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
manucian-official
wants to merge
6
commits into
tinyhumansai:main
Choose a base branch
from
manucian-official:patch-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+103
−19
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
678edfa
refactor: improve matrix parsing validation and performance
manucian-official 796d2c6
Merge remote-tracking branch 'upstream/main' into pr/3129
senamakel 198d554
Merge branch 'main' into pr/3129
senamakel fe54cfc
Merge remote-tracking branch 'upstream/main' into pr/3129
senamakel 7233e61
fix(scripts): loosen ROW_REGEX so invalid statuses reach validators
senamakel b70a436
fix(scripts): address review feedback on coverage-matrix-parser
senamakel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,37 +1,86 @@ | ||
| const ROW_REGEX = /^\| (\d+(?:\.\d+){2,3}) \| ([^|]+) \| ([^|]+) \| ([^|]+) \| ([^|]+) \| ([^|]+) \|/; | ||
| const ROW_REGEX = | ||
| /^\|\s*(\d+(?:\.\d+){2,3})\s*\|\s*([^|]+?)\s*\|\s*([^|]+?)\s*\|\s*([^|]+?)\s*\|\s*([^|]+?)\s*\|\s*([^|]*?)\s*\|\s*$/u; | ||
|
|
||
| const ID_REGEX = /^\d+(?:\.\d+){2,3}$/; | ||
| const VALID_STATUS = new Set(['✅', '🟡', '❌', '🚫']); | ||
|
|
||
| const VALID_STATUS = new Set(["✅", "🟡", "❌", "🚫"]); | ||
|
|
||
| export function parseMatrix(markdown) { | ||
| if (typeof markdown !== "string") { | ||
| return { | ||
| rows: [], | ||
| errors: ["Input must be a string"], | ||
| }; | ||
| } | ||
|
|
||
| const rows = []; | ||
| const errors = []; | ||
| if (typeof markdown !== 'string') { | ||
| return { rows, errors }; | ||
| } | ||
| for (const line of markdown.split(/\r?\n/)) { | ||
| const match = line.match(ROW_REGEX); | ||
|
|
||
| const lines = markdown.split(/\r?\n/); | ||
|
|
||
| for (let i = 0; i < lines.length; i++) { | ||
| const line = lines[i]; | ||
|
|
||
| const match = ROW_REGEX.exec(line); | ||
| if (!match) continue; | ||
| const [, id, name, layer, path, status, notes] = match.map((v) => (typeof v === 'string' ? v.trim() : v)); | ||
|
|
||
| const [, id, name, layer, path, rawStatus, notes] = match; | ||
|
|
||
| const status = rawStatus.trim(); | ||
|
|
||
| if (!ID_REGEX.test(id)) { | ||
| errors.push(`Invalid ID format: ${id}`); | ||
| errors.push(`Line ${i + 1}: Invalid ID format "${id}"`); | ||
| continue; | ||
| } | ||
|
|
||
| if (!VALID_STATUS.has(status)) { | ||
| errors.push(`Row ${id}: invalid status "${status}" (must be one of ${[...VALID_STATUS].join(' ')})`); | ||
| errors.push(`Line ${i + 1} (${id}): invalid status "${status}"`); | ||
| continue; | ||
| } | ||
| rows.push({ id, name, layer, path, status, notes }); | ||
|
|
||
| rows.push({ | ||
| id, | ||
| name: name.trim(), | ||
| layer: layer.trim(), | ||
| path: path.trim(), | ||
| status, | ||
| notes: notes.trim(), | ||
| }); | ||
| } | ||
|
|
||
| return { rows, errors }; | ||
| } | ||
|
|
||
| export function validateAgainstCatalog(parsedRows, catalogIds) { | ||
| const seen = new Map(); | ||
| for (const row of parsedRows) { | ||
| seen.set(row.id, (seen.get(row.id) ?? 0) + 1); | ||
| export function validateAgainstCatalog(rows, catalogIds) { | ||
| const counts = new Map(); | ||
|
|
||
| for (const { id } of rows) { | ||
| counts.set(id, (counts.get(id) ?? 0) + 1); | ||
| } | ||
|
|
||
| const duplicates = []; | ||
|
|
||
| for (const [id, count] of counts) { | ||
| if (count > 1) { | ||
| duplicates.push(id); | ||
| } | ||
| } | ||
|
|
||
| const catalogSet = | ||
| catalogIds instanceof Set ? catalogIds : new Set(catalogIds); | ||
|
|
||
| const missingFromMatrix = []; | ||
|
|
||
| for (const id of catalogSet) { | ||
| if (!counts.has(id)) { | ||
| missingFromMatrix.push(id); | ||
| } | ||
| } | ||
| const duplicates = [...seen.entries()].filter(([, count]) => count > 1).map(([id]) => id); | ||
| const present = new Set(seen.keys()); | ||
| const missingFromMatrix = [...catalogIds].filter((id) => !present.has(id)); | ||
| return { missingFromMatrix, duplicates }; | ||
|
|
||
| return { | ||
| missingFromMatrix, | ||
| duplicates, | ||
| totalRows: rows.length, | ||
| uniqueRows: counts.size, | ||
|
senamakel marked this conversation as resolved.
|
||
| }; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.