Roo-Code/src/services/tree-sitter/__tests__/parseSourceCodeDefinitions.python.test.ts
KJ7LNW 8e85a121f1
feat: standardize tree-sitter test implementations (#2960)
* refactor: standardize C# Tree-Sitter parser tests

This commit standardizes the C# Tree-Sitter parser tests to ensure:
- Variable/function names in sample code match the tests
- Each test only verifies specific identifiers relevant to that test
- Test names clearly indicate what they're testing
- No skipped tests remain
- Only one test exists per structure or combination of structures
- All debug output uses debugLog from helpers.ts

The standardization improves maintainability and clarity while preserving
all existing functionality.

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>

* refactor: standardize C Tree-Sitter parser tests

This commit standardizes the C Tree-Sitter parser tests to ensure:
- Variable/function names in sample code match the tests (using snake_case)
- Each test only verifies specific identifiers relevant to that test
- Test names clearly indicate what they're testing
- No skipped tests remain - tests for unsupported features are removed
- Only one test exists per structure or combination of structures
- All debug output uses debugLog from helpers.ts

The standardization improves maintainability and clarity while preserving
all existing functionality. Documentation has been added for C language
constructs that are not currently supported by the parser.

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>

* feat: standardize Kotlin Tree-Sitter parser

Standardized Kotlin Tree-Sitter parser to ensure consistent naming and structure:
- Renamed identifiers to clearly indicate what they're testing
- Ensured all code sections are at least 4 lines long
- Organized query patterns logically with clear comments
- Removed skipped tests and unused query patterns
- Ensured only one test per structure or combination of structures

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>

* feat: standardize PHP Tree-Sitter parser

Standardized PHP Tree-Sitter parser to ensure consistent naming and structure:
- Renamed identifiers to clearly indicate what they're testing
- Ensured all code sections are at least 4 lines long
- Organized query patterns logically with clear comments
- Consolidated related tests to ensure only one test per structure
- Ensured all debug output uses debugLog from helpers.ts

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>

* feat: standardize Ruby Tree-Sitter parser

Standardized Ruby Tree-Sitter parser to ensure consistent naming and structure:
- Expanded query to support many more Ruby language constructs
- Added comprehensive test file with clear section headers
- Ensured all code sections are at least 4 lines long
- Added tests for each language construct
- Used consistent naming conventions for test identifiers

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>

* feat: standardize Swift Tree-Sitter parser

Standardized Swift Tree-Sitter parser to ensure consistent naming and structure:
- Renamed identifiers to clearly indicate what they're testing
- Ensured all code sections are at least 4 lines long
- Organized query patterns logically with clear comments
- Removed skipped tests and unused query patterns
- Ensured only one test per structure or combination of structures

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>

* feat: dynamically include all tree-sitter WASM files in build process

- Modified esbuild.js to dynamically read and copy all WASM files from tree-sitter-wasms package
- Added error handling and logging for better debugging
- Ensures all language parsers are automatically included without requiring manual updates

* refactor: move test mocks and fixtures to shared locations

Move test mocks to helpers.ts to be shared between test files
Create fixtures directory for shared test data
Move sample TSX content to fixtures/sample-tsx.ts
Update test files to use shared mocks and fixtures

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>

* refactor: simplify tree-sitter debug output

- Remove detailed node inspection to reduce noise
- Keep only essential tree structure output
- Remove duplicate content logging

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>

* refactor: standardize C# tree-sitter test structure

Move C# sample code to dedicated fixture file and create inspectCSharp test
following the same pattern as other language tests. This improves test
organization and maintainability by:

- Extracting sample C# code to fixtures/sample-c-sharp.ts
- Adding inspectCSharp.test.ts for tree structure inspection
- Updating parseSourceCodeDefinitions.c-sharp.test.ts to use fixture

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>

* feat: add language-specific capture processing

- Add language parameter to processCaptures for language-specific handling
- Implement selective HTML filtering for jsx/tsx files
- Update all call sites to pass correct language parameter
- Fix type safety by properly passing language strings

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>

* refactor: move tree-sitter test samples to fixtures

Move sample code from parseSourceCodeDefinitions test files into dedicated
fixtures directory for better organization and reuse:

- Created fixtures directory for language samples
- Moved sample code for C#, C, JSON, Kotlin, PHP, Ruby, Rust, and Swift
- Created inspect test files for each language
- Updated original test files to import from fixtures
- Standardized test options across languages
- No behavior changes, all tests passing

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>

* refactor: standardize tree-sitter test code across languages

Standardize test code structure and naming across all language parsers:
- Rename identifiers to clearly indicate test purpose
- Ensure each code section has descriptive comments
- Group related code sections together
- Maintain language-specific naming conventions
- Keep test structure consistent across languages
- Remove tests for unsupported features

Changes made to:
- C, JSON, Kotlin, PHP, Ruby, Rust, and Swift test files
- Sample code fixtures
- Parser definition tests
- Inspect structure tests

No functional changes - all tests passing with improved maintainability.

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>

* refactor: standardize tree-sitter parser tests across all languages

- Ensure all test structures span at least 4 lines for better parsing
- Create exactly one consolidated test per data structure type
- Use descriptive names that clearly indicate test purpose
- Improve query pattern organization and documentation
- Simplify inspect test files to focus on structure validation
- Implement result caching in test files for better performance
- Remove duplicate and skipped tests
- Follow consistent naming conventions across all languages

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>

* first pass all but elm-bash

* test: inspectTreeStructure helper now returns a string

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>

* refactor: establish single source of truth for MIN_COMPONENT_LINES

- Move minComponentLines parameter to a file-level constant
- Remove parameter passing to processCaptures function
- Update all references to use the global constant
- Remove redundant local constant declaration in parseFile

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>

* refactor: improve MIN_COMPONENT_LINES implementation

- Changed MIN_COMPONENT_LINES constant to use getter/setter functions
- Updated references to use getMinComponentLines() function
- Modified test helper to set value to 0 during tests
- This establishes a single source of truth while making testing easier

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>

* feat: standardize tree-sitter test implementations

Standardized tree-sitter test implementations across all supported languages:
- Removed debugLog calls from test files
- Implemented consistent line number pattern matching
- Ensured proper test structure and execution order
- Added comprehensive test coverage for supported structures
- Maintained 1:1 mapping between queries, tests and samples
- Documented unsupported features in TODO sections

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>

* docs: document tree-sitter parseable but unsupported structures

Added TODO sections in test files documenting structures that can be parsed
by tree-sitter but currently lack query pattern support:

- Python: f-strings, complex type annotations, pattern matching
- C++: virtual methods, field initializers, base class clauses
- Java: import declarations, field declarations with modifiers
- TSX: React hooks, context providers, event handlers

Added examples and clarifying comments to existing TODO sections.
Enhanced Java query patterns for better structure capture including
lambda expressions, field declarations, and type parameters.

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>

* fix: use correct distDir variable for WASM file copying

The copyWasmFiles plugin was using an undefined targetDir variable when copying
tree-sitter WASM files. Changed to use the correctly defined distDir variable,
fixing the build process.

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>

* lint: remove unused imports and variables from tree-sitter tests

Removed unused imports and variables from tree-sitter test files to fix linting errors:
- Removed unused goQuery import from helpers.ts
- Removed unused imports and mockedFs variables from language-specific test files
- Cleaned up test files to only import what they use

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>

---------

Signed-off-by: Eric Wheeler <roo-code@z.ewheeler.org>
Co-authored-by: Eric Wheeler <roo-code@z.ewheeler.org>
2025-04-28 11:27:49 -04:00

81 lines
3.3 KiB
TypeScript

/*
TODO: The following structures can be parsed by tree-sitter but lack query support:
1. String Interpolation (f-strings):
(string (string_start) (interpolation expression: (identifier)) (string_content) (string_end))
Example: f"{result}: {param3}"
2. Complex Type Annotations with Generics:
(type (generic_type (identifier) (type_parameter (type (generic_type)))))
Example: dict[str, Union[List[int], Dict[str, bool], Optional[ComplexType]]]
3. Multiple Context Managers in With Statements:
(with_clause (with_item) (with_item) (with_item))
Example: with (open('file1.txt') as f1, open('file2.txt') as f2)
4. Pattern Matching with As-Patterns:
(case_pattern (as_pattern (case_pattern (class_pattern)) (identifier)))
Example: case {"name": str() as name, "age": int() as age}
5. Nested Function Definitions with Scope Modifiers:
(function_definition (block (function_definition (block (nonlocal_statement) (global_statement)))))
Example: Nested functions with nonlocal/global declarations
*/
import { describe, expect, it, beforeAll } from "@jest/globals"
import { testParseSourceCodeDefinitions, debugLog } from "./helpers"
import { samplePythonContent } from "./fixtures/sample-python"
import { pythonQuery } from "../queries"
// Python test options
const pythonOptions = {
language: "python",
wasmFile: "tree-sitter-python.wasm",
queryString: pythonQuery,
extKey: "py",
}
describe("parseSourceCodeDefinitionsForFile with Python", () => {
let parseResult: string | undefined
beforeAll(async () => {
// Cache parse result for all tests
parseResult = await testParseSourceCodeDefinitions("test.py", samplePythonContent, pythonOptions)
debugLog("Python Parse Result:", parseResult)
})
it("should parse class and method definitions", () => {
expect(parseResult).toMatch(/\d+--\d+ \| class MultiLineDecoratedClass:/)
expect(parseResult).toMatch(/\d+--\d+ \| class MethodContainer:/)
expect(parseResult).toMatch(/\d+--\d+ \| def multi_line_method\(/)
debugLog("Class and method definitions found:", parseResult)
})
it("should parse decorated and async function definitions", () => {
expect(parseResult).toMatch(/\d+--\d+ \| @class_decorator_one/)
expect(parseResult).toMatch(/\d+--\d+ \| @function_decorator_one/)
expect(parseResult).toMatch(/\d+--\d+ \| async def multi_line_async_function\(/)
debugLog("Decorated and async functions found:", parseResult)
})
it("should parse special functions and expressions", () => {
expect(parseResult).toMatch(/\d+--\d+ \| def multi_line_generator\(/)
expect(parseResult).toMatch(/\d+--\d+ \| multi_line_lambda = \(/)
expect(parseResult).toMatch(/\d+--\d+ \| multi_line_comprehension = \[/)
debugLog("Special functions and expressions found:", parseResult)
})
it("should parse control flow structures", () => {
expect(parseResult).toMatch(/\d+--\d+ \| with \(/)
expect(parseResult).toMatch(/\d+--\d+ \| try:/)
expect(parseResult).toMatch(/\d+--\d+ \| def scope_demonstration\(\):/)
debugLog("Control flow structures found:", parseResult)
})
it("should parse module-level structures", () => {
expect(parseResult).toMatch(/\d+--\d+ \| from typing import \(/)
expect(parseResult).toMatch(/\d+--\d+ \| def multi_line_pattern_match\(/)
expect(parseResult).toMatch(/\d+--\d+ \| multi_line_type_annotation: dict\[/)
debugLog("Module-level structures found:", parseResult)
})
})