@skathio/somi 5.0.4 → 5.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,7 +13,7 @@
13
13
  "url": "https://github.com/skathio/somi.git"
14
14
  },
15
15
  "description": "A workflow plugin for coding agents: say what you want or pass a ticket, and it picks the right workflow and keeps the cost down, while your agent stays in charge.",
16
- "version": "5.0.4",
16
+ "version": "5.1.0",
17
17
  "tags": [
18
18
  "workflow",
19
19
  "agents",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "somi",
3
- "version": "5.0.4",
3
+ "version": "5.1.0",
4
4
  "displayName": "SoMi",
5
5
  "description": "A workflow plugin for coding agents: say what you want or pass a ticket, and it picks the right workflow and keeps the cost down, while your agent stays in charge.",
6
6
  "keywords": [
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "somi",
3
3
  "displayName": "SoMi",
4
- "version": "5.0.4",
4
+ "version": "5.1.0",
5
5
  "description": "A workflow plugin for coding agents: say what you want or pass a ticket, and it picks the right workflow and keeps the cost down, while your agent stays in charge.",
6
6
  "keywords": [
7
7
  "workflow",
@@ -11,7 +11,7 @@
11
11
  "name": "somi",
12
12
  "source": "github:skathio/somi",
13
13
  "description": "A workflow plugin for coding agents: say what you want or pass a ticket, and it picks the right workflow and keeps the cost down, while your agent stays in charge.",
14
- "version": "5.0.4",
14
+ "version": "5.1.0",
15
15
  "tags": [
16
16
  "workflow",
17
17
  "agents",
package/VERSION CHANGED
@@ -1 +1 @@
1
- 5.0.4
1
+ 5.1.0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@skathio/somi",
3
- "version": "5.0.4",
3
+ "version": "5.1.0",
4
4
  "scripts": {
5
5
  "test": "node --test --test-reporter=spec tests/*.test.mjs",
6
6
  "test:coverage": "node -e \"require('fs').mkdirSync('coverage',{recursive:true})\" && node --test --experimental-test-coverage --test-reporter=spec --test-reporter-destination=stdout --test-reporter=lcov --test-reporter-destination=coverage/lcov.info tests/*.test.mjs",
@@ -43,10 +43,17 @@ export function exemption(ctx, lines, unit, rule) {
43
43
  return { reason: m[2], expires: m[3] };
44
44
  }
45
45
 
46
+ // A function is paired with its old version by name and signature first, so a new overload added above an old one is new, not the
47
+ // old one got different. What is left is paired by name alone, so a function whose parameters changed is still the same function.
46
48
  function pairUnits(before, after) {
47
- const pool = new Map();
48
- for (const u of before) { const k = `${u.kind}:${u.name}`; pool.set(k, [...(pool.get(k) ?? []), u]); }
49
- return after.map((u) => ({ after: u, before: pool.get(`${u.kind}:${u.name}`)?.shift() ?? null }));
49
+ const taken = new Set();
50
+ const match = (u, key) => {
51
+ const old = before.find((b) => !taken.has(b) && key(b) === key(u));
52
+ if (old) taken.add(old);
53
+ return old ?? null;
54
+ };
55
+ const exact = after.map((u) => match(u, (x) => `${x.kind}:${x.name}:${x.signature}`));
56
+ return after.map((u, i) => ({ after: u, before: exact[i] ?? match(u, (x) => `${x.kind}:${x.name}`) }));
50
57
  }
51
58
 
52
59
  const inAdded = (added, line) => added.some(([a, b]) => line >= a && line <= b);
@@ -1,17 +1,21 @@
1
- // quality/metrics.mjs: per-function structure of JavaScript and TypeScript, from a tree-sitter tree. SoMi-CC v1 follows SonarSource's
1
+ // quality/metrics.mjs: per-function structure of JavaScript, TypeScript and C#, from a tree-sitter tree. SoMi-CC v1 follows SonarSource's
2
2
  // cognitive complexity: +1 for each break in linear flow, plus the nesting level for one that is nested, +1 per sequence of like
3
3
  // boolean operators, +1 for recursion, and nothing for shorthand such as ?? or ?. . It deviates in one way, written down so nobody
4
4
  // reads it as Sonar parity: a function nested in another is scored as its own unit, starting at the control-flow nesting around the
5
5
  // point where it is defined. Sonar folds it into the outer function, which would score a test file's describe block as one function.
6
6
 
7
- const FUNCTIONS = new Set(['function_declaration', 'function_expression', 'function', 'arrow_function', 'method_definition', 'generator_function_declaration', 'generator_function']);
8
- const LOOPS = new Set(['for_statement', 'for_in_statement', 'for_of_statement', 'while_statement', 'do_statement']);
7
+ // The C# grammar's node types sit beside the JavaScript ones. An else-if there is an if_statement held directly as the alternative.
8
+ const FUNCTIONS = new Set(['function_declaration', 'function_expression', 'function', 'arrow_function', 'method_definition', 'generator_function_declaration', 'generator_function',
9
+ 'method_declaration', 'constructor_declaration', 'destructor_declaration', 'operator_declaration', 'conversion_operator_declaration', 'local_function_statement', 'lambda_expression', 'anonymous_method_expression']);
10
+ const LOOPS = new Set(['for_statement', 'for_in_statement', 'for_of_statement', 'while_statement', 'do_statement', 'foreach_statement']);
11
+ const TERNARY = new Set(['ternary_expression', 'conditional_expression', 'switch_expression']);
9
12
  const DEPTH = new Set(['if_statement', 'switch_statement', 'try_statement', ...LOOPS]);
10
13
  const BOOLEAN = new Set(['&&', '||']);
11
14
  const JSX = new Set(['jsx_element', 'jsx_self_closing_element', 'jsx_fragment']);
12
15
 
13
16
  const field = (node, name) => node.childForFieldName(name);
14
17
  const same = (a, b) => Boolean(a && b) && a.startIndex === b.startIndex && a.endIndex === b.endIndex && a.type === b.type;
18
+ const isElseIf = (node) => node.type === 'if_statement' && (node.parent?.type === 'else_clause' || same(node, field(node.parent, 'alternative')));
15
19
  const operator = (node) => field(node, 'operator')?.type ?? node.children.find((c) => !c.isNamed && BOOLEAN.has(c.type))?.type;
16
20
 
17
21
  function nameOf(node) {
@@ -28,6 +32,7 @@ function nameOf(node) {
28
32
  function paramCount(node) {
29
33
  if (field(node, 'parameter')) return 1;
30
34
  const params = field(node, 'parameters');
35
+ if (params?.type === 'implicit_parameter') return 1;
31
36
  return params ? params.namedChildren.filter((c) => c.type !== 'comment').length : 0;
32
37
  }
33
38
 
@@ -52,12 +57,13 @@ function hasJsx(node) {
52
57
  // What each structure that breaks linear flow adds, and which of its parts sit one nesting level deeper. An else-if adds one and keeps
53
58
  // the nesting of the if it continues; an else adds one.
54
59
  function ifStatement(node, nesting, ctx) {
55
- const elseIf = node.parent?.type === 'else_clause';
60
+ const elseIf = isElseIf(node);
56
61
  ctx.score += elseIf ? 1 : 1 + nesting;
57
62
  const base = elseIf ? nesting - 1 : nesting;
58
63
  ctx.walk(field(node, 'condition'), base);
59
64
  ctx.walk(field(node, 'consequence'), base + 1);
60
- const inner = field(node, 'alternative')?.namedChildren.find((c) => c.type !== 'comment');
65
+ const alternative = field(node, 'alternative');
66
+ const inner = alternative?.type === 'else_clause' ? alternative.namedChildren.find((c) => c.type !== 'comment') : alternative;
61
67
  if (inner && inner.type !== 'if_statement') ctx.score += 1;
62
68
  ctx.walk(inner, base + 1);
63
69
  }
@@ -71,6 +77,11 @@ const nestsIn = (fields) => (node, nesting, ctx) => {
71
77
  const STRUCTURES = {
72
78
  if_statement: ifStatement,
73
79
  ternary_expression: nestsIn(['consequence', 'alternative']),
80
+ conditional_expression: nestsIn(['consequence', 'alternative']),
81
+ switch_expression: (node, nesting, ctx) => {
82
+ ctx.score += 1 + nesting;
83
+ for (const c of node.namedChildren) ctx.walk(c, c.type === 'switch_expression_arm' ? nesting + 1 : nesting);
84
+ },
74
85
  switch_statement: nestsIn(['body']),
75
86
  catch_clause: nestsIn(['body']),
76
87
  ...Object.fromEntries([...LOOPS].map((type) => [type, nestsIn(['body'])])),
@@ -86,7 +97,7 @@ const recursion = (node, ctx) => {
86
97
  const callee = field(node, 'function');
87
98
  return ctx.name && callee?.type === 'identifier' && callee.text === ctx.name ? 1 : 0;
88
99
  };
89
- const FLAT = { break_statement: labelled, continue_statement: labelled, binary_expression: sequence, call_expression: recursion };
100
+ const FLAT = { break_statement: labelled, continue_statement: labelled, goto_statement: () => 1, binary_expression: sequence, call_expression: recursion, invocation_expression: recursion };
90
101
 
91
102
  // Cognitive complexity of one unit. Nested functions are left out here and scored as their own units by unitsOf.
92
103
  function cognitive(unit, name, startNesting) {
@@ -107,7 +118,7 @@ function maxDepth(unit) {
107
118
  let max = 0;
108
119
  const walk = (node, depth) => {
109
120
  if (node !== unit && FUNCTIONS.has(node.type)) return;
110
- const counts = DEPTH.has(node.type) && !(node.type === 'if_statement' && node.parent?.type === 'else_clause');
121
+ const counts = DEPTH.has(node.type) && !isElseIf(node);
111
122
  const d = counts ? depth + 1 : depth;
112
123
  if (d > max) max = d;
113
124
  for (const c of node.namedChildren) walk(c, d);
@@ -120,7 +131,7 @@ function maxDepth(unit) {
120
131
  function nestingAt(node) {
121
132
  let n = 0;
122
133
  for (let p = node.parent, child = node; p && !FUNCTIONS.has(p.type); child = p, p = p.parent) {
123
- if (LOOPS.has(p.type) || p.type === 'switch_statement' || p.type === 'catch_clause' || p.type === 'ternary_expression') n += 1;
134
+ if (LOOPS.has(p.type) || p.type === 'switch_statement' || p.type === 'catch_clause' || TERNARY.has(p.type)) n += 1;
124
135
  if (p.type === 'if_statement' && !same(child, field(p, 'condition'))) n += 1;
125
136
  }
126
137
  return n;
@@ -143,6 +154,8 @@ export function unitsOf(tree, { file = '', isTest = false } = {}) {
143
154
  cognitive: cognitive(node, name, start),
144
155
  depth: maxDepth(node),
145
156
  params: paramCount(node),
157
+ // C# overloads, and a constructor beside its destructor, share a name: the gate tells them apart by this.
158
+ signature: `${node.type}(${(field(node, 'parameters') ?? field(node, 'parameter'))?.text.replace(/\s+/g, ' ') ?? ''})`,
146
159
  });
147
160
  for (const c of node.namedChildren) visit(c, start);
148
161
  return;
@@ -10,7 +10,7 @@ import { fileURLToPath, pathToFileURL } from 'node:url';
10
10
  const DIR = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', '..', 'vendor', 'tree-sitter');
11
11
  export const MAX_FILE_BYTES = 512 * 1024;
12
12
  export const PARSE_TIMEOUT_MS = 5000;
13
- const LANGUAGES = { '.js': 'javascript', '.mjs': 'javascript', '.cjs': 'javascript', '.jsx': 'javascript', '.ts': 'typescript', '.mts': 'typescript', '.cts': 'typescript', '.tsx': 'tsx', '.css': 'css' };
13
+ const LANGUAGES = { '.js': 'javascript', '.mjs': 'javascript', '.cjs': 'javascript', '.jsx': 'javascript', '.ts': 'typescript', '.mts': 'typescript', '.cts': 'typescript', '.tsx': 'tsx', '.css': 'css', '.cs': 'c-sharp' };
14
14
 
15
15
  /** The grammar a file is parsed with, or null when the gate does not cover its language yet. */
16
16
  export const languageOf = (file) => LANGUAGES[path.extname(file).toLowerCase()] ?? null;
@@ -54,6 +54,15 @@
54
54
  "license": "MIT",
55
55
  "bytes": 128668,
56
56
  "sha256": "8a23977fe271357cce6f254ef88c9bebf3854602d8046605aef6a45c02135c59"
57
+ },
58
+ "tree-sitter-c-sharp.wasm": {
59
+ "package": "@vscode/tree-sitter-wasm",
60
+ "version": "0.3.1",
61
+ "tarball_integrity": "sha512-RJFoomET6FajjG511fmQxeBQfU6M24a0aFZPqpid+ttIxanWf1VGytBG0UmsGjt07qmIPJS8U31D+aecuCucsQ==",
62
+ "repository": "https://github.com/microsoft/vscode-tree-sitter-wasm",
63
+ "license": "MIT",
64
+ "bytes": 5103332,
65
+ "sha256": "d12d85996c25957b4c1b71e26db2d7cc8a294997b60642e9c2a3b031b2c66dd3"
57
66
  }
58
67
  }
59
68
  }