repometry 0.0.0-stage → 0.2.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 +28 -2
- package/package.json +64 -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 +33 -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 +252 -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 +120 -0
- package/target/main/resources/viewer/projects.html +52 -0
- package/target/main/resources/viewer/styles.css +560 -0
- package/target/main/resources/viewer/viewer-pulls.css +64 -0
- package/target/main/ts/api/gh/github.d.ts +6 -0
- package/target/main/ts/api/gh/github.js +92 -0
- package/target/main/ts/api/git/blob-batch.d.ts +10 -0
- package/target/main/ts/api/git/blob-batch.js +106 -0
- package/target/main/ts/api/git/changes.d.ts +22 -0
- package/target/main/ts/api/git/changes.js +108 -0
- package/target/main/ts/api/git/commit-metadata.d.ts +3 -0
- package/target/main/ts/api/git/commit-metadata.js +53 -0
- package/target/main/ts/api/git/git-churn.d.ts +9 -0
- package/target/main/ts/api/git/git-churn.js +71 -0
- package/target/main/ts/api/git/git-reader.d.ts +16 -0
- package/target/main/ts/api/git/git-reader.js +74 -0
- package/target/main/ts/api/git/git.d.ts +21 -0
- package/target/main/ts/api/git/git.js +153 -0
- package/target/main/ts/api/git/inspection-source.d.ts +15 -0
- package/target/main/ts/api/git/inspection-source.js +31 -0
- package/target/main/ts/api/git/local-sources.d.ts +5 -0
- package/target/main/ts/api/git/local-sources.js +47 -0
- package/target/main/ts/api/git/local.d.ts +3 -0
- package/target/main/ts/api/git/local.js +36 -0
- package/target/main/ts/api/git/metabranch-worktree.d.ts +1 -0
- package/target/main/ts/api/git/metabranch-worktree.js +35 -0
- package/target/main/ts/api/git/source-cache.d.ts +3 -0
- package/target/main/ts/api/git/source-cache.js +57 -0
- package/target/main/ts/api/import/finding-import.d.ts +9 -0
- package/target/main/ts/api/import/finding-import.js +227 -0
- package/target/main/ts/api/import/lcov-files.d.ts +5 -0
- package/target/main/ts/api/import/lcov-files.js +14 -0
- package/target/main/ts/api/import/profile-file.d.ts +4 -0
- package/target/main/ts/api/import/profile-file.js +17 -0
- package/target/main/ts/api/worker/remote-executor.d.ts +2 -0
- package/target/main/ts/api/worker/remote-executor.js +46 -0
- package/target/main/ts/api/worker/worker-protocol.d.ts +7 -0
- package/target/main/ts/api/worker/worker-protocol.js +102 -0
- package/target/main/ts/api/worker/worker.d.ts +5 -0
- package/target/main/ts/api/worker/worker.js +86 -0
- package/target/main/ts/cli/arguments.d.ts +28 -0
- package/target/main/ts/cli/arguments.js +111 -0
- package/target/main/ts/cli/config.d.ts +13 -0
- package/target/main/ts/cli/config.js +58 -0
- package/target/main/ts/cli/imports.d.ts +7 -0
- package/target/main/ts/cli/imports.js +30 -0
- package/target/main/ts/cli/main.d.ts +2 -0
- package/target/main/ts/cli/main.js +379 -0
- package/target/main/ts/cli/reporting.d.ts +18 -0
- package/target/main/ts/cli/reporting.js +104 -0
- package/target/main/ts/cli/revisions.d.ts +6 -0
- package/target/main/ts/cli/revisions.js +16 -0
- package/target/main/ts/cli/store.d.ts +13 -0
- package/target/main/ts/cli/store.js +39 -0
- package/target/main/ts/compose/analyze-snapshot.d.ts +4 -0
- package/target/main/ts/compose/analyze-snapshot.js +164 -0
- package/target/main/ts/compose/attach-churn.d.ts +2 -0
- package/target/main/ts/compose/attach-churn.js +50 -0
- package/target/main/ts/compose/diff-coverage.d.ts +20 -0
- package/target/main/ts/compose/diff-coverage.js +57 -0
- package/target/main/ts/compose/inspect.d.ts +23 -0
- package/target/main/ts/compose/inspect.js +45 -0
- package/target/main/ts/compose/plugin-list.d.ts +6 -0
- package/target/main/ts/compose/plugin-list.js +10 -0
- package/target/main/ts/compose/prepare-metabranch.d.ts +9 -0
- package/target/main/ts/compose/prepare-metabranch.js +17 -0
- package/target/main/ts/compose/review.d.ts +16 -0
- package/target/main/ts/compose/review.js +39 -0
- package/target/main/ts/compose/sources.d.ts +3 -0
- package/target/main/ts/compose/sources.js +30 -0
- package/target/main/ts/core/engine/execute.d.ts +10 -0
- package/target/main/ts/core/engine/execute.js +50 -0
- package/target/main/ts/core/engine/manifest.d.ts +4 -0
- package/target/main/ts/core/engine/manifest.js +105 -0
- package/target/main/ts/core/engine/plan.d.ts +22 -0
- package/target/main/ts/core/engine/plan.js +107 -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 +86 -0
- package/target/main/ts/core/model/changes.d.ts +8 -0
- package/target/main/ts/core/model/changes.js +2 -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 +15 -0
- package/target/main/ts/core/model/code-structure.d.ts +30 -0
- package/target/main/ts/core/model/code-structure.js +32 -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 +36 -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 +33 -0
- package/target/main/ts/core/model/measurement.js +30 -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 +40 -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 +29 -0
- package/target/main/ts/core/model/snapshot.d.ts +15 -0
- package/target/main/ts/core/model/snapshot.js +51 -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 +2 -0
- package/target/main/ts/core/ports/source-provider.d.ts +14 -0
- package/target/main/ts/core/ports/source-provider.js +5 -0
- package/target/main/ts/core/profile/exclusions.d.ts +11 -0
- package/target/main/ts/core/profile/exclusions.js +58 -0
- package/target/main/ts/core/profile/maintainability.d.ts +31 -0
- package/target/main/ts/core/profile/maintainability.js +10 -0
- package/target/main/ts/core/profile/profile.d.ts +4 -0
- package/target/main/ts/core/profile/profile.js +11 -0
- package/target/main/ts/core/usecases/compare.d.ts +6 -0
- package/target/main/ts/core/usecases/compare.js +146 -0
- package/target/main/ts/core/usecases/diff-coverage-policy.d.ts +6 -0
- package/target/main/ts/core/usecases/diff-coverage-policy.js +11 -0
- package/target/main/ts/core/usecases/diff-coverage.d.ts +40 -0
- package/target/main/ts/core/usecases/diff-coverage.js +16 -0
- package/target/main/ts/core/usecases/inspect.d.ts +38 -0
- package/target/main/ts/core/usecases/inspect.js +42 -0
- package/target/main/ts/core/usecases/review.d.ts +31 -0
- package/target/main/ts/core/usecases/review.js +129 -0
- package/target/main/ts/index.d.ts +35 -0
- package/target/main/ts/index.js +18 -0
- package/target/main/ts/plugin.d.ts +14 -0
- package/target/main/ts/plugin.js +6 -0
- package/target/main/ts/plugins/coverage/coverage.d.ts +25 -0
- package/target/main/ts/plugins/coverage/coverage.js +199 -0
- package/target/main/ts/plugins/coverage/diff-coverage.d.ts +7 -0
- package/target/main/ts/plugins/coverage/diff-coverage.js +90 -0
- package/target/main/ts/plugins/duplication/duplication.d.ts +4 -0
- package/target/main/ts/plugins/duplication/duplication.js +94 -0
- package/target/main/ts/plugins/language-js/babel-provider.d.ts +10 -0
- package/target/main/ts/plugins/language-js/babel-provider.js +47 -0
- package/target/main/ts/plugins/language-js/clone-candidates.d.ts +3 -0
- package/target/main/ts/plugins/language-js/clone-candidates.js +65 -0
- package/target/main/ts/plugins/language-js/code-lines.d.ts +6 -0
- package/target/main/ts/plugins/language-js/code-lines.js +36 -0
- package/target/main/ts/plugins/language-js/function-facts.d.ts +8 -0
- package/target/main/ts/plugins/language-js/function-facts.js +61 -0
- package/target/main/ts/plugins/language-js/structural-observations.d.ts +8 -0
- package/target/main/ts/plugins/language-js/structural-observations.js +80 -0
- package/target/main/ts/plugins/maintainability/maintainability.d.ts +9 -0
- package/target/main/ts/plugins/maintainability/maintainability.js +43 -0
- package/target/main/ts/plugins/maintainability/structural-smells.d.ts +3 -0
- package/target/main/ts/plugins/maintainability/structural-smells.js +16 -0
- package/target/main/ts/plugins/source/forges.d.ts +4 -0
- package/target/main/ts/plugins/source/forges.js +80 -0
- package/target/main/ts/plugins/structure/structure.d.ts +2 -0
- package/target/main/ts/plugins/structure/structure.js +38 -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 +2104 -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 +70 -0
- package/target/main/ts/report/html/app/viewer-metric-registry.js +224 -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 +73 -0
- package/target/main/ts/report/html/app/viewer-trends.js +423 -0
- package/target/main/ts/report/html/app/viewer.d.ts +1 -0
- package/target/main/ts/report/html/app/viewer.js +735 -0
- package/target/main/ts/report/html/site/badges.d.ts +14 -0
- package/target/main/ts/report/html/site/badges.js +40 -0
- package/target/main/ts/report/html/site/export-site.d.ts +22 -0
- package/target/main/ts/report/html/site/export-site.js +150 -0
- package/target/main/ts/report/html/site/exported-server.d.ts +6 -0
- package/target/main/ts/report/html/site/exported-server.js +71 -0
- package/target/main/ts/report/html/site/local-server.d.ts +7 -0
- package/target/main/ts/report/html/site/local-server.js +65 -0
- package/target/main/ts/report/html/site/projects.d.ts +3 -0
- package/target/main/ts/report/html/site/projects.js +80 -0
- package/target/main/ts/report/html/site/server.d.ts +4 -0
- package/target/main/ts/report/html/site/server.js +65 -0
- package/target/main/ts/report/html/site/source-route.d.ts +6 -0
- package/target/main/ts/report/html/site/source-route.js +23 -0
- package/target/main/ts/report/json/projections/commit-summary.d.ts +12 -0
- package/target/main/ts/report/json/projections/commit-summary.js +124 -0
- package/target/main/ts/report/json/projections/components.d.ts +4 -0
- package/target/main/ts/report/json/projections/components.js +30 -0
- package/target/main/ts/report/json/projections/file-metrics.d.ts +5 -0
- package/target/main/ts/report/json/projections/file-metrics.js +244 -0
- package/target/main/ts/report/json/projections/inspection-context.d.ts +6 -0
- package/target/main/ts/report/json/projections/inspection-context.js +93 -0
- package/target/main/ts/report/json/projections/inspection-details.d.ts +11 -0
- package/target/main/ts/report/json/projections/inspection-details.js +70 -0
- package/target/main/ts/report/json/projections/metric-values.d.ts +7 -0
- package/target/main/ts/report/json/projections/metric-values.js +58 -0
- package/target/main/ts/report/json/projections/public-history.d.ts +4 -0
- package/target/main/ts/report/json/projections/public-history.js +17 -0
- package/target/main/ts/report/json/store/components.d.ts +2 -0
- package/target/main/ts/report/json/store/components.js +32 -0
- package/target/main/ts/report/json/store/local-store.d.ts +13 -0
- package/target/main/ts/report/json/store/local-store.js +180 -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 +52 -0
- package/target/main/ts/report/json/validation/metabranch.d.ts +4 -0
- package/target/main/ts/report/json/validation/metabranch.js +8 -0
- package/target/main/ts/report/json/validation/report.d.ts +15 -0
- package/target/main/ts/report/json/validation/report.js +65 -0
- package/target/main/ts/report/json/validation/summary-validation.d.ts +4 -0
- package/target/main/ts/report/json/validation/summary-validation.js +28 -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 +6 -0
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
const whole = new Intl.NumberFormat();
|
|
2
|
+
const percentage = new Intl.NumberFormat(undefined, { style: 'percent', maximumFractionDigits: 1 });
|
|
3
|
+
const points = new Intl.NumberFormat(undefined, { signDisplay: 'exceptZero', maximumFractionDigits: 1 });
|
|
4
|
+
const date = new Intl.DateTimeFormat(undefined, { dateStyle: 'medium', timeStyle: 'short' });
|
|
5
|
+
const coverageReasons = {
|
|
6
|
+
'reports-unavailable': 'Save inspections for the base and head commits to compare total coverage.',
|
|
7
|
+
'observations-unavailable': 'The saved inspections lack complete line coverage data.',
|
|
8
|
+
'source-scope-unknown': 'The inspected source scope is unavailable for one of the revisions.',
|
|
9
|
+
'source-scope-changed': 'The base and head used different source scopes.',
|
|
10
|
+
'method-unknown': 'The coverage method is unavailable for one of the revisions.',
|
|
11
|
+
'method-changed': 'The base and head used different coverage methods.',
|
|
12
|
+
'head-coverage-changed': 'The review used a different LCOV input than the saved head inspection.',
|
|
13
|
+
};
|
|
14
|
+
const findingReasons = {
|
|
15
|
+
'reports-unavailable': 'Save inspections for the base and head commits to compare findings.',
|
|
16
|
+
'findings-unavailable': 'One or both revisions lack findings data.',
|
|
17
|
+
'partial-inspection': 'One or both revisions have incomplete analysis.',
|
|
18
|
+
'analysis-changed': 'The base and head used different analysis settings.',
|
|
19
|
+
};
|
|
20
|
+
export function renderPulls(container, records) {
|
|
21
|
+
const heading = node('h2', 'Pull requests');
|
|
22
|
+
// The page title stands outside the card, as on every other page; the review picker shares its row.
|
|
23
|
+
const toolbar = node('div', '', 'section-heading pull-heading');
|
|
24
|
+
toolbar.append(heading);
|
|
25
|
+
const selection = node('section', '', 'panel');
|
|
26
|
+
container.replaceChildren(toolbar, selection);
|
|
27
|
+
if (records.length === 0) {
|
|
28
|
+
selection.append(node('p', 'No saved PR reviews. Run Repometry on a pull request with its base revision and PR number.', 'detail empty'));
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const view = node('div');
|
|
32
|
+
selection.append(view);
|
|
33
|
+
const display = (review) => view.replaceChildren(reviewSummary(review), reviewFiles(review), reviewIssues(review));
|
|
34
|
+
display(records[0]);
|
|
35
|
+
if (records.length === 1) {
|
|
36
|
+
const number = records[0].number;
|
|
37
|
+
if (number !== null)
|
|
38
|
+
heading.append(node('span', ` #${number}`, 'pull-number'));
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const label = node('label', 'Saved review', 'pull-picker');
|
|
42
|
+
const select = node('select');
|
|
43
|
+
for (const [index, review] of records.entries()) {
|
|
44
|
+
select.append(new Option(`#${review.number} · ${review.head.slice(0, 7)}`, String(index)));
|
|
45
|
+
}
|
|
46
|
+
label.append(select);
|
|
47
|
+
toolbar.append(label);
|
|
48
|
+
select.addEventListener('change', () => {
|
|
49
|
+
const review = records[select.selectedIndex];
|
|
50
|
+
if (review)
|
|
51
|
+
display(review);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
export function renderDiffCoverageTrend(container, records) {
|
|
55
|
+
const heading = node('h2', 'Changed-line coverage by pull request');
|
|
56
|
+
container.replaceChildren(heading);
|
|
57
|
+
const reviews = [...records].sort((left, right) => right.generatedAt.localeCompare(left.generatedAt)).slice(0, 12);
|
|
58
|
+
if (reviews.length === 0) {
|
|
59
|
+
container.append(node('p', 'No saved PR reviews.', 'detail'));
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const list = node('ol', '', 'pull-trend-list');
|
|
63
|
+
const axis = node('div', '', 'pull-trend-axis');
|
|
64
|
+
axis.setAttribute('aria-hidden', 'true');
|
|
65
|
+
for (const value of [0, 25, 50, 75, 100])
|
|
66
|
+
axis.append(node('span', `${value}%`));
|
|
67
|
+
container.append(axis, list);
|
|
68
|
+
for (const review of reviews) {
|
|
69
|
+
const row = node('li', '', 'pull-trend-row');
|
|
70
|
+
const label = review.number === null ? review.head.slice(0, 7) : `#${review.number}`;
|
|
71
|
+
const subject = node('div', '', 'pull-trend-subject');
|
|
72
|
+
const time = node('time', date.format(new Date(review.generatedAt)), 'detail');
|
|
73
|
+
time.dateTime = review.generatedAt;
|
|
74
|
+
subject.append(node('strong', label), time);
|
|
75
|
+
const track = node('div', '', 'pull-trend-track');
|
|
76
|
+
track.setAttribute('aria-hidden', 'true');
|
|
77
|
+
const { percent, status, covered, total } = review.diff;
|
|
78
|
+
if (percent !== null) {
|
|
79
|
+
const bar = node('div', '', 'pull-trend-bar');
|
|
80
|
+
bar.dataset.status = status;
|
|
81
|
+
bar.style.inlineSize = `${percent}%`;
|
|
82
|
+
track.append(bar);
|
|
83
|
+
}
|
|
84
|
+
const value = node('div', '', 'pull-trend-result');
|
|
85
|
+
if (percent === null) {
|
|
86
|
+
value.append(node('span', status === 'partial' ? 'Partial · Unmeasured'
|
|
87
|
+
: status === 'not-applicable' ? 'No measurable changes' : 'Coverage unavailable', 'detail'));
|
|
88
|
+
track.classList.add('pull-trend-empty');
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
const result = `${whole.format(covered)} / ${whole.format(total)} lines`;
|
|
92
|
+
value.append(node('strong', formatPercent(percent)), node('span', status === 'partial' ? `Partial · ${result}` : result, 'detail'));
|
|
93
|
+
}
|
|
94
|
+
row.append(subject, track, value);
|
|
95
|
+
list.append(row);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function reviewSummary({ base, head, coverage, diff, findings }) {
|
|
99
|
+
const header = node('div', '', 'pull-revisions');
|
|
100
|
+
header.append(node('p', 'Base ', 'detail'), node('code', base.slice(0, 7)), node('span', '→', 'detail'), node('p', 'Head ', 'detail'), node('code', head.slice(0, 7)));
|
|
101
|
+
const summary = node('div', '', 'pull-summary');
|
|
102
|
+
const total = coverage.head?.value == null ? 'Unavailable' : formatPercent(coverage.head.value);
|
|
103
|
+
const change = coverage.delta === null ? 'No comparable base coverage' : `${points.format(coverage.delta)} pp from base`;
|
|
104
|
+
summary.append(summaryMetric('Total coverage', total, change, coverage.gate?.status), summaryMetric('Changed-line coverage', formatPercent(diff.percent), diff.percent === null
|
|
105
|
+
? diff.status === 'not-applicable' ? 'No measured changed lines' : 'Coverage unavailable'
|
|
106
|
+
: `${whole.format(diff.covered)} of ${whole.format(diff.total)} measured lines${diff.status === 'partial' ? ' · Partial' : ''}`, diff.gate?.status), summaryMetric('New issues', findings.added === null ? 'Unavailable' : whole.format(findings.added.length), findings.gate && findings.qualityCount !== null && findings.qualityCount !== findings.added?.length
|
|
107
|
+
? `Gate counted ${whole.format(findings.qualityCount)} quality issues`
|
|
108
|
+
: findings.added === null ? '' : 'Compared with base', findings.gate?.status));
|
|
109
|
+
const result = node('div', '', 'pull-summary-block');
|
|
110
|
+
result.append(header, summary);
|
|
111
|
+
if (coverage.reason && (coverage.base || coverage.head || coverage.gate))
|
|
112
|
+
result.append(node('p', coverageReasons[coverage.reason], 'detail'));
|
|
113
|
+
return result;
|
|
114
|
+
}
|
|
115
|
+
function summaryMetric(label, value, detail, gate) {
|
|
116
|
+
const figure = node('div', '', 'pull-fact');
|
|
117
|
+
figure.append(node('span', label, 'detail'), node('strong', value, 'pull-value'));
|
|
118
|
+
if (detail)
|
|
119
|
+
figure.append(node('p', detail, 'detail'));
|
|
120
|
+
if (gate) {
|
|
121
|
+
const badge = node('span', {
|
|
122
|
+
passed: 'Gate passed', failed: 'Gate failed', 'not-applicable': 'Gate skipped', indeterminate: 'Gate pending data',
|
|
123
|
+
}[gate] ?? gate, 'status pull-gate');
|
|
124
|
+
badge.dataset.status = gate === 'passed' ? 'complete' : gate === 'indeterminate' ? 'partial' : gate;
|
|
125
|
+
figure.append(badge);
|
|
126
|
+
}
|
|
127
|
+
return figure;
|
|
128
|
+
}
|
|
129
|
+
function reviewFiles(review) {
|
|
130
|
+
const section = node('section', '', 'pull-section');
|
|
131
|
+
const heading = node('h3', 'Changed files');
|
|
132
|
+
section.append(heading);
|
|
133
|
+
const { diff } = review;
|
|
134
|
+
if (diff.excludedPaths?.length) {
|
|
135
|
+
section.append(node('p', `Path exclusions · ${diff.excludedPaths.join(', ')}`, 'detail pull-exclusions'));
|
|
136
|
+
}
|
|
137
|
+
if (diff.files.length === 0 && diff.gaps.length === 0) {
|
|
138
|
+
section.append(node('p', diff.status === 'unavailable' ? 'Provide LCOV for this head revision to inspect its changed lines.'
|
|
139
|
+
: 'No added or modified source lines in the inspected scope.', 'detail'));
|
|
140
|
+
return section;
|
|
141
|
+
}
|
|
142
|
+
const files = new Map(diff.uncovered.map(file => [file.path, file.lines]));
|
|
143
|
+
const gaps = new Map(diff.gaps.map(gap => [gap.path, gap.reason]));
|
|
144
|
+
const rows = [...diff.files].sort((left, right) => missedCount(right) - missedCount(left) || right.addedLines - left.addedLines)
|
|
145
|
+
.map(record => {
|
|
146
|
+
const { path, addedLines, covered, total } = record;
|
|
147
|
+
const label = node('th');
|
|
148
|
+
const parts = path.split('/');
|
|
149
|
+
for (const [index, part] of parts.entries()) {
|
|
150
|
+
label.append(part);
|
|
151
|
+
if (index < parts.length - 1)
|
|
152
|
+
label.append('/', node('wbr'));
|
|
153
|
+
}
|
|
154
|
+
label.scope = 'row';
|
|
155
|
+
const missing = files.get(path);
|
|
156
|
+
const gap = gaps.get(path);
|
|
157
|
+
gaps.delete(path);
|
|
158
|
+
if (missing) {
|
|
159
|
+
const details = node('details', '', 'pull-missed-lines');
|
|
160
|
+
const ranges = lineRanges(missing);
|
|
161
|
+
details.append(node('summary', `${whole.format(missing.length)} missed ${missing.length === 1 ? 'line' : 'lines'}`), node('span', ranges.join(', ')));
|
|
162
|
+
label.append(details);
|
|
163
|
+
}
|
|
164
|
+
if (gap)
|
|
165
|
+
label.append(node('p', gap, 'detail'));
|
|
166
|
+
const row = node('tr');
|
|
167
|
+
row.append(label, cell(whole.format(addedLines), true), cell(covered === null || total === null ? '—' : `${whole.format(covered)} / ${whole.format(total)}`, true));
|
|
168
|
+
return row;
|
|
169
|
+
});
|
|
170
|
+
if (rows.length > 0)
|
|
171
|
+
section.append(table('Changed file coverage', ['File', 'Added lines', 'Covered / measured'], rows, 'pull-coverage-table'));
|
|
172
|
+
if (gaps.size > 0) {
|
|
173
|
+
const notes = node('ul', '', 'pull-gaps');
|
|
174
|
+
for (const [path, reason] of gaps)
|
|
175
|
+
notes.append(node('li', `${path} · ${reason}`));
|
|
176
|
+
section.append(node('h4', 'Coverage gaps'), notes);
|
|
177
|
+
}
|
|
178
|
+
return section;
|
|
179
|
+
}
|
|
180
|
+
function reviewIssues({ findings }) {
|
|
181
|
+
const section = node('section', '', 'pull-section');
|
|
182
|
+
section.append(node('h3', 'New issues'));
|
|
183
|
+
if (findings.added === null || findings.added.length === 0) {
|
|
184
|
+
section.append(node('p', findings.reason ? findingReasons[findings.reason] : 'No new issues in the inspected scope.', 'detail'));
|
|
185
|
+
return section;
|
|
186
|
+
}
|
|
187
|
+
const rows = findings.added.map(finding => {
|
|
188
|
+
const row = node('tr');
|
|
189
|
+
const message = node('th', finding.message);
|
|
190
|
+
message.scope = 'row';
|
|
191
|
+
message.append(node('p', [finding.tool, finding.rule].join(' · '), 'detail'));
|
|
192
|
+
const location = finding.path ? `${finding.path}${finding.line ? `:${finding.line}` : ''}` : '—';
|
|
193
|
+
row.append(message, cell(location), cell(finding.level));
|
|
194
|
+
return row;
|
|
195
|
+
});
|
|
196
|
+
section.append(table('Newly introduced findings', ['Finding', 'Source', 'Severity'], rows));
|
|
197
|
+
return section;
|
|
198
|
+
}
|
|
199
|
+
function lineRanges(lines) {
|
|
200
|
+
const ranges = [];
|
|
201
|
+
for (let start = 0; start < lines.length;) {
|
|
202
|
+
let end = start;
|
|
203
|
+
while (lines[end + 1] === lines[end] + 1)
|
|
204
|
+
end++;
|
|
205
|
+
ranges.push(start === end ? String(lines[start]) : `${lines[start]}–${lines[end]}`);
|
|
206
|
+
start = end + 1;
|
|
207
|
+
}
|
|
208
|
+
return ranges;
|
|
209
|
+
}
|
|
210
|
+
function missedCount({ covered, total }) {
|
|
211
|
+
return covered === null || total === null ? -1 : total - covered;
|
|
212
|
+
}
|
|
213
|
+
function formatPercent(value) {
|
|
214
|
+
return value === null ? 'Unavailable' : percentage.format(value / 100);
|
|
215
|
+
}
|
|
216
|
+
function table(label, headers, rows, className = '') {
|
|
217
|
+
const table = node('table', '', `pull-table ${className}`.trim());
|
|
218
|
+
const thead = node('thead');
|
|
219
|
+
const headings = node('tr');
|
|
220
|
+
for (const text of headers) {
|
|
221
|
+
const th = node('th', text);
|
|
222
|
+
th.scope = 'col';
|
|
223
|
+
headings.append(th);
|
|
224
|
+
}
|
|
225
|
+
thead.append(headings);
|
|
226
|
+
const body = node('tbody');
|
|
227
|
+
body.append(...rows);
|
|
228
|
+
table.append(thead, body);
|
|
229
|
+
table.setAttribute('aria-label', label);
|
|
230
|
+
return table;
|
|
231
|
+
}
|
|
232
|
+
function cell(text, numeric = false) {
|
|
233
|
+
return node('td', text, numeric ? 'numeric' : '');
|
|
234
|
+
}
|
|
235
|
+
function node(name, text = '', className = '') {
|
|
236
|
+
const element = document.createElement(name);
|
|
237
|
+
if (text)
|
|
238
|
+
element.textContent = text;
|
|
239
|
+
if (className)
|
|
240
|
+
element.className = className;
|
|
241
|
+
return element;
|
|
242
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { SourceLocator } from '../../json/wire/summary-model.js';
|
|
2
|
+
/** Where the viewer fetches a file's text, as the export names it: a URL template and the page a person would open. */
|
|
3
|
+
export type { SourceLocator } from '../../json/wire/summary-model.js';
|
|
4
|
+
export interface SourcePlace {
|
|
5
|
+
readonly revision: string;
|
|
6
|
+
readonly path: string;
|
|
7
|
+
readonly line?: number | undefined;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Fills a locator's template. A path keeps its slashes under {path} and loses them under {path:component}, as the
|
|
11
|
+
* GitLab API wants. A fragment that names {line} is dropped when there is no line, so the link opens the file.
|
|
12
|
+
*/
|
|
13
|
+
export declare function expandLocator(template: string, { revision, path, line }: SourcePlace): string;
|
|
14
|
+
/** A locator the viewer can use: a fetch URL that is web or site-relative, and a page link only over http(s). */
|
|
15
|
+
export declare function sourceLocator(value: unknown): SourceLocator | undefined;
|
|
16
|
+
/** The export's locator, from the summary; none means the report has no source navigation. */
|
|
17
|
+
export declare function useSources(value: SourceLocator | undefined): void;
|
|
18
|
+
/**
|
|
19
|
+
* The file's text at the commit, or null when it cannot be had: no locator, a private repository, a commit that was
|
|
20
|
+
* never pushed, a network failure. Nothing is requested until a file or an excerpt opens; each URL is fetched once.
|
|
21
|
+
*/
|
|
22
|
+
export declare function loadSource(sha: string, path: string): Promise<string | null>;
|
|
23
|
+
/** The page that shows the file on its forge, when the export names one. */
|
|
24
|
+
export declare function sourcePage(sha: string, path: string, line?: number): {
|
|
25
|
+
readonly href: string;
|
|
26
|
+
readonly provider: string;
|
|
27
|
+
} | null;
|
|
28
|
+
export interface SourceFunction {
|
|
29
|
+
readonly line: number;
|
|
30
|
+
readonly complexity: number;
|
|
31
|
+
readonly name: string;
|
|
32
|
+
}
|
|
33
|
+
export interface SourceNote {
|
|
34
|
+
readonly line: number;
|
|
35
|
+
readonly level: string;
|
|
36
|
+
readonly title: string;
|
|
37
|
+
readonly message: string;
|
|
38
|
+
}
|
|
39
|
+
export interface SourceOptions {
|
|
40
|
+
readonly path: string;
|
|
41
|
+
readonly functions: readonly SourceFunction[];
|
|
42
|
+
readonly notes: readonly SourceNote[];
|
|
43
|
+
/** The line to bring into view and mark, from the URL. */
|
|
44
|
+
readonly line?: number | undefined;
|
|
45
|
+
/** Called when a line number is chosen, so the page can put it in the URL. */
|
|
46
|
+
readonly choose: (line: number) => void;
|
|
47
|
+
}
|
|
48
|
+
/** The source as numbered lines; returns a function that brings a line into view and marks it. */
|
|
49
|
+
export declare function renderSource(container: HTMLElement, text: string, options: SourceOptions): (line: number) => void;
|
|
50
|
+
export interface ExcerptOptions {
|
|
51
|
+
readonly path: string;
|
|
52
|
+
/** The flagged range; the excerpt shows its head with a line of context before it. */
|
|
53
|
+
readonly line: number;
|
|
54
|
+
readonly endLine?: number | undefined;
|
|
55
|
+
/** Where the rest of a long range opens in full. */
|
|
56
|
+
readonly more?: ((line: number) => string) | undefined;
|
|
57
|
+
readonly size?: number;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* A few lines of source around a finding, bounded so a 160-line function stays a card: one line of context,
|
|
61
|
+
* the head of the flagged range marked, then a link to the rest.
|
|
62
|
+
*/
|
|
63
|
+
export declare function renderExcerpt(text: string, options: ExcerptOptions): HTMLElement;
|
|
64
|
+
/** The first code on a line, trimmed: a name for an anonymous function that says which one it is. */
|
|
65
|
+
export declare function lineSnippet(text: string, line: number, limit?: number): string | null;
|
|
66
|
+
/** A single line of code, coloured as the source view colours it; for a snippet that stands on its own. */
|
|
67
|
+
export declare function highlightCode(path: string, line: string): Node[];
|
|
68
|
+
export type TokenKind = '' | 'c' | 's' | 'n' | 'k';
|
|
69
|
+
/**
|
|
70
|
+
* Splits lines of JavaScript or TypeScript into coloured runs: comments, strings, regular expressions, numbers and
|
|
71
|
+
* keywords. It is a scanner, not a parser, but it honours escapes and character classes, and it tells a regular
|
|
72
|
+
* expression from a division by what comes before the slash. State carries across lines, so a block comment or a
|
|
73
|
+
* template literal stays coloured to its end. Other languages come back as plain text.
|
|
74
|
+
*/
|
|
75
|
+
export declare function tokenizer(path: string): (line: string) => [TokenKind, string][];
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fills a locator's template. A path keeps its slashes under {path} and loses them under {path:component}, as the
|
|
3
|
+
* GitLab API wants. A fragment that names {line} is dropped when there is no line, so the link opens the file.
|
|
4
|
+
*/
|
|
5
|
+
export function expandLocator(template, { revision, path, line }) {
|
|
6
|
+
const hash = template.indexOf('#');
|
|
7
|
+
const fragment = hash < 0 ? '' : template.slice(hash);
|
|
8
|
+
const base = hash < 0 ? template : template.slice(0, hash);
|
|
9
|
+
const kept = line === undefined && fragment.includes('{line}') ? '' : fragment;
|
|
10
|
+
return (base + kept).replace(/\{(revision|path:component|path|line)\}/g, (_, name) => {
|
|
11
|
+
switch (name) {
|
|
12
|
+
case 'revision': return encodeURIComponent(revision);
|
|
13
|
+
case 'path:component': return encodeURIComponent(path);
|
|
14
|
+
case 'path': return path.split('/').map(encodeURIComponent).join('/');
|
|
15
|
+
default: return String(line);
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
/** A locator the viewer can use: a fetch URL that is web or site-relative, and a page link only over http(s). */
|
|
20
|
+
export function sourceLocator(value) {
|
|
21
|
+
if (typeof value !== 'object' || value === null)
|
|
22
|
+
return undefined;
|
|
23
|
+
const { provider, url, web } = value;
|
|
24
|
+
if (typeof provider !== 'string' || typeof url !== 'string' || !url.includes('{path'))
|
|
25
|
+
return undefined;
|
|
26
|
+
if (/^[a-z][a-z\d+.-]*:/i.test(url) && !/^https?:\/\//i.test(url))
|
|
27
|
+
return undefined;
|
|
28
|
+
return { provider, url, ...(typeof web === 'string' && /^https?:\/\//i.test(web) ? { web } : {}) };
|
|
29
|
+
}
|
|
30
|
+
const providerNames = { github: 'GitHub', gitlab: 'GitLab', bitbucket: 'Bitbucket', gitea: 'Gitea' };
|
|
31
|
+
let locator;
|
|
32
|
+
const requests = new Map();
|
|
33
|
+
/** The export's locator, from the summary; none means the report has no source navigation. */
|
|
34
|
+
export function useSources(value) {
|
|
35
|
+
locator = value;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The file's text at the commit, or null when it cannot be had: no locator, a private repository, a commit that was
|
|
39
|
+
* never pushed, a network failure. Nothing is requested until a file or an excerpt opens; each URL is fetched once.
|
|
40
|
+
*/
|
|
41
|
+
export function loadSource(sha, path) {
|
|
42
|
+
if (!locator)
|
|
43
|
+
return Promise.resolve(null);
|
|
44
|
+
const url = expandLocator(locator.url, { revision: sha, path });
|
|
45
|
+
let request = requests.get(url);
|
|
46
|
+
if (!request) {
|
|
47
|
+
request = fetch(url).then(response => response.ok ? response.text() : null).catch(() => null);
|
|
48
|
+
requests.set(url, request);
|
|
49
|
+
}
|
|
50
|
+
return request;
|
|
51
|
+
}
|
|
52
|
+
/** The page that shows the file on its forge, when the export names one. */
|
|
53
|
+
export function sourcePage(sha, path, line) {
|
|
54
|
+
if (!locator?.web)
|
|
55
|
+
return null;
|
|
56
|
+
const name = providerNames[locator.provider.toLowerCase()] ?? locator.provider.charAt(0).toUpperCase() + locator.provider.slice(1);
|
|
57
|
+
return { href: expandLocator(locator.web, { revision: sha, path, line }), provider: name };
|
|
58
|
+
}
|
|
59
|
+
/** The source as numbered lines; returns a function that brings a line into view and marks it. */
|
|
60
|
+
export function renderSource(container, text, options) {
|
|
61
|
+
const lines = text.replace(/\r\n?/g, '\n').replace(/\n$/, '').split('\n');
|
|
62
|
+
const highlight = highlighter(options.path);
|
|
63
|
+
const starts = new Map(options.functions.map(entry => [entry.line, entry]));
|
|
64
|
+
const notes = new Map();
|
|
65
|
+
for (const note of options.notes)
|
|
66
|
+
notes.set(note.line, [...notes.get(note.line) ?? [], note]);
|
|
67
|
+
const code = element('div', 'src');
|
|
68
|
+
code.setAttribute('role', 'region');
|
|
69
|
+
code.setAttribute('aria-label', `Source of ${options.path}`);
|
|
70
|
+
const rows = [];
|
|
71
|
+
const fragment = document.createDocumentFragment();
|
|
72
|
+
for (const [index, line] of lines.entries()) {
|
|
73
|
+
const number = index + 1;
|
|
74
|
+
const row = element('div', 'src-line');
|
|
75
|
+
row.id = `L${number}`;
|
|
76
|
+
const mark = element('span', 'src-mark');
|
|
77
|
+
const start = starts.get(number);
|
|
78
|
+
if (start) {
|
|
79
|
+
const badge = element('span', `src-complexity src-band-${start.complexity >= 20 ? 3 : start.complexity >= 10 ? 2 : start.complexity >= 5 ? 1 : 0}`, String(start.complexity));
|
|
80
|
+
badge.title = `Cyclomatic complexity ${start.complexity}`;
|
|
81
|
+
mark.append(badge);
|
|
82
|
+
}
|
|
83
|
+
const anchor = element('a', 'src-number', String(number));
|
|
84
|
+
anchor.href = `#L${number}`;
|
|
85
|
+
anchor.addEventListener('click', event => {
|
|
86
|
+
event.preventDefault();
|
|
87
|
+
reveal(number, false);
|
|
88
|
+
options.choose(number);
|
|
89
|
+
});
|
|
90
|
+
const content = element('code', 'src-code');
|
|
91
|
+
content.append(...highlight(line));
|
|
92
|
+
if (notes.has(number))
|
|
93
|
+
row.classList.add('src-flagged');
|
|
94
|
+
row.append(mark, anchor, content);
|
|
95
|
+
fragment.append(row);
|
|
96
|
+
rows.push(row);
|
|
97
|
+
for (const note of notes.get(number) ?? []) {
|
|
98
|
+
const box = element('div', `src-note src-note-${note.level}`);
|
|
99
|
+
box.append(element('strong', '', note.title), element('span', '', note.message));
|
|
100
|
+
fragment.append(box);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
code.append(fragment);
|
|
104
|
+
container.replaceChildren(code);
|
|
105
|
+
function reveal(line, scroll = true) {
|
|
106
|
+
for (const row of code.querySelectorAll('.src-target'))
|
|
107
|
+
row.classList.remove('src-target');
|
|
108
|
+
const row = rows[line - 1];
|
|
109
|
+
if (!row)
|
|
110
|
+
return;
|
|
111
|
+
row.classList.add('src-target');
|
|
112
|
+
if (scroll)
|
|
113
|
+
row.scrollIntoView({ block: 'center' });
|
|
114
|
+
}
|
|
115
|
+
if (options.line)
|
|
116
|
+
requestAnimationFrame(() => reveal(options.line));
|
|
117
|
+
return reveal;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* A few lines of source around a finding, bounded so a 160-line function stays a card: one line of context,
|
|
121
|
+
* the head of the flagged range marked, then a link to the rest.
|
|
122
|
+
*/
|
|
123
|
+
export function renderExcerpt(text, options) {
|
|
124
|
+
const lines = text.replace(/\r\n?/g, '\n').replace(/\n$/, '').split('\n');
|
|
125
|
+
const end = Math.min(lines.length, Math.max(options.line, options.endLine ?? options.line));
|
|
126
|
+
const from = Math.max(1, options.line - 1);
|
|
127
|
+
const to = Math.min(end, from + (options.size ?? 6) - 1, lines.length);
|
|
128
|
+
const highlight = highlighter(options.path);
|
|
129
|
+
const box = element('div', 'src src-excerpt');
|
|
130
|
+
for (const [index, line] of lines.entries()) {
|
|
131
|
+
// Highlighting runs from the top so a comment or template opened above the excerpt keeps its colour.
|
|
132
|
+
const nodes = highlight(line);
|
|
133
|
+
const number = index + 1;
|
|
134
|
+
if (number < from)
|
|
135
|
+
continue;
|
|
136
|
+
if (number > to)
|
|
137
|
+
break;
|
|
138
|
+
const row = element('div', number >= options.line && number <= end ? 'src-line src-range' : 'src-line');
|
|
139
|
+
const code = element('code', 'src-code');
|
|
140
|
+
code.append(...nodes);
|
|
141
|
+
row.append(element('span', 'src-mark'), element('span', 'src-number', String(number)), code);
|
|
142
|
+
box.append(row);
|
|
143
|
+
}
|
|
144
|
+
if (end > to) {
|
|
145
|
+
const rest = element(options.more ? 'a' : 'span', 'src-more', `⋯ ${end - to} more ${end - to === 1 ? 'line' : 'lines'}, to line ${end}`);
|
|
146
|
+
if (options.more && rest instanceof HTMLAnchorElement)
|
|
147
|
+
rest.href = options.more(to + 1);
|
|
148
|
+
box.append(rest);
|
|
149
|
+
}
|
|
150
|
+
return box;
|
|
151
|
+
}
|
|
152
|
+
/** The first code on a line, trimmed: a name for an anonymous function that says which one it is. */
|
|
153
|
+
export function lineSnippet(text, line, limit = 60) {
|
|
154
|
+
const found = text.split('\n')[line - 1]?.trim();
|
|
155
|
+
if (!found)
|
|
156
|
+
return null;
|
|
157
|
+
return found.length > limit ? `${found.slice(0, limit - 1)}…` : found;
|
|
158
|
+
}
|
|
159
|
+
/** A single line of code, coloured as the source view colours it; for a snippet that stands on its own. */
|
|
160
|
+
export function highlightCode(path, line) {
|
|
161
|
+
return highlighter(path)(line);
|
|
162
|
+
}
|
|
163
|
+
const keywords = new Set(('abstract as async await break case catch class const continue debugger declare default delete do else enum export '
|
|
164
|
+
+ 'extends false finally for from function get if implements import in instanceof interface let new null of package private protected '
|
|
165
|
+
+ 'public readonly return satisfies set static super switch this throw true try type typeof undefined var void while with yield').split(' '));
|
|
166
|
+
/** Words after which a slash opens a regular expression rather than divides. */
|
|
167
|
+
const beforeRegex = new Set('return typeof instanceof in of new delete void throw case do else yield await'.split(' '));
|
|
168
|
+
/**
|
|
169
|
+
* Splits lines of JavaScript or TypeScript into coloured runs: comments, strings, regular expressions, numbers and
|
|
170
|
+
* keywords. It is a scanner, not a parser, but it honours escapes and character classes, and it tells a regular
|
|
171
|
+
* expression from a division by what comes before the slash. State carries across lines, so a block comment or a
|
|
172
|
+
* template literal stays coloured to its end. Other languages come back as plain text.
|
|
173
|
+
*/
|
|
174
|
+
export function tokenizer(path) {
|
|
175
|
+
if (!/\.(m|c)?[jt]sx?$/.test(path))
|
|
176
|
+
return line => [['', line]];
|
|
177
|
+
let open = '';
|
|
178
|
+
let regexAllowed = true;
|
|
179
|
+
return line => {
|
|
180
|
+
const runs = [];
|
|
181
|
+
let plain = '';
|
|
182
|
+
const push = (kind, value) => {
|
|
183
|
+
if (!value)
|
|
184
|
+
return;
|
|
185
|
+
if (!kind) {
|
|
186
|
+
plain += value;
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
if (plain) {
|
|
190
|
+
runs.push(['', plain]);
|
|
191
|
+
plain = '';
|
|
192
|
+
}
|
|
193
|
+
runs.push([kind, value]);
|
|
194
|
+
};
|
|
195
|
+
// Past an escape: the index after the closing quote, or the line's end when it stays open.
|
|
196
|
+
const close = (from, quote) => {
|
|
197
|
+
let at = from;
|
|
198
|
+
while (at < line.length && line[at] !== quote)
|
|
199
|
+
at += line[at] === '\\' ? 2 : 1;
|
|
200
|
+
return at < line.length ? [at + 1, true] : [line.length, false];
|
|
201
|
+
};
|
|
202
|
+
let at = 0;
|
|
203
|
+
while (at < line.length) {
|
|
204
|
+
if (open === 'comment') {
|
|
205
|
+
const end = line.indexOf('*/', at);
|
|
206
|
+
const stop = end < 0 ? line.length : end + 2;
|
|
207
|
+
push('c', line.slice(at, stop));
|
|
208
|
+
at = stop;
|
|
209
|
+
if (end >= 0)
|
|
210
|
+
open = '';
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
if (open === 'template') {
|
|
214
|
+
const [stop, closed] = close(at, '`');
|
|
215
|
+
push('s', line.slice(at, stop));
|
|
216
|
+
at = stop;
|
|
217
|
+
if (closed) {
|
|
218
|
+
open = '';
|
|
219
|
+
regexAllowed = false;
|
|
220
|
+
}
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
const char = line[at];
|
|
224
|
+
const next = line[at + 1];
|
|
225
|
+
if (char === '/' && next === '/') {
|
|
226
|
+
push('c', line.slice(at));
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
if (char === '/' && next === '*') {
|
|
230
|
+
push('c', '/*');
|
|
231
|
+
at += 2;
|
|
232
|
+
open = 'comment';
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
if (char === '`') {
|
|
236
|
+
push('s', '`');
|
|
237
|
+
at += 1;
|
|
238
|
+
open = 'template';
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
if (char === '"' || char === "'") {
|
|
242
|
+
const [stop] = close(at + 1, char);
|
|
243
|
+
push('s', line.slice(at, stop));
|
|
244
|
+
at = stop;
|
|
245
|
+
regexAllowed = false;
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
if (char === '/' && regexAllowed) {
|
|
249
|
+
let end = at + 1;
|
|
250
|
+
let inClass = false;
|
|
251
|
+
while (end < line.length) {
|
|
252
|
+
const current = line[end];
|
|
253
|
+
if (current === '\\') {
|
|
254
|
+
end += 2;
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
if (inClass) {
|
|
258
|
+
if (current === ']')
|
|
259
|
+
inClass = false;
|
|
260
|
+
}
|
|
261
|
+
else if (current === '[')
|
|
262
|
+
inClass = true;
|
|
263
|
+
else if (current === '/')
|
|
264
|
+
break;
|
|
265
|
+
end += 1;
|
|
266
|
+
}
|
|
267
|
+
if (end < line.length) {
|
|
268
|
+
end += 1;
|
|
269
|
+
while (end < line.length && /[a-z]/i.test(line[end]))
|
|
270
|
+
end += 1;
|
|
271
|
+
push('s', line.slice(at, end));
|
|
272
|
+
at = end;
|
|
273
|
+
regexAllowed = false;
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
const word = /^[A-Za-z_$][\w$]*/.exec(line.slice(at))?.[0];
|
|
278
|
+
if (word) {
|
|
279
|
+
push(keywords.has(word) ? 'k' : '', word);
|
|
280
|
+
regexAllowed = beforeRegex.has(word);
|
|
281
|
+
at += word.length;
|
|
282
|
+
continue;
|
|
283
|
+
}
|
|
284
|
+
const number = /^(?:0x[\da-f_]+n?|\d[\d_]*(?:\.\d[\d_]*)?(?:e[+-]?\d+)?n?)/i.exec(line.slice(at))?.[0];
|
|
285
|
+
if (number) {
|
|
286
|
+
push('n', number);
|
|
287
|
+
regexAllowed = false;
|
|
288
|
+
at += number.length;
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
push('', char);
|
|
292
|
+
// After a closing bracket a slash divides; after any other punctuation it opens a regular expression.
|
|
293
|
+
if (!/\s/.test(char))
|
|
294
|
+
regexAllowed = char !== ')' && char !== ']';
|
|
295
|
+
at += 1;
|
|
296
|
+
}
|
|
297
|
+
if (plain)
|
|
298
|
+
runs.push(['', plain]);
|
|
299
|
+
return runs;
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
function highlighter(path) {
|
|
303
|
+
const tokenize = tokenizer(path);
|
|
304
|
+
return line => tokenize(line).map(([kind, value]) => kind ? element('span', `tok-${kind}`, value) : document.createTextNode(value));
|
|
305
|
+
}
|
|
306
|
+
function element(tag, className, text) {
|
|
307
|
+
const node = document.createElement(tag);
|
|
308
|
+
if (className)
|
|
309
|
+
node.className = className;
|
|
310
|
+
if (text !== undefined)
|
|
311
|
+
node.textContent = text;
|
|
312
|
+
return node;
|
|
313
|
+
}
|