@repometry/core 0.2.0 → 0.4.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@repometry/core",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "description": "The Repometry plugin API and inspection engine",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -1,11 +1,12 @@
1
1
  import type { DataContract } from './plugin.js';
2
+ import type { recoveredSyntax } from '../model/code-structure.js';
2
3
  import type { CodeStructure as StoredStructure, StructuralObservation as StoredObservation } from '../model/code-structure.js';
3
4
  import type { Finding as StoredFinding, FindingResult as StoredFindings } from '../model/finding.js';
4
5
  import type { PluginCategory } from '../model/inspection-category.js';
5
6
  export type StructuralObservation = Omit<StoredObservation, 'rule'> & {
6
7
  readonly check: StoredObservation['rule'];
7
8
  };
8
- export type CodeStructure = Omit<StoredStructure, 'observations'> & {
9
+ export type CodeStructure = Omit<StoredStructure, 'observations' | typeof recoveredSyntax> & {
9
10
  readonly observations?: readonly StructuralObservation[];
10
11
  };
11
12
  export type Finding = Omit<StoredFinding, 'tool' | 'rule' | 'category'> & {
@@ -38,5 +38,23 @@ interface PluginOperations {
38
38
  readonly operations: readonly Operation[];
39
39
  readonly produces: readonly ProductOutput[];
40
40
  }
41
+ export declare function manifestOperations({ id, category }: Pick<PluginManifest, 'id' | 'category'>, { operations, produces }: PluginOperations): {
42
+ operations: Operation[];
43
+ produces: ({
44
+ contract: string;
45
+ persistence: 'report' | 'cache' | 'transient';
46
+ metrics?: readonly import("./plugin.js").MetricDescriptor[];
47
+ } | {
48
+ contract: string;
49
+ persistence: 'report' | 'cache' | 'transient';
50
+ metrics?: readonly import("./plugin.js").MetricDescriptor[];
51
+ schema: boolean | Readonly<Record<string, unknown>>;
52
+ })[];
53
+ };
54
+ export type BundledManifest = PluginManifest & {
55
+ readonly version: string;
56
+ create(): PluginOperations;
57
+ };
58
+ export declare function bundledPlugin(manifest: BundledManifest): Plugin;
41
59
  export declare function pluginCategory(plugin: Plugin): PluginCategory;
42
60
  export declare function manifestPlugin({ id, category, languages, optionsSchema, tools, checks }: Omit<PluginManifest, 'create'>, version: string): Omit<Plugin, 'operations' | 'produces' | 'settings'>;
@@ -1,7 +1,15 @@
1
1
  import { category } from '../model/inspection-category.js';
2
2
  import { findingCategory } from './author-contracts.js';
3
+ import { authorOperation } from './author-operation.js';
3
4
  import { pluginApi } from './plugin.js';
4
5
  export const pluginManifestSchema = 'urn:repometry:schema:plugin:1';
6
+ export function manifestOperations({ id, category }, { operations, produces }) {
7
+ return { operations: operations.map(operation => authorOperation(id, category, operation)),
8
+ produces: produces.map(({ valueSchema, ...product }) => valueSchema === undefined ? product : { ...product, schema: valueSchema }) };
9
+ }
10
+ export function bundledPlugin(manifest) {
11
+ return { ...manifestPlugin(manifest, manifest.version), ...manifestOperations(manifest, manifest.create()) };
12
+ }
5
13
  export function pluginCategory(plugin) {
6
14
  const categories = [category.language, category.source, category.security, category.coverage];
7
15
  return categories.find(value => plugin.kinds.includes(value)) ?? category.maintainability;
@@ -5,12 +5,14 @@ import type { PlannedOperation } from './plan.js';
5
5
  export declare class ReportProducts {
6
6
  private readonly metrics;
7
7
  private readonly measured;
8
+ private readonly alwaysReport;
8
9
  private readonly privateMetrics;
9
10
  private readonly products;
10
11
  private readonly contracts;
11
12
  private readonly rules;
12
13
  private readonly declared;
13
14
  constructor(plugins: readonly Plugin[]);
15
+ private registerMetric;
14
16
  record(task: PlannedOperation, { contract, value }: Product, path?: string): FindingResult | undefined;
15
17
  result(): PluginData | undefined;
16
18
  }
@@ -6,6 +6,7 @@ import { isRecord } from './plugin.js';
6
6
  export class ReportProducts {
7
7
  metrics = new Map();
8
8
  measured = new Set();
9
+ alwaysReport = new Set();
9
10
  privateMetrics = new Set();
10
11
  products = new Map();
11
12
  contracts = new Map();
@@ -25,17 +26,21 @@ export class ReportProducts {
25
26
  }
26
27
  this.contracts.set(product.contract, product.schema);
27
28
  }
28
- for (const metric of product.metrics ?? []) {
29
- const existing = this.metrics.get(metric.id);
30
- if (existing && !isDeepStrictEqual(existing, metric))
31
- throw new Error(`Conflicting metric declaration: ${metric.id}`);
32
- this.metrics.set(metric.id, metric);
33
- if (plugin.kinds?.includes('security'))
34
- this.privateMetrics.add(metric.id);
35
- }
29
+ for (const metric of product.metrics ?? [])
30
+ this.registerMetric(plugin, metric);
36
31
  }
37
32
  }
38
33
  }
34
+ registerMetric(plugin, metric) {
35
+ const existing = this.metrics.get(metric.id);
36
+ if (existing && !isDeepStrictEqual(existing, metric))
37
+ throw new Error(`Conflicting metric declaration: ${metric.id}`);
38
+ this.metrics.set(metric.id, metric);
39
+ if (!plugin.languageProvenanceRequiresSource)
40
+ this.alwaysReport.add(metric.id);
41
+ if (plugin.kinds?.includes('security'))
42
+ this.privateMetrics.add(metric.id);
43
+ }
39
44
  record(task, { contract, value }, path) {
40
45
  if (task.declaration?.persistence !== 'report')
41
46
  return undefined;
@@ -67,8 +72,11 @@ export class ReportProducts {
67
72
  result() {
68
73
  if (!this.declared)
69
74
  return undefined;
70
- return { metrics: [...this.metrics.values()], measured: [...this.measured],
71
- ...(this.privateMetrics.size ? { privateMetrics: [...this.privateMetrics] } : {}),
75
+ const visible = (id) => this.alwaysReport.has(id) || this.measured.has(id);
76
+ const metrics = [...this.metrics.values()].filter(({ id }) => visible(id));
77
+ const privateMetrics = [...this.privateMetrics].filter(visible);
78
+ return { metrics, measured: [...this.measured],
79
+ ...(privateMetrics.length ? { privateMetrics } : {}),
72
80
  ...(this.contracts.size ? { contracts: Object.fromEntries(this.contracts) } : {}),
73
81
  rules: this.rules, products: Object.fromEntries(this.products) };
74
82
  }
@@ -5,6 +5,12 @@ declare const structuralCheckIds: readonly ['function-parameters', 'return-state
5
5
  export type StructuralCheck = typeof structuralCheckIds[number];
6
6
  export declare const structureMeasures: readonly ['functions', 'cyclomaticComplexity', 'cognitiveComplexity', 'classes', 'commentLines', 'codeLines', 'duplication', 'structuralRules'];
7
7
  export type StructureMeasure = typeof structureMeasures[number];
8
+ export declare const recoveredSyntax: unique symbol;
9
+ export interface RecoveryDetails {
10
+ readonly reason: string;
11
+ readonly location: string;
12
+ readonly omitted: number;
13
+ }
8
14
  export interface FunctionComplexity {
9
15
  /** Functions without a name omit this field. Reports display them as (anonymous). */
10
16
  readonly name?: string;
@@ -22,10 +28,13 @@ export interface StructuralObservation {
22
28
  readonly anchor?: string;
23
29
  }
24
30
  export interface CodeStructure {
31
+ readonly [recoveredSyntax]?: RecoveryDetails;
25
32
  readonly path: string;
26
33
  readonly lines: number;
27
34
  readonly functions?: number;
28
35
  readonly classes?: number;
36
+ /** Named or unnamed declarations counted by the language provider. */
37
+ readonly declarations?: number;
29
38
  readonly cognitiveComplexity?: number;
30
39
  readonly commentLines?: number;
31
40
  readonly cyclomaticFunctions?: readonly FunctionComplexity[];
@@ -7,6 +7,9 @@ const structuralCheckIds = [
7
7
  ];
8
8
  export const structureMeasures = ['functions', 'cyclomaticComplexity', 'cognitiveComplexity', 'classes',
9
9
  'commentLines', 'codeLines', 'duplication', 'structuralRules'];
10
+ // The CLI and a separately installed parser can load different instances of this package.
11
+ // The operation cache excludes symbol-keyed data, so the transient marker never lands on disk.
12
+ export const recoveredSyntax = Symbol.for('repometry.internal.recovered-syntax');
10
13
  const checkIds = new Set(structuralCheckIds);
11
14
  function isCount(value) {
12
15
  return typeof value === 'number' && Number.isSafeInteger(value) && value >= 0;
@@ -32,7 +35,7 @@ export const codeStructure = {
32
35
  id: 'repometry.code-structure/v1',
33
36
  accepts: (value) => isRecord(value)
34
37
  && typeof value.path === 'string' && isCount(value.lines)
35
- && [value.functions, value.classes, value.cognitiveComplexity, value.commentLines].every(count => count === undefined || isCount(count))
38
+ && [value.functions, value.classes, value.declarations, value.cognitiveComplexity, value.commentLines].every(count => count === undefined || isCount(count))
36
39
  && (value.cyclomaticFunctions === undefined || Array.isArray(value.cyclomaticFunctions) && value.cyclomaticFunctions.every(isFunctionComplexity))
37
40
  && (value.cognitiveFunctions === undefined || Array.isArray(value.cognitiveFunctions) && value.cognitiveFunctions.every(isFunctionComplexity))
38
41
  && (value.functionBodies === undefined || isMergedLineRanges(value.functionBodies) && (value.functionBodies.at(-1)?.[1] ?? 0) <= value.lines)
@@ -0,0 +1,6 @@
1
+ import type { LanguageSyntax } from './language.js';
2
+ export declare const cSyntax: LanguageSyntax;
3
+ export declare const cppSyntax: LanguageSyntax;
4
+ export declare const jsonSyntax: LanguageSyntax;
5
+ export declare const cssSyntax: LanguageSyntax;
6
+ export declare const dockerfileSyntax: LanguageSyntax;
@@ -0,0 +1,50 @@
1
+ const cFamily = {
2
+ lineComments: ['//'],
3
+ strings: [{ quote: '"' }, { quote: "'" }],
4
+ stringPrefixes: ['L', 'u', 'U', 'u8'],
5
+ };
6
+ // WG14 N3096 §6.4.1, including the alternative spellings. Line splices can hide block markers.
7
+ export const cSyntax = {
8
+ ...cFamily,
9
+ keywords: [
10
+ 'alignas', 'alignof', 'auto', 'bool', 'break', 'case', 'char', 'const', 'constexpr', 'continue', 'default',
11
+ 'do', 'double', 'else', 'enum', 'extern', 'false', 'float', 'for', 'goto', 'if', 'inline', 'int', 'long',
12
+ 'nullptr', 'register', 'restrict', 'return', 'short', 'signed', 'sizeof', 'static', 'static_assert', 'struct',
13
+ 'switch', 'thread_local', 'true', 'typedef', 'typeof', 'typeof_unqual', 'union', 'unsigned', 'void',
14
+ 'volatile', 'while', '_Alignas', '_Alignof', '_Atomic', '_BitInt', '_Bool', '_Complex', '_Decimal128',
15
+ '_Decimal32', '_Decimal64', '_Generic', '_Imaginary', '_Noreturn', '_Static_assert', '_Thread_local',
16
+ ],
17
+ };
18
+ // WG21 N4950 §5.11. Delimited raw strings can contain unbalanced block markers.
19
+ export const cppSyntax = {
20
+ ...cFamily,
21
+ keywords: [
22
+ 'alignas', 'alignof', 'and', 'and_eq', 'asm', 'auto', 'bitand', 'bitor', 'bool', 'break', 'case', 'catch',
23
+ 'char', 'char8_t', 'char16_t', 'char32_t', 'class', 'compl', 'concept', 'const', 'consteval', 'constexpr',
24
+ 'constinit', 'const_cast', 'continue', 'co_await', 'co_return', 'co_yield', 'decltype', 'default', 'delete',
25
+ 'do', 'double', 'dynamic_cast', 'else', 'enum', 'explicit', 'export', 'extern', 'false', 'float', 'for',
26
+ 'friend', 'goto', 'if', 'inline', 'int', 'long', 'mutable', 'namespace', 'new', 'noexcept', 'not', 'not_eq',
27
+ 'nullptr', 'operator', 'or', 'or_eq', 'private', 'protected', 'public', 'register', 'reinterpret_cast',
28
+ 'requires', 'return', 'short', 'signed', 'sizeof', 'static', 'static_assert', 'static_cast', 'struct',
29
+ 'switch', 'template', 'this', 'thread_local', 'throw', 'true', 'try', 'typedef', 'typeid', 'typename', 'union',
30
+ 'unsigned', 'using', 'virtual', 'void', 'volatile', 'wchar_t', 'while', 'xor', 'xor_eq',
31
+ ],
32
+ };
33
+ // RFC 8259 §§3 and 7.
34
+ export const jsonSyntax = {
35
+ strings: [{ quote: '"' }],
36
+ keywords: ['false', 'null', 'true'],
37
+ };
38
+ // CSS Syntax Level 3 §4.3.5. Unquoted URL tokens can contain block markers.
39
+ export const cssSyntax = {
40
+ strings: [{ quote: '"' }, { quote: "'" }],
41
+ };
42
+ // Dockerfile reference: conventional uppercase instructions and the FROM stage marker.
43
+ export const dockerfileSyntax = {
44
+ strings: [{ quote: '"' }, { quote: "'" }],
45
+ keywords: [
46
+ 'ADD', 'ARG', 'CMD', 'COPY', 'ENTRYPOINT', 'ENV', 'EXPOSE', 'FROM', 'HEALTHCHECK', 'LABEL', 'MAINTAINER',
47
+ 'ONBUILD', 'RUN', 'SHELL', 'STOPSIGNAL', 'USER', 'VOLUME', 'WORKDIR', 'AS',
48
+ ],
49
+ };
50
+ //# sourceMappingURL=file-type-syntax.js.map
@@ -1,18 +1,20 @@
1
1
  import { posix } from 'node:path';
2
+ import { cppSyntax, cssSyntax, cSyntax, dockerfileSyntax, jsonSyntax } from './file-type-syntax.js';
2
3
  export const javascriptType = { id: 'javascript', label: 'JavaScript', color: '#f1e05a', extensions: ['.js', '.mjs', '.cjs', '.jsx'] };
3
4
  export const typescriptType = { id: 'typescript', label: 'TypeScript', color: '#3178c6', extensions: ['.ts', '.mts', '.cts', '.tsx'] };
4
5
  export const fileTypes = [
5
6
  javascriptType, typescriptType,
6
7
  { id: 'markdown', label: 'Markdown', color: '#768390', extensions: ['.md', '.mdx', '.markdown'] },
7
8
  { id: 'yaml', label: 'YAML', color: '#c47d84', extensions: ['.yaml', '.yml'] },
8
- { id: 'json', label: 'JSON', color: '#9b86b5', extensions: ['.json'] },
9
- { id: 'css', label: 'CSS', color: '#663399', extensions: ['.css'] },
9
+ { id: 'json', label: 'JSON', color: '#9b86b5', extensions: ['.json'], syntax: jsonSyntax },
10
+ { id: 'css', label: 'CSS', color: '#663399', extensions: ['.css'], syntax: cssSyntax },
10
11
  { id: 'scss', label: 'SCSS', color: '#c6538c', extensions: ['.scss'] },
11
12
  { id: 'less', label: 'Less', color: '#1d365d', extensions: ['.less'] },
12
13
  { id: 'html', label: 'HTML', color: '#e34c26', extensions: ['.html', '.htm'] },
13
14
  { id: 'shell', label: 'Shell', color: '#89e051', extensions: ['.sh', '.bash', '.zsh', '.fish'], names: ['.bashrc', '.bash_profile'] },
14
15
  { id: 'batch', label: 'Batch', color: '#788f32', extensions: ['.bat'] },
15
- { id: 'dockerfile', label: 'Dockerfile', color: '#4f9a94', names: ['dockerfile', 'containerfile'], extensions: ['.dockerfile'] },
16
+ { id: 'dockerfile', label: 'Dockerfile', color: '#4f9a94', names: ['dockerfile', 'containerfile'],
17
+ extensions: ['.dockerfile'], syntax: dockerfileSyntax },
16
18
  { id: 'python', label: 'Python', color: '#3572a5', extensions: ['.py'] },
17
19
  { id: 'go', label: 'Go', color: '#00add8', extensions: ['.go'] },
18
20
  { id: 'go-module', label: 'Go module', color: '#008ba8', exactNames: ['go.mod'] },
@@ -22,9 +24,10 @@ export const fileTypes = [
22
24
  { id: 'kotlin', label: 'Kotlin', color: '#a97bff', extensions: ['.kt', '.kts'] },
23
25
  { id: 'ruby', label: 'Ruby', color: '#701516', extensions: ['.rb'] },
24
26
  { id: 'php', label: 'PHP', color: '#4f5d95', extensions: ['.php'] },
27
+ { id: 'hcl', label: 'HCL', color: '#844fba', extensions: ['.hcl', '.tf', '.tfvars'] },
25
28
  { id: 'csharp', label: 'C#', color: '#178600', extensions: ['.cs'] },
26
- { id: 'c', label: 'C', color: '#555555', extensions: ['.c', '.h'] },
27
- { id: 'cpp', label: 'C++', color: '#f34b7d', extensions: ['.cc', '.cpp', '.cxx', '.hpp', '.hh'] },
29
+ { id: 'c', label: 'C', color: '#555555', extensions: ['.c', '.h'], syntax: cSyntax },
30
+ { id: 'cpp', label: 'C++', color: '#f34b7d', extensions: ['.cc', '.cpp', '.cxx', '.hpp', '.hh'], syntax: cppSyntax },
28
31
  { id: 'swift', label: 'Swift', color: '#f05138', extensions: ['.swift'] },
29
32
  { id: 'scala', label: 'Scala', color: '#c22d40', extensions: ['.scala'] },
30
33
  { id: 'vue', label: 'Vue', color: '#41b883', extensions: ['.vue'] },
@@ -4,6 +4,7 @@ import type { IgnoredFinding } from './ignore-rule.js';
4
4
  import type { PluginData } from './plugin-data.js';
5
5
  import type { Subject } from './source.js';
6
6
  export declare const reportSchema = "urn:repometry:schema:report:2";
7
+ export declare const recoveredSourceOperation = "source/recovered";
7
8
  export interface Diagnostic {
8
9
  readonly operation: string;
9
10
  readonly state: 'failed' | 'blocked' | 'partial';
@@ -28,6 +29,7 @@ export interface Report {
28
29
  readonly scope: {
29
30
  readonly files: number;
30
31
  readonly parsed: number;
32
+ readonly recovered?: number;
31
33
  };
32
34
  readonly status: 'complete' | 'partial' | 'not-applicable';
33
35
  readonly measurements: readonly Measurement[];
@@ -1,4 +1,5 @@
1
1
  export const reportSchema = 'urn:repometry:schema:report:2';
2
+ export const recoveredSourceOperation = 'source/recovered';
2
3
  export function validSubjectId(kind, id) {
3
4
  return /^[a-f0-9]{64}$/.test(id) || (kind === 'commit' && /^[a-f0-9]{40}$/.test(id));
4
5
  }