repometry 0.0.0-dev.0 → 0.1.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 (281) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +24 -1
  3. package/package.json +48 -7
  4. package/target/main/resources/report/json/compact-json-v1.schema.json +24 -0
  5. package/target/main/resources/report/json/comparison-v1.schema.json +65 -0
  6. package/target/main/resources/report/json/config-v1.schema.json +34 -0
  7. package/target/main/resources/report/json/diff-coverage-v1.schema.json +55 -0
  8. package/target/main/resources/report/json/file-metrics-v1.schema.json +134 -0
  9. package/target/main/resources/report/json/inspection-context-v1.schema.json +172 -0
  10. package/target/main/resources/report/json/metabranch-v1.schema.json +9 -0
  11. package/target/main/resources/report/json/metric-catalog-v1.schema.json +31 -0
  12. package/target/main/resources/report/json/public-history-v1.schema.json +50 -0
  13. package/target/main/resources/report/json/pull-review-v1.schema.json +97 -0
  14. package/target/main/resources/report/json/quality-history-v1.schema.json +51 -0
  15. package/target/main/resources/report/json/report-v1.schema.json +65 -0
  16. package/target/main/resources/report/json/report-v2.schema.json +238 -0
  17. package/target/main/resources/report/json/repository-summary-v1.schema.json +181 -0
  18. package/target/main/resources/report/json/viewer-details-v1.schema.json +343 -0
  19. package/target/main/resources/report/json/viewer-inspection-v1.schema.json +13 -0
  20. package/target/main/resources/viewer/files.css +578 -0
  21. package/target/main/resources/viewer/index.html +130 -0
  22. package/target/main/resources/viewer/projects.html +52 -0
  23. package/target/main/resources/viewer/styles.css +586 -0
  24. package/target/main/resources/viewer/viewer-pulls.css +64 -0
  25. package/target/main/ts/api/gh/github-pr-comment.d.ts +4 -0
  26. package/target/main/ts/api/gh/github-pr-comment.js +107 -0
  27. package/target/main/ts/api/gh/github.d.ts +6 -0
  28. package/target/main/ts/api/gh/github.js +91 -0
  29. package/target/main/ts/api/git/blob-batch.d.ts +10 -0
  30. package/target/main/ts/api/git/blob-batch.js +105 -0
  31. package/target/main/ts/api/git/changes.d.ts +22 -0
  32. package/target/main/ts/api/git/changes.js +107 -0
  33. package/target/main/ts/api/git/commit-metadata.d.ts +3 -0
  34. package/target/main/ts/api/git/commit-metadata.js +54 -0
  35. package/target/main/ts/api/git/git-churn.d.ts +9 -0
  36. package/target/main/ts/api/git/git-churn.js +70 -0
  37. package/target/main/ts/api/git/git-reader.d.ts +16 -0
  38. package/target/main/ts/api/git/git-reader.js +73 -0
  39. package/target/main/ts/api/git/git.d.ts +21 -0
  40. package/target/main/ts/api/git/git.js +152 -0
  41. package/target/main/ts/api/git/inspection-source.d.ts +15 -0
  42. package/target/main/ts/api/git/inspection-source.js +30 -0
  43. package/target/main/ts/api/git/local-sources.d.ts +5 -0
  44. package/target/main/ts/api/git/local-sources.js +46 -0
  45. package/target/main/ts/api/git/local.d.ts +3 -0
  46. package/target/main/ts/api/git/local.js +35 -0
  47. package/target/main/ts/api/git/metabranch-worktree.d.ts +1 -0
  48. package/target/main/ts/api/git/metabranch-worktree.js +34 -0
  49. package/target/main/ts/api/git/source-cache.d.ts +3 -0
  50. package/target/main/ts/api/git/source-cache.js +56 -0
  51. package/target/main/ts/api/import/finding-import.d.ts +9 -0
  52. package/target/main/ts/api/import/finding-import.js +226 -0
  53. package/target/main/ts/api/import/lcov-files.d.ts +5 -0
  54. package/target/main/ts/api/import/lcov-files.js +13 -0
  55. package/target/main/ts/api/import/profile-file.d.ts +4 -0
  56. package/target/main/ts/api/import/profile-file.js +16 -0
  57. package/target/main/ts/api/worker/remote-executor.d.ts +2 -0
  58. package/target/main/ts/api/worker/remote-executor.js +45 -0
  59. package/target/main/ts/api/worker/worker-protocol.d.ts +7 -0
  60. package/target/main/ts/api/worker/worker-protocol.js +101 -0
  61. package/target/main/ts/api/worker/worker.d.ts +5 -0
  62. package/target/main/ts/api/worker/worker.js +85 -0
  63. package/target/main/ts/cli/arguments.d.ts +28 -0
  64. package/target/main/ts/cli/arguments.js +110 -0
  65. package/target/main/ts/cli/config.d.ts +14 -0
  66. package/target/main/ts/cli/config.js +60 -0
  67. package/target/main/ts/cli/github-pr-comment.d.ts +1 -0
  68. package/target/main/ts/cli/github-pr-comment.js +12 -0
  69. package/target/main/ts/cli/imports.d.ts +7 -0
  70. package/target/main/ts/cli/imports.js +29 -0
  71. package/target/main/ts/cli/main.d.ts +2 -0
  72. package/target/main/ts/cli/main.js +343 -284
  73. package/target/main/ts/cli/reporting.d.ts +18 -0
  74. package/target/main/ts/cli/reporting.js +108 -0
  75. package/target/main/ts/cli/revisions.d.ts +6 -0
  76. package/target/main/ts/cli/revisions.js +15 -0
  77. package/target/main/ts/cli/store.d.ts +13 -0
  78. package/target/main/ts/cli/store.js +38 -0
  79. package/target/main/ts/compose/analyze-snapshot.d.ts +4 -0
  80. package/target/main/ts/compose/analyze-snapshot.js +163 -0
  81. package/target/main/ts/compose/attach-churn.d.ts +2 -0
  82. package/target/main/ts/compose/attach-churn.js +37 -0
  83. package/target/main/ts/compose/diff-coverage.d.ts +20 -0
  84. package/target/main/ts/compose/diff-coverage.js +56 -0
  85. package/target/main/ts/compose/inspect.d.ts +23 -0
  86. package/target/main/ts/compose/inspect.js +44 -0
  87. package/target/main/ts/compose/plugin-list.d.ts +6 -0
  88. package/target/main/ts/compose/plugin-list.js +9 -0
  89. package/target/main/ts/compose/prepare-metabranch.d.ts +9 -0
  90. package/target/main/ts/compose/prepare-metabranch.js +16 -0
  91. package/target/main/ts/compose/review.d.ts +16 -0
  92. package/target/main/ts/compose/review.js +38 -0
  93. package/target/main/ts/compose/sources.d.ts +3 -0
  94. package/target/main/ts/compose/sources.js +29 -0
  95. package/target/main/ts/core/engine/execute.d.ts +10 -0
  96. package/target/main/ts/core/engine/execute.js +49 -0
  97. package/target/main/ts/core/engine/manifest.d.ts +4 -0
  98. package/target/main/ts/core/engine/manifest.js +104 -0
  99. package/target/main/ts/core/engine/plan.d.ts +22 -0
  100. package/target/main/ts/core/engine/plan.js +106 -0
  101. package/target/main/ts/core/engine/plugin.d.ts +59 -0
  102. package/target/main/ts/core/engine/plugin.js +4 -0
  103. package/target/main/ts/core/engine/report-products.d.ts +16 -0
  104. package/target/main/ts/core/engine/report-products.js +85 -0
  105. package/target/main/ts/core/model/changes.d.ts +8 -0
  106. package/target/main/ts/core/model/changes.js +1 -0
  107. package/target/main/ts/core/model/churn.d.ts +11 -0
  108. package/target/main/ts/core/model/churn.js +5 -0
  109. package/target/main/ts/core/model/clone-source.d.ts +16 -0
  110. package/target/main/ts/core/model/clone-source.js +14 -0
  111. package/target/main/ts/core/model/code-structure.d.ts +30 -0
  112. package/target/main/ts/core/model/code-structure.js +31 -0
  113. package/target/main/ts/core/model/comparison.d.ts +24 -0
  114. package/target/main/ts/core/model/comparison.js +1 -0
  115. package/target/main/ts/core/model/diff-coverage.d.ts +23 -0
  116. package/target/main/ts/core/model/diff-coverage.js +1 -0
  117. package/target/main/ts/core/model/finding-ignore.d.ts +8 -0
  118. package/target/main/ts/core/model/finding-ignore.js +35 -0
  119. package/target/main/ts/core/model/finding.d.ts +28 -0
  120. package/target/main/ts/core/model/finding.js +29 -0
  121. package/target/main/ts/core/model/ignore-rule.d.ts +11 -0
  122. package/target/main/ts/core/model/ignore-rule.js +1 -0
  123. package/target/main/ts/core/model/measurement.d.ts +27 -0
  124. package/target/main/ts/core/model/measurement.js +24 -0
  125. package/target/main/ts/core/model/metric-descriptor.d.ts +17 -0
  126. package/target/main/ts/core/model/metric-descriptor.js +1 -0
  127. package/target/main/ts/core/model/plugin-data.d.ts +25 -0
  128. package/target/main/ts/core/model/plugin-data.js +1 -0
  129. package/target/main/ts/core/model/pull-review.d.ts +48 -0
  130. package/target/main/ts/core/model/pull-review.js +1 -0
  131. package/target/main/ts/core/model/quality-summary.d.ts +42 -0
  132. package/target/main/ts/core/model/quality-summary.js +64 -0
  133. package/target/main/ts/core/model/report.d.ts +32 -0
  134. package/target/main/ts/core/model/report.js +5 -0
  135. package/target/main/ts/core/model/scanner-import.d.ts +39 -0
  136. package/target/main/ts/core/model/scanner-import.js +28 -0
  137. package/target/main/ts/core/model/snapshot.d.ts +15 -0
  138. package/target/main/ts/core/model/snapshot.js +50 -0
  139. package/target/main/ts/core/model/source-locator.d.ts +7 -0
  140. package/target/main/ts/core/model/source-locator.js +1 -0
  141. package/target/main/ts/core/model/source.d.ts +23 -0
  142. package/target/main/ts/core/model/source.js +1 -0
  143. package/target/main/ts/core/ports/report.d.ts +9 -0
  144. package/target/main/ts/core/ports/report.js +1 -0
  145. package/target/main/ts/core/ports/source-provider.d.ts +14 -0
  146. package/target/main/ts/core/ports/source-provider.js +4 -0
  147. package/target/main/ts/core/profile/exclusions.d.ts +11 -0
  148. package/target/main/ts/core/profile/exclusions.js +57 -0
  149. package/target/main/ts/core/profile/maintainability.d.ts +31 -0
  150. package/target/main/ts/core/profile/maintainability.js +9 -0
  151. package/target/main/ts/core/profile/profile.d.ts +4 -0
  152. package/target/main/ts/core/profile/profile.js +10 -0
  153. package/target/main/ts/core/usecases/compare.d.ts +6 -0
  154. package/target/main/ts/core/usecases/compare.js +145 -0
  155. package/target/main/ts/core/usecases/diff-coverage-policy.d.ts +6 -0
  156. package/target/main/ts/core/usecases/diff-coverage-policy.js +10 -0
  157. package/target/main/ts/core/usecases/diff-coverage.d.ts +40 -0
  158. package/target/main/ts/core/usecases/diff-coverage.js +15 -0
  159. package/target/main/ts/core/usecases/inspect.d.ts +38 -0
  160. package/target/main/ts/core/usecases/inspect.js +41 -0
  161. package/target/main/ts/core/usecases/review.d.ts +31 -0
  162. package/target/main/ts/core/usecases/review.js +128 -0
  163. package/target/main/ts/index.d.ts +35 -0
  164. package/target/main/ts/index.js +17 -13
  165. package/target/main/ts/plugin.d.ts +14 -0
  166. package/target/main/ts/plugin.js +5 -0
  167. package/target/main/ts/plugins/coverage/coverage.d.ts +23 -0
  168. package/target/main/ts/plugins/coverage/coverage.js +120 -0
  169. package/target/main/ts/plugins/coverage/diff-coverage.d.ts +7 -0
  170. package/target/main/ts/plugins/coverage/diff-coverage.js +89 -0
  171. package/target/main/ts/plugins/duplication/duplication.d.ts +4 -0
  172. package/target/main/ts/plugins/duplication/duplication.js +93 -0
  173. package/target/main/ts/plugins/language-js/babel-provider.d.ts +10 -0
  174. package/target/main/ts/plugins/language-js/babel-provider.js +46 -0
  175. package/target/main/ts/plugins/language-js/clone-candidates.d.ts +3 -0
  176. package/target/main/ts/plugins/language-js/clone-candidates.js +64 -0
  177. package/target/main/ts/plugins/language-js/code-lines.d.ts +6 -0
  178. package/target/main/ts/plugins/language-js/code-lines.js +35 -0
  179. package/target/main/ts/plugins/language-js/function-facts.d.ts +8 -0
  180. package/target/main/ts/plugins/language-js/function-facts.js +60 -0
  181. package/target/main/ts/plugins/language-js/structural-observations.d.ts +8 -0
  182. package/target/main/ts/plugins/language-js/structural-observations.js +79 -0
  183. package/target/main/ts/plugins/maintainability/maintainability.d.ts +9 -0
  184. package/target/main/ts/plugins/maintainability/maintainability.js +42 -0
  185. package/target/main/ts/plugins/maintainability/structural-smells.d.ts +3 -0
  186. package/target/main/ts/plugins/maintainability/structural-smells.js +15 -0
  187. package/target/main/ts/plugins/source/forges.d.ts +4 -0
  188. package/target/main/ts/plugins/source/forges.js +79 -0
  189. package/target/main/ts/plugins/structure/structure.d.ts +2 -0
  190. package/target/main/ts/plugins/structure/structure.js +37 -0
  191. package/target/main/ts/report/html/app/viewer-access.d.ts +19 -0
  192. package/target/main/ts/report/html/app/viewer-access.js +25 -0
  193. package/target/main/ts/report/html/app/viewer-chart.d.ts +101 -0
  194. package/target/main/ts/report/html/app/viewer-chart.js +711 -0
  195. package/target/main/ts/report/html/app/viewer-commits.d.ts +36 -0
  196. package/target/main/ts/report/html/app/viewer-commits.js +280 -0
  197. package/target/main/ts/report/html/app/viewer-data.d.ts +23 -0
  198. package/target/main/ts/report/html/app/viewer-data.js +105 -0
  199. package/target/main/ts/report/html/app/viewer-files.d.ts +117 -0
  200. package/target/main/ts/report/html/app/viewer-files.js +2087 -0
  201. package/target/main/ts/report/html/app/viewer-filter.d.ts +113 -0
  202. package/target/main/ts/report/html/app/viewer-filter.js +492 -0
  203. package/target/main/ts/report/html/app/viewer-metric-registry.d.ts +68 -0
  204. package/target/main/ts/report/html/app/viewer-metric-registry.js +206 -0
  205. package/target/main/ts/report/html/app/viewer-metrics.d.ts +44 -0
  206. package/target/main/ts/report/html/app/viewer-metrics.js +172 -0
  207. package/target/main/ts/report/html/app/viewer-profile.d.ts +17 -0
  208. package/target/main/ts/report/html/app/viewer-profile.js +401 -0
  209. package/target/main/ts/report/html/app/viewer-projects.d.ts +1 -0
  210. package/target/main/ts/report/html/app/viewer-projects.js +90 -0
  211. package/target/main/ts/report/html/app/viewer-pulls.d.ts +3 -0
  212. package/target/main/ts/report/html/app/viewer-pulls.js +242 -0
  213. package/target/main/ts/report/html/app/viewer-source.d.ts +75 -0
  214. package/target/main/ts/report/html/app/viewer-source.js +313 -0
  215. package/target/main/ts/report/html/app/viewer-trends.d.ts +74 -0
  216. package/target/main/ts/report/html/app/viewer-trends.js +413 -0
  217. package/target/main/ts/report/html/app/viewer.d.ts +1 -0
  218. package/target/main/ts/report/html/app/viewer.js +794 -0
  219. package/target/main/ts/report/html/site/badges.d.ts +14 -0
  220. package/target/main/ts/report/html/site/badges.js +39 -0
  221. package/target/main/ts/report/html/site/export-site.d.ts +21 -0
  222. package/target/main/ts/report/html/site/export-site.js +157 -0
  223. package/target/main/ts/report/html/site/exported-server.d.ts +6 -0
  224. package/target/main/ts/report/html/site/exported-server.js +70 -0
  225. package/target/main/ts/report/html/site/local-server.d.ts +7 -0
  226. package/target/main/ts/report/html/site/local-server.js +64 -0
  227. package/target/main/ts/report/html/site/projects.d.ts +3 -0
  228. package/target/main/ts/report/html/site/projects.js +79 -0
  229. package/target/main/ts/report/html/site/server.d.ts +4 -0
  230. package/target/main/ts/report/html/site/server.js +64 -0
  231. package/target/main/ts/report/html/site/source-route.d.ts +6 -0
  232. package/target/main/ts/report/html/site/source-route.js +22 -0
  233. package/target/main/ts/report/json/projections/commit-summary.d.ts +11 -0
  234. package/target/main/ts/report/json/projections/commit-summary.js +118 -0
  235. package/target/main/ts/report/json/projections/components.d.ts +4 -0
  236. package/target/main/ts/report/json/projections/components.js +29 -0
  237. package/target/main/ts/report/json/projections/file-metrics.d.ts +5 -0
  238. package/target/main/ts/report/json/projections/file-metrics.js +243 -0
  239. package/target/main/ts/report/json/projections/inspection-context.d.ts +6 -0
  240. package/target/main/ts/report/json/projections/inspection-context.js +92 -0
  241. package/target/main/ts/report/json/projections/inspection-details.d.ts +11 -0
  242. package/target/main/ts/report/json/projections/inspection-details.js +69 -0
  243. package/target/main/ts/report/json/projections/metric-values.d.ts +7 -0
  244. package/target/main/ts/report/json/projections/metric-values.js +57 -0
  245. package/target/main/ts/report/json/projections/public-history.d.ts +4 -0
  246. package/target/main/ts/report/json/projections/public-history.js +16 -0
  247. package/target/main/ts/report/json/store/components.d.ts +2 -0
  248. package/target/main/ts/report/json/store/components.js +31 -0
  249. package/target/main/ts/report/json/store/local-store.d.ts +13 -0
  250. package/target/main/ts/report/json/store/local-store.js +179 -0
  251. package/target/main/ts/report/json/store/pull-review-store.d.ts +3 -0
  252. package/target/main/ts/report/json/store/pull-review-store.js +51 -0
  253. package/target/main/ts/report/json/validation/metabranch.d.ts +4 -0
  254. package/target/main/ts/report/json/validation/metabranch.js +7 -0
  255. package/target/main/ts/report/json/validation/report.d.ts +15 -0
  256. package/target/main/ts/report/json/validation/report.js +64 -0
  257. package/target/main/ts/report/json/validation/summary-validation.d.ts +4 -0
  258. package/target/main/ts/report/json/validation/summary-validation.js +27 -0
  259. package/target/main/ts/report/json/wire/compact-json.d.ts +11 -0
  260. package/target/main/ts/report/json/wire/compact-json.js +76 -0
  261. package/target/main/ts/report/json/wire/component-model.d.ts +9 -0
  262. package/target/main/ts/report/json/wire/component-model.js +1 -0
  263. package/target/main/ts/report/json/wire/domain-types.d.ts +2 -0
  264. package/target/main/ts/report/json/wire/domain-types.js +1 -0
  265. package/target/main/ts/report/json/wire/file-metrics-model.d.ts +77 -0
  266. package/target/main/ts/report/json/wire/file-metrics-model.js +1 -0
  267. package/target/main/ts/report/json/wire/inspection-context-model.d.ts +26 -0
  268. package/target/main/ts/report/json/wire/inspection-context-model.js +1 -0
  269. package/target/main/ts/report/json/wire/inspection-details-model.d.ts +82 -0
  270. package/target/main/ts/report/json/wire/inspection-details-model.js +1 -0
  271. package/target/main/ts/report/json/wire/project-model.d.ts +7 -0
  272. package/target/main/ts/report/json/wire/project-model.js +1 -0
  273. package/target/main/ts/report/json/wire/public-history-model.d.ts +42 -0
  274. package/target/main/ts/report/json/wire/public-history-model.js +6 -0
  275. package/target/main/ts/report/json/wire/pull-review-model.d.ts +7 -0
  276. package/target/main/ts/report/json/wire/pull-review-model.js +1 -0
  277. package/target/main/ts/report/json/wire/summary-model.d.ts +45 -0
  278. package/target/main/ts/report/json/wire/summary-model.js +1 -0
  279. package/target/main/ts/report.d.ts +11 -0
  280. package/target/main/ts/report.js +5 -0
  281. package/index.js +0 -1
@@ -0,0 +1,128 @@
1
+ import { isDeepStrictEqual } from 'node:util';
2
+ import { isRecord } from '../engine/plugin.js';
3
+ import { isSensitiveFinding } from '../model/finding.js';
4
+ import { pullReviewSchema } from '../model/pull-review.js';
5
+ import { findingImportMethods } from '../model/scanner-import.js';
6
+ import { compareInspections, observeCoverage } from './compare.js';
7
+ export async function executeReview(options, ports) {
8
+ if (!ports.measureChanged && options.minimumPercent !== undefined)
9
+ throw new Error('Minimum diff coverage requires --lcov');
10
+ if (ports.measureChanged) {
11
+ const { report, uncovered } = await ports.measureChanged();
12
+ const [head, base] = await Promise.all([ports.readHead(report.head), ports.readBase(report.requestedBase)]);
13
+ return projectReview(options, report, uncovered, head, base);
14
+ }
15
+ const revisions = await ports.readRevisions();
16
+ const [head, base, mergeBase] = await Promise.all([
17
+ ports.readHead(revisions.head), ports.readBase(revisions.base), revisions.mergeBase(),
18
+ ]);
19
+ return projectReview(options, {
20
+ generatedAt: new Date().toISOString(), requestedBase: revisions.base, base: mergeBase, head: revisions.head,
21
+ status: 'unavailable', covered: 0, total: 0, percent: null, files: [], gaps: [], decision: null,
22
+ }, [], head, base);
23
+ }
24
+ export function projectReview(request, diff, uncovered, head, base) {
25
+ const minimumLines = request.minimumLines ?? 0;
26
+ if (request.number !== undefined && (!Number.isSafeInteger(request.number) || request.number < 1)) {
27
+ throw new Error('Pull request number must be a positive integer');
28
+ }
29
+ if (!Number.isSafeInteger(minimumLines) || minimumLines < 0)
30
+ throw new Error('Minimum diff lines must be a non-negative integer');
31
+ if (request.maxNewIssues !== undefined && (!Number.isSafeInteger(request.maxNewIssues) || request.maxNewIssues < 0)) {
32
+ throw new Error('Maximum new issues must be a non-negative integer');
33
+ }
34
+ if (request.maxCoverageDrop !== undefined && (!Number.isFinite(request.maxCoverageDrop) || request.maxCoverageDrop < 0 || request.maxCoverageDrop > 100)) {
35
+ throw new Error('Maximum coverage drop must be a number from 0 to 100');
36
+ }
37
+ if (request.minimumLines !== undefined && diff.decision === null) {
38
+ throw new Error('Minimum diff lines requires a minimum diff coverage percentage');
39
+ }
40
+ const { status, covered, total, percent, files, gaps, decision } = diff;
41
+ const config = diff.appliedConfig;
42
+ const source = config?.source;
43
+ const lcov = config?.lcov;
44
+ const excludedPaths = isRecord(source) && Array.isArray(source.excludedPaths)
45
+ && source.excludedPaths.every((path) => typeof path === 'string') ? source.excludedPaths : null;
46
+ const coverageInputDigest = isRecord(lcov) && typeof lcov.inputDigest === 'string' ? lcov.inputDigest : null;
47
+ const gate = decision && {
48
+ minimumPercent: decision.minimumPercent,
49
+ minimumLines,
50
+ status: total < minimumLines && status !== 'partial' ? 'not-applicable' : decision.status,
51
+ };
52
+ return { schema: pullReviewSchema, number: request.number ?? null, generatedAt: diff.generatedAt,
53
+ base: diff.requestedBase, mergeBase: diff.base, head: diff.head,
54
+ coverage: coverageImpact(head, base, diff, request.maxCoverageDrop),
55
+ diff: { status, covered, total, percent, files, gaps, excludedPaths, coverageInputDigest, uncovered, gate },
56
+ findings: findingImpact(head, base, request.maxNewIssues),
57
+ };
58
+ }
59
+ export function reviewExitCode(review) {
60
+ const decisions = [review.diff.gate, review.coverage.gate, review.findings.gate].map(gate => gate?.status);
61
+ if (review.diff.status === 'partial' || decisions.includes('indeterminate')
62
+ || (review.diff.status === 'unavailable' && review.findings.added === null && review.coverage.delta === null))
63
+ return 2;
64
+ return decisions.includes('failed') ? 3 : 0;
65
+ }
66
+ function coverageImpact(head, base, diff, maxDrop) {
67
+ const method = head?.appliedConfig.coverage;
68
+ const input = diff.appliedConfig?.lcov;
69
+ const usesHeadCoverage = isRecord(method) && isRecord(input) && method.inputDigest === input.inputDigest;
70
+ const mismatch = head !== null && diff.appliedConfig !== undefined && !usesHeadCoverage;
71
+ const comparison = base && head && !mismatch ? compareInspections(base, head).metrics.lineCoverage : null;
72
+ const delta = comparison?.delta ?? null;
73
+ const gate = maxDrop === undefined ? null : { maxDrop,
74
+ status: delta === null ? 'indeterminate' : delta + maxDrop < -Number.EPSILON * 100 ? 'failed' : 'passed' };
75
+ return {
76
+ base: base ? observeCoverage(base) : null,
77
+ head: head && !mismatch ? observeCoverage(head) : null,
78
+ delta, reason: comparison ? comparison.reason : mismatch ? 'head-coverage-changed' : 'reports-unavailable', gate,
79
+ };
80
+ }
81
+ function findingImpact(head, base, maximum) {
82
+ const { added, reason } = comparableFindings(head, base, false);
83
+ const quality = added ?? comparableFindings(head, base, true).added;
84
+ const qualityCount = quality ? quality.filter(finding => !isSensitiveFinding(finding.category)).length : null;
85
+ return { added, qualityCount, reason, gate: maximum === undefined ? null : { maximum,
86
+ status: qualityCount === null ? 'indeterminate' : qualityCount > maximum ? 'failed' : 'passed' } };
87
+ }
88
+ function comparableFindings(head, base, qualityOnly) {
89
+ let reason = null;
90
+ let added = null;
91
+ if (!head || !base)
92
+ reason = 'reports-unavailable';
93
+ else if (!head.findings || !base.findings)
94
+ reason = 'findings-unavailable';
95
+ else if (incompleteAnalysis(head, qualityOnly) || incompleteAnalysis(base, qualityOnly))
96
+ reason = 'partial-inspection';
97
+ else if (!isDeepStrictEqual(findingMethod(head, qualityOnly), findingMethod(base, qualityOnly)))
98
+ reason = 'analysis-changed';
99
+ else
100
+ added = newFindings(base.findings, head.findings);
101
+ return { added, reason };
102
+ }
103
+ function incompleteAnalysis(report, qualityOnly) {
104
+ return report.scope.parsed < report.scope.files || report.diagnostics.some(({ operation }) => operation !== 'lcov/import' && operation !== 'git/churn'
105
+ && !(qualityOnly && /^findings\/(security|dependency|secret|infrastructure)\//.test(operation)));
106
+ }
107
+ function newFindings(base, head) {
108
+ const remaining = new Map();
109
+ const key = ({ category, path, tool, rule, message }) => JSON.stringify([category, path, tool, rule, message]);
110
+ for (const finding of base) {
111
+ const id = key(finding);
112
+ remaining.set(id, (remaining.get(id) ?? 0) + 1);
113
+ }
114
+ return head.filter(finding => {
115
+ const id = key(finding);
116
+ const count = remaining.get(id) ?? 0;
117
+ if (count === 0)
118
+ return true;
119
+ remaining.set(id, count - 1);
120
+ return false;
121
+ });
122
+ }
123
+ function findingMethod(report, qualityOnly) {
124
+ const { coverage, findingImports, churn, churnWindow, plugins, operations, ...settings } = report.appliedConfig;
125
+ const exclude = (entries, key, value) => Array.isArray(entries) ? entries.filter(entry => !isRecord(entry) || entry[key] !== value) : entries;
126
+ const imported = findingImportMethods(findingImports).filter(({ category }) => !qualityOnly || !isSensitiveFinding(category));
127
+ return { ...settings, imported, plugins: exclude(plugins, 'id', 'lcov'), operations: exclude(operations, 'key', 'lcov/import') };
128
+ }
@@ -0,0 +1,35 @@
1
+ export { pluginApi } from './core/engine/plugin.js';
2
+ export type { DataContract, InspectionInputs, Operation, Plugin, Product } from './core/engine/plugin.js';
3
+ export { plan } from './core/engine/plan.js';
4
+ export type { InputBinding, PlanRequest, PlannedOperation } from './core/engine/plan.js';
5
+ export { execute } from './core/engine/execute.js';
6
+ export type { Outcome } from './core/engine/execute.js';
7
+ export { analyzeSnapshot, inspect } from './compose/inspect.js';
8
+ export type { AnalysisExecutor, AnalysisInput, InspectionPorts, InspectRequest } from './compose/inspect.js';
9
+ export { diffCoverageSchema, measureDiffCoverage } from './compose/diff-coverage.js';
10
+ export type { DiffCoverageReport, DiffCoverageRequest } from './compose/diff-coverage.js';
11
+ export type { DiffCoveragePorts, DiffCoverageSource } from './core/usecases/diff-coverage.js';
12
+ export { reviewPull, reviewExitCode } from './compose/review.js';
13
+ export type { ReviewRequest } from './compose/review.js';
14
+ export type { ReviewPorts } from './core/usecases/review.js';
15
+ export { compareInspections, comparisonSchema } from './core/usecases/compare.js';
16
+ export { compareInspections as compare } from './core/usecases/compare.js';
17
+ export { measureDiffCoverage as diffCoverage } from './compose/diff-coverage.js';
18
+ export { review } from './compose/review.js';
19
+ export type { InspectionComparison } from './core/usecases/compare.js';
20
+ export { fileMetrics, fileMetricsSchema } from './report/json/projections/file-metrics.js';
21
+ export type { FileMetricsReport } from './report/json/projections/file-metrics.js';
22
+ export { LocalReportStore } from './report/json/store/local-store.js';
23
+ export { GitReportReader } from './api/git/git-reader.js';
24
+ export { readReport, reportSchema } from './report/json/validation/report.js';
25
+ export type { Diagnostic, Report, ReportReader, ReportStore } from './report/json/validation/report.js';
26
+ export type { Measurement } from './core/model/measurement.js';
27
+ export type { Finding, FindingLocation } from './core/model/finding.js';
28
+ export type { FindingInput } from './api/import/finding-import.js';
29
+ export type { FindingIgnoreRule, IgnoredFinding } from './core/model/finding-ignore.js';
30
+ export { coverageGrade, qualitySummary } from './core/model/quality-summary.js';
31
+ export type { Grade, QualitySummary } from './core/model/quality-summary.js';
32
+ export { pullReviewSchema } from './core/model/pull-review.js';
33
+ export { pullReviewsSchema } from './report/json/wire/pull-review-model.js';
34
+ export type { PullReview, PullReviews } from './report/json/wire/pull-review-model.js';
35
+ export type { Subject } from './core/model/snapshot.js';
@@ -1,13 +1,17 @@
1
- export { pluginApi } from './analysis/plugin.js';
2
- export { plan } from './analysis/plan.js';
3
- export { execute } from './analysis/execute.js';
4
- export { analyzeSnapshot, inspect } from './application/inspect.js';
5
- export { diffCoverageSchema, measureDiffCoverage } from './application/diff-coverage.js';
6
- export { reviewPull, reviewExitCode } from './application/review.js';
7
- export { compareInspections, comparisonSchema } from './comparison/compare.js';
8
- export { fileMetrics, fileMetricsSchema } from './presentation/file-metrics.js';
9
- export { LocalReportStore } from './reporting/local-store.js';
10
- export { GitReportReader } from './reporting/git-reader.js';
11
- export { readReport, reportSchema } from './reporting/report.js';
12
- export { coverageGrade, qualitySummary } from './inspections/quality-summary.js';
13
- export { pullReviewSchema, pullReviewsSchema } from './reporting/pull-review-model.js';
1
+ export { pluginApi } from './core/engine/plugin.js';
2
+ export { plan } from './core/engine/plan.js';
3
+ export { execute } from './core/engine/execute.js';
4
+ export { analyzeSnapshot, inspect } from './compose/inspect.js';
5
+ export { diffCoverageSchema, measureDiffCoverage } from './compose/diff-coverage.js';
6
+ export { reviewPull, reviewExitCode } from './compose/review.js';
7
+ export { compareInspections, comparisonSchema } from './core/usecases/compare.js';
8
+ export { compareInspections as compare } from './core/usecases/compare.js';
9
+ export { measureDiffCoverage as diffCoverage } from './compose/diff-coverage.js';
10
+ export { review } from './compose/review.js';
11
+ export { fileMetrics, fileMetricsSchema } from './report/json/projections/file-metrics.js';
12
+ export { LocalReportStore } from './report/json/store/local-store.js';
13
+ export { GitReportReader } from './api/git/git-reader.js';
14
+ export { readReport, reportSchema } from './report/json/validation/report.js';
15
+ export { coverageGrade, qualitySummary } from './core/model/quality-summary.js';
16
+ export { pullReviewSchema } from './core/model/pull-review.js';
17
+ export { pullReviewsSchema } from './report/json/wire/pull-review-model.js';
@@ -0,0 +1,14 @@
1
+ export { pluginApi } from './core/engine/plugin.js';
2
+ export type { DataContract, InspectionInputs, Operation, Plugin, PluginConfigSchema, Product, RuleDescriptor } from './core/engine/plugin.js';
3
+ export type { FileClaims, LegacyPlugin, MetricDescriptor, MetricScope, PluginKind, ProductDeclaration } from './core/engine/plugin.js';
4
+ export { codeStructure } from './core/model/code-structure.js';
5
+ export type { CodeStructure, FunctionComplexity, StructuralCheck, StructuralObservation } from './core/model/code-structure.js';
6
+ export type { Candidate, CloneSource } from './core/model/clone-source.js';
7
+ export { sourceFile } from './core/model/snapshot.js';
8
+ export type { InventoryFile, SourceFile, SourceSnapshot } from './core/model/source.js';
9
+ export { measurements } from './core/model/measurement.js';
10
+ export type { Measurement } from './core/model/measurement.js';
11
+ export { findingResult } from './core/model/finding.js';
12
+ export type { Finding, FindingResult } from './core/model/finding.js';
13
+ export type { SourceContext, SourcePlugin, SourceProvider } from './core/ports/source-provider.js';
14
+ export type { SourceLocator } from './core/model/source-locator.js';
@@ -0,0 +1,5 @@
1
+ export { pluginApi } from './core/engine/plugin.js';
2
+ export { codeStructure } from './core/model/code-structure.js';
3
+ export { sourceFile } from './core/model/snapshot.js';
4
+ export { measurements } from './core/model/measurement.js';
5
+ export { findingResult } from './core/model/finding.js';
@@ -0,0 +1,23 @@
1
+ import type { DataContract, Plugin } from '../../core/engine/plugin.js';
2
+ import type { Measurement } from '../../core/model/measurement.js';
3
+ export interface CoverageInput {
4
+ readonly text: string;
5
+ readonly root: string;
6
+ readonly sourceLines: ReadonlyMap<string, number>;
7
+ readonly excludes?: (path: string) => boolean;
8
+ }
9
+ interface CoverageResult {
10
+ readonly measurements: readonly Measurement[];
11
+ readonly gaps: readonly {
12
+ readonly path: string;
13
+ readonly reason: string;
14
+ }[];
15
+ }
16
+ export declare const lcovInput: DataContract<CoverageInput>;
17
+ export declare const coverageResult: DataContract<CoverageResult>;
18
+ export declare const coverage: Plugin;
19
+ export declare function mapCoverage({ text, root, sourceLines, excludes }: CoverageInput): {
20
+ files: Map<string, Map<number, boolean>>;
21
+ gaps: Map<string, string>;
22
+ };
23
+ export {};
@@ -0,0 +1,120 @@
1
+ import { posix, win32 } from 'node:path';
2
+ import { isRecord, pluginApi } from '../../core/engine/plugin.js';
3
+ import { comparePaths } from '../../core/model/snapshot.js';
4
+ import { definitions, measurements } from '../../core/model/measurement.js';
5
+ export const lcovInput = {
6
+ id: 'repometry.lcov-input/v1',
7
+ accepts: (value) => isRecord(value) && typeof value.text === 'string'
8
+ && typeof value.root === 'string' && value.sourceLines instanceof Map
9
+ && (value.excludes === undefined || typeof value.excludes === 'function'),
10
+ };
11
+ export const coverageResult = {
12
+ id: 'repometry.coverage-result/v1',
13
+ accepts: (value) => isRecord(value) && measurements.accepts(value.measurements)
14
+ && Array.isArray(value.gaps) && value.gaps.every(gap => isRecord(gap) && typeof gap.path === 'string' && typeof gap.reason === 'string'),
15
+ };
16
+ export const coverage = {
17
+ id: 'lcov',
18
+ version: '1.1.0',
19
+ api: pluginApi,
20
+ kinds: ['coverage', 'import'],
21
+ produces: [{ contract: coverageResult.id, persistence: 'report', metrics: [
22
+ { id: definitions.coverage, label: 'Line coverage', unit: 'ratio', scope: 'file', aggregate: 'sum', better: 'higher',
23
+ ratio: { of: definitions.coveredLines, over: definitions.coverableLines }, bands: [0.6, 0.7, 0.8, 0.9], group: 'Coverage' },
24
+ { id: definitions.coveredLines, label: 'Covered lines', unit: 'count', scope: 'file', aggregate: 'sum', better: 'higher', group: 'Coverage' },
25
+ { id: definitions.coverableLines, label: 'Measured lines', unit: 'count', scope: 'file', aggregate: 'sum', better: 'none', group: 'Coverage' },
26
+ ] }],
27
+ operations: [{
28
+ id: 'import',
29
+ role: 'inspection',
30
+ inputs: { report: lcovInput },
31
+ output: coverageResult,
32
+ execute: inputs => importCoverage(inputs.read('report', lcovInput)),
33
+ }],
34
+ };
35
+ export function mapCoverage({ text, root, sourceLines, excludes }) {
36
+ const files = new Map();
37
+ const gaps = new Map();
38
+ let active;
39
+ let ignored = false;
40
+ let sawSource = false;
41
+ const lines = text.split(/\r?\n/);
42
+ for (const [index, line] of lines.entries()) {
43
+ if (line.startsWith('SF:')) {
44
+ if (active !== undefined || line.length === 3)
45
+ throw new Error(`Unexpected LCOV source at line ${index + 1}`);
46
+ active = normalizeLcovPath(root, line.slice(3));
47
+ sawSource = true;
48
+ ignored = excludes?.(active) ?? false;
49
+ if (ignored)
50
+ continue;
51
+ if (!sourceLines.has(active))
52
+ gaps.set(active, 'LCOV source is outside the inspected file set');
53
+ else if (!files.has(active))
54
+ files.set(active, new Map());
55
+ }
56
+ else if (line.startsWith('DA:')) {
57
+ if (active === undefined)
58
+ throw new Error(`LCOV line data outside a source record at line ${index + 1}`);
59
+ if (ignored)
60
+ continue;
61
+ const match = /^DA:([1-9]\d*),(\d+)(?:,[^,]+)?$/.exec(line);
62
+ const number = Number(match?.[1]);
63
+ const hits = Number(match?.[2]);
64
+ if (!Number.isSafeInteger(number) || !Number.isSafeInteger(hits))
65
+ throw new Error(`Invalid LCOV line data at line ${index + 1}`);
66
+ const file = files.get(active);
67
+ const limit = sourceLines.get(active);
68
+ if (limit !== undefined && number > limit)
69
+ gaps.set(active, 'LCOV refers to a line outside the inspected source');
70
+ else if (file)
71
+ file.set(number, (file.get(number) ?? false) || hits > 0);
72
+ }
73
+ else if (line === 'end_of_record') {
74
+ if (active === undefined)
75
+ throw new Error(`Unexpected LCOV record end at line ${index + 1}`);
76
+ active = undefined;
77
+ }
78
+ }
79
+ if (active !== undefined)
80
+ throw new Error('Unterminated LCOV source record');
81
+ if (!sawSource && text.trim() !== '')
82
+ throw new Error('LCOV contains no source records');
83
+ return { files, gaps };
84
+ }
85
+ function importCoverage(input) {
86
+ const { files, gaps } = mapCoverage(input);
87
+ const rows = [];
88
+ let totalCovered = 0;
89
+ let totalLines = 0;
90
+ for (const [path, hits] of [...files].sort(([left], [right]) => comparePaths(left, right))) {
91
+ if (gaps.has(path))
92
+ continue;
93
+ let covered = 0;
94
+ for (const hit of hits.values())
95
+ if (hit)
96
+ covered++;
97
+ totalCovered += covered;
98
+ totalLines += hits.size;
99
+ rows.push({ definition: definitions.coverage, target: 'file', path, covered, total: hits.size, value: ratio(covered, hits.size) });
100
+ }
101
+ rows.push({ definition: definitions.coverage, target: 'repository', covered: totalCovered, total: totalLines, value: ratio(totalCovered, totalLines) });
102
+ const counts = rows.flatMap(({ target, path, covered, total }) => [
103
+ { definition: definitions.coveredLines, target, path, value: covered ?? null },
104
+ { definition: definitions.coverableLines, target, path, value: total ?? null },
105
+ ]);
106
+ return { measurements: [...rows, ...counts],
107
+ gaps: [...gaps].sort(([left], [right]) => comparePaths(left, right)).map(([path, reason]) => ({ path, reason })) };
108
+ }
109
+ function ratio(covered, total) {
110
+ return total === 0 ? null : covered / total;
111
+ }
112
+ function normalizeLcovPath(root, path) {
113
+ const platform = /^(?:[a-z]:[\\/]|\\\\)/i.test(root) ? win32 : posix;
114
+ const candidate = platform.isAbsolute(path) ? platform.relative(root, path) : path;
115
+ if (platform.isAbsolute(candidate))
116
+ return candidate;
117
+ if (platform === win32)
118
+ return posix.normalize(candidate.replaceAll('\\', '/'));
119
+ return posix.normalize(candidate);
120
+ }
@@ -0,0 +1,7 @@
1
+ import type { DataContract, Plugin } from '../../core/engine/plugin.js';
2
+ import type { GitChanges } from '../../core/model/changes.js';
3
+ import type { DiffCoverageResult } from '../../core/model/diff-coverage.js';
4
+ export type { DiffCoverage } from '../../core/model/diff-coverage.js';
5
+ export declare const changesInput: DataContract<GitChanges>;
6
+ export declare const diffCoverageResult: DataContract<DiffCoverageResult>;
7
+ export declare const diffCoverage: Plugin;
@@ -0,0 +1,89 @@
1
+ import { isRecord, pluginApi } from '../../core/engine/plugin.js';
2
+ import { comparePaths } from '../../core/model/snapshot.js';
3
+ import { lcovInput, mapCoverage } from './coverage.js';
4
+ export const changesInput = {
5
+ id: 'repometry.git-line-changes/v1',
6
+ accepts: (value) => isRecord(value) && value.files instanceof Map && value.gaps instanceof Map,
7
+ };
8
+ export const diffCoverageResult = {
9
+ id: 'repometry.diff-coverage-result/v1',
10
+ accepts: (value) => isRecord(value) && Array.isArray(value.files) && Array.isArray(value.gaps)
11
+ && Array.isArray(value.uncovered)
12
+ && (value.status === 'complete' || value.status === 'partial' || value.status === 'not-applicable')
13
+ && typeof value.covered === 'number' && typeof value.total === 'number'
14
+ && (value.percent === null || typeof value.percent === 'number'),
15
+ };
16
+ export const diffCoverage = {
17
+ id: 'diff-coverage',
18
+ version: '1.0.0',
19
+ api: pluginApi,
20
+ kinds: ['coverage'],
21
+ produces: [{ contract: diffCoverageResult.id, persistence: 'transient' }],
22
+ operations: [{
23
+ id: 'measure',
24
+ role: 'inspection',
25
+ inputs: { changes: changesInput, lcov: lcovInput },
26
+ output: diffCoverageResult,
27
+ execute: inputs => measure(inputs.read('changes', changesInput), inputs.read('lcov', lcovInput)),
28
+ }],
29
+ };
30
+ function measure(changes, input) {
31
+ if (changes.files.size === 0 && changes.gaps.size === 0) {
32
+ return { status: 'not-applicable', covered: 0, total: 0, percent: null, files: [], gaps: [], uncovered: [] };
33
+ }
34
+ const lcov = mapCoverage(input);
35
+ const gaps = new Map([...lcov.gaps, ...changes.gaps]);
36
+ const files = [];
37
+ const uncovered = [];
38
+ let covered = 0;
39
+ let total = 0;
40
+ for (const [path, ranges] of changes.files) {
41
+ const addedLines = ranges.reduce((sum, range) => sum + range.last - range.first + 1, 0);
42
+ const lines = lcov.files.get(path);
43
+ if (gaps.has(path) || lines === undefined) {
44
+ if (!gaps.has(path))
45
+ gaps.set(path, 'Changed file has no LCOV source record');
46
+ files.push({ path, addedLines, covered: null, total: null });
47
+ continue;
48
+ }
49
+ let fileCovered = 0;
50
+ let fileTotal = 0;
51
+ const missed = [];
52
+ for (const [line, hit] of lines) {
53
+ if (!containsLine(ranges, line))
54
+ continue;
55
+ fileTotal++;
56
+ if (hit)
57
+ fileCovered++;
58
+ else
59
+ missed.push(line);
60
+ }
61
+ covered += fileCovered;
62
+ total += fileTotal;
63
+ files.push({ path, addedLines, covered: fileCovered, total: fileTotal });
64
+ if (missed.length > 0)
65
+ uncovered.push({ path, lines: missed.sort((left, right) => left - right) });
66
+ }
67
+ return {
68
+ status: gaps.size > 0 ? 'partial' : total === 0 ? 'not-applicable' : 'complete',
69
+ covered, total, percent: total === 0 ? null : 100 * covered / total,
70
+ files: files.sort((a, b) => comparePaths(a.path, b.path)),
71
+ gaps: [...gaps].sort(([a], [b]) => comparePaths(a, b)).map(([path, reason]) => ({ path, reason })),
72
+ uncovered: uncovered.sort((left, right) => comparePaths(left.path, right.path)),
73
+ };
74
+ }
75
+ function containsLine(ranges, line) {
76
+ let lower = 0;
77
+ let upper = ranges.length;
78
+ while (lower < upper) {
79
+ const middle = Math.floor((lower + upper) / 2);
80
+ const { first, last } = ranges[middle];
81
+ if (line < first)
82
+ upper = middle;
83
+ else if (line > last)
84
+ lower = middle + 1;
85
+ else
86
+ return true;
87
+ }
88
+ return false;
89
+ }
@@ -0,0 +1,4 @@
1
+ import type { DataContract, Plugin } from '../../core/engine/plugin.js';
2
+ import type { CloneSource } from '../../core/model/clone-source.js';
3
+ export declare const duplication: Plugin;
4
+ export declare const duplicationInput: DataContract<readonly CloneSource[]>;
@@ -0,0 +1,93 @@
1
+ import { pluginApi } from '../../core/engine/plugin.js';
2
+ import { comparePaths } from '../../core/model/snapshot.js';
3
+ import { findingResult } from '../../core/model/finding.js';
4
+ import { definitions } from '../../core/model/measurement.js';
5
+ import { isCloneSource } from '../../core/model/clone-source.js';
6
+ import { duplicationSettings } from '../../core/profile/maintainability.js';
7
+ const cloneSources = {
8
+ id: 'repometry.clone-sources-babel/v1',
9
+ accepts: (value) => Array.isArray(value) && value.every(isCloneSource),
10
+ };
11
+ export const duplication = {
12
+ id: 'duplication', version: '1.0.0', api: pluginApi,
13
+ kinds: ['analysis'],
14
+ produces: [{ contract: findingResult.id, persistence: 'report', metrics: [
15
+ { id: definitions.duplicatedLines, label: 'Duplicated lines', unit: 'count', scope: 'file', aggregate: 'sum', better: 'lower',
16
+ group: 'Maintainability' },
17
+ ] }],
18
+ operations: [{
19
+ id: 'detect', role: 'inspection', inputs: { sources: cloneSources }, output: findingResult,
20
+ execute(inputs) { return detectClones(inputs.read('sources', cloneSources)); },
21
+ }],
22
+ };
23
+ export const duplicationInput = cloneSources;
24
+ function detectClones(sources) {
25
+ const byFingerprint = new Map();
26
+ for (const { path, candidates } of sources) {
27
+ for (const candidate of candidates) {
28
+ const occurrences = byFingerprint.get(candidate.similar);
29
+ const occurrence = { ...candidate, path };
30
+ if (occurrences)
31
+ occurrences.push(occurrence);
32
+ else
33
+ byFingerprint.set(candidate.similar, [occurrence]);
34
+ }
35
+ }
36
+ const groups = [...byFingerprint.values()].filter(group => group.length > 1)
37
+ .sort((left, right) => right[0].nodes - left[0].nodes || comparePaths(left[0].similar, right[0].similar));
38
+ const matched = new Map(sources.map(source => [source.path, []]));
39
+ const findings = [];
40
+ for (const group of groups) {
41
+ const fresh = group.filter(occurrence => {
42
+ const selected = matched.get(occurrence.path);
43
+ const index = insertionIndex(selected, occurrence.start);
44
+ return (selected[index - 1]?.end ?? -1) <= occurrence.start && (selected[index]?.start ?? Infinity) >= occurrence.end;
45
+ });
46
+ if (fresh.length < 2)
47
+ continue;
48
+ fresh.sort((left, right) => comparePaths(left.path, right.path) || left.start - right.start);
49
+ const first = fresh[0];
50
+ const identical = fresh.every(item => item.exact === first.exact);
51
+ for (const occurrence of fresh) {
52
+ const selected = matched.get(occurrence.path);
53
+ selected.splice(insertionIndex(selected, occurrence.start), 0, occurrence);
54
+ }
55
+ const rule = identical ? 'identical-code' : 'similar-code';
56
+ const effortMinutes = fresh.slice(1).reduce((total, occurrence) => total + occurrence.codeLines, 0) * duplicationSettings.minutesPerRepeatedLine;
57
+ findings.push({ tool: 'repometry', rule, category: 'duplication', level: 'medium',
58
+ message: `${identical ? 'Identical' : 'Similar'} code in ${fresh.length} locations`, ...location(first),
59
+ related: fresh.slice(1).map(location), effortMinutes });
60
+ }
61
+ return {
62
+ findings,
63
+ measurements: sources.map(({ path, codeLines }) => ({ definition: definitions.duplicatedLines, target: 'file',
64
+ path, value: matchedLineCount(codeLines, matched.get(path)) })),
65
+ };
66
+ }
67
+ function location({ path, line, endLine }) {
68
+ return { path, line, endLine };
69
+ }
70
+ function insertionIndex(selected, start) {
71
+ let left = 0;
72
+ let right = selected.length;
73
+ while (left < right) {
74
+ const middle = Math.floor((left + right) / 2);
75
+ if (selected[middle].start < start)
76
+ left = middle + 1;
77
+ else
78
+ right = middle;
79
+ }
80
+ return left;
81
+ }
82
+ function matchedLineCount(codeLines, matches) {
83
+ let index = 0;
84
+ let count = 0;
85
+ for (const line of codeLines) {
86
+ while (index < matches.length && matches[index].endLine < line)
87
+ index++;
88
+ const match = matches[index];
89
+ if (match && match.line <= line)
90
+ count++;
91
+ }
92
+ return count;
93
+ }
@@ -0,0 +1,10 @@
1
+ import type { Plugin } from '../../core/engine/plugin.js';
2
+ export declare const parserDefaults: {
3
+ readonly sourceType: 'unambiguous';
4
+ readonly errorRecovery: false;
5
+ readonly attachComment: false;
6
+ };
7
+ export declare const babelTypes: {
8
+ '@babel/types': string;
9
+ };
10
+ export declare const babelProvider: Plugin;
@@ -0,0 +1,46 @@
1
+ import { createRequire } from 'node:module';
2
+ import { extname } from 'node:path';
3
+ import { parse } from '@babel/parser';
4
+ import { pluginApi } from '../../core/engine/plugin.js';
5
+ import { codeStructure } from '../../core/model/code-structure.js';
6
+ import { physicalLines, sourceExtensions, sourceFile, utf8 } from '../../core/model/snapshot.js';
7
+ import { cloneCandidates } from './clone-candidates.js';
8
+ import { classifyLines } from './code-lines.js';
9
+ import { functionFacts } from './function-facts.js';
10
+ import { structuralObservations } from './structural-observations.js';
11
+ const require = createRequire(import.meta.url);
12
+ export const parserDefaults = { sourceType: 'unambiguous', errorRecovery: false, attachComment: false };
13
+ export const babelTypes = { '@babel/types': require('@babel/types/package.json').version };
14
+ export const babelProvider = {
15
+ id: 'babel-syntax',
16
+ version: '1.0.0',
17
+ api: pluginApi,
18
+ kinds: ['language'],
19
+ claims: { extensions: sourceExtensions, languages: ['javascript', 'typescript'] },
20
+ produces: [{ contract: codeStructure.id, persistence: 'transient' }],
21
+ tools: { '@babel/parser': require('@babel/parser/package.json').version },
22
+ operations: [{
23
+ id: 'parse',
24
+ role: 'provider',
25
+ inputs: { source: sourceFile },
26
+ output: codeStructure,
27
+ execute(inputs) {
28
+ const file = inputs.read('source', sourceFile);
29
+ const source = utf8(file.bytes);
30
+ const ext = extname(file.path);
31
+ const plugins = [];
32
+ if (['.ts', '.mts', '.cts', '.tsx'].includes(ext)) {
33
+ plugins.push(['typescript', { dts: /\.d\.[cm]?ts$/.test(file.path), disallowAmbiguousJSXLike: ext === '.mts' || ext === '.cts' }]);
34
+ }
35
+ if (ext === '.jsx' || ext === '.tsx')
36
+ plugins.push('jsx');
37
+ const sourceType = ext === '.mjs' || ext === '.mts' ? 'module'
38
+ : ext === '.cjs' || ext === '.cts' ? 'commonjs' : parserDefaults.sourceType;
39
+ const ast = parse(source, { ...parserDefaults, sourceType, plugins });
40
+ const lines = physicalLines(source);
41
+ const { code, comments } = classifyLines(source, ast, lines);
42
+ return { path: file.path, lines, commentLines: comments, ...functionFacts(ast),
43
+ ...structuralObservations(ast), clones: cloneCandidates(file.path, ast, code) };
44
+ },
45
+ }],
46
+ };
@@ -0,0 +1,3 @@
1
+ import type { Node } from '@babel/types';
2
+ import type { CloneSource } from '../../core/model/clone-source.js';
3
+ export declare function cloneCandidates(path: string, ast: Node, codeLines: readonly number[]): CloneSource;