eslint-plugin-traceability 1.0.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.
Files changed (159) hide show
  1. package/.env.example +6 -0
  2. package/.github/workflows/ci-cd.yml +56 -0
  3. package/.husky/pre-commit +1 -0
  4. package/.husky/pre-push +1 -0
  5. package/.prettierignore +23 -0
  6. package/.prettierrc +4 -0
  7. package/.voder/history.md +164 -0
  8. package/.voder/implementation-progress.md +145 -0
  9. package/.voder/last-action.md +106 -0
  10. package/.voder/plan.md +15 -0
  11. package/.voder/progress-chart.png +0 -0
  12. package/.voder/progress-log-areas.csv +34 -0
  13. package/.voder/progress-log.csv +33 -0
  14. package/.voder/traceability/docs-stories-001.0-DEV-PLUGIN-SETUP.story.xml +17 -0
  15. package/.voder/traceability/docs-stories-002.0-DEV-ESLINT-CONFIG.story.xml +13 -0
  16. package/.voder/traceability/docs-stories-003.0-DEV-FUNCTION-ANNOTATIONS.story.xml +9 -0
  17. package/.voder/traceability/docs-stories-004.0-DEV-BRANCH-ANNOTATIONS.story.xml +9 -0
  18. package/.voder/traceability/docs-stories-005.0-DEV-ANNOTATION-VALIDATION.story.xml +9 -0
  19. package/.voder/traceability/docs-stories-006.0-DEV-FILE-VALIDATION.story.xml +9 -0
  20. package/.voder/traceability/docs-stories-007.0-DEV-ERROR-REPORTING.story.xml +9 -0
  21. package/.voder/traceability/docs-stories-008.0-DEV-AUTO-FIX.story.xml +9 -0
  22. package/.voder/traceability/docs-stories-009.0-DEV-MAINTENANCE-TOOLS.story.xml +16 -0
  23. package/.voder/traceability/docs-stories-010.0-DEV-DEEP-VALIDATION.story.xml +11 -0
  24. package/CHANGELOG.md +31 -0
  25. package/CONTRIBUTING.md +97 -0
  26. package/LICENSE +21 -0
  27. package/README.md +159 -0
  28. package/cli-integration.js +157 -0
  29. package/docs/cli-integration.md +103 -0
  30. package/docs/config-presets.md +38 -0
  31. package/docs/decisions/001-typescript-for-eslint-plugin.accepted.md +111 -0
  32. package/docs/decisions/002-jest-for-eslint-testing.accepted.md +137 -0
  33. package/docs/decisions/003-code-quality-ratcheting-plan.md +48 -0
  34. package/docs/eslint-9-setup-guide.md +517 -0
  35. package/docs/eslint-plugin-development-guide.md +483 -0
  36. package/docs/jest-testing-guide.md +100 -0
  37. package/docs/rules/require-branch-annotation.md +34 -0
  38. package/docs/rules/require-req-annotation.md +39 -0
  39. package/docs/rules/require-story-annotation.md +36 -0
  40. package/docs/rules/valid-annotation-format.md +52 -0
  41. package/docs/rules/valid-req-reference.md +58 -0
  42. package/docs/rules/valid-story-reference.md +47 -0
  43. package/docs/security-incidents/unresolved-vulnerabilities.md +11 -0
  44. package/docs/stories/001.0-DEV-PLUGIN-SETUP.story.md +82 -0
  45. package/docs/stories/002.0-DEV-ESLINT-CONFIG.story.md +82 -0
  46. package/docs/stories/003.0-DEV-FUNCTION-ANNOTATIONS.story.md +85 -0
  47. package/docs/stories/004.0-DEV-BRANCH-ANNOTATIONS.story.md +107 -0
  48. package/docs/stories/005.0-DEV-ANNOTATION-VALIDATION.story.md +119 -0
  49. package/docs/stories/006.0-DEV-FILE-VALIDATION.story.md +127 -0
  50. package/docs/stories/007.0-DEV-ERROR-REPORTING.story.md +89 -0
  51. package/docs/stories/008.0-DEV-AUTO-FIX.story.md +104 -0
  52. package/docs/stories/009.0-DEV-MAINTENANCE-TOOLS.story.md +104 -0
  53. package/docs/stories/010.0-DEV-DEEP-VALIDATION.story.md +110 -0
  54. package/docs/stories/developer-story.map.md +118 -0
  55. package/eslint.config.js +146 -0
  56. package/jest.config.js +21 -0
  57. package/lib/index.d.ts +26 -0
  58. package/lib/index.js +11 -0
  59. package/lib/src/index.d.ts +80 -0
  60. package/lib/src/index.js +58 -0
  61. package/lib/src/maintenance/batch.d.ts +16 -0
  62. package/lib/src/maintenance/batch.js +28 -0
  63. package/lib/src/maintenance/detect.d.ts +6 -0
  64. package/lib/src/maintenance/detect.js +69 -0
  65. package/lib/src/maintenance/index.d.ts +14 -0
  66. package/lib/src/maintenance/index.js +22 -0
  67. package/lib/src/maintenance/report.d.ts +7 -0
  68. package/lib/src/maintenance/report.js +17 -0
  69. package/lib/src/maintenance/update.d.ts +6 -0
  70. package/lib/src/maintenance/update.js +67 -0
  71. package/lib/src/maintenance/utils.d.ts +6 -0
  72. package/lib/src/maintenance/utils.js +64 -0
  73. package/lib/src/rules/require-branch-annotation.d.ts +7 -0
  74. package/lib/src/rules/require-branch-annotation.js +111 -0
  75. package/lib/src/rules/require-req-annotation.d.ts +7 -0
  76. package/lib/src/rules/require-req-annotation.js +38 -0
  77. package/lib/src/rules/require-story-annotation.d.ts +7 -0
  78. package/lib/src/rules/require-story-annotation.js +50 -0
  79. package/lib/src/rules/valid-annotation-format.d.ts +10 -0
  80. package/lib/src/rules/valid-annotation-format.js +60 -0
  81. package/lib/src/rules/valid-req-reference.d.ts +3 -0
  82. package/lib/src/rules/valid-req-reference.js +104 -0
  83. package/lib/src/rules/valid-story-reference.d.ts +3 -0
  84. package/lib/src/rules/valid-story-reference.js +168 -0
  85. package/lib/tests/basic.test.d.ts +1 -0
  86. package/lib/tests/basic.test.js +51 -0
  87. package/lib/tests/fixtures/stale/example.d.ts +0 -0
  88. package/lib/tests/fixtures/stale/example.js +3 -0
  89. package/lib/tests/fixtures/update/example.d.ts +0 -0
  90. package/lib/tests/fixtures/update/example.js +3 -0
  91. package/lib/tests/fixtures/valid-annotations/example.d.ts +0 -0
  92. package/lib/tests/fixtures/valid-annotations/example.js +3 -0
  93. package/lib/tests/index.test.d.ts +1 -0
  94. package/lib/tests/index.test.js +68 -0
  95. package/lib/tests/integration/file-validation.test.d.ts +1 -0
  96. package/lib/tests/integration/file-validation.test.js +60 -0
  97. package/lib/tests/integration/plugin-validation.test.d.ts +1 -0
  98. package/lib/tests/integration/plugin-validation.test.js +77 -0
  99. package/lib/tests/maintenance/batch.test.d.ts +1 -0
  100. package/lib/tests/maintenance/batch.test.js +79 -0
  101. package/lib/tests/maintenance/detect-isolated.test.d.ts +1 -0
  102. package/lib/tests/maintenance/detect-isolated.test.js +90 -0
  103. package/lib/tests/maintenance/detect.test.d.ts +1 -0
  104. package/lib/tests/maintenance/detect.test.js +23 -0
  105. package/lib/tests/maintenance/report.test.d.ts +1 -0
  106. package/lib/tests/maintenance/report.test.js +67 -0
  107. package/lib/tests/maintenance/update-isolated.test.d.ts +1 -0
  108. package/lib/tests/maintenance/update-isolated.test.js +66 -0
  109. package/lib/tests/maintenance/update.test.d.ts +1 -0
  110. package/lib/tests/maintenance/update.test.js +26 -0
  111. package/lib/tests/rules/require-branch-annotation.test.d.ts +1 -0
  112. package/lib/tests/rules/require-branch-annotation.test.js +251 -0
  113. package/lib/tests/rules/require-req-annotation.test.d.ts +1 -0
  114. package/lib/tests/rules/require-req-annotation.test.js +41 -0
  115. package/lib/tests/rules/require-story-annotation.test.d.ts +1 -0
  116. package/lib/tests/rules/require-story-annotation.test.js +35 -0
  117. package/lib/tests/rules/valid-annotation-format.test.d.ts +1 -0
  118. package/lib/tests/rules/valid-annotation-format.test.js +58 -0
  119. package/lib/tests/rules/valid-req-reference.test.d.ts +1 -0
  120. package/lib/tests/rules/valid-req-reference.test.js +87 -0
  121. package/lib/tests/rules/valid-story-reference.test.d.ts +1 -0
  122. package/lib/tests/rules/valid-story-reference.test.js +69 -0
  123. package/package.json +67 -0
  124. package/src/index.ts +56 -0
  125. package/src/maintenance/batch.ts +29 -0
  126. package/src/maintenance/detect.ts +42 -0
  127. package/src/maintenance/index.ts +14 -0
  128. package/src/maintenance/report.ts +15 -0
  129. package/src/maintenance/update.ts +40 -0
  130. package/src/maintenance/utils.ts +28 -0
  131. package/src/rules/require-branch-annotation.ts +114 -0
  132. package/src/rules/require-req-annotation.ts +36 -0
  133. package/src/rules/require-story-annotation.ts +52 -0
  134. package/src/rules/valid-annotation-format.ts +62 -0
  135. package/src/rules/valid-req-reference.ts +114 -0
  136. package/src/rules/valid-story-reference.ts +213 -0
  137. package/tests/basic.test.ts +17 -0
  138. package/tests/fixtures/stale/example.ts +2 -0
  139. package/tests/fixtures/story_bullet.md +6 -0
  140. package/tests/fixtures/update/example.ts +2 -0
  141. package/tests/fixtures/valid-annotations/example.ts +2 -0
  142. package/tests/index.test.ts +46 -0
  143. package/tests/integration/file-validation.test.ts +67 -0
  144. package/tests/integration/plugin-validation.test.ts +79 -0
  145. package/tests/maintenance/batch.test.ts +55 -0
  146. package/tests/maintenance/detect-isolated.test.ts +61 -0
  147. package/tests/maintenance/detect.test.ts +19 -0
  148. package/tests/maintenance/report.test.ts +37 -0
  149. package/tests/maintenance/update-isolated.test.ts +39 -0
  150. package/tests/maintenance/update.test.ts +21 -0
  151. package/tests/rules/require-branch-annotation.test.ts +248 -0
  152. package/tests/rules/require-req-annotation.test.ts +38 -0
  153. package/tests/rules/require-story-annotation.test.ts +32 -0
  154. package/tests/rules/valid-annotation-format.test.ts +55 -0
  155. package/tests/rules/valid-req-reference.test.ts +85 -0
  156. package/tests/rules/valid-story-reference.test.ts +66 -0
  157. package/tsconfig.json +15 -0
  158. package/user-docs/api-reference.md +135 -0
  159. package/user-docs/examples.md +73 -0
