repometry 0.8.1 → 0.9.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/README.md +2 -1
- package/package.json +1 -1
- package/target/main/resources/languages/go/parser.go +25 -5
- package/target/main/resources/languages/python/parser.py +12 -1
- package/target/main/resources/report/json/config-v1.schema.json +0 -1
- package/target/main/resources/report/json/report-v2.schema.json +12 -0
- package/target/main/ts/api/gh/github.d.ts +3 -1
- package/target/main/ts/api/gh/github.js +39 -54
- package/target/main/ts/api/git/changes.d.ts +6 -1
- package/target/main/ts/api/git/changes.js +19 -10
- package/target/main/ts/api/git/line-changes.d.ts +6 -0
- package/target/main/ts/api/git/line-changes.js +57 -0
- package/target/main/ts/cli/config.d.ts +2 -1
- package/target/main/ts/cli/config.js +12 -5
- package/target/main/ts/cli/main.js +33 -38
- package/target/main/ts/compose/review.d.ts +24 -13
- package/target/main/ts/compose/review.js +44 -32
- package/target/main/ts/core/model/changes.d.ts +29 -0
- package/target/main/ts/core/model/code-structure.d.ts +2 -0
- package/target/main/ts/core/model/code-structure.js +8 -2
- package/target/main/ts/core/model/finding.d.ts +6 -0
- package/target/main/ts/core/model/finding.js +23 -8
- package/target/main/ts/core/model/pull-review.d.ts +52 -30
- package/target/main/ts/core/model/pull-review.js +4 -1
- package/target/main/ts/core/usecases/review-effect.d.ts +18 -0
- package/target/main/ts/core/usecases/review-effect.js +37 -0
- package/target/main/ts/core/usecases/review-match.d.ts +14 -0
- package/target/main/ts/core/usecases/review-match.js +72 -0
- package/target/main/ts/core/usecases/review-position.d.ts +14 -0
- package/target/main/ts/core/usecases/review-position.js +53 -0
- package/target/main/ts/core/usecases/review-scope.d.ts +21 -0
- package/target/main/ts/core/usecases/review-scope.js +41 -0
- package/target/main/ts/core/usecases/review.d.ts +32 -21
- package/target/main/ts/core/usecases/review.js +89 -114
- package/target/main/ts/index.d.ts +4 -3
- package/target/main/ts/index.js +1 -1
- package/target/main/ts/plugins/duplication/duplication.js +1 -1
- package/target/main/ts/plugins/language-js/babel-provider.js +4 -2
- package/target/main/ts/plugins/language-js/function-facts.d.ts +3 -3
- package/target/main/ts/plugins/language-js/function-facts.js +3 -15
- package/target/main/ts/plugins/language-js/qualified-names.d.ts +11 -0
- package/target/main/ts/plugins/language-js/qualified-names.js +63 -0
- package/target/main/ts/plugins/language-js/structural-observations.d.ts +2 -1
- package/target/main/ts/plugins/language-js/structural-observations.js +18 -14
- package/target/main/ts/plugins/maintainability/structural-smells.js +3 -2
- package/target/main/ts/report/html/app/viewer-chart-bars.js +9 -5
- package/target/main/ts/report/json/store/pull-review-store.d.ts +0 -1
- package/target/main/ts/report/json/store/pull-review-store.js +2 -20
- package/target/main/ts/report/json/wire/pull-review-model.d.ts +49 -2
- package/target/main/ts/report/json/wire/pull-review-model.js +2 -0
- package/target/main/ts/report/markdown/pull-review.d.ts +5 -0
- package/target/main/ts/report/markdown/pull-review.js +122 -0
|
@@ -1,31 +1,42 @@
|
|
|
1
|
+
import type { LineChanges } from '../model/changes.js';
|
|
1
2
|
import type { PullReview } from '../model/pull-review.js';
|
|
2
3
|
import type { Report } from '../model/report.js';
|
|
4
|
+
import type { SourceLines } from './review-effect.js';
|
|
3
5
|
import type { DiffCoverageReport } from './diff-coverage.js';
|
|
4
6
|
export interface ReviewOptions {
|
|
5
7
|
readonly number?: number;
|
|
6
|
-
readonly minimumLines?: number;
|
|
7
8
|
readonly minimumPercent?: number;
|
|
8
|
-
readonly
|
|
9
|
+
readonly minimumLines?: number;
|
|
9
10
|
readonly maxNewIssues?: number;
|
|
10
11
|
}
|
|
11
|
-
export interface
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
12
|
+
export interface ReviewInputs {
|
|
13
|
+
/** The base as requested, such as `origin/main`. */
|
|
14
|
+
readonly requestedBase: string;
|
|
15
|
+
/** The inspection of the merge base. */
|
|
16
|
+
readonly base: Report;
|
|
17
|
+
readonly head: Report;
|
|
18
|
+
readonly changes: LineChanges;
|
|
19
|
+
readonly sources: {
|
|
20
|
+
readonly base: SourceLines;
|
|
21
|
+
readonly head: SourceLines;
|
|
22
|
+
};
|
|
23
|
+
readonly coverage: {
|
|
24
|
+
readonly report: DiffCoverageReport;
|
|
25
|
+
readonly uncovered: readonly {
|
|
26
|
+
readonly path: string;
|
|
27
|
+
readonly lines: readonly number[];
|
|
28
|
+
}[];
|
|
29
|
+
} | null;
|
|
30
|
+
/** The change edits Repometry's settings. */
|
|
31
|
+
readonly settingsChanged: boolean;
|
|
23
32
|
}
|
|
24
|
-
export declare
|
|
25
|
-
|
|
26
|
-
readonly
|
|
27
|
-
readonly appliedConfig?: DiffCoverageReport['appliedConfig'];
|
|
33
|
+
export declare const coverageDefaults: {
|
|
34
|
+
readonly minimumPercent: 80;
|
|
35
|
+
readonly minimumLines: 20;
|
|
28
36
|
};
|
|
29
|
-
export declare function projectReview(
|
|
30
|
-
|
|
31
|
-
export {};
|
|
37
|
+
export declare function projectReview(options: ReviewOptions, inputs: ReviewInputs): PullReview;
|
|
38
|
+
/** Throws for options the review can't apply. */
|
|
39
|
+
export declare function checkOptions({ number, maxNewIssues, minimumPercent }: ReviewOptions, coverage: boolean): void;
|
|
40
|
+
/** The verdict and its reason, in one line. */
|
|
41
|
+
export declare function reviewReason({ verdict, unmeasured, issueGate, coverage }: PullReview): string;
|
|
42
|
+
export declare function reviewExitCode(review: PullReview): 0 | 2 | 3;
|
|
@@ -1,129 +1,104 @@
|
|
|
1
|
-
import { isDeepStrictEqual } from 'node:util';
|
|
2
|
-
import { isRecord } from '../engine/plugin.js';
|
|
3
|
-
import { isSensitiveFinding } from '../model/finding.js';
|
|
4
1
|
import { pullReviewSchema } from '../model/pull-review.js';
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
2
|
+
import { classifyIssues, debtEffect } from './review-effect.js';
|
|
3
|
+
export const coverageDefaults = { minimumPercent: 80, minimumLines: 20 };
|
|
4
|
+
const ignoredOperations = new Set(['lcov/import', 'git/churn', 'source/claims']);
|
|
5
|
+
const settingsNote = 'This change edits Repometry settings, which can hide issues from this check.';
|
|
6
|
+
export function projectReview(options, inputs) {
|
|
7
|
+
checkOptions(options, inputs.coverage !== null);
|
|
8
|
+
const { number, maxNewIssues = 0 } = options;
|
|
9
|
+
const issues = classifyIssues(inputs.base.findings ?? [], inputs.head.findings ?? [], inputs.changes, inputs.sources);
|
|
10
|
+
const count = issues.filter(issue => issue.effect === 'new' || issue.effect === 'worsened').length;
|
|
11
|
+
const issueGate = { maximum: maxNewIssues, count, status: count > maxNewIssues ? 'failed' : 'passed' };
|
|
12
|
+
const coverage = changedCoverage(options, inputs.coverage);
|
|
13
|
+
const { unmeasured, notes } = diagnose(inputs);
|
|
14
|
+
const failed = issueGate.status === 'failed' || coverage.gate?.status === 'failed';
|
|
15
|
+
return { schema: pullReviewSchema, number: number ?? null, generatedAt: new Date().toISOString(),
|
|
16
|
+
base: inputs.requestedBase, mergeBase: inputs.base.subject.id, head: inputs.head.subject.id, changedFiles: inputs.changes.files.length,
|
|
17
|
+
verdict: unmeasured.length ? 'unmeasured' : failed ? 'failed' : 'passed',
|
|
18
|
+
issues, debt: debtEffect(issues), coverage, issueGate, unmeasured,
|
|
19
|
+
notes: inputs.settingsChanged ? [...notes, { message: settingsNote }] : notes };
|
|
23
20
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
if (
|
|
21
|
+
/** Throws for options the review can't apply. */
|
|
22
|
+
export function checkOptions({ number, maxNewIssues = 0, minimumPercent }, coverage) {
|
|
23
|
+
if (number !== undefined && (!Number.isSafeInteger(number) || number < 1))
|
|
27
24
|
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)) {
|
|
25
|
+
if (!Number.isSafeInteger(maxNewIssues) || maxNewIssues < 0)
|
|
32
26
|
throw new Error('Maximum new issues must be a non-negative integer');
|
|
27
|
+
if (minimumPercent !== undefined && !coverage)
|
|
28
|
+
throw new Error('Minimum changed-line coverage requires --import lcov=PATH or go-cover=PATH');
|
|
29
|
+
}
|
|
30
|
+
/** The verdict and its reason, in one line. */
|
|
31
|
+
export function reviewReason({ verdict, unmeasured, issueGate, coverage }) {
|
|
32
|
+
if (verdict === 'unmeasured') {
|
|
33
|
+
const [first] = unmeasured;
|
|
34
|
+
const more = unmeasured.length > 1 ? `, and ${unmeasured.length - 1} more` : '';
|
|
35
|
+
return `Review not measured: ${first.operation}${first.path === undefined ? '' : ` on ${first.path}`}: ${first.message}${more}.`;
|
|
33
36
|
}
|
|
34
|
-
|
|
35
|
-
|
|
37
|
+
const reasons = [];
|
|
38
|
+
if (issueGate.status === 'failed') {
|
|
39
|
+
reasons.push(`${issueGate.count} new or worsened ${issueGate.count === 1 ? 'issue' : 'issues'} on the changed lines, ${issueGate.maximum} allowed`);
|
|
36
40
|
}
|
|
37
|
-
if (
|
|
38
|
-
|
|
41
|
+
if (coverage.gate?.status === 'failed') {
|
|
42
|
+
reasons.push(`changed-line coverage ${coverage.percent.toFixed(2)}% is under the ${coverage.gate.minimumPercent}% minimum`);
|
|
39
43
|
}
|
|
40
|
-
|
|
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
|
-
};
|
|
44
|
+
return reasons.length ? `Review failed: ${reasons.join('; ')}.` : 'Review passed.';
|
|
58
45
|
}
|
|
59
46
|
export function reviewExitCode(review) {
|
|
60
|
-
|
|
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
|
-
};
|
|
47
|
+
return review.verdict === 'unmeasured' ? 2 : review.verdict === 'failed' ? 3 : 0;
|
|
80
48
|
}
|
|
81
|
-
function
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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)));
|
|
49
|
+
function changedCoverage(options, input) {
|
|
50
|
+
if (input === null)
|
|
51
|
+
return { imported: false, covered: 0, total: 0, percent: null, uncovered: [], unmapped: [], gate: null };
|
|
52
|
+
const { minimumPercent = coverageDefaults.minimumPercent, minimumLines = coverageDefaults.minimumLines } = options;
|
|
53
|
+
if (!Number.isFinite(minimumPercent) || minimumPercent < 0 || minimumPercent > 100) {
|
|
54
|
+
throw new Error('Minimum changed-line coverage must be a percentage from 0 to 100');
|
|
55
|
+
}
|
|
56
|
+
if (!Number.isSafeInteger(minimumLines) || minimumLines < 0)
|
|
57
|
+
throw new Error('Minimum changed lines must be a non-negative integer');
|
|
58
|
+
const { covered, total, percent, gaps } = input.report;
|
|
59
|
+
const status = total < minimumLines || percent === null ? 'not-applicable'
|
|
60
|
+
: percent + Number.EPSILON * 100 >= minimumPercent ? 'passed' : 'failed';
|
|
61
|
+
return { imported: true, covered, total, percent, uncovered: input.uncovered.flatMap(({ path, lines }) => ranges(path, lines)),
|
|
62
|
+
unmapped: gaps, gate: { minimumPercent, minimumLines, status } };
|
|
106
63
|
}
|
|
107
|
-
function
|
|
108
|
-
const
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
64
|
+
function ranges(path, lines) {
|
|
65
|
+
const merged = [];
|
|
66
|
+
for (const line of [...lines].sort((left, right) => left - right)) {
|
|
67
|
+
const last = merged.at(-1);
|
|
68
|
+
if (last && line <= last.last + 1)
|
|
69
|
+
last.last = Math.max(last.last, line);
|
|
70
|
+
else
|
|
71
|
+
merged.push({ path, first: line, last: line });
|
|
113
72
|
}
|
|
114
|
-
return
|
|
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
|
-
});
|
|
73
|
+
return merged;
|
|
122
74
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
const
|
|
126
|
-
const
|
|
127
|
-
|
|
75
|
+
/** Failures on the head only leave the check unmeasured. Failures on both sides, and skipped large files, are notes. */
|
|
76
|
+
function diagnose({ base, head, changes }) {
|
|
77
|
+
const changed = new Set(changes.files.flatMap(file => file.path === null ? [] : [file.path]));
|
|
78
|
+
const failing = (diagnostic) => diagnostic.state === 'failed' || diagnostic.state === 'blocked';
|
|
79
|
+
const failedOnBase = new Set(base.diagnostics.filter(failing).map(({ operation }) => operation));
|
|
80
|
+
const unmeasured = [];
|
|
81
|
+
const notes = [];
|
|
82
|
+
const brokenPaths = new Set(head.diagnostics.filter(item => item.state === 'failed' && item.path !== undefined).map(item => item.path));
|
|
83
|
+
// An operation blocked by a failure on the same file adds nothing to that failure.
|
|
84
|
+
const reported = head.diagnostics.filter(item => item.state !== 'blocked' || item.path === undefined || !brokenPaths.has(item.path));
|
|
85
|
+
for (const diagnostic of reported) {
|
|
86
|
+
const reason = { operation: diagnostic.operation, ...diagnostic.path === undefined ? {} : { path: diagnostic.path },
|
|
87
|
+
message: diagnostic.message };
|
|
88
|
+
if (diagnostic.operation === 'source/claims') {
|
|
89
|
+
if (diagnostic.path !== undefined && changed.has(diagnostic.path))
|
|
90
|
+
notes.push(reason);
|
|
91
|
+
}
|
|
92
|
+
else if (ignoredOperations.has(diagnostic.operation) || !failing(diagnostic)) {
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
else if (failedOnBase.has(diagnostic.operation) && (diagnostic.path === undefined || !changed.has(diagnostic.path))) {
|
|
96
|
+
notes.push(reason);
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
unmeasured.push(reason);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return { unmeasured, notes };
|
|
128
103
|
}
|
|
129
104
|
//# sourceMappingURL=review.js.map
|
|
@@ -9,9 +9,9 @@ export type { AnalysisExecutor, AnalysisInput, InspectionPorts, InspectRequest }
|
|
|
9
9
|
export { diffCoverageSchema, measureDiffCoverage } from './compose/diff-coverage.js';
|
|
10
10
|
export type { DiffCoverageReport, DiffCoverageRequest } from './compose/diff-coverage.js';
|
|
11
11
|
export type { DiffCoveragePorts, DiffCoverageSource } from './core/usecases/diff-coverage.js';
|
|
12
|
-
export {
|
|
12
|
+
export { reviewExitCode } from './compose/review.js';
|
|
13
13
|
export type { ReviewRequest } from './compose/review.js';
|
|
14
|
-
export type {
|
|
14
|
+
export type { ReviewOptions } from './core/usecases/review.js';
|
|
15
15
|
export { compareInspections, comparisonSchema } from './core/usecases/compare.js';
|
|
16
16
|
export { compareInspections as compare } from './core/usecases/compare.js';
|
|
17
17
|
export { measureDiffCoverage as diffCoverage } from './compose/diff-coverage.js';
|
|
@@ -32,5 +32,6 @@ export { coverageGrade, qualitySummary } from './core/model/quality-summary.js';
|
|
|
32
32
|
export type { Grade, QualitySummary } from './core/model/quality-summary.js';
|
|
33
33
|
export { pullReviewSchema } from './core/model/pull-review.js';
|
|
34
34
|
export { pullReviewsSchema } from './report/json/wire/pull-review-model.js';
|
|
35
|
-
export type { PullReview,
|
|
35
|
+
export type { IssueEffect, PullReview, ReviewedIssue, ReviewReason } from './core/model/pull-review.js';
|
|
36
|
+
export type { PullReviews } from './report/json/wire/pull-review-model.js';
|
|
36
37
|
export type { Subject } from './core/model/snapshot.js';
|
package/target/main/ts/index.js
CHANGED
|
@@ -3,7 +3,7 @@ export { plan } from './core/engine/plan.js';
|
|
|
3
3
|
export { execute } from './core/engine/execute.js';
|
|
4
4
|
export { analyzeSnapshot, inspect } from './compose/inspect.js';
|
|
5
5
|
export { diffCoverageSchema, measureDiffCoverage } from './compose/diff-coverage.js';
|
|
6
|
-
export {
|
|
6
|
+
export { reviewExitCode } from './compose/review.js';
|
|
7
7
|
export { compareInspections, comparisonSchema } from './core/usecases/compare.js';
|
|
8
8
|
export { compareInspections as compare } from './core/usecases/compare.js';
|
|
9
9
|
export { measureDiffCoverage as diffCoverage } from './compose/diff-coverage.js';
|
|
@@ -61,7 +61,7 @@ function detectClones(sources) {
|
|
|
61
61
|
const effortMinutes = fresh.slice(1).reduce((total, occurrence) => total + occurrence.codeLines, 0) * duplicationSettings.minutesPerRepeatedLine;
|
|
62
62
|
findings.push({ tool: 'repometry', rule, category: 'duplication', level: 'medium',
|
|
63
63
|
message: `${identical ? 'Identical' : 'Similar'} code in ${fresh.length} locations`, ...location(first),
|
|
64
|
-
related: fresh.slice(1).map(location), effortMinutes });
|
|
64
|
+
related: fresh.slice(1).map(location), effortMinutes, value: fresh.length, anchor: first.similar });
|
|
65
65
|
}
|
|
66
66
|
return {
|
|
67
67
|
findings,
|
|
@@ -8,6 +8,7 @@ import { physicalLines, sourceFile, utf8 } from '../../core/model/snapshot.js';
|
|
|
8
8
|
import { cloneCandidates } from './clone-candidates.js';
|
|
9
9
|
import { classifyLines } from './code-lines.js';
|
|
10
10
|
import { functionFacts } from './function-facts.js';
|
|
11
|
+
import { qualifiedNames } from './qualified-names.js';
|
|
11
12
|
import { structuralObservations } from './structural-observations.js';
|
|
12
13
|
import { jsTsExtensions } from './extensions.js';
|
|
13
14
|
const require = createRequire(import.meta.url);
|
|
@@ -43,8 +44,9 @@ export const babelProvider = {
|
|
|
43
44
|
const ast = parse(source, { ...parserDefaults, sourceType, plugins });
|
|
44
45
|
const lines = physicalLines(source);
|
|
45
46
|
const { code, comments } = classifyLines(source, ast, lines);
|
|
46
|
-
|
|
47
|
-
|
|
47
|
+
const names = qualifiedNames(ast);
|
|
48
|
+
return { path: file.path, lines, commentLines: comments, ...functionFacts(ast, names),
|
|
49
|
+
...structuralObservations(ast, names), clones: cloneCandidates(file.path, ast, code) };
|
|
48
50
|
},
|
|
49
51
|
}],
|
|
50
52
|
};
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Node } from '@babel/types';
|
|
2
2
|
import type { FunctionComplexity } from '../../core/model/code-structure.js';
|
|
3
|
-
|
|
3
|
+
import type { FunctionNaming } from './qualified-names.js';
|
|
4
|
+
export declare function functionFacts(ast: Node, names: ReadonlyMap<Node, FunctionNaming>): {
|
|
4
5
|
functions: number;
|
|
5
6
|
cyclomaticFunctions: FunctionComplexity[];
|
|
6
7
|
functionBodies: [number, number][];
|
|
7
8
|
};
|
|
8
9
|
export declare function children(node: Node): Iterable<Node>;
|
|
9
|
-
export declare function functionName(node: BabelFunction): string | undefined;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { VISITOR_KEYS, isBlockStatement, isFunction
|
|
1
|
+
import { VISITOR_KEYS, isBlockStatement, isFunction } from '@babel/types';
|
|
2
2
|
const decisionNodes = new Set(['IfStatement', 'ConditionalExpression', 'ForStatement', 'ForInStatement', 'ForOfStatement',
|
|
3
3
|
'WhileStatement', 'DoWhileStatement', 'CatchClause', 'LogicalExpression']);
|
|
4
|
-
export function functionFacts(ast) {
|
|
4
|
+
export function functionFacts(ast, names) {
|
|
5
5
|
let functions = 0;
|
|
6
6
|
const bodyRanges = [];
|
|
7
7
|
walk(ast, node => {
|
|
@@ -24,7 +24,7 @@ export function functionFacts(ast) {
|
|
|
24
24
|
visit(parameter, undefined);
|
|
25
25
|
const start = node.loc?.start;
|
|
26
26
|
cyclomaticFunctions.push({ line: start?.line,
|
|
27
|
-
column: start === undefined ? undefined : start.column + 1, name:
|
|
27
|
+
column: start === undefined ? undefined : start.column + 1, name: names.get(node)?.name, value: own.value });
|
|
28
28
|
return;
|
|
29
29
|
}
|
|
30
30
|
if (frame && (decisionNodes.has(node.type) || (node.type === 'SwitchCase' && node.test !== null)))
|
|
@@ -71,16 +71,4 @@ function walk(node, visit) {
|
|
|
71
71
|
for (const child of children(node))
|
|
72
72
|
walk(child, visit);
|
|
73
73
|
}
|
|
74
|
-
export function functionName(node) {
|
|
75
|
-
if ('id' in node && node.id && isIdentifier(node.id))
|
|
76
|
-
return node.id.name;
|
|
77
|
-
if ('key' in node) {
|
|
78
|
-
if (isIdentifier(node.key))
|
|
79
|
-
return node.key.name;
|
|
80
|
-
if (isStringLiteral(node.key))
|
|
81
|
-
return node.key.value;
|
|
82
|
-
if (node.key.type === 'PrivateName')
|
|
83
|
-
return `#${node.key.id.name}`;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
74
|
//# sourceMappingURL=function-facts.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Node } from '@babel/types';
|
|
2
|
+
/** A function's qualified name, absent when the function has none, and the anchor its observations carry. */
|
|
3
|
+
export interface FunctionNaming {
|
|
4
|
+
readonly name?: string;
|
|
5
|
+
readonly anchor: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Names every function by the classes, object literals and functions around it, joined with dots, as in
|
|
9
|
+
* `commands.review.run`. A function without a name of its own is `(anonymous)` in the anchors of it and its contents.
|
|
10
|
+
*/
|
|
11
|
+
export declare function qualifiedNames(ast: Node): ReadonlyMap<Node, FunctionNaming>;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { isClass, isFunction, isIdentifier, isMemberExpression, isObjectExpression, isStringLiteral } from '@babel/types';
|
|
2
|
+
import { children } from './function-facts.js';
|
|
3
|
+
/**
|
|
4
|
+
* Names every function by the classes, object literals and functions around it, joined with dots, as in
|
|
5
|
+
* `commands.review.run`. A function without a name of its own is `(anonymous)` in the anchors of it and its contents.
|
|
6
|
+
*/
|
|
7
|
+
export function qualifiedNames(ast) {
|
|
8
|
+
const names = new Map();
|
|
9
|
+
const scope = [];
|
|
10
|
+
visit(ast, undefined);
|
|
11
|
+
return names;
|
|
12
|
+
function visit(node, parent) {
|
|
13
|
+
const own = segment(node, parent);
|
|
14
|
+
const opens = own !== undefined || isFunction(node);
|
|
15
|
+
if (isFunction(node)) {
|
|
16
|
+
const anchor = [...scope, own ?? '(anonymous)'].join('.');
|
|
17
|
+
names.set(node, own === undefined ? { anchor } : { name: anchor, anchor });
|
|
18
|
+
}
|
|
19
|
+
if (opens)
|
|
20
|
+
scope.push(own ?? '(anonymous)');
|
|
21
|
+
for (const child of children(node))
|
|
22
|
+
visit(child, node);
|
|
23
|
+
if (opens)
|
|
24
|
+
scope.pop();
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function segment(node, parent) {
|
|
28
|
+
if (isFunction(node))
|
|
29
|
+
return ('id' in node && node.id ? node.id.name : 'key' in node ? keyName(node.key) : undefined)
|
|
30
|
+
?? binding(node, parent);
|
|
31
|
+
if (isClass(node))
|
|
32
|
+
return node.id?.name ?? binding(node, parent);
|
|
33
|
+
return isObjectExpression(node) ? binding(node, parent) : undefined;
|
|
34
|
+
}
|
|
35
|
+
/** The name a value takes from the declaration, property or assignment that holds it. */
|
|
36
|
+
function binding(node, parent) {
|
|
37
|
+
switch (parent?.type) {
|
|
38
|
+
case 'VariableDeclarator':
|
|
39
|
+
return parent.init === node && isIdentifier(parent.id) ? parent.id.name : undefined;
|
|
40
|
+
case 'ObjectProperty':
|
|
41
|
+
case 'ClassProperty':
|
|
42
|
+
case 'ClassPrivateProperty':
|
|
43
|
+
return parent.value === node ? keyName(parent.key) : undefined;
|
|
44
|
+
case 'AssignmentExpression': {
|
|
45
|
+
if (parent.right !== node)
|
|
46
|
+
return undefined;
|
|
47
|
+
const target = parent.left;
|
|
48
|
+
if (isIdentifier(target))
|
|
49
|
+
return target.name;
|
|
50
|
+
return isMemberExpression(target) && !target.computed && isIdentifier(target.property) ? target.property.name : undefined;
|
|
51
|
+
}
|
|
52
|
+
default:
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function keyName(key) {
|
|
57
|
+
if (isIdentifier(key))
|
|
58
|
+
return key.name;
|
|
59
|
+
if (isStringLiteral(key))
|
|
60
|
+
return key.value;
|
|
61
|
+
return key.type === 'PrivateName' ? `#${key.id.name}` : undefined;
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=qualified-names.js.map
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Node } from '@babel/types';
|
|
2
2
|
import type { FunctionComplexity, StructuralObservation } from '../../core/model/code-structure.js';
|
|
3
|
-
|
|
3
|
+
import type { FunctionNaming } from './qualified-names.js';
|
|
4
|
+
export declare function structuralObservations(ast: Node, names: ReadonlyMap<Node, FunctionNaming>): {
|
|
4
5
|
observations: StructuralObservation[];
|
|
5
6
|
cognitiveFunctions: FunctionComplexity[];
|
|
6
7
|
cognitiveComplexity: number;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { isClass, isFunction
|
|
2
|
-
import { children
|
|
1
|
+
import { isClass, isFunction } from '@babel/types';
|
|
2
|
+
import { children } from './function-facts.js';
|
|
3
3
|
const complexityBlocks = new Set([
|
|
4
4
|
'IfStatement', 'ConditionalExpression', 'ForStatement', 'ForInStatement', 'ForOfStatement',
|
|
5
5
|
'WhileStatement', 'DoWhileStatement', 'CatchClause', 'SwitchStatement',
|
|
6
6
|
]);
|
|
7
|
-
export function structuralObservations(ast) {
|
|
7
|
+
export function structuralObservations(ast, names) {
|
|
8
8
|
const observations = [];
|
|
9
9
|
const cognitiveFunctions = [];
|
|
10
10
|
let cognitive = 0;
|
|
@@ -20,21 +20,23 @@ export function structuralObservations(ast) {
|
|
|
20
20
|
if (isClass(node))
|
|
21
21
|
classes++;
|
|
22
22
|
if (isFunction(node) && node.body) {
|
|
23
|
-
const
|
|
23
|
+
const naming = names.get(node);
|
|
24
|
+
const own = { cognitive: 0, returns: 0, maxNesting: 0, anchor: naming?.anchor };
|
|
24
25
|
visit(node.body, own, 0, node);
|
|
25
26
|
for (const parameter of node.params)
|
|
26
27
|
visit(parameter, own, 0, node);
|
|
27
|
-
const name =
|
|
28
|
+
const name = naming?.name;
|
|
28
29
|
const start = node.loc?.start;
|
|
29
30
|
cognitiveFunctions.push({ line: start?.line,
|
|
30
31
|
column: start === undefined ? undefined : start.column + 1, name, value: own.cognitive });
|
|
31
32
|
const label = name ?? '(anonymous)';
|
|
32
33
|
cognitive += own.cognitive;
|
|
33
|
-
observe('function-parameters', node.params.length, `${label} · Parameters`, node);
|
|
34
|
-
observe('return-statements', own.returns, `${label} · Return statements`, node);
|
|
35
|
-
observe('function-complexity', own.cognitive, `${label} · Cognitive complexity`, node);
|
|
36
|
-
if (own.nestedRange !== undefined)
|
|
37
|
-
observe('nested-control-flow', own.maxNesting, `${label} · Control-flow depth`, own.nestedRange);
|
|
34
|
+
observe('function-parameters', node.params.length, `${label} · Parameters`, node, own.anchor);
|
|
35
|
+
observe('return-statements', own.returns, `${label} · Return statements`, node, own.anchor);
|
|
36
|
+
observe('function-complexity', own.cognitive, `${label} · Cognitive complexity`, node, own.anchor);
|
|
37
|
+
if (own.nestedRange !== undefined) {
|
|
38
|
+
observe('nested-control-flow', own.maxNesting, `${label} · Control-flow depth`, own.nestedRange, own.anchor);
|
|
39
|
+
}
|
|
38
40
|
return;
|
|
39
41
|
}
|
|
40
42
|
if (node.type === 'ReturnStatement')
|
|
@@ -44,8 +46,9 @@ export function structuralObservations(ast) {
|
|
|
44
46
|
if (node.type === 'LogicalExpression') {
|
|
45
47
|
if (parent?.type !== 'LogicalExpression' || parent.operator !== node.operator)
|
|
46
48
|
frame.cognitive++;
|
|
47
|
-
if (parent?.type !== 'LogicalExpression')
|
|
48
|
-
observe('boolean-logic', countLogicalOperators(node), 'Logical operators in one expression', node);
|
|
49
|
+
if (parent?.type !== 'LogicalExpression') {
|
|
50
|
+
observe('boolean-logic', countLogicalOperators(node), 'Logical operators in one expression', node, frame.anchor);
|
|
51
|
+
}
|
|
49
52
|
}
|
|
50
53
|
const block = complexityBlocks.has(node.type);
|
|
51
54
|
const chained = node.type === 'IfStatement' && parent?.type === 'IfStatement' && parent.alternate === node;
|
|
@@ -69,8 +72,9 @@ export function structuralObservations(ast) {
|
|
|
69
72
|
visit(child, frame, depth + Number(block || node.type === 'TryStatement'), node);
|
|
70
73
|
}
|
|
71
74
|
}
|
|
72
|
-
function observe(rule, value, label, location) {
|
|
73
|
-
observations.push({ rule, value, label, line: location?.loc?.start.line, endLine: location?.loc?.end.line
|
|
75
|
+
function observe(rule, value, label, location, anchor) {
|
|
76
|
+
observations.push({ rule, value, label, line: location?.loc?.start.line, endLine: location?.loc?.end.line,
|
|
77
|
+
...(anchor === undefined ? {} : { anchor }) });
|
|
74
78
|
}
|
|
75
79
|
}
|
|
76
80
|
function countLogicalOperators(node) {
|
|
@@ -3,13 +3,14 @@ export function structuralSmells(path, observations) {
|
|
|
3
3
|
const findings = [];
|
|
4
4
|
const functionLimit = structuralRules['function-complexity'].limit;
|
|
5
5
|
const functionExcess = observations.reduce((total, { rule, value }) => total + (rule === 'function-complexity' ? Math.max(0, value - functionLimit) : 0), 0);
|
|
6
|
-
for (const { rule, value, label, line, endLine } of observations) {
|
|
6
|
+
for (const { rule, value, label, line, endLine, anchor } of observations) {
|
|
7
7
|
const { limit, minutesPerExcess } = structuralRules[rule];
|
|
8
8
|
if (value <= limit)
|
|
9
9
|
continue;
|
|
10
10
|
const chargedExcess = rule === 'file-complexity' ? Math.max(0, value - limit - functionExcess) : value - limit;
|
|
11
11
|
findings.push({ tool: 'repometry', rule, category: 'structure', level: 'medium', path,
|
|
12
|
-
message: `${label} ${value} (limit ${limit})`, line, endLine, effortMinutes: chargedExcess * minutesPerExcess
|
|
12
|
+
message: `${label} ${value} (limit ${limit})`, line, endLine, effortMinutes: chargedExcess * minutesPerExcess,
|
|
13
|
+
value, limit, anchor: anchor ?? '(file)' });
|
|
13
14
|
}
|
|
14
15
|
return findings;
|
|
15
16
|
}
|
|
@@ -2,6 +2,11 @@ import { markBoundaries, measuredValues, pointLink } from './viewer-chart-points
|
|
|
2
2
|
import { yAxis } from './viewer-chart-scale.js';
|
|
3
3
|
import { centredText, line, setAttributes, svgNode } from './viewer-chart-svg.js';
|
|
4
4
|
import { placeX } from './viewer-chart-x.js';
|
|
5
|
+
/** One width for every bar, set by the two closest inspections: only a bar's height says how much changed. */
|
|
6
|
+
function barWidth(xs) {
|
|
7
|
+
const gaps = xs.slice(1).map((at, index) => at - xs[index]);
|
|
8
|
+
return Math.max(2, Math.min(28, gaps.length ? Math.min(...gaps) * .62 : 28));
|
|
9
|
+
}
|
|
5
10
|
/** The room beside a bar: the distance to the nearer neighbour. */
|
|
6
11
|
function room(x, count, index) {
|
|
7
12
|
const before = index > 0 ? x(index) - x(index - 1) : Infinity;
|
|
@@ -9,7 +14,7 @@ function room(x, count, index) {
|
|
|
9
14
|
return Math.min(before, after);
|
|
10
15
|
}
|
|
11
16
|
/** A bar from zero to the value, labelled where the label fits or the commit is the selected one. */
|
|
12
|
-
function bar({ points, x, y, format, upIsWorse }, index,
|
|
17
|
+
function bar({ points, x, y, format, upIsWorse, width }, index, space) {
|
|
13
18
|
const point = points[index];
|
|
14
19
|
const value = point.value;
|
|
15
20
|
const worse = upIsWorse ? value > 0 : value < 0;
|
|
@@ -22,16 +27,15 @@ function bar({ points, x, y, format, upIsWorse }, index, width, space) {
|
|
|
22
27
|
}
|
|
23
28
|
/** A point's marks: its bar, a flat mark for no change, or a tick on the zero line where nothing was measured. */
|
|
24
29
|
function barMarks(context, index) {
|
|
25
|
-
const { points, x, y } = context;
|
|
30
|
+
const { points, x, y, width } = context;
|
|
26
31
|
const value = points[index].value;
|
|
27
32
|
const space = room(x, points.length, index);
|
|
28
|
-
const width = Math.max(2, Math.min(28, Number.isFinite(space) ? space * .62 : 28));
|
|
29
33
|
const cx = x(index);
|
|
30
34
|
if (value === null)
|
|
31
35
|
return [line('tc-missing-mark', { x1: cx, x2: cx, y1: y(0) - 3.5, y2: y(0) + 3.5 })];
|
|
32
36
|
if (value === 0)
|
|
33
37
|
return [line('tc-zero-mark', { x1: cx - width / 2, x2: cx + width / 2, y1: y(0), y2: y(0) })];
|
|
34
|
-
return bar(context, index,
|
|
38
|
+
return bar(context, index, space);
|
|
35
39
|
}
|
|
36
40
|
export function barChart(svg, layout, points, options) {
|
|
37
41
|
svg.classList.add('tc-bars');
|
|
@@ -43,7 +47,7 @@ export function barChart(svg, layout, points, options) {
|
|
|
43
47
|
layout.ys = points.map(point => point.value === null ? null : y(point.value));
|
|
44
48
|
// In a long history the zero line already says "no change" and "not measured"; only bars that moved are drawn.
|
|
45
49
|
const dense = layout.xs.some((at, index) => index > 0 && at - layout.xs[index - 1] < 7);
|
|
46
|
-
const context = { ...options, points, x, y };
|
|
50
|
+
const context = { ...options, points, x, y, width: barWidth(layout.xs) };
|
|
47
51
|
for (const [index, point] of points.entries()) {
|
|
48
52
|
const link = pointLink(point, index);
|
|
49
53
|
const quiet = !point.current && !point.value;
|