@repometry/tree-sitter 0.3.0 → 0.5.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/tree-sitter",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "description": "The tree-sitter language plugin engine for Repometry",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -38,7 +38,7 @@
38
38
  "prepack": "npm run build"
39
39
  },
40
40
  "dependencies": {
41
- "@repometry/core": "0.4.0",
41
+ "@repometry/core": "0.6.0",
42
42
  "web-tree-sitter": "0.27.2"
43
43
  }
44
44
  }
@@ -1,3 +1,4 @@
1
+ import { ruleFor } from './language.js';
1
2
  import { expressionOperators, logicalOperator } from './logic.js';
2
3
  export class Control {
3
4
  walk;
@@ -53,8 +54,12 @@ export class Control {
53
54
  conditional(node, flow, depth, chained) {
54
55
  flow.block(node, depth, chained);
55
56
  const work = [];
56
- const rule = this.walk.grammar.rules[node.type];
57
+ const rule = ruleFor(this.walk.grammar, node);
57
58
  const alternate = rule?.alternative?.(node);
59
+ const children = rule?.conditionalChildren?.(node);
60
+ if (children) {
61
+ return this.walk.next(children.filter(child => !this.walk.ignored(child.type)).map(child => child.id === alternate?.id ? this.alternative(child, flow, depth) : { node: child, flow, depth: depth + 1 }));
62
+ }
58
63
  for (let index = 0; index < node.childCount; index++) {
59
64
  const child = node.child(index);
60
65
  const explicitAlternative = child.id === alternate?.id;
@@ -71,7 +76,7 @@ export class Control {
71
76
  this.walk.next(work);
72
77
  }
73
78
  alternative(node, flow, depth) {
74
- const rule = this.walk.grammar.rules[node.type];
79
+ const rule = ruleFor(this.walk.grammar, node);
75
80
  const chained = rule?.chainedAlternative ? this.chainedChild(node) : undefined;
76
81
  if (chained)
77
82
  return { node: chained, flow, depth, chained: true };
@@ -87,13 +92,13 @@ export class Control {
87
92
  }
88
93
  chainedChild(node) {
89
94
  const [child, sibling] = this.walk.named(node);
90
- return child && sibling === undefined && this.walk.grammar.rules[child.type]?.flow === 'if' ? child : undefined;
95
+ return child && sibling === undefined && ruleFor(this.walk.grammar, child)?.flow === 'if' ? child : undefined;
91
96
  }
92
97
  sequence(node, flow, depth) {
93
98
  const work = [];
94
99
  const values = [];
95
100
  for (const child of this.walk.named(node)) {
96
- const kind = this.walk.grammar.rules[child.type]?.flow;
101
+ const kind = ruleFor(this.walk.grammar, child)?.flow;
97
102
  if (kind === 'clause' || kind === 'filter') {
98
103
  const at = depth++;
99
104
  work.push(() => {
@@ -1,3 +1,4 @@
1
+ import { ruleFor } from './language.js';
1
2
  export class Flow {
2
3
  cyclomatic = 1;
3
4
  cognitive = 0;
@@ -60,9 +61,9 @@ export class Traversal {
60
61
  return this.grammar.comments.has(type) || this.grammar.extras.has(type);
61
62
  }
62
63
  children(node, flow, depth, parent, logical) {
63
- const ordered = this.grammar.rules[node.type]?.order;
64
+ const ordered = ruleFor(this.grammar, node)?.order;
64
65
  const prepare = (child) => {
65
- if (this.grammar.rules[child.type]?.flow !== 'filter')
66
+ if (ruleFor(this.grammar, child)?.flow !== 'filter')
66
67
  return { node: child, flow, depth, parent, logical };
67
68
  const at = depth++;
68
69
  return () => {
@@ -12,9 +12,13 @@ declare global {
12
12
  export interface TokenRules {
13
13
  readonly candidates: ReadonlySet<string>;
14
14
  readonly kinds: ReadonlyMap<string, string>;
15
+ /** Opaque literal tokens whose descendants may contain source comments. */
16
+ readonly literalsWithComments?: ReadonlySet<string>;
15
17
  readonly normalize?: (type: string, text: string, kind: string | undefined) => readonly [string, string | undefined];
16
18
  }
17
19
  export interface NodeRule {
20
+ /** Resolve a semantic rule when multiple constructs share a syntax node type. */
21
+ readonly select?: (node: Node) => NodeRule | undefined;
18
22
  readonly flow?: 'function' | 'class' | 'decorated' | 'if' | 'elif' | 'else' | 'nest' | 'sequence' | 'clause' | 'filter';
19
23
  readonly decision?: true | ((node: Node) => number);
20
24
  readonly exit?: 'return' | 'label';
@@ -25,6 +29,8 @@ export interface NodeRule {
25
29
  readonly requiresBody?: boolean;
26
30
  /** Lexical parameter count when the grammar does not expose a list of formal parameters. */
27
31
  readonly parameters?: (node: Node) => number | undefined;
32
+ /** Conditional members in source order when an ordinary child wraps both branches. */
33
+ readonly conditionalChildren?: (node: Node) => readonly Node[];
28
34
  /** Alternate branch when the grammar does not expose an alternative field. */
29
35
  readonly alternative?: (node: Node) => Node | null;
30
36
  /** Authored alternatives stored in one transparent conditional child. */
@@ -57,6 +63,10 @@ export interface Grammar {
57
63
  readonly extras: ReadonlySet<string>;
58
64
  readonly logicalTypes: ReadonlySet<string>;
59
65
  readonly logicalOperators: ReadonlySet<string>;
66
+ /** Match named operator tokens by source spelling. */
67
+ readonly logicalOperatorSpelling?: true;
68
+ /** Logical operator tokens are named direct children and use their node types. */
69
+ readonly namedLogicalOperators?: true;
60
70
  readonly logicalCognitive: 'parent' | 'sequence';
61
71
  /** A parameters field with one of these node types is a single parameter. */
62
72
  readonly singleParameterTypes?: ReadonlySet<string>;
@@ -70,3 +80,4 @@ export interface Grammar {
70
80
  readonly tokens: TokenRules;
71
81
  }
72
82
  export declare const measures: readonly ['functions', 'cyclomaticComplexity', 'cognitiveComplexity', 'commentLines', 'codeLines', 'structuralRules', 'duplication'];
83
+ export declare function ruleFor(grammar: Grammar, node: Node): NodeRule | undefined;
@@ -1,3 +1,7 @@
1
1
  export const measures = ['functions', 'cyclomaticComplexity', 'cognitiveComplexity', 'commentLines', 'codeLines',
2
2
  'structuralRules', 'duplication'];
3
+ export function ruleFor(grammar, node) {
4
+ const rule = grammar.rules[node.type];
5
+ return rule?.select ? rule.select(node) : rule;
6
+ }
3
7
  //# sourceMappingURL=language.js.map
@@ -1,8 +1,12 @@
1
1
  export function logicalOperator(node, grammar) {
2
2
  if (!grammar.logicalTypes.has(node.type))
3
3
  return undefined;
4
- const operator = node.childForFieldName('operator') ?? node.children.find(child => !child.isNamed);
5
- return operator && grammar.logicalOperators.has(operator.type) ? operator.type : undefined;
4
+ const operator = node.childForFieldName('operator') ?? node.children.find(child => grammar.namedLogicalOperators
5
+ ? grammar.logicalOperators.has(child.type) : !child.isNamed);
6
+ if (!operator)
7
+ return undefined;
8
+ const spelling = grammar.logicalOperatorSpelling ? operator.text : operator.type;
9
+ return grammar.logicalOperators.has(spelling) ? spelling : undefined;
6
10
  }
7
11
  export function expressionOperators(root, grammar) {
8
12
  const sequence = [];
@@ -23,7 +27,8 @@ function operatorWork(node, grammar) {
23
27
  const operator = logicalOperator(node, grammar);
24
28
  if (operator === undefined)
25
29
  return [];
26
- const children = node.namedChildren.filter(child => !grammar.comments.has(child.type) && !grammar.extras.has(child.type));
30
+ const children = node.namedChildren.filter(child => !grammar.comments.has(child.type) && !grammar.extras.has(child.type)
31
+ && !(grammar.namedLogicalOperators && grammar.logicalOperators.has(child.type)));
27
32
  const first = node.childForFieldName('left') ?? children[0];
28
33
  const second = node.childForFieldName('right') ?? children[1];
29
34
  return [...(second ? [second] : []), operator, ...(first ? [first] : [])];
@@ -1,3 +1,4 @@
1
+ import { ruleFor } from './language.js';
1
2
  import { lowerBound } from './source-lines.js';
2
3
  /** Distinguish missing tokens from error intervals because a missing token can sit on a node boundary. */
3
4
  export function damagedSyntax(root) {
@@ -26,7 +27,7 @@ export function damagedSyntax(root) {
26
27
  };
27
28
  }
28
29
  function isDeclaration(node, grammar) {
29
- const rule = grammar.rules[node.type];
30
+ const rule = ruleFor(grammar, node);
30
31
  if (rule?.flow !== 'function' && rule?.flow !== 'class')
31
32
  return false;
32
33
  return !rule.requiresBody || (rule.body?.(node).length ?? Number(node.childForFieldName('body') !== null)) > 0;
@@ -1,3 +1,4 @@
1
+ import { ruleFor } from './language.js';
1
2
  import { Control } from './control.js';
2
3
  import { Flow, Traversal } from './flow.js';
3
4
  import { lowerBound } from './source-lines.js';
@@ -46,7 +47,7 @@ export class Structure {
46
47
  observations: this.observations, ...(this.grammar.language.measures?.includes('classes') ? { classes: this.classes } : {}) };
47
48
  }
48
49
  visit(task) {
49
- const rule = this.grammar.rules[task.node.type];
50
+ const rule = ruleFor(this.grammar, task.node);
50
51
  if (rule?.flow === 'function')
51
52
  this.scope(task, false);
52
53
  else if (rule?.flow === 'class')
@@ -61,10 +62,10 @@ export class Structure {
61
62
  if (!definition)
62
63
  return;
63
64
  const decorators = this.walk.named(task.node).filter(child => child.id !== definition.id);
64
- this.scope({ ...task, node: definition }, this.grammar.rules[definition.type]?.flow === 'class', decorators);
65
+ this.scope({ ...task, node: definition }, ruleFor(this.grammar, definition)?.flow === 'class', decorators);
65
66
  }
66
67
  scope({ node, flow, depth }, isClass, decorators = []) {
67
- const rule = this.grammar.rules[node.type];
68
+ const rule = ruleFor(this.grammar, node);
68
69
  const body = rule?.body?.(node) ?? fieldBody(node);
69
70
  if (rule?.requiresBody && body.length === 0) {
70
71
  this.walk.children(node, flow, depth);
@@ -113,7 +114,7 @@ export class Structure {
113
114
  this.observe('function-complexity', own.cognitive, label + ' · Cognitive complexity', node);
114
115
  }
115
116
  parameters(node) {
116
- const specified = this.grammar.rules[node.type]?.parameters?.(node);
117
+ const specified = ruleFor(this.grammar, node)?.parameters?.(node);
117
118
  if (specified !== undefined)
118
119
  return specified;
119
120
  const list = node.childForFieldName('parameters');
@@ -61,9 +61,9 @@ class TokenScanner {
61
61
  const lines = this.source.classify(this.comments);
62
62
  return { ...lines, clones: this.cloneSource(path, lines.codeLines) };
63
63
  }
64
- advance() {
65
- while (!this.cursor.gotoNextSibling()) {
66
- if (!this.cursor.gotoParent())
64
+ advance(cursor = this.cursor) {
65
+ while (!cursor.gotoNextSibling()) {
66
+ if (!cursor.gotoParent())
67
67
  return false;
68
68
  }
69
69
  return true;
@@ -80,6 +80,8 @@ class TokenScanner {
80
80
  const kind = this.collect ? this.grammar.tokens.kinds.get(type) : undefined;
81
81
  if (!kind && this.cursor.gotoFirstChild())
82
82
  return true;
83
+ if (kind && this.grammar.tokens.literalsWithComments?.has(type))
84
+ this.literalComments();
83
85
  if (this.candidateEnd) {
84
86
  const start = this.cursor.startIndex;
85
87
  if (start < this.candidateEnd)
@@ -87,6 +89,25 @@ class TokenScanner {
87
89
  }
88
90
  return false;
89
91
  }
92
+ literalComments() {
93
+ const cursor = this.cursor.currentNode.walk();
94
+ try {
95
+ if (!cursor.gotoFirstChild())
96
+ return;
97
+ do {
98
+ const type = cursor.nodeType;
99
+ if (this.grammar.comments.has(type))
100
+ this.comments.push(span(cursor));
101
+ else if (!this.grammar.extras.has(type) && cursor.gotoFirstChild())
102
+ continue;
103
+ if (!this.advance(cursor))
104
+ break;
105
+ } while (true);
106
+ }
107
+ finally {
108
+ cursor.delete();
109
+ }
110
+ }
90
111
  candidate() {
91
112
  const range = span(this.cursor);
92
113
  if (this.damaged && this.damaged(range))