package/src/index.ts ADDED
@@ -0,0 +1,56 @@
1
+ /**
2
+ * ESLint Traceability Plugin
3
+ * @story docs/stories/001.0-DEV-PLUGIN-SETUP.story.md
4
+ * @req REQ-PLUGIN-STRUCTURE - Provide foundational plugin export and registration
5
+ */
6
+
7
+ import requireStoryAnnotation from "./rules/require-story-annotation";
8
+ import requireReqAnnotation from "./rules/require-req-annotation";
9
+ import requireBranchAnnotation from "./rules/require-branch-annotation";
10
+ import validAnnotationFormat from "./rules/valid-annotation-format";
11
+ import validStoryReference from "./rules/valid-story-reference";
12
+ import validReqReference from "./rules/valid-req-reference";
13
+
14
+ export const rules = {
15
+ "require-story-annotation": requireStoryAnnotation,
16
+ "require-req-annotation": requireReqAnnotation,
17
+ "require-branch-annotation": requireBranchAnnotation,
18
+ "valid-annotation-format": validAnnotationFormat,
19
+ "valid-story-reference": validStoryReference,
20
+ "valid-req-reference": validReqReference,
21
+ };
22
+
23
+ export const configs = {
24
+ recommended: [
25
+ {
26
+ plugins: {
27
+ traceability: {},
28
+ },
29
+ rules: {
30
+ "traceability/require-story-annotation": "error",
31
+ "traceability/require-req-annotation": "error",
32
+ "traceability/require-branch-annotation": "error",
33
+ "traceability/valid-annotation-format": "error",
34
+ "traceability/valid-story-reference": "error",
35
+ "traceability/valid-req-reference": "error",
36
+ },
37
+ },
38
+ ],
39
+ strict: [
40
+ {
41
+ plugins: {
42
+ traceability: {},
43
+ },
44
+ rules: {
45
+ "traceability/require-story-annotation": "error",
46
+ "traceability/require-req-annotation": "error",
47
+ "traceability/require-branch-annotation": "error",
48
+ "traceability/valid-annotation-format": "error",
49
+ "traceability/valid-story-reference": "error",
50
+ "traceability/valid-req-reference": "error",
51
+ },
52
+ },
53
+ ],
54
+ };
55
+
56
+ export default { rules, configs };
@@ -0,0 +1,29 @@
1
+ import { updateAnnotationReferences } from "./update";
2
+ import { detectStaleAnnotations } from "./detect";
3
+
4
+ /**
5
+ * Batch update annotations and verify references
6
+ * @story docs/stories/009.0-DEV-MAINTENANCE-TOOLS.story.md
7
+ * @req REQ-MAINT-BATCH - Perform batch updates
8
+ * @req REQ-MAINT-VERIFY - Verify annotation references
9
+ */
10
+ export function batchUpdateAnnotations(
11
+ codebasePath: string,
12
+ mappings: { oldPath: string; newPath: string }[],
13
+ ): number {
14
+ let totalUpdated = 0;
15
+ for (const { oldPath, newPath } of mappings) {
16
+ totalUpdated += updateAnnotationReferences(codebasePath, oldPath, newPath);
17
+ }
18
+ return totalUpdated;
19
+ }
20
+
21
+ /**
22
+ * Verify annotation references in codebase after maintenance operations
23
+ * @story docs/stories/009.0-DEV-MAINTENANCE-TOOLS.story.md
24
+ * @req REQ-MAINT-VERIFY - Verify annotation references
25
+ */
26
+ export function verifyAnnotations(codebasePath: string): boolean {
27
+ const staleAnnotations = detectStaleAnnotations(codebasePath);
28
+ return staleAnnotations.length === 0;
29
+ }
@@ -0,0 +1,42 @@
1
+ import * as fs from "fs";
2
+ import * as path from "path";
3
+ import { getAllFiles } from "./utils";
4
+
5
+ /**
6
+ * Detect stale annotation references that point to moved or deleted story files
7
+ * @story docs/stories/009.0-DEV-MAINTENANCE-TOOLS.story.md
8
+ * @req REQ-MAINT-DETECT - Detect stale annotation references
9
+ */
10
+ export function detectStaleAnnotations(codebasePath: string): string[] {
11
+ if (
12
+ !fs.existsSync(codebasePath) ||
13
+ !fs.statSync(codebasePath).isDirectory()
14
+ ) {
15
+ return [];
16
+ }
17
+
18
+ const cwd = process.cwd();
19
+ const baseDir = path.resolve(cwd, codebasePath);
20
+
21
+ const stale = new Set<string>();
22
+
23
+ const files = getAllFiles(codebasePath);
24
+ for (const file of files) {
25
+ const content = fs.readFileSync(file, "utf8");
26
+ const regex = /@story\s+([^\s]+)/g;
27
+ let match: RegExpExecArray | null;
28
+ while ((match = regex.exec(content)) !== null) {
29
+ const storyPath = match[1];
30
+ const storyProjectPath = path.resolve(cwd, storyPath);
31
+ const storyCodebasePath = path.resolve(baseDir, storyPath);
32
+ if (
33
+ !fs.existsSync(storyProjectPath) &&
34
+ !fs.existsSync(storyCodebasePath)
35
+ ) {
36
+ stale.add(storyPath);
37
+ }
38
+ }
39
+ }
40
+
41
+ return Array.from(stale);
42
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Maintenance Tools Module
3
+ * @story docs/stories/009.0-DEV-MAINTENANCE-TOOLS.story.md
4
+ * @req REQ-MAINT-DETECT
5
+ * @req REQ-MAINT-UPDATE
6
+ * @req REQ-MAINT-BATCH
7
+ * @req REQ-MAINT-VERIFY
8
+ * @req REQ-MAINT-REPORT
9
+ * @req REQ-MAINT-SAFE
10
+ */
11
+ export { detectStaleAnnotations } from "./detect";
12
+ export { updateAnnotationReferences } from "./update";
13
+ export { batchUpdateAnnotations, verifyAnnotations } from "./batch";
14
+ export { generateMaintenanceReport } from "./report";
@@ -0,0 +1,15 @@
1
+ import { detectStaleAnnotations } from "./detect";
2
+
3
+ /**
4
+ * Generate a report of maintenance operations performed
5
+ * @story docs/stories/009.0-DEV-MAINTENANCE-TOOLS.story.md
6
+ * @req REQ-MAINT-REPORT - Generate maintenance report
7
+ * @req REQ-MAINT-SAFE - Ensure operations are safe and reversible
8
+ */
9
+ export function generateMaintenanceReport(codebasePath: string): string {
10
+ const staleAnnotations = detectStaleAnnotations(codebasePath);
11
+ if (staleAnnotations.length === 0) {
12
+ return "";
13
+ }
14
+ return staleAnnotations.join("\n");
15
+ }
@@ -0,0 +1,40 @@
1
+ import * as fs from "fs";
2
+ import { getAllFiles } from "./utils";
3
+
4
+ /**
5
+ * Update annotation references when story files are moved or renamed
6
+ * @story docs/stories/009.0-DEV-MAINTENANCE-TOOLS.story.md
7
+ * @req REQ-MAINT-UPDATE - Update annotation references
8
+ */
9
+ export function updateAnnotationReferences(
10
+ codebasePath: string,
11
+ oldPath: string,
12
+ newPath: string,
13
+ ): number {
14
+ if (
15
+ !fs.existsSync(codebasePath) ||
16
+ !fs.statSync(codebasePath).isDirectory()
17
+ ) {
18
+ return 0;
19
+ }
20
+
21
+ let replacementCount = 0;
22
+ const escapedOldPath = oldPath.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
23
+ const regex = new RegExp(`(@story\\s*)${escapedOldPath}`, "g");
24
+
25
+ const files = getAllFiles(codebasePath);
26
+ for (const fullPath of files) {
27
+ const stat = fs.statSync(fullPath);
28
+ if (!stat.isFile()) continue;
29
+ const content = fs.readFileSync(fullPath, "utf8");
30
+ const newContent = content.replace(regex, (match, p1) => {
31
+ replacementCount++;
32
+ return `${p1}${newPath}`;
33
+ });
34
+ if (newContent !== content) {
35
+ fs.writeFileSync(fullPath, newContent, "utf8");
36
+ }
37
+ }
38
+
39
+ return replacementCount;
40
+ }
@@ -0,0 +1,28 @@
1
+ import * as fs from "fs";
2
+ import * as path from "path";
3
+
4
+ /**
5
+ * Recursively retrieve all files in a directory.
6
+ * @story docs/stories/009.0-DEV-MAINTENANCE-TOOLS.story.md
7
+ * @req REQ-MAINT-UTILS - Extract common file traversal logic for maintenance tools
8
+ */
9
+ export function getAllFiles(dir: string): string[] {
10
+ const fileList: string[] = [];
11
+ if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) {
12
+ return fileList;
13
+ }
14
+ function traverse(currentDir: string) {
15
+ const entries = fs.readdirSync(currentDir);
16
+ for (const entry of entries) {
17
+ const fullPath = path.join(currentDir, entry);
18
+ const stat = fs.statSync(fullPath);
19
+ if (stat.isDirectory()) {
20
+ traverse(fullPath);
21
+ } else if (stat.isFile()) {
22
+ fileList.push(fullPath);
23
+ }
24
+ }
25
+ }
26
+ traverse(dir);
27
+ return fileList;
28
+ }
@@ -0,0 +1,114 @@
1
+ /**
2
+ * Rule to enforce @story and @req annotations on significant code branches
3
+ * @story docs/stories/004.0-DEV-BRANCH-ANNOTATIONS.story.md
4
+ * @req REQ-BRANCH-DETECTION - Detect significant code branches for traceability annotations
5
+ */
6
+ export default {
7
+ meta: {
8
+ type: "problem",
9
+ docs: {
10
+ description: "Require @story and @req annotations on code branches",
11
+ recommended: "error",
12
+ },
13
+ fixable: "code",
14
+ messages: {
15
+ missingAnnotation: "Missing {{missing}} annotation on code branch",
16
+ },
17
+ schema: [],
18
+ },
19
+ create(context: any) {
20
+ const sourceCode = context.getSourceCode();
21
+ /**
22
+ * Helper to check a branch AST node for traceability annotations.
23
+ * @story docs/stories/004.0-DEV-BRANCH-ANNOTATIONS.story.md
24
+ * @req REQ-BRANCH-DETECTION - Detect significant code branches for traceability annotations
25
+ */
26
+ function checkBranch(node: any) {
27
+ // @story docs/stories/004.0-DEV-BRANCH-ANNOTATIONS.story.md
28
+ // @req REQ-BRANCH-DETECTION - Skip default switch cases during annotation checks
29
+ // skip default cases in switch
30
+ if (node.type === "SwitchCase" && node.test == null) {
31
+ return;
32
+ }
33
+ // collect comments before node
34
+ let comments = sourceCode.getCommentsBefore(node) || [];
35
+ // @story docs/stories/004.0-DEV-BRANCH-ANNOTATIONS.story.md
36
+ // @req REQ-BRANCH-DETECTION - Fallback scanning for SwitchCase when leading comments are absent
37
+ // fallback scanning for SwitchCase if no leading comment nodes
38
+ /* istanbul ignore if */
39
+ if (node.type === "SwitchCase" && comments.length === 0) {
40
+ const lines = sourceCode.lines;
41
+ const startLine = node.loc.start.line;
42
+ let i = startLine - 1;
43
+ const fallbackComments: string[] = [];
44
+ while (i > 0) {
45
+ const lineText = lines[i - 1];
46
+ if (/^\s*(\/\/|\/\*)/.test(lineText)) {
47
+ fallbackComments.unshift(lineText.trim());
48
+ i--;
49
+ } else if (/^\s*$/.test(lineText)) {
50
+ break;
51
+ } else {
52
+ break;
53
+ }
54
+ }
55
+ comments = fallbackComments.map((text) => ({ value: text }));
56
+ }
57
+ const text = comments.map((c: any) => c.value).join(" ");
58
+
59
+ const missingStory = !/@story\b/.test(text);
60
+ const missingReq = !/@req\b/.test(text);
61
+
62
+ if (missingStory) {
63
+ const reportObj: any = {
64
+ node,
65
+ messageId: "missingAnnotation",
66
+ data: { missing: "@story" },
67
+ };
68
+ if (node.type !== "CatchClause") {
69
+ if (node.type === "SwitchCase") {
70
+ const indent = " ".repeat(node.loc.start.column);
71
+ reportObj.fix = (fixer: any) =>
72
+ fixer.insertTextBefore(
73
+ node,
74
+ `// @story <story-file>.story.md\n${indent}`,
75
+ );
76
+ } else {
77
+ reportObj.fix = (fixer: any) =>
78
+ fixer.insertTextBefore(node, `// @story <story-file>.story.md\n`);
79
+ }
80
+ }
81
+ context.report(reportObj);
82
+ }
83
+ if (missingReq) {
84
+ const reportObj: any = {
85
+ node,
86
+ messageId: "missingAnnotation",
87
+ data: { missing: "@req" },
88
+ };
89
+ if (!missingStory && node.type !== "CatchClause") {
90
+ if (node.type === "SwitchCase") {
91
+ const indent = " ".repeat(node.loc.start.column);
92
+ reportObj.fix = (fixer: any) =>
93
+ fixer.insertTextBefore(node, `// @req <REQ-ID>\n${indent}`);
94
+ } else {
95
+ reportObj.fix = (fixer: any) =>
96
+ fixer.insertTextBefore(node, `// @req <REQ-ID>\n`);
97
+ }
98
+ }
99
+ context.report(reportObj);
100
+ }
101
+ }
102
+ return {
103
+ IfStatement: checkBranch,
104
+ SwitchCase: checkBranch,
105
+ TryStatement: checkBranch,
106
+ CatchClause: checkBranch,
107
+ ForStatement: checkBranch,
108
+ ForOfStatement: checkBranch,
109
+ ForInStatement: checkBranch,
110
+ WhileStatement: checkBranch,
111
+ DoWhileStatement: checkBranch,
112
+ };
113
+ },
114
+ } as any;
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Rule to enforce @req annotation on functions
3
+ * @story docs/stories/003.0-DEV-FUNCTION-ANNOTATIONS.story.md
4
+ * @req REQ-ANNOTATION-REQUIRED - Require @req annotation on functions
5
+ */
6
+ export default {
7
+ meta: {
8
+ type: "problem",
9
+ fixable: "code",
10
+ docs: {
11
+ description: "Require @req annotations on functions",
12
+ recommended: "error",
13
+ },
14
+ messages: {
15
+ missingReq: "Missing @req annotation",
16
+ },
17
+ schema: [],
18
+ },
19
+ create(context: any) {
20
+ const sourceCode = context.getSourceCode();
21
+ return {
22
+ FunctionDeclaration(node: any) {
23
+ const jsdoc = sourceCode.getJSDocComment(node);
24
+ if (!jsdoc || !jsdoc.value.includes("@req")) {
25
+ context.report({
26
+ node,
27
+ messageId: "missingReq",
28
+ fix(fixer: any) {
29
+ return fixer.insertTextBefore(node, "/** @req <REQ-ID> */\n");
30
+ },
31
+ });
32
+ }
33
+ },
34
+ };
35
+ },
36
+ } as any;
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Rule to enforce @story annotation on functions
3
+ * @story docs/stories/003.0-DEV-FUNCTION-ANNOTATIONS.story.md
4
+ * @req REQ-ANNOTATION-REQUIRED - Require @story annotation on functions
5
+ */
6
+ export default {
7
+ meta: {
8
+ type: "problem",
9
+ docs: {
10
+ description: "Require @story annotations on functions",
11
+ recommended: "error",
12
+ },
13
+ fixable: "code",
14
+ messages: {
15
+ missingStory: "Missing @story annotation",
16
+ },
17
+ schema: [],
18
+ },
19
+ create(context: any) {
20
+ const sourceCode = context.getSourceCode();
21
+ return {
22
+ FunctionDeclaration(node: any) {
23
+ const jsdoc = sourceCode.getJSDocComment(node);
24
+ let hasStory = false;
25
+ // @story docs/stories/003.0-DEV-FUNCTION-ANNOTATIONS.story.md
26
+ // @req REQ-JSDOC-PARSING - Detect JSDoc @story annotation presence
27
+ if (jsdoc && jsdoc.value.includes("@story")) {
28
+ hasStory = true;
29
+ // @story docs/stories/003.0-DEV-FUNCTION-ANNOTATIONS.story.md
30
+ // @req REQ-JSDOC-PARSING - Fallback to loading comments before node for @story annotation detection
31
+ } else {
32
+ const commentsBefore = sourceCode.getCommentsBefore(node) || [];
33
+ hasStory = commentsBefore.some((comment: any) =>
34
+ comment.value.includes("@story"),
35
+ );
36
+ }
37
+ if (!hasStory) {
38
+ context.report({
39
+ node,
40
+ messageId: "missingStory",
41
+ fix(fixer: any) {
42
+ return fixer.insertTextBefore(
43
+ node,
44
+ "/** @story <story-file>.story.md */\n",
45
+ );
46
+ },
47
+ });
48
+ }
49
+ },
50
+ };
51
+ },
52
+ } as any;
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Rule to validate @story and @req annotation format and syntax
3
+ * @story docs/stories/005.0-DEV-ANNOTATION-VALIDATION.story.md
4
+ * @req REQ-FORMAT-SPECIFICATION - Define clear format rules for @story and @req annotations
5
+ * @req REQ-SYNTAX-VALIDATION - Validate annotation syntax matches specification
6
+ * @req REQ-PATH-FORMAT - Validate @story paths follow expected patterns
7
+ * @req REQ-REQ-FORMAT - Validate @req identifiers follow expected patterns
8
+ */
9
+ export default {
10
+ meta: {
11
+ type: "problem",
12
+ docs: {
13
+ description: "Validate format and syntax of @story and @req annotations",
14
+ recommended: "error",
15
+ },
16
+ messages: {
17
+ invalidStoryFormat: "Invalid @story annotation format",
18
+ invalidReqFormat: "Invalid @req annotation format",
19
+ },
20
+ schema: [],
21
+ },
22
+ create(context: any) {
23
+ const sourceCode = context.getSourceCode();
24
+ return {
25
+ Program() {
26
+ const comments = sourceCode.getAllComments() || [];
27
+ comments.forEach((comment: any) => {
28
+ const lines = comment.value
29
+ .split(/\r?\n/)
30
+ .map((l: string) => l.replace(/^[^@]*/, "").trim());
31
+ lines.forEach((line: string) => {
32
+ if (line.startsWith("@story")) {
33
+ const parts = line.split(/\s+/);
34
+ const storyPath = parts[1];
35
+ if (
36
+ !storyPath ||
37
+ !/^docs\/stories\/[0-9]+\.[0-9]+-DEV-[\w-]+\.story\.md$/.test(
38
+ storyPath,
39
+ )
40
+ ) {
41
+ context.report({
42
+ node: comment as any,
43
+ messageId: "invalidStoryFormat",
44
+ });
45
+ }
46
+ }
47
+ if (line.startsWith("@req")) {
48
+ const parts = line.split(/\s+/);
49
+ const reqId = parts[1];
50
+ if (!reqId || !/^REQ-[A-Z0-9-]+$/.test(reqId)) {
51
+ context.report({
52
+ node: comment as any,
53
+ messageId: "invalidReqFormat",
54
+ });
55
+ }
56
+ }
57
+ });
58
+ });
59
+ },
60
+ };
61
+ },
62
+ } as any;
@@ -0,0 +1,114 @@
1
+ /* eslint-env node */
2
+ /**
3
+ * Rule to validate @req annotation references refer to existing requirements in story files
4
+ * @story docs/stories/010.0-DEV-DEEP-VALIDATION.story.md
5
+ * @req REQ-DEEP-PARSE - Parse story files to extract requirement identifiers
6
+ * @req REQ-DEEP-MATCH - Validate @req references against story file content
7
+ * @req REQ-DEEP-CACHE - Cache parsed story content for performance
8
+ * @req REQ-DEEP-PATH - Protect against path traversal in story paths
9
+ */
10
+ import fs from "fs";
11
+ import path from "path";
12
+ import type { Rule } from "eslint";
13
+
14
+ export default {
15
+ meta: {
16
+ type: "problem",
17
+ docs: {
18
+ description:
19
+ "Validate that @req annotations reference existing requirements in referenced story files",
20
+ recommended: "error",
21
+ },
22
+ messages: {
23
+ reqMissing: "Requirement '{{reqId}}' not found in '{{storyPath}}'",
24
+ invalidPath: "Invalid story path '{{storyPath}}'",
25
+ },
26
+ schema: [],
27
+ },
28
+ create(context) {
29
+ const sourceCode = context.getSourceCode();
30
+ const cwd = process.cwd();
31
+
32
+ // Cache for resolved story file paths to parsed set of requirement IDs
33
+ const reqCache = new Map<string, Set<string>>();
34
+
35
+ let rawStoryPath: string | null = null;
36
+
37
+ return {
38
+ Program() {
39
+ const comments = sourceCode.getAllComments() || [];
40
+ comments.forEach((comment) => {
41
+ const rawLines = comment.value.split(/\r?\n/);
42
+ const lines = rawLines.map((rawLine) =>
43
+ rawLine.trim().replace(/^\*+\s*/, ""),
44
+ );
45
+ lines.forEach((line) => {
46
+ if (line.startsWith("@story")) {
47
+ const parts = line.split(/\s+/);
48
+ rawStoryPath = parts[1] || null;
49
+ }
50
+ if (line.startsWith("@req")) {
51
+ const parts = line.split(/\s+/);
52
+ const reqId = parts[1];
53
+ if (!reqId || !rawStoryPath) {
54
+ return;
55
+ }
56
+
57
+ // Protect against path traversal and absolute paths
58
+ if (
59
+ rawStoryPath.includes("..") ||
60
+ path.isAbsolute(rawStoryPath)
61
+ ) {
62
+ context.report({
63
+ node: comment as any,
64
+ messageId: "invalidPath",
65
+ data: { storyPath: rawStoryPath },
66
+ });
67
+ return;
68
+ }
69
+
70
+ const resolvedStoryPath = path.resolve(cwd, rawStoryPath);
71
+ if (
72
+ !resolvedStoryPath.startsWith(cwd + path.sep) &&
73
+ resolvedStoryPath !== cwd
74
+ ) {
75
+ context.report({
76
+ node: comment as any,
77
+ messageId: "invalidPath",
78
+ data: { storyPath: rawStoryPath },
79
+ });
80
+ return;
81
+ }
82
+
83
+ // Load and parse story file if not cached
84
+ if (!reqCache.has(resolvedStoryPath)) {
85
+ try {
86
+ const content = fs.readFileSync(resolvedStoryPath, "utf8");
87
+ const found = new Set<string>();
88
+ const regex = /REQ-[A-Z0-9-]+/g;
89
+ let match;
90
+ while ((match = regex.exec(content)) !== null) {
91
+ found.add(match[0]);
92
+ }
93
+ reqCache.set(resolvedStoryPath, found);
94
+ } catch {
95
+ // Unable to read file, treat as no requirements
96
+ reqCache.set(resolvedStoryPath, new Set());
97
+ }
98
+ }
99
+
100
+ const reqSet = reqCache.get(resolvedStoryPath)!;
101
+ if (!reqSet.has(reqId)) {
102
+ context.report({
103
+ node: comment as any,
104
+ messageId: "reqMissing",
105
+ data: { reqId, storyPath: rawStoryPath },
106
+ });
107
+ }
108
+ }
109
+ });
110
+ });
111
+ },
112
+ };
113
+ },
114
+ } as Rule.RuleModule;