@rulebookguard/symbols 0.0.0-setup.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Juan Carlos Jimenez
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # @rulebookguard/symbols
2
+
3
+ Gives [rulebookguard](https://github.com/JuanCJR/rulebookguard#readme) the exact line ranges of the symbols named in rule `evidence`, with [tree-sitter](https://tree-sitter.github.io/). Install it next to `rulebookguard`:
4
+
5
+ ```sh
6
+ npm install --save-dev rulebookguard @rulebookguard/symbols
7
+ ```
8
+
9
+ Without it, rulebookguard finds the line where a symbol is defined with a heuristic. With it, files in these languages resolve to the full range of the definition, or of the test whose name the entry quotes: JavaScript, TypeScript (and TSX), Python, Go, Java, C# and Rust. Other languages keep using the heuristic.
10
+
11
+ It is kept apart so that `rulebookguard` stays small: the grammars add about 11 MB. They ship inside this package, under `grammars/`, with the license of each grammar; installing it builds nothing.
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014-2023 Max Brunsfeld, Damien Guard, Amaan Qureshi, and contributors.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,23 @@
1
+ (class_declaration name: (identifier) @name) @definition.class
2
+
3
+ (class_declaration (base_list (_) @name)) @reference.class
4
+
5
+ (interface_declaration name: (identifier) @name) @definition.interface
6
+
7
+ (interface_declaration (base_list (_) @name)) @reference.interface
8
+
9
+ (method_declaration name: (identifier) @name) @definition.method
10
+
11
+ (object_creation_expression type: (identifier) @name) @reference.class
12
+
13
+ (type_parameter_constraints_clause (identifier) @name) @reference.class
14
+
15
+ (type_parameter_constraint (type type: (identifier) @name)) @reference.class
16
+
17
+ (variable_declaration type: (identifier) @name) @reference.class
18
+
19
+ (invocation_expression function: (member_access_expression name: (identifier) @name)) @reference.send
20
+
21
+ (namespace_declaration name: (identifier) @name) @definition.module
22
+
23
+ (namespace_declaration name: (identifier) @name) @module
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Max Brunsfeld
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,42 @@
1
+ (
2
+ (comment)* @doc
3
+ .
4
+ (function_declaration
5
+ name: (identifier) @name) @definition.function
6
+ (#strip! @doc "^//\\s*")
7
+ (#set-adjacent! @doc @definition.function)
8
+ )
9
+
10
+ (
11
+ (comment)* @doc
12
+ .
13
+ (method_declaration
14
+ name: (field_identifier) @name) @definition.method
15
+ (#strip! @doc "^//\\s*")
16
+ (#set-adjacent! @doc @definition.method)
17
+ )
18
+
19
+ (call_expression
20
+ function: [
21
+ (identifier) @name
22
+ (parenthesized_expression (identifier) @name)
23
+ (selector_expression field: (field_identifier) @name)
24
+ (parenthesized_expression (selector_expression field: (field_identifier) @name))
25
+ ]) @reference.call
26
+
27
+ (type_spec
28
+ name: (type_identifier) @name) @definition.type
29
+
30
+ (type_identifier) @name @reference.type
31
+
32
+ (package_clause "package" (package_identifier) @name)
33
+
34
+ (type_declaration (type_spec name: (type_identifier) @name type: (interface_type)))
35
+
36
+ (type_declaration (type_spec name: (type_identifier) @name type: (struct_type)))
37
+
38
+ (import_declaration (import_spec) @name)
39
+
40
+ (var_declaration (var_spec name: (identifier) @name))
41
+
42
+ (const_declaration (const_spec name: (identifier) @name))
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Ayman Nadeem
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,20 @@
1
+ (class_declaration
2
+ name: (identifier) @name) @definition.class
3
+
4
+ (method_declaration
5
+ name: (identifier) @name) @definition.method
6
+
7
+ (method_invocation
8
+ name: (identifier) @name
9
+ arguments: (argument_list) @reference.call)
10
+
11
+ (interface_declaration
12
+ name: (identifier) @name) @definition.interface
13
+
14
+ (type_list
15
+ (type_identifier) @name) @reference.implementation
16
+
17
+ (object_creation_expression
18
+ type: (type_identifier) @name) @reference.class
19
+
20
+ (superclass (type_identifier) @name) @reference.class
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Max Brunsfeld
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,99 @@
1
+ (
2
+ (comment)* @doc
3
+ .
4
+ (method_definition
5
+ name: (property_identifier) @name) @definition.method
6
+ (#not-eq? @name "constructor")
7
+ (#strip! @doc "^[\\s\\*/]+|^[\\s\\*/]$")
8
+ (#select-adjacent! @doc @definition.method)
9
+ )
10
+
11
+ (
12
+ (comment)* @doc
13
+ .
14
+ [
15
+ (class
16
+ name: (_) @name)
17
+ (class_declaration
18
+ name: (_) @name)
19
+ ] @definition.class
20
+ (#strip! @doc "^[\\s\\*/]+|^[\\s\\*/]$")
21
+ (#select-adjacent! @doc @definition.class)
22
+ )
23
+
24
+ (
25
+ (comment)* @doc
26
+ .
27
+ [
28
+ (function_expression
29
+ name: (identifier) @name)
30
+ (function_declaration
31
+ name: (identifier) @name)
32
+ (generator_function
33
+ name: (identifier) @name)
34
+ (generator_function_declaration
35
+ name: (identifier) @name)
36
+ ] @definition.function
37
+ (#strip! @doc "^[\\s\\*/]+|^[\\s\\*/]$")
38
+ (#select-adjacent! @doc @definition.function)
39
+ )
40
+
41
+ (
42
+ (comment)* @doc
43
+ .
44
+ (lexical_declaration
45
+ (variable_declarator
46
+ name: (identifier) @name
47
+ value: [(arrow_function) (function_expression)]) @definition.function)
48
+ (#strip! @doc "^[\\s\\*/]+|^[\\s\\*/]$")
49
+ (#select-adjacent! @doc @definition.function)
50
+ )
51
+
52
+ (
53
+ (comment)* @doc
54
+ .
55
+ (variable_declaration
56
+ (variable_declarator
57
+ name: (identifier) @name
58
+ value: [(arrow_function) (function_expression)]) @definition.function)
59
+ (#strip! @doc "^[\\s\\*/]+|^[\\s\\*/]$")
60
+ (#select-adjacent! @doc @definition.function)
61
+ )
62
+
63
+ (assignment_expression
64
+ left: [
65
+ (identifier) @name
66
+ (member_expression
67
+ property: (property_identifier) @name)
68
+ ]
69
+ right: [(arrow_function) (function_expression)]
70
+ ) @definition.function
71
+
72
+ (pair
73
+ key: (property_identifier) @name
74
+ value: [(arrow_function) (function_expression)]) @definition.function
75
+
76
+ (
77
+ (call_expression
78
+ function: (identifier) @name) @reference.call
79
+ (#not-match? @name "^(require)$")
80
+ )
81
+
82
+ (call_expression
83
+ function: (member_expression
84
+ property: (property_identifier) @name)
85
+ arguments: (_) @reference.call)
86
+
87
+ (new_expression
88
+ constructor: (_) @name) @reference.class
89
+
90
+ (export_statement value: (assignment_expression left: (identifier) @name right: ([
91
+ (number)
92
+ (string)
93
+ (identifier)
94
+ (undefined)
95
+ (null)
96
+ (new_expression)
97
+ (binary_expression)
98
+ (call_expression)
99
+ ]))) @definition.constant
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Max Brunsfeld
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,14 @@
1
+ (module (expression_statement (assignment left: (identifier) @name) @definition.constant))
2
+
3
+ (class_definition
4
+ name: (identifier) @name) @definition.class
5
+
6
+ (function_definition
7
+ name: (identifier) @name) @definition.function
8
+
9
+ (call
10
+ function: [
11
+ (identifier) @name
12
+ (attribute
13
+ attribute: (identifier) @name)
14
+ ]) @reference.call
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Maxim Sokolov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,60 @@
1
+ ; ADT definitions
2
+
3
+ (struct_item
4
+ name: (type_identifier) @name) @definition.class
5
+
6
+ (enum_item
7
+ name: (type_identifier) @name) @definition.class
8
+
9
+ (union_item
10
+ name: (type_identifier) @name) @definition.class
11
+
12
+ ; type aliases
13
+
14
+ (type_item
15
+ name: (type_identifier) @name) @definition.class
16
+
17
+ ; method definitions
18
+
19
+ (declaration_list
20
+ (function_item
21
+ name: (identifier) @name) @definition.method)
22
+
23
+ ; function definitions
24
+
25
+ (function_item
26
+ name: (identifier) @name) @definition.function
27
+
28
+ ; trait definitions
29
+ (trait_item
30
+ name: (type_identifier) @name) @definition.interface
31
+
32
+ ; module definitions
33
+ (mod_item
34
+ name: (identifier) @name) @definition.module
35
+
36
+ ; macro definitions
37
+
38
+ (macro_definition
39
+ name: (identifier) @name) @definition.macro
40
+
41
+ ; references
42
+
43
+ (call_expression
44
+ function: (identifier) @name) @reference.call
45
+
46
+ (call_expression
47
+ function: (field_expression
48
+ field: (field_identifier) @name)) @reference.call
49
+
50
+ (macro_invocation
51
+ macro: (identifier) @name) @reference.call
52
+
53
+ ; implementations
54
+
55
+ (impl_item
56
+ trait: (type_identifier) @name) @reference.implementation
57
+
58
+ (impl_item
59
+ type: (type_identifier) @name
60
+ !trait) @reference.implementation
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Max Brunsfeld
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,23 @@
1
+ (function_signature
2
+ name: (identifier) @name) @definition.function
3
+
4
+ (method_signature
5
+ name: (property_identifier) @name) @definition.method
6
+
7
+ (abstract_method_signature
8
+ name: (property_identifier) @name) @definition.method
9
+
10
+ (abstract_class_declaration
11
+ name: (type_identifier) @name) @definition.class
12
+
13
+ (module
14
+ name: (identifier) @name) @definition.module
15
+
16
+ (interface_declaration
17
+ name: (type_identifier) @name) @definition.interface
18
+
19
+ (type_annotation
20
+ (type_identifier) @name) @reference.type
21
+
22
+ (new_expression
23
+ constructor: (identifier) @name) @reference.class
package/grammars.mjs ADDED
@@ -0,0 +1,66 @@
1
+ // The grammar files this package ships, by language: the parser and the tags queries that name
2
+ // its definitions. TypeScript extends JavaScript, so its tags are JavaScript's plus its own.
3
+ const javascriptTags = ['tree-sitter-javascript', 'queries/tags.scm'];
4
+ const typescriptTags = ['tree-sitter-typescript', 'queries/tags.scm'];
5
+
6
+ export const GRAMMARS = {
7
+ javascript: {
8
+ wasm: ['tree-sitter-javascript', 'tree-sitter-javascript.wasm'],
9
+ tags: [javascriptTags],
10
+ },
11
+ typescript: {
12
+ wasm: ['tree-sitter-typescript', 'tree-sitter-typescript.wasm'],
13
+ tags: [javascriptTags, typescriptTags],
14
+ },
15
+ tsx: {
16
+ wasm: ['tree-sitter-typescript', 'tree-sitter-tsx.wasm'],
17
+ tags: [javascriptTags, typescriptTags],
18
+ },
19
+ python: {
20
+ wasm: ['tree-sitter-python', 'tree-sitter-python.wasm'],
21
+ tags: [['tree-sitter-python', 'queries/tags.scm']],
22
+ },
23
+ go: {
24
+ wasm: ['tree-sitter-go', 'tree-sitter-go.wasm'],
25
+ tags: [['tree-sitter-go', 'queries/tags.scm']],
26
+ },
27
+ java: {
28
+ wasm: ['tree-sitter-java', 'tree-sitter-java.wasm'],
29
+ tags: [['tree-sitter-java', 'queries/tags.scm']],
30
+ },
31
+ csharp: {
32
+ wasm: ['tree-sitter-c-sharp', 'tree-sitter-c_sharp.wasm'],
33
+ tags: [['tree-sitter-c-sharp', 'queries/tags.scm']],
34
+ },
35
+ rust: {
36
+ wasm: ['tree-sitter-rust', 'tree-sitter-rust.wasm'],
37
+ tags: [['tree-sitter-rust', 'queries/tags.scm']],
38
+ },
39
+ };
40
+
41
+ export const EXTENSIONS = {
42
+ '.js': 'javascript',
43
+ '.mjs': 'javascript',
44
+ '.cjs': 'javascript',
45
+ '.jsx': 'javascript',
46
+ '.ts': 'typescript',
47
+ '.mts': 'typescript',
48
+ '.cts': 'typescript',
49
+ '.tsx': 'tsx',
50
+ '.py': 'python',
51
+ '.go': 'go',
52
+ '.java': 'java',
53
+ '.cs': 'csharp',
54
+ '.rs': 'rust',
55
+ };
56
+
57
+ /** Every [package, file] pair the grammars need, once each, with each package's license. */
58
+ export function grammarFiles() {
59
+ const seen = new Map();
60
+ for (const { wasm, tags } of Object.values(GRAMMARS)) {
61
+ for (const pair of [wasm, ...tags, [wasm[0], 'LICENSE']]) {
62
+ seen.set(pair.join('/'), pair);
63
+ }
64
+ }
65
+ return [...seen.values()];
66
+ }
package/index.d.mts ADDED
@@ -0,0 +1,26 @@
1
+ export interface LineSpan {
2
+ /** 1-based first line. */
3
+ line: number;
4
+ /** 1-based last line, inclusive. */
5
+ endLine: number;
6
+ }
7
+
8
+ export interface SymbolDefinition extends LineSpan {
9
+ name: string;
10
+ }
11
+
12
+ export interface SourceSymbols {
13
+ /** The definitions the language's tags query names, by first line. */
14
+ definitions: SymbolDefinition[];
15
+ /** The calls holding `text` as a whole string literal, such as a test with that name. */
16
+ calls(text: string): LineSpan[];
17
+ }
18
+
19
+ /** Whether `file`'s language has a grammar in this package. */
20
+ export declare function supports(file: string): boolean;
21
+
22
+ /** The symbols of `source`, or undefined when the language of `file` is not supported. */
23
+ export declare function readSymbols(
24
+ file: string,
25
+ source: string,
26
+ ): Promise<SourceSymbols | undefined>;
package/index.mjs ADDED
@@ -0,0 +1,105 @@
1
+ import { existsSync } from 'node:fs';
2
+ import { readFile } from 'node:fs/promises';
3
+ import { createRequire } from 'node:module';
4
+ import { dirname, extname, join } from 'node:path';
5
+ import { fileURLToPath } from 'node:url';
6
+ import { Language, Parser, Query } from 'web-tree-sitter';
7
+ import { EXTENSIONS, GRAMMARS } from './grammars.mjs';
8
+
9
+ const here = dirname(fileURLToPath(import.meta.url));
10
+ const require = createRequire(import.meta.url);
11
+
12
+ // A published package carries its grammar files in grammars/ (see copy-grammars.mjs); a workspace
13
+ // checkout reads them from the grammar packages it has as dev dependencies.
14
+ function grammarFile(name, file) {
15
+ const shipped = join(here, 'grammars', name, file);
16
+ return existsSync(shipped)
17
+ ? shipped
18
+ : join(dirname(require.resolve(`${name}/package.json`)), file);
19
+ }
20
+
21
+ let ready;
22
+ const loaded = new Map();
23
+
24
+ function load(language) {
25
+ let grammar = loaded.get(language);
26
+ if (grammar === undefined) {
27
+ grammar = (async () => {
28
+ ready ??= Parser.init();
29
+ await ready;
30
+ const { wasm, tags } = GRAMMARS[language];
31
+ const parsed = await Language.load(grammarFile(...wasm));
32
+ const sources = await Promise.all(tags.map((pair) => readFile(grammarFile(...pair), 'utf8')));
33
+ return { language: parsed, query: new Query(parsed, sources.join('\n')) };
34
+ })();
35
+ loaded.set(language, grammar);
36
+ }
37
+ return grammar;
38
+ }
39
+
40
+ /** Whether `file`'s language has a grammar here. */
41
+ export function supports(file) {
42
+ return Object.hasOwn(EXTENSIONS, extname(file).toLowerCase());
43
+ }
44
+
45
+ const STRING = /string(?:_literal)?$/;
46
+ const CALL = /call|invocation/;
47
+ const QUOTES = /^[a-zA-Z]*(?:"""|'''|["'`])|(?:"""|'''|["'`])$/g;
48
+
49
+ function span(node) {
50
+ return { line: node.startPosition.row + 1, endLine: node.endPosition.row + 1 };
51
+ }
52
+
53
+ /** The definitions and string calls of `source`, or undefined when its language is not supported. */
54
+ export async function readSymbols(file, source) {
55
+ const language = EXTENSIONS[extname(file).toLowerCase()];
56
+ if (language === undefined) {
57
+ return undefined;
58
+ }
59
+ const grammar = await load(language);
60
+ const parser = new Parser();
61
+ parser.setLanguage(grammar.language);
62
+ const tree = parser.parse(source);
63
+ if (tree === null) {
64
+ parser.delete();
65
+ return undefined;
66
+ }
67
+ try {
68
+ const definitions = [];
69
+ for (const match of grammar.query.matches(tree.rootNode)) {
70
+ const name = match.captures.find((capture) => capture.name === 'name');
71
+ const definition = match.captures.find((capture) => capture.name.startsWith('definition.'));
72
+ if (name !== undefined && definition !== undefined) {
73
+ definitions.push({ name: name.node.text, ...span(definition.node) });
74
+ }
75
+ }
76
+ definitions.sort((a, b) => a.line - b.line || b.endLine - a.endLine);
77
+
78
+ const strings = [];
79
+ const visit = (node) => {
80
+ if (STRING.test(node.type)) {
81
+ let call = node.parent;
82
+ while (call !== null && !CALL.test(call.type)) {
83
+ call = call.parent;
84
+ }
85
+ strings.push({ text: node.text.replace(QUOTES, ''), ...span(call ?? node) });
86
+ return;
87
+ }
88
+ for (const child of node.namedChildren) {
89
+ visit(child);
90
+ }
91
+ };
92
+ visit(tree.rootNode);
93
+
94
+ return {
95
+ definitions,
96
+ calls: (text) =>
97
+ strings
98
+ .filter((string) => string.text === text)
99
+ .map(({ line, endLine }) => ({ line, endLine })),
100
+ };
101
+ } finally {
102
+ tree.delete();
103
+ parser.delete();
104
+ }
105
+ }
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@rulebookguard/symbols",
3
+ "version": "0.0.0-setup.0",
4
+ "description": "Tree-sitter symbol ranges for the evidence of rulebookguard rules.",
5
+ "homepage": "https://github.com/JuanCJR/rulebookguard#readme",
6
+ "bugs": {
7
+ "url": "https://github.com/JuanCJR/rulebookguard/issues"
8
+ },
9
+ "license": "MIT",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/JuanCJR/rulebookguard.git",
13
+ "directory": "packages/symbols"
14
+ },
15
+ "files": [
16
+ "index.mjs",
17
+ "index.d.mts",
18
+ "grammars.mjs",
19
+ "grammars"
20
+ ],
21
+ "type": "module",
22
+ "exports": {
23
+ ".": {
24
+ "types": "./index.d.mts",
25
+ "default": "./index.mjs"
26
+ },
27
+ "./package.json": "./package.json"
28
+ },
29
+ "scripts": {
30
+ "prepack": "node copy-grammars.mjs"
31
+ },
32
+ "dependencies": {
33
+ "web-tree-sitter": "0.27.1"
34
+ },
35
+ "devDependencies": {
36
+ "tree-sitter-c-sharp": "0.23.5",
37
+ "tree-sitter-go": "0.25.0",
38
+ "tree-sitter-java": "0.23.5",
39
+ "tree-sitter-javascript": "0.25.0",
40
+ "tree-sitter-python": "0.25.0",
41
+ "tree-sitter-rust": "0.24.0",
42
+ "tree-sitter-typescript": "0.23.2"
43
+ },
44
+ "engines": {
45
+ "node": ">=22"
46
+ }
47
+ }