@repometry/tree-sitter 0.2.0 → 0.3.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 +2 -2
- package/target/main/ts/control.js +7 -1
- package/target/main/ts/flow.d.ts +2 -0
- package/target/main/ts/flow.js +12 -3
- package/target/main/ts/language.d.ts +5 -0
- package/target/main/ts/recovered-file.d.ts +27 -0
- package/target/main/ts/recovered-file.js +18 -0
- package/target/main/ts/recovery.d.ts +14 -0
- package/target/main/ts/recovery.js +59 -0
- package/target/main/ts/structure.d.ts +11 -0
- package/target/main/ts/structure.js +11 -0
- package/target/main/ts/tokens.d.ts +2 -2
- package/target/main/ts/tokens.js +8 -4
- package/target/main/ts/tree-sitter.js +12 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@repometry/tree-sitter",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.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.
|
|
41
|
+
"@repometry/core": "0.4.0",
|
|
42
42
|
"web-tree-sitter": "0.27.2"
|
|
43
43
|
}
|
|
44
44
|
}
|
|
@@ -53,12 +53,18 @@ export class Control {
|
|
|
53
53
|
conditional(node, flow, depth, chained) {
|
|
54
54
|
flow.block(node, depth, chained);
|
|
55
55
|
const work = [];
|
|
56
|
-
const
|
|
56
|
+
const rule = this.walk.grammar.rules[node.type];
|
|
57
|
+
const alternate = rule?.alternative?.(node);
|
|
57
58
|
for (let index = 0; index < node.childCount; index++) {
|
|
58
59
|
const child = node.child(index);
|
|
59
60
|
const explicitAlternative = child.id === alternate?.id;
|
|
60
61
|
if ((!child.isNamed && !explicitAlternative) || this.walk.ignored(child.type))
|
|
61
62
|
continue;
|
|
63
|
+
const branches = rule?.alternativeBranches?.(child);
|
|
64
|
+
if (branches) {
|
|
65
|
+
work.push(...branches.map(branch => this.alternative(branch, flow, depth)));
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
62
68
|
const alternative = explicitAlternative || node.fieldNameForChild(index) === 'alternative';
|
|
63
69
|
work.push(alternative ? this.alternative(child, flow, depth) : { node: child, flow, depth: depth + 1 });
|
|
64
70
|
}
|
package/target/main/ts/flow.d.ts
CHANGED
|
@@ -26,6 +26,8 @@ export declare class Traversal {
|
|
|
26
26
|
private readonly batchChildren;
|
|
27
27
|
constructor(grammar: Grammar, source: SourceLines);
|
|
28
28
|
run(root: Node, module: Flow, visit: (task: Visit) => void): void;
|
|
29
|
+
runScope(node: Node, visit: (task: Visit) => void): void;
|
|
30
|
+
private runWork;
|
|
29
31
|
next(items: readonly Work[]): void;
|
|
30
32
|
named(node: Node): Node[];
|
|
31
33
|
ignored(type: string): boolean;
|
package/target/main/ts/flow.js
CHANGED
|
@@ -25,8 +25,14 @@ export class Traversal {
|
|
|
25
25
|
this.batchChildren = source.text.length >= 256 * 1024;
|
|
26
26
|
}
|
|
27
27
|
run(root, module, visit) {
|
|
28
|
+
this.runWork(() => this.children(root, module, 0), visit);
|
|
29
|
+
}
|
|
30
|
+
runScope(node, visit) {
|
|
31
|
+
this.runWork(() => this.next([{ node, flow: new Flow(), depth: 0 }]), visit);
|
|
32
|
+
}
|
|
33
|
+
runWork(schedule, visit) {
|
|
28
34
|
try {
|
|
29
|
-
|
|
35
|
+
schedule();
|
|
30
36
|
while (this.pending.length) {
|
|
31
37
|
const item = this.pending.pop();
|
|
32
38
|
if (typeof item === 'function')
|
|
@@ -46,7 +52,9 @@ export class Traversal {
|
|
|
46
52
|
this.pending.push(items[index]);
|
|
47
53
|
}
|
|
48
54
|
named(node) {
|
|
49
|
-
|
|
55
|
+
const field = this.grammar.ignoredFields?.[node.type];
|
|
56
|
+
const excluded = field === undefined ? undefined : node.childForFieldName(field)?.id;
|
|
57
|
+
return node.namedChildren.filter(child => child.id !== excluded && !this.ignored(child.type));
|
|
50
58
|
}
|
|
51
59
|
ignored(type) {
|
|
52
60
|
return this.grammar.comments.has(type) || this.grammar.extras.has(type);
|
|
@@ -62,8 +70,9 @@ export class Traversal {
|
|
|
62
70
|
this.children(child, flow, at + 1);
|
|
63
71
|
};
|
|
64
72
|
};
|
|
65
|
-
if (this.batchChildren && !ordered && node.namedChildCount > childBatchSize)
|
|
73
|
+
if (this.batchChildren && !ordered && !this.grammar.ignoredFields?.[node.type] && node.namedChildCount > childBatchSize) {
|
|
66
74
|
return this.batches(node, prepare);
|
|
75
|
+
}
|
|
67
76
|
const names = this.named(node);
|
|
68
77
|
const children = ordered ? ordered.flatMap(index => names[index] ?? []) : names;
|
|
69
78
|
if (!children.length)
|
|
@@ -27,6 +27,8 @@ export interface NodeRule {
|
|
|
27
27
|
readonly parameters?: (node: Node) => number | undefined;
|
|
28
28
|
/** Alternate branch when the grammar does not expose an alternative field. */
|
|
29
29
|
readonly alternative?: (node: Node) => Node | null;
|
|
30
|
+
/** Authored alternatives stored in one transparent conditional child. */
|
|
31
|
+
readonly alternativeBranches?: (child: Node) => readonly Node[] | undefined;
|
|
30
32
|
/** Whether an alternative wrapper contributes its flat cognitive point. */
|
|
31
33
|
readonly elseCognitive?: (node: Node) => boolean;
|
|
32
34
|
/** A wrapper is transparent when its sole named child is a chained conditional. */
|
|
@@ -47,6 +49,9 @@ export interface Grammar {
|
|
|
47
49
|
readonly name: (node: Node) => string | undefined;
|
|
48
50
|
readonly countDeclarations?: (root: Node) => number;
|
|
49
51
|
readonly rules: Readonly<Record<string, NodeRule>>;
|
|
52
|
+
/** A named field carrying syntax outside the executable source, such as a preprocessor condition. */
|
|
53
|
+
readonly ignoredFields?: Readonly<Record<string, string>>;
|
|
54
|
+
readonly recoverDeclarations?: true;
|
|
50
55
|
readonly parens: ReadonlySet<string>;
|
|
51
56
|
readonly comments: ReadonlySet<string>;
|
|
52
57
|
readonly extras: ReadonlySet<string>;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Tree } from 'web-tree-sitter';
|
|
2
|
+
import type { Grammar } from './language.js';
|
|
3
|
+
import type { SourceLines } from './source-lines.js';
|
|
4
|
+
/** Whole-file line facts plus measurable declarations from a syntax-error tree. */
|
|
5
|
+
export declare function extractRecovered(path: string, tree: Tree, source: SourceLines, grammar: Grammar): {
|
|
6
|
+
structure: {
|
|
7
|
+
codeLines: number[];
|
|
8
|
+
commentLines: number;
|
|
9
|
+
lines: number;
|
|
10
|
+
functions: number;
|
|
11
|
+
cognitiveComplexity: number;
|
|
12
|
+
cyclomaticFunctions: import("@repometry/core").FunctionComplexity[];
|
|
13
|
+
cognitiveFunctions: import("@repometry/core").FunctionComplexity[];
|
|
14
|
+
observations: import("@repometry/core/internal/model/code-structure.js").StructuralObservation[];
|
|
15
|
+
classes?: number | undefined;
|
|
16
|
+
clones: import("@repometry/core").CloneSource;
|
|
17
|
+
path: string;
|
|
18
|
+
} | {
|
|
19
|
+
path: string;
|
|
20
|
+
lines: number;
|
|
21
|
+
codeLines: number[];
|
|
22
|
+
commentLines: number;
|
|
23
|
+
};
|
|
24
|
+
omitted: number;
|
|
25
|
+
reason: string;
|
|
26
|
+
location: string;
|
|
27
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { damagedSyntax, recoveredScopes } from './recovery.js';
|
|
2
|
+
import { Structure } from './structure.js';
|
|
3
|
+
import { firstSyntaxIssue } from './syntax-issue.js';
|
|
4
|
+
import { scanClones } from './tokens.js';
|
|
5
|
+
/** Whole-file line facts plus measurable declarations from a syntax-error tree. */
|
|
6
|
+
export function extractRecovered(path, tree, source, grammar) {
|
|
7
|
+
const root = tree.rootNode;
|
|
8
|
+
const damaged = damagedSyntax(root);
|
|
9
|
+
const { scopes, omitted } = recoveredScopes(root, grammar, damaged);
|
|
10
|
+
const facts = scanClones(path, tree, source, grammar, scopes.length ? damaged : false);
|
|
11
|
+
const measured = scopes.length ? new Structure(grammar, source, facts.codeLines).measureRecovered(scopes) : undefined;
|
|
12
|
+
const structure = measured && (measured.functions || measured.classes)
|
|
13
|
+
? { path, ...measured, ...facts }
|
|
14
|
+
: { path, lines: source.count, codeLines: facts.codeLines, commentLines: facts.commentLines };
|
|
15
|
+
const { reason, offset } = firstSyntaxIssue(root, grammar);
|
|
16
|
+
return { structure, omitted, reason, location: source.location(offset, grammar.columns) };
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=recovered-file.js.map
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Node } from 'web-tree-sitter';
|
|
2
|
+
import type { Grammar } from './language.js';
|
|
3
|
+
import type { Span } from './source-lines.js';
|
|
4
|
+
export interface RecoveredScope {
|
|
5
|
+
readonly node: Node;
|
|
6
|
+
readonly parents: readonly string[];
|
|
7
|
+
}
|
|
8
|
+
/** Distinguish missing tokens from error intervals because a missing token can sit on a node boundary. */
|
|
9
|
+
export declare function damagedSyntax(root: Node): (span: Span) => boolean;
|
|
10
|
+
/** Select maximal intact declarations. Their nested declarations are measured by the normal structure traversal. */
|
|
11
|
+
export declare function recoveredScopes(root: Node, grammar: Grammar, damaged: (span: Span) => boolean): {
|
|
12
|
+
scopes: RecoveredScope[];
|
|
13
|
+
omitted: number;
|
|
14
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { lowerBound } from './source-lines.js';
|
|
2
|
+
/** Distinguish missing tokens from error intervals because a missing token can sit on a node boundary. */
|
|
3
|
+
export function damagedSyntax(root) {
|
|
4
|
+
const intervals = [];
|
|
5
|
+
const missing = [];
|
|
6
|
+
const pending = [root];
|
|
7
|
+
while (pending.length) {
|
|
8
|
+
const node = pending.pop();
|
|
9
|
+
const children = node.children.filter(child => child.hasError || child.isError || child.isMissing);
|
|
10
|
+
if (node.isMissing)
|
|
11
|
+
missing.push(node.startIndex);
|
|
12
|
+
else if (node.isError || !children.length)
|
|
13
|
+
intervals.push({ start: node.startIndex, end: node.endIndex });
|
|
14
|
+
pending.push(...children);
|
|
15
|
+
}
|
|
16
|
+
intervals.sort((left, right) => left.start - right.start);
|
|
17
|
+
missing.sort((left, right) => left - right);
|
|
18
|
+
const starts = intervals.map(interval => interval.start);
|
|
19
|
+
let lastEnd = 0;
|
|
20
|
+
const ends = intervals.map(interval => lastEnd = Math.max(lastEnd, interval.end));
|
|
21
|
+
return ({ start, end }) => {
|
|
22
|
+
const previous = lowerBound(starts, end) - 1;
|
|
23
|
+
const point = missing[lowerBound(missing, start)];
|
|
24
|
+
// An error can swallow a modifier immediately before the declaration.
|
|
25
|
+
return (ends[previous] ?? -1) >= start || (point !== undefined && point <= end);
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function isDeclaration(node, grammar) {
|
|
29
|
+
const rule = grammar.rules[node.type];
|
|
30
|
+
if (rule?.flow !== 'function' && rule?.flow !== 'class')
|
|
31
|
+
return false;
|
|
32
|
+
return !rule.requiresBody || (rule.body?.(node).length ?? Number(node.childForFieldName('body') !== null)) > 0;
|
|
33
|
+
}
|
|
34
|
+
/** Select maximal intact declarations. Their nested declarations are measured by the normal structure traversal. */
|
|
35
|
+
export function recoveredScopes(root, grammar, damaged) {
|
|
36
|
+
const scopes = [];
|
|
37
|
+
const pending = [{ node: root, parents: [] }];
|
|
38
|
+
let omitted = 0;
|
|
39
|
+
while (pending.length) {
|
|
40
|
+
const scope = pending.pop();
|
|
41
|
+
const { node, parents } = scope;
|
|
42
|
+
let context = parents;
|
|
43
|
+
if (isDeclaration(node, grammar)) {
|
|
44
|
+
if (!damaged({ start: node.startIndex, end: node.endIndex })) {
|
|
45
|
+
scopes.push(scope);
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
omitted++;
|
|
49
|
+
const name = grammar.name(node);
|
|
50
|
+
if (name)
|
|
51
|
+
context = [...parents, name];
|
|
52
|
+
}
|
|
53
|
+
const children = node.namedChildren;
|
|
54
|
+
for (let index = children.length - 1; index >= 0; index--)
|
|
55
|
+
pending.push({ node: children[index], parents: context });
|
|
56
|
+
}
|
|
57
|
+
return { scopes, omitted };
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=recovery.js.map
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Node } from 'web-tree-sitter';
|
|
2
2
|
import type { FunctionComplexity, StructuralObservation } from '@repometry/core/internal/model/code-structure.js';
|
|
3
3
|
import type { Grammar } from './language.js';
|
|
4
|
+
import type { RecoveredScope } from './recovery.js';
|
|
4
5
|
import type { SourceLines } from './source-lines.js';
|
|
5
6
|
export declare class Structure {
|
|
6
7
|
private readonly grammar;
|
|
@@ -25,6 +26,16 @@ export declare class Structure {
|
|
|
25
26
|
observations: StructuralObservation[];
|
|
26
27
|
classes?: number | undefined;
|
|
27
28
|
};
|
|
29
|
+
measureRecovered(scopes: readonly RecoveredScope[]): {
|
|
30
|
+
lines: number;
|
|
31
|
+
functions: number;
|
|
32
|
+
cognitiveComplexity: number;
|
|
33
|
+
cyclomaticFunctions: FunctionComplexity[];
|
|
34
|
+
cognitiveFunctions: FunctionComplexity[];
|
|
35
|
+
observations: StructuralObservation[];
|
|
36
|
+
classes?: number | undefined;
|
|
37
|
+
};
|
|
38
|
+
private result;
|
|
28
39
|
private visit;
|
|
29
40
|
private decorated;
|
|
30
41
|
private scope;
|
|
@@ -27,6 +27,17 @@ export class Structure {
|
|
|
27
27
|
this.finish(module, this.grammar.moduleDepthLabel);
|
|
28
28
|
const position = this.grammar.reportFileRange ? { line: 1, endLine: this.lines.count } : {};
|
|
29
29
|
this.observations.push({ rule: 'file-complexity', value: this.cognitive, label: 'File cognitive complexity', ...position });
|
|
30
|
+
return this.result();
|
|
31
|
+
}
|
|
32
|
+
measureRecovered(scopes) {
|
|
33
|
+
for (const { node, parents } of scopes) {
|
|
34
|
+
this.names.push(...parents);
|
|
35
|
+
this.walk.runScope(node, task => this.visit(task));
|
|
36
|
+
this.names.length = 0;
|
|
37
|
+
}
|
|
38
|
+
return this.result();
|
|
39
|
+
}
|
|
40
|
+
result() {
|
|
30
41
|
const byPosition = (left, right) => left.line - right.line || left.column - right.column;
|
|
31
42
|
this.cyclomaticFunctions.sort(byPosition);
|
|
32
43
|
this.cognitiveFunctions.sort(byPosition);
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { Tree } from 'web-tree-sitter';
|
|
2
2
|
import type { CloneSource } from '@repometry/core/internal/model/clone-source.js';
|
|
3
3
|
import type { Grammar } from './language.js';
|
|
4
|
-
import type { SourceLines } from './source-lines.js';
|
|
5
|
-
export declare function scanClones(path: string, tree: Tree, source: SourceLines, grammar: Grammar): {
|
|
4
|
+
import type { SourceLines, Span } from './source-lines.js';
|
|
5
|
+
export declare function scanClones(path: string, tree: Tree, source: SourceLines, grammar: Grammar, damaged?: false | ((span: Span) => boolean)): {
|
|
6
6
|
codeLines: number[];
|
|
7
7
|
commentLines: number;
|
|
8
8
|
clones: CloneSource;
|
package/target/main/ts/tokens.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { hash } from 'node:crypto';
|
|
2
2
|
import { duplicationSettings as limits } from '@repometry/core/internal/profile/maintainability.js';
|
|
3
3
|
import { lowerBound } from './source-lines.js';
|
|
4
|
-
export function scanClones(path, tree, source, grammar) {
|
|
4
|
+
export function scanClones(path, tree, source, grammar, damaged) {
|
|
5
5
|
const cursor = tree.walk();
|
|
6
6
|
try {
|
|
7
|
-
return new TokenScanner(cursor, source, grammar).scan(path);
|
|
7
|
+
return new TokenScanner(cursor, source, grammar, damaged).scan(path);
|
|
8
8
|
}
|
|
9
9
|
finally {
|
|
10
10
|
cursor.delete();
|
|
@@ -35,6 +35,7 @@ class TokenScanner {
|
|
|
35
35
|
cursor;
|
|
36
36
|
source;
|
|
37
37
|
grammar;
|
|
38
|
+
damaged;
|
|
38
39
|
comments = [];
|
|
39
40
|
ranges = [];
|
|
40
41
|
starts = [];
|
|
@@ -43,11 +44,12 @@ class TokenScanner {
|
|
|
43
44
|
similar = new FingerprintInput();
|
|
44
45
|
collect;
|
|
45
46
|
candidateEnd = 0;
|
|
46
|
-
constructor(cursor, source, grammar) {
|
|
47
|
+
constructor(cursor, source, grammar, damaged) {
|
|
47
48
|
this.cursor = cursor;
|
|
48
49
|
this.source = source;
|
|
49
50
|
this.grammar = grammar;
|
|
50
|
-
this.
|
|
51
|
+
this.damaged = damaged;
|
|
52
|
+
this.collect = damaged !== false && source.count >= limits.minCodeLines;
|
|
51
53
|
}
|
|
52
54
|
scan(path) {
|
|
53
55
|
do {
|
|
@@ -87,6 +89,8 @@ class TokenScanner {
|
|
|
87
89
|
}
|
|
88
90
|
candidate() {
|
|
89
91
|
const range = span(this.cursor);
|
|
92
|
+
if (this.damaged && this.damaged(range))
|
|
93
|
+
return;
|
|
90
94
|
if (range.end - range.start < limits.minNodes)
|
|
91
95
|
return;
|
|
92
96
|
if (this.source.line(range.end - 1) - this.source.line(range.start) + 1 < limits.minCodeLines)
|
|
@@ -2,7 +2,9 @@ import { readFile } from 'node:fs/promises';
|
|
|
2
2
|
import { extname } from 'node:path';
|
|
3
3
|
import { codeStructure, pluginManifestSchema } from '@repometry/core';
|
|
4
4
|
import { readStructure } from '@repometry/core/internal/engine/author-contracts.js';
|
|
5
|
+
import { recoveredSyntax } from '@repometry/core/internal/model/code-structure.js';
|
|
5
6
|
import { sourceFile } from '@repometry/core/internal/model/snapshot.js';
|
|
7
|
+
import { extractRecovered } from './recovered-file.js';
|
|
6
8
|
import { SourceLines } from './source-lines.js';
|
|
7
9
|
import { Structure } from './structure.js';
|
|
8
10
|
import { firstSyntaxIssue } from './syntax-issue.js';
|
|
@@ -56,10 +58,8 @@ function extract(path, grammar, source, tree) {
|
|
|
56
58
|
if (!tree)
|
|
57
59
|
throw new SyntaxError('No syntax tree at ' + source.location(0, grammar.columns));
|
|
58
60
|
const root = tree.rootNode;
|
|
59
|
-
if (root.hasError)
|
|
60
|
-
|
|
61
|
-
throw new SyntaxError(reason + ' at ' + source.location(offset, grammar.columns));
|
|
62
|
-
}
|
|
61
|
+
if (root.hasError)
|
|
62
|
+
return recoverOrReject(path, grammar, source, tree);
|
|
63
63
|
const header = grammar.requireFirst && firstHeader(root, grammar);
|
|
64
64
|
if (grammar.requireFirst && (!header || header.type !== grammar.requireFirst)) {
|
|
65
65
|
throw new SyntaxError('Expected a ' + grammar.requireFirst.replaceAll('_', ' ') +
|
|
@@ -71,6 +71,14 @@ function extract(path, grammar, source, tree) {
|
|
|
71
71
|
const declarations = grammar.countDeclarations?.(root);
|
|
72
72
|
return { path, ...structure, ...facts, ...(declarations === undefined ? {} : { declarations }) };
|
|
73
73
|
}
|
|
74
|
+
function recoverOrReject(path, grammar, source, tree) {
|
|
75
|
+
if (grammar.recoverDeclarations) {
|
|
76
|
+
const { structure, ...details } = extractRecovered(path, tree, source, grammar);
|
|
77
|
+
return { ...structure, [recoveredSyntax]: details };
|
|
78
|
+
}
|
|
79
|
+
const { offset, reason } = firstSyntaxIssue(tree.rootNode, grammar);
|
|
80
|
+
throw new SyntaxError(reason + ' at ' + source.location(offset, grammar.columns));
|
|
81
|
+
}
|
|
74
82
|
function firstHeader(root, grammar) {
|
|
75
83
|
let node = root.firstNamedChild;
|
|
76
84
|
while (node && grammar.comments.has(node.type))
|