repometry 0.0.0-stage → 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.
- package/LICENSE +21 -0
- package/README.md +27 -2
- package/package.json +63 -4
- package/target/main/resources/report/json/compact-json-v1.schema.json +24 -0
- package/target/main/resources/report/json/comparison-v1.schema.json +65 -0
- package/target/main/resources/report/json/config-v1.schema.json +34 -0
- package/target/main/resources/report/json/diff-coverage-v1.schema.json +55 -0
- package/target/main/resources/report/json/file-metrics-v1.schema.json +134 -0
- package/target/main/resources/report/json/inspection-context-v1.schema.json +172 -0
- package/target/main/resources/report/json/metabranch-v1.schema.json +9 -0
- package/target/main/resources/report/json/metric-catalog-v1.schema.json +31 -0
- package/target/main/resources/report/json/public-history-v1.schema.json +50 -0
- package/target/main/resources/report/json/pull-review-v1.schema.json +97 -0
- package/target/main/resources/report/json/quality-history-v1.schema.json +51 -0
- package/target/main/resources/report/json/report-v1.schema.json +65 -0
- package/target/main/resources/report/json/report-v2.schema.json +238 -0
- package/target/main/resources/report/json/repository-summary-v1.schema.json +181 -0
- package/target/main/resources/report/json/viewer-details-v1.schema.json +343 -0
- package/target/main/resources/report/json/viewer-inspection-v1.schema.json +13 -0
- package/target/main/resources/viewer/files.css +578 -0
- package/target/main/resources/viewer/index.html +130 -0
- package/target/main/resources/viewer/projects.html +52 -0
- package/target/main/resources/viewer/styles.css +586 -0
- package/target/main/resources/viewer/viewer-pulls.css +64 -0
- package/target/main/ts/api/gh/github-pr-comment.d.ts +4 -0
- package/target/main/ts/api/gh/github-pr-comment.js +107 -0
- package/target/main/ts/api/gh/github.d.ts +6 -0
- package/target/main/ts/api/gh/github.js +91 -0
- package/target/main/ts/api/git/blob-batch.d.ts +10 -0
- package/target/main/ts/api/git/blob-batch.js +105 -0
- package/target/main/ts/api/git/changes.d.ts +22 -0
- package/target/main/ts/api/git/changes.js +107 -0
- package/target/main/ts/api/git/commit-metadata.d.ts +3 -0
- package/target/main/ts/api/git/commit-metadata.js +54 -0
- package/target/main/ts/api/git/git-churn.d.ts +9 -0
- package/target/main/ts/api/git/git-churn.js +70 -0
- package/target/main/ts/api/git/git-reader.d.ts +16 -0
- package/target/main/ts/api/git/git-reader.js +73 -0
- package/target/main/ts/api/git/git.d.ts +21 -0
- package/target/main/ts/api/git/git.js +152 -0
- package/target/main/ts/api/git/inspection-source.d.ts +15 -0
- package/target/main/ts/api/git/inspection-source.js +30 -0
- package/target/main/ts/api/git/local-sources.d.ts +5 -0
- package/target/main/ts/api/git/local-sources.js +46 -0
- package/target/main/ts/api/git/local.d.ts +3 -0
- package/target/main/ts/api/git/local.js +35 -0
- package/target/main/ts/api/git/metabranch-worktree.d.ts +1 -0
- package/target/main/ts/api/git/metabranch-worktree.js +34 -0
- package/target/main/ts/api/git/source-cache.d.ts +3 -0
- package/target/main/ts/api/git/source-cache.js +56 -0
- package/target/main/ts/api/import/finding-import.d.ts +9 -0
- package/target/main/ts/api/import/finding-import.js +226 -0
- package/target/main/ts/api/import/lcov-files.d.ts +5 -0
- package/target/main/ts/api/import/lcov-files.js +13 -0
- package/target/main/ts/api/import/profile-file.d.ts +4 -0
- package/target/main/ts/api/import/profile-file.js +16 -0
- package/target/main/ts/api/worker/remote-executor.d.ts +2 -0
- package/target/main/ts/api/worker/remote-executor.js +45 -0
- package/target/main/ts/api/worker/worker-protocol.d.ts +7 -0
- package/target/main/ts/api/worker/worker-protocol.js +101 -0
- package/target/main/ts/api/worker/worker.d.ts +5 -0
- package/target/main/ts/api/worker/worker.js +85 -0
- package/target/main/ts/cli/arguments.d.ts +28 -0
- package/target/main/ts/cli/arguments.js +110 -0
- package/target/main/ts/cli/config.d.ts +14 -0
- package/target/main/ts/cli/config.js +60 -0
- package/target/main/ts/cli/github-pr-comment.d.ts +1 -0
- package/target/main/ts/cli/github-pr-comment.js +12 -0
- package/target/main/ts/cli/imports.d.ts +7 -0
- package/target/main/ts/cli/imports.js +29 -0
- package/target/main/ts/cli/main.d.ts +2 -0
- package/target/main/ts/cli/main.js +378 -0
- package/target/main/ts/cli/reporting.d.ts +18 -0
- package/target/main/ts/cli/reporting.js +108 -0
- package/target/main/ts/cli/revisions.d.ts +6 -0
- package/target/main/ts/cli/revisions.js +15 -0
- package/target/main/ts/cli/store.d.ts +13 -0
- package/target/main/ts/cli/store.js +38 -0
- package/target/main/ts/compose/analyze-snapshot.d.ts +4 -0
- package/target/main/ts/compose/analyze-snapshot.js +163 -0
- package/target/main/ts/compose/attach-churn.d.ts +2 -0
- package/target/main/ts/compose/attach-churn.js +37 -0
- package/target/main/ts/compose/diff-coverage.d.ts +20 -0
- package/target/main/ts/compose/diff-coverage.js +56 -0
- package/target/main/ts/compose/inspect.d.ts +23 -0
- package/target/main/ts/compose/inspect.js +44 -0
- package/target/main/ts/compose/plugin-list.d.ts +6 -0
- package/target/main/ts/compose/plugin-list.js +9 -0
- package/target/main/ts/compose/prepare-metabranch.d.ts +9 -0
- package/target/main/ts/compose/prepare-metabranch.js +16 -0
- package/target/main/ts/compose/review.d.ts +16 -0
- package/target/main/ts/compose/review.js +38 -0
- package/target/main/ts/compose/sources.d.ts +3 -0
- package/target/main/ts/compose/sources.js +29 -0
- package/target/main/ts/core/engine/execute.d.ts +10 -0
- package/target/main/ts/core/engine/execute.js +49 -0
- package/target/main/ts/core/engine/manifest.d.ts +4 -0
- package/target/main/ts/core/engine/manifest.js +104 -0
- package/target/main/ts/core/engine/plan.d.ts +22 -0
- package/target/main/ts/core/engine/plan.js +106 -0
- package/target/main/ts/core/engine/plugin.d.ts +59 -0
- package/target/main/ts/core/engine/plugin.js +4 -0
- package/target/main/ts/core/engine/report-products.d.ts +16 -0
- package/target/main/ts/core/engine/report-products.js +85 -0
- package/target/main/ts/core/model/changes.d.ts +8 -0
- package/target/main/ts/core/model/changes.js +1 -0
- package/target/main/ts/core/model/churn.d.ts +11 -0
- package/target/main/ts/core/model/churn.js +5 -0
- package/target/main/ts/core/model/clone-source.d.ts +16 -0
- package/target/main/ts/core/model/clone-source.js +14 -0
- package/target/main/ts/core/model/code-structure.d.ts +30 -0
- package/target/main/ts/core/model/code-structure.js +31 -0
- package/target/main/ts/core/model/comparison.d.ts +24 -0
- package/target/main/ts/core/model/comparison.js +1 -0
- package/target/main/ts/core/model/diff-coverage.d.ts +23 -0
- package/target/main/ts/core/model/diff-coverage.js +1 -0
- package/target/main/ts/core/model/finding-ignore.d.ts +8 -0
- package/target/main/ts/core/model/finding-ignore.js +35 -0
- package/target/main/ts/core/model/finding.d.ts +28 -0
- package/target/main/ts/core/model/finding.js +29 -0
- package/target/main/ts/core/model/ignore-rule.d.ts +11 -0
- package/target/main/ts/core/model/ignore-rule.js +1 -0
- package/target/main/ts/core/model/measurement.d.ts +27 -0
- package/target/main/ts/core/model/measurement.js +24 -0
- package/target/main/ts/core/model/metric-descriptor.d.ts +17 -0
- package/target/main/ts/core/model/metric-descriptor.js +1 -0
- package/target/main/ts/core/model/plugin-data.d.ts +25 -0
- package/target/main/ts/core/model/plugin-data.js +1 -0
- package/target/main/ts/core/model/pull-review.d.ts +48 -0
- package/target/main/ts/core/model/pull-review.js +1 -0
- package/target/main/ts/core/model/quality-summary.d.ts +42 -0
- package/target/main/ts/core/model/quality-summary.js +64 -0
- package/target/main/ts/core/model/report.d.ts +32 -0
- package/target/main/ts/core/model/report.js +5 -0
- package/target/main/ts/core/model/scanner-import.d.ts +39 -0
- package/target/main/ts/core/model/scanner-import.js +28 -0
- package/target/main/ts/core/model/snapshot.d.ts +15 -0
- package/target/main/ts/core/model/snapshot.js +50 -0
- package/target/main/ts/core/model/source-locator.d.ts +7 -0
- package/target/main/ts/core/model/source-locator.js +1 -0
- package/target/main/ts/core/model/source.d.ts +23 -0
- package/target/main/ts/core/model/source.js +1 -0
- package/target/main/ts/core/ports/report.d.ts +9 -0
- package/target/main/ts/core/ports/report.js +1 -0
- package/target/main/ts/core/ports/source-provider.d.ts +14 -0
- package/target/main/ts/core/ports/source-provider.js +4 -0
- package/target/main/ts/core/profile/exclusions.d.ts +11 -0
- package/target/main/ts/core/profile/exclusions.js +57 -0
- package/target/main/ts/core/profile/maintainability.d.ts +31 -0
- package/target/main/ts/core/profile/maintainability.js +9 -0
- package/target/main/ts/core/profile/profile.d.ts +4 -0
- package/target/main/ts/core/profile/profile.js +10 -0
- package/target/main/ts/core/usecases/compare.d.ts +6 -0
- package/target/main/ts/core/usecases/compare.js +145 -0
- package/target/main/ts/core/usecases/diff-coverage-policy.d.ts +6 -0
- package/target/main/ts/core/usecases/diff-coverage-policy.js +10 -0
- package/target/main/ts/core/usecases/diff-coverage.d.ts +40 -0
- package/target/main/ts/core/usecases/diff-coverage.js +15 -0
- package/target/main/ts/core/usecases/inspect.d.ts +38 -0
- package/target/main/ts/core/usecases/inspect.js +41 -0
- package/target/main/ts/core/usecases/review.d.ts +31 -0
- package/target/main/ts/core/usecases/review.js +128 -0
- package/target/main/ts/index.d.ts +35 -0
- package/target/main/ts/index.js +17 -0
- package/target/main/ts/plugin.d.ts +14 -0
- package/target/main/ts/plugin.js +5 -0
- package/target/main/ts/plugins/coverage/coverage.d.ts +23 -0
- package/target/main/ts/plugins/coverage/coverage.js +120 -0
- package/target/main/ts/plugins/coverage/diff-coverage.d.ts +7 -0
- package/target/main/ts/plugins/coverage/diff-coverage.js +89 -0
- package/target/main/ts/plugins/duplication/duplication.d.ts +4 -0
- package/target/main/ts/plugins/duplication/duplication.js +93 -0
- package/target/main/ts/plugins/language-js/babel-provider.d.ts +10 -0
- package/target/main/ts/plugins/language-js/babel-provider.js +46 -0
- package/target/main/ts/plugins/language-js/clone-candidates.d.ts +3 -0
- package/target/main/ts/plugins/language-js/clone-candidates.js +64 -0
- package/target/main/ts/plugins/language-js/code-lines.d.ts +6 -0
- package/target/main/ts/plugins/language-js/code-lines.js +35 -0
- package/target/main/ts/plugins/language-js/function-facts.d.ts +8 -0
- package/target/main/ts/plugins/language-js/function-facts.js +60 -0
- package/target/main/ts/plugins/language-js/structural-observations.d.ts +8 -0
- package/target/main/ts/plugins/language-js/structural-observations.js +79 -0
- package/target/main/ts/plugins/maintainability/maintainability.d.ts +9 -0
- package/target/main/ts/plugins/maintainability/maintainability.js +42 -0
- package/target/main/ts/plugins/maintainability/structural-smells.d.ts +3 -0
- package/target/main/ts/plugins/maintainability/structural-smells.js +15 -0
- package/target/main/ts/plugins/source/forges.d.ts +4 -0
- package/target/main/ts/plugins/source/forges.js +79 -0
- package/target/main/ts/plugins/structure/structure.d.ts +2 -0
- package/target/main/ts/plugins/structure/structure.js +37 -0
- package/target/main/ts/report/html/app/viewer-access.d.ts +19 -0
- package/target/main/ts/report/html/app/viewer-access.js +25 -0
- package/target/main/ts/report/html/app/viewer-chart.d.ts +101 -0
- package/target/main/ts/report/html/app/viewer-chart.js +711 -0
- package/target/main/ts/report/html/app/viewer-commits.d.ts +36 -0
- package/target/main/ts/report/html/app/viewer-commits.js +280 -0
- package/target/main/ts/report/html/app/viewer-data.d.ts +23 -0
- package/target/main/ts/report/html/app/viewer-data.js +105 -0
- package/target/main/ts/report/html/app/viewer-files.d.ts +117 -0
- package/target/main/ts/report/html/app/viewer-files.js +2087 -0
- package/target/main/ts/report/html/app/viewer-filter.d.ts +113 -0
- package/target/main/ts/report/html/app/viewer-filter.js +492 -0
- package/target/main/ts/report/html/app/viewer-metric-registry.d.ts +68 -0
- package/target/main/ts/report/html/app/viewer-metric-registry.js +206 -0
- package/target/main/ts/report/html/app/viewer-metrics.d.ts +44 -0
- package/target/main/ts/report/html/app/viewer-metrics.js +172 -0
- package/target/main/ts/report/html/app/viewer-profile.d.ts +17 -0
- package/target/main/ts/report/html/app/viewer-profile.js +401 -0
- package/target/main/ts/report/html/app/viewer-projects.d.ts +1 -0
- package/target/main/ts/report/html/app/viewer-projects.js +90 -0
- package/target/main/ts/report/html/app/viewer-pulls.d.ts +3 -0
- package/target/main/ts/report/html/app/viewer-pulls.js +242 -0
- package/target/main/ts/report/html/app/viewer-source.d.ts +75 -0
- package/target/main/ts/report/html/app/viewer-source.js +313 -0
- package/target/main/ts/report/html/app/viewer-trends.d.ts +74 -0
- package/target/main/ts/report/html/app/viewer-trends.js +413 -0
- package/target/main/ts/report/html/app/viewer.d.ts +1 -0
- package/target/main/ts/report/html/app/viewer.js +794 -0
- package/target/main/ts/report/html/site/badges.d.ts +14 -0
- package/target/main/ts/report/html/site/badges.js +39 -0
- package/target/main/ts/report/html/site/export-site.d.ts +21 -0
- package/target/main/ts/report/html/site/export-site.js +157 -0
- package/target/main/ts/report/html/site/exported-server.d.ts +6 -0
- package/target/main/ts/report/html/site/exported-server.js +70 -0
- package/target/main/ts/report/html/site/local-server.d.ts +7 -0
- package/target/main/ts/report/html/site/local-server.js +64 -0
- package/target/main/ts/report/html/site/projects.d.ts +3 -0
- package/target/main/ts/report/html/site/projects.js +79 -0
- package/target/main/ts/report/html/site/server.d.ts +4 -0
- package/target/main/ts/report/html/site/server.js +64 -0
- package/target/main/ts/report/html/site/source-route.d.ts +6 -0
- package/target/main/ts/report/html/site/source-route.js +22 -0
- package/target/main/ts/report/json/projections/commit-summary.d.ts +11 -0
- package/target/main/ts/report/json/projections/commit-summary.js +118 -0
- package/target/main/ts/report/json/projections/components.d.ts +4 -0
- package/target/main/ts/report/json/projections/components.js +29 -0
- package/target/main/ts/report/json/projections/file-metrics.d.ts +5 -0
- package/target/main/ts/report/json/projections/file-metrics.js +243 -0
- package/target/main/ts/report/json/projections/inspection-context.d.ts +6 -0
- package/target/main/ts/report/json/projections/inspection-context.js +92 -0
- package/target/main/ts/report/json/projections/inspection-details.d.ts +11 -0
- package/target/main/ts/report/json/projections/inspection-details.js +69 -0
- package/target/main/ts/report/json/projections/metric-values.d.ts +7 -0
- package/target/main/ts/report/json/projections/metric-values.js +57 -0
- package/target/main/ts/report/json/projections/public-history.d.ts +4 -0
- package/target/main/ts/report/json/projections/public-history.js +16 -0
- package/target/main/ts/report/json/store/components.d.ts +2 -0
- package/target/main/ts/report/json/store/components.js +31 -0
- package/target/main/ts/report/json/store/local-store.d.ts +13 -0
- package/target/main/ts/report/json/store/local-store.js +179 -0
- package/target/main/ts/report/json/store/pull-review-store.d.ts +3 -0
- package/target/main/ts/report/json/store/pull-review-store.js +51 -0
- package/target/main/ts/report/json/validation/metabranch.d.ts +4 -0
- package/target/main/ts/report/json/validation/metabranch.js +7 -0
- package/target/main/ts/report/json/validation/report.d.ts +15 -0
- package/target/main/ts/report/json/validation/report.js +64 -0
- package/target/main/ts/report/json/validation/summary-validation.d.ts +4 -0
- package/target/main/ts/report/json/validation/summary-validation.js +27 -0
- package/target/main/ts/report/json/wire/compact-json.d.ts +11 -0
- package/target/main/ts/report/json/wire/compact-json.js +76 -0
- package/target/main/ts/report/json/wire/component-model.d.ts +9 -0
- package/target/main/ts/report/json/wire/component-model.js +1 -0
- package/target/main/ts/report/json/wire/domain-types.d.ts +2 -0
- package/target/main/ts/report/json/wire/domain-types.js +1 -0
- package/target/main/ts/report/json/wire/file-metrics-model.d.ts +77 -0
- package/target/main/ts/report/json/wire/file-metrics-model.js +1 -0
- package/target/main/ts/report/json/wire/inspection-context-model.d.ts +26 -0
- package/target/main/ts/report/json/wire/inspection-context-model.js +1 -0
- package/target/main/ts/report/json/wire/inspection-details-model.d.ts +82 -0
- package/target/main/ts/report/json/wire/inspection-details-model.js +1 -0
- package/target/main/ts/report/json/wire/project-model.d.ts +7 -0
- package/target/main/ts/report/json/wire/project-model.js +1 -0
- package/target/main/ts/report/json/wire/public-history-model.d.ts +42 -0
- package/target/main/ts/report/json/wire/public-history-model.js +6 -0
- package/target/main/ts/report/json/wire/pull-review-model.d.ts +7 -0
- package/target/main/ts/report/json/wire/pull-review-model.js +1 -0
- package/target/main/ts/report/json/wire/summary-model.d.ts +45 -0
- package/target/main/ts/report/json/wire/summary-model.js +1 -0
- package/target/main/ts/report.d.ts +11 -0
- package/target/main/ts/report.js +5 -0
|
@@ -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,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,64 @@
|
|
|
1
|
+
import { NODE_FIELDS, VISITOR_KEYS, isFunction, isStatement } from '@babel/types';
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { duplicationSettings } from '../../core/profile/maintainability.js';
|
|
4
|
+
export function cloneCandidates(path, ast, codeLines) {
|
|
5
|
+
const candidates = [];
|
|
6
|
+
if (codeLines.length >= duplicationSettings.minCodeLines)
|
|
7
|
+
walk(ast);
|
|
8
|
+
return { path, codeLines, candidates };
|
|
9
|
+
function walk(node) {
|
|
10
|
+
let nodes = 1;
|
|
11
|
+
const exact = [node.type];
|
|
12
|
+
const similar = [node.type];
|
|
13
|
+
const properties = node;
|
|
14
|
+
const keys = VISITOR_KEYS[node.type] ?? [];
|
|
15
|
+
for (const key of keys) {
|
|
16
|
+
const value = properties[key];
|
|
17
|
+
const descendants = (Array.isArray(value) ? value : [value]).map((child) => child ? walk(child) : null);
|
|
18
|
+
for (const descendant of descendants)
|
|
19
|
+
nodes += descendant?.nodes ?? 0;
|
|
20
|
+
exact.push(descendants.map(descendant => descendant?.exact ?? null));
|
|
21
|
+
similar.push(descendants.map(descendant => descendant?.similar ?? null));
|
|
22
|
+
}
|
|
23
|
+
for (const key of Object.keys(NODE_FIELDS[node.type] ?? {})) {
|
|
24
|
+
if (keys.includes(key))
|
|
25
|
+
continue;
|
|
26
|
+
const value = properties[key] ?? null;
|
|
27
|
+
exact.push(value);
|
|
28
|
+
similar.push(normalized(node.type, key) ? null : value);
|
|
29
|
+
}
|
|
30
|
+
const fingerprint = { exact: hash(exact), similar: hash(similar), nodes };
|
|
31
|
+
const { start, end, loc } = node;
|
|
32
|
+
const eligible = (isFunction(node) || isStatement(node) || node.type === 'SwitchCase') && node.type !== 'EmptyStatement';
|
|
33
|
+
if (eligible && nodes >= duplicationSettings.minNodes && loc && start != null && end != null) {
|
|
34
|
+
const endLine = loc.end.line - Number(loc.end.column === 0);
|
|
35
|
+
const codeCount = firstAtLeast(codeLines, endLine + 1) - firstAtLeast(codeLines, loc.start.line);
|
|
36
|
+
if (codeCount >= duplicationSettings.minCodeLines) {
|
|
37
|
+
candidates.push({ ...fingerprint, start, end, line: loc.start.line, endLine, codeLines: codeCount });
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return fingerprint;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function normalized(type, property) {
|
|
44
|
+
if ((type === 'Identifier' || type === 'JSXIdentifier') && property === 'name')
|
|
45
|
+
return true;
|
|
46
|
+
if (type === 'RegExpLiteral' && property === 'pattern')
|
|
47
|
+
return true;
|
|
48
|
+
return property === 'value' && (type.endsWith('Literal') || type === 'TemplateElement');
|
|
49
|
+
}
|
|
50
|
+
function hash(parts) {
|
|
51
|
+
return createHash('sha256').update(JSON.stringify(parts)).digest('base64url');
|
|
52
|
+
}
|
|
53
|
+
function firstAtLeast(numbers, number) {
|
|
54
|
+
let left = 0;
|
|
55
|
+
let right = numbers.length;
|
|
56
|
+
while (left < right) {
|
|
57
|
+
const middle = Math.floor((left + right) / 2);
|
|
58
|
+
if (numbers[middle] < number)
|
|
59
|
+
left = middle + 1;
|
|
60
|
+
else
|
|
61
|
+
right = middle;
|
|
62
|
+
}
|
|
63
|
+
return left;
|
|
64
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export function classifyLines(source, ast, physical) {
|
|
2
|
+
const lines = source.split(/\r\n|[\n\r\u2028\u2029]/);
|
|
3
|
+
const masks = new Map();
|
|
4
|
+
for (const { loc } of [...(ast.program.interpreter ? [ast.program.interpreter] : []), ...ast.comments ?? []]) {
|
|
5
|
+
if (!loc)
|
|
6
|
+
continue;
|
|
7
|
+
for (let line = loc.start.line; line <= loc.end.line && line <= physical; line++) {
|
|
8
|
+
const start = line === loc.start.line ? loc.start.column : 0;
|
|
9
|
+
const end = line === loc.end.line ? loc.end.column : lines[line - 1].length;
|
|
10
|
+
const mask = { start, end };
|
|
11
|
+
const intervals = masks.get(line);
|
|
12
|
+
if (intervals)
|
|
13
|
+
intervals.push(mask);
|
|
14
|
+
else
|
|
15
|
+
masks.set(line, [mask]);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
const code = [];
|
|
19
|
+
let comments = 0;
|
|
20
|
+
for (let line = 1; line <= physical; line++) {
|
|
21
|
+
const text = lines[line - 1];
|
|
22
|
+
const intervals = masks.get(line) ?? [];
|
|
23
|
+
let offset = 0;
|
|
24
|
+
let hasCode = false;
|
|
25
|
+
for (const { start, end } of intervals) {
|
|
26
|
+
hasCode ||= text.slice(offset, start).trim() !== '';
|
|
27
|
+
offset = end;
|
|
28
|
+
}
|
|
29
|
+
if (hasCode || text.slice(offset).trim() !== '')
|
|
30
|
+
code.push(line);
|
|
31
|
+
else if (intervals.length > 0)
|
|
32
|
+
comments++;
|
|
33
|
+
}
|
|
34
|
+
return { code, comments };
|
|
35
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Function as BabelFunction, Node } from '@babel/types';
|
|
2
|
+
import type { FunctionComplexity } from '../../core/model/code-structure.js';
|
|
3
|
+
export declare function functionFacts(ast: Node): {
|
|
4
|
+
functions: number;
|
|
5
|
+
cyclomaticFunctions: FunctionComplexity[];
|
|
6
|
+
};
|
|
7
|
+
export declare function children(node: Node): Iterable<Node>;
|
|
8
|
+
export declare function functionName(node: BabelFunction): string;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { VISITOR_KEYS, isFunction, isIdentifier, isStringLiteral } from '@babel/types';
|
|
2
|
+
const decisionNodes = new Set(['IfStatement', 'ConditionalExpression', 'ForStatement', 'ForInStatement', 'ForOfStatement',
|
|
3
|
+
'WhileStatement', 'DoWhileStatement', 'CatchClause', 'LogicalExpression']);
|
|
4
|
+
export function functionFacts(ast) {
|
|
5
|
+
let functions = 0;
|
|
6
|
+
walk(ast, node => { if (isFunction(node) && node.body)
|
|
7
|
+
functions++; });
|
|
8
|
+
const cyclomaticFunctions = [];
|
|
9
|
+
visit(ast, undefined);
|
|
10
|
+
return { functions, cyclomaticFunctions };
|
|
11
|
+
function visit(node, frame) {
|
|
12
|
+
if (isFunction(node) && node.body) {
|
|
13
|
+
const own = { value: 1 };
|
|
14
|
+
// The body defines this method's decision scope. Nested functions get separate frames.
|
|
15
|
+
visit(node.body, own);
|
|
16
|
+
for (const parameter of node.params)
|
|
17
|
+
visit(parameter, undefined);
|
|
18
|
+
const start = node.loc?.start;
|
|
19
|
+
cyclomaticFunctions.push({ line: start?.line,
|
|
20
|
+
column: start === undefined ? undefined : start.column + 1, name: functionName(node), value: own.value });
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (frame && (decisionNodes.has(node.type) || (node.type === 'SwitchCase' && node.test !== null)))
|
|
24
|
+
frame.value++;
|
|
25
|
+
for (const child of children(node))
|
|
26
|
+
visit(child, frame);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export function* children(node) {
|
|
30
|
+
const properties = node;
|
|
31
|
+
for (const key of VISITOR_KEYS[node.type] ?? []) {
|
|
32
|
+
const value = properties[key];
|
|
33
|
+
if (Array.isArray(value)) {
|
|
34
|
+
for (const child of value)
|
|
35
|
+
if (child)
|
|
36
|
+
yield child;
|
|
37
|
+
}
|
|
38
|
+
else if (value && 'type' in value) {
|
|
39
|
+
yield value;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function walk(node, visit) {
|
|
44
|
+
visit(node);
|
|
45
|
+
for (const child of children(node))
|
|
46
|
+
walk(child, visit);
|
|
47
|
+
}
|
|
48
|
+
export function functionName(node) {
|
|
49
|
+
if ('id' in node && node.id && isIdentifier(node.id))
|
|
50
|
+
return node.id.name;
|
|
51
|
+
if ('key' in node) {
|
|
52
|
+
if (isIdentifier(node.key))
|
|
53
|
+
return node.key.name;
|
|
54
|
+
if (isStringLiteral(node.key))
|
|
55
|
+
return node.key.value;
|
|
56
|
+
if (node.key.type === 'PrivateName')
|
|
57
|
+
return `#${node.key.id.name}`;
|
|
58
|
+
}
|
|
59
|
+
return '(anonymous)';
|
|
60
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Node } from '@babel/types';
|
|
2
|
+
import type { FunctionComplexity, StructuralObservation } from '../../core/model/code-structure.js';
|
|
3
|
+
export declare function structuralObservations(ast: Node): {
|
|
4
|
+
observations: StructuralObservation[];
|
|
5
|
+
cognitiveFunctions: FunctionComplexity[];
|
|
6
|
+
cognitiveComplexity: number;
|
|
7
|
+
classes: number;
|
|
8
|
+
};
|