-
Notifications
You must be signed in to change notification settings - Fork 2.7k
fix: detect hidden requirements in main specs #966
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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 |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| const REQUIREMENTS_SECTION_HEADER = /^##\s+Requirements\s*$/i; | ||
| const TOP_LEVEL_SECTION_HEADER = /^##\s+/; | ||
| const DELTA_HEADER = /^##\s+(ADDED|MODIFIED|REMOVED|RENAMED)\s+Requirements\s*$/i; | ||
| const REQUIREMENT_HEADER = /^###\s+Requirement:\s*(.+)\s*$/; | ||
|
Comment on lines
+1
to
+4
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Support Markdown-valid indentation bounds for headings/fences. Current patterns can miss valid indented headings (1–3 spaces) and can over-match 4-space-indented fence-like lines. 💡 Proposed fix-const REQUIREMENTS_SECTION_HEADER = /^##\s+Requirements\s*$/i;
-const TOP_LEVEL_SECTION_HEADER = /^##\s+/;
-const DELTA_HEADER = /^##\s+(ADDED|MODIFIED|REMOVED|RENAMED)\s+Requirements\s*$/i;
-const REQUIREMENT_HEADER = /^###\s+Requirement:\s*(.+)\s*$/;
+const REQUIREMENTS_SECTION_HEADER = /^\s{0,3}##\s+Requirements\s*$/i;
+const TOP_LEVEL_SECTION_HEADER = /^\s{0,3}##\s+/;
+const DELTA_HEADER = /^\s{0,3}##\s+(ADDED|MODIFIED|REMOVED|RENAMED)\s+Requirements\s*$/i;
+const REQUIREMENT_HEADER = /^\s{0,3}###\s+Requirement:\s*(.+)\s*$/;
- const fenceMatch = line.match(/^\s*(`{3,}|~{3,})(.*)$/);
+ const fenceMatch = line.match(/^\s{0,3}(`{3,}|~{3,})(.*)$/);Also applies to: 82-82 🤖 Prompt for AI Agents |
||
|
|
||
| export interface MainSpecStructureIssue { | ||
| kind: 'delta-header' | 'requirement-outside-requirements'; | ||
| line: number; | ||
| header: string; | ||
| message: string; | ||
| } | ||
|
|
||
| export function findMainSpecStructureIssues(content: string): MainSpecStructureIssue[] { | ||
| const normalized = content.replace(/\r\n?/g, '\n'); | ||
| const stripped = stripFencedCodeBlocksPreservingLines(normalized); | ||
| const lines = stripped.split('\n'); | ||
| const issues: MainSpecStructureIssue[] = []; | ||
|
|
||
| const requirementsHeaderIndex = lines.findIndex(line => REQUIREMENTS_SECTION_HEADER.test(line)); | ||
| let requirementsEndIndex = lines.length; | ||
|
|
||
| if (requirementsHeaderIndex !== -1) { | ||
| for (let i = requirementsHeaderIndex + 1; i < lines.length; i++) { | ||
| if (TOP_LEVEL_SECTION_HEADER.test(lines[i])) { | ||
| requirementsEndIndex = i; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (let i = 0; i < lines.length; i++) { | ||
| const line = lines[i]; | ||
| const trimmed = line.trim(); | ||
| if (!trimmed) { | ||
| continue; | ||
| } | ||
|
|
||
| if (DELTA_HEADER.test(line)) { | ||
| issues.push({ | ||
| kind: 'delta-header', | ||
| line: i + 1, | ||
| header: trimmed, | ||
| message: | ||
| `Main spec contains delta header "${trimmed}". ` + | ||
| 'Delta headers are only valid inside openspec/changes/<name>/specs/<capability>/spec.md ' + | ||
| 'and truncate the parsed ## Requirements section.', | ||
| }); | ||
| continue; | ||
| } | ||
|
|
||
| const requirementMatch = line.match(REQUIREMENT_HEADER); | ||
| if (!requirementMatch) { | ||
| continue; | ||
| } | ||
|
|
||
| const insideRequirements = | ||
| requirementsHeaderIndex !== -1 && | ||
| i > requirementsHeaderIndex && | ||
| i < requirementsEndIndex; | ||
|
|
||
| if (!insideRequirements) { | ||
| issues.push({ | ||
| kind: 'requirement-outside-requirements', | ||
| line: i + 1, | ||
| header: trimmed, | ||
| message: | ||
| `Requirement header "${trimmed}" appears outside the main ## Requirements section. ` + | ||
| 'Main specs only parse requirements inside that section, so this requirement is currently invisible to validate, list, and archive.', | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| return issues; | ||
| } | ||
|
|
||
| export function stripFencedCodeBlocksPreservingLines(content: string): string { | ||
| const lines = content.split('\n'); | ||
| const output: string[] = []; | ||
| let activeFence: { marker: '`' | '~'; length: number } | null = null; | ||
|
|
||
| for (const line of lines) { | ||
| const fenceMatch = line.match(/^\s*(`{3,}|~{3,})(.*)$/); | ||
|
|
||
| if (!activeFence) { | ||
| if (fenceMatch) { | ||
| activeFence = { | ||
| marker: fenceMatch[1][0] as '`' | '~', | ||
| length: fenceMatch[1].length, | ||
| }; | ||
| output.push(''); | ||
| } else { | ||
| output.push(line); | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| output.push(''); | ||
|
|
||
| if (isClosingFence(line, activeFence)) { | ||
| activeFence = null; | ||
| } | ||
| } | ||
|
|
||
| return output.join('\n'); | ||
| } | ||
|
|
||
| function isClosingFence( | ||
| line: string, | ||
| activeFence: { marker: '`' | '~'; length: number } | ||
| ): boolean { | ||
| const fenceMatch = line.match(/^\s*(`{3,}|~{3,})\s*$/); | ||
| return Boolean( | ||
| fenceMatch && | ||
| fenceMatch[1][0] === activeFence.marker && | ||
| fenceMatch[1].length >= activeFence.length | ||
| ); | ||
| } | ||
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
Oops, something went wrong.
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.