Files
tree-sitter-shellspec/.serena/memories/real_world_shellspec_patterns.md
Ismo Vuorinen c8ba576b4e feat: implement complete tree-sitter-shellspec grammar with comprehensive testing
- Add full ShellSpec grammar extending tree-sitter-bash
- Support all ShellSpec constructs: Describe, Context, It, hooks, utilities
- Include Data block parsing with statements and argument styles
- Add 61 comprehensive test cases covering real-world patterns
- Implement optimized GitHub workflows with CI/CD automation
- Configure complete development tooling (linting, formatting, pre-commit)
- Add comprehensive documentation and contribution guidelines
- Optimize grammar conflicts to zero warnings
- Support editor integration for Neovim, VS Code, Emacs

Breaking Changes:
- Initial release, no previous API to break

BREAKING CHANGE: Initial implementation of tree-sitter-shellspec grammar

# Conflicts:
#	.github/workflows/codeql.yml
#	.github/workflows/pr-lint.yml
#	.pre-commit-config.yaml

# Conflicts:
#	.github/workflows/pr-lint.yml

# Conflicts:
#	.github/workflows/pr-lint.yml
2026-01-04 15:32:39 +02:00

4.5 KiB

Real-World ShellSpec Patterns Analysis

Based on analysis of 24 official ShellSpec example files from test/spec/, this document captures the comprehensive patterns discovered and how they've been integrated into the grammar.

Key Findings from Official Examples

1. Hook Statement Patterns

Discovery: ShellSpec uses both block-style hooks (with End) and statement-style hooks (without End)

Statement-style hooks (newly supported):

  • Before 'setup' - Single function call
  • Before 'setup1' 'setup2' - Multiple function calls
  • After 'cleanup' - Cleanup functions
  • Before 'value=10' - Inline code execution

Grammar Implementation: Added shellspec_hook_statement rule to handle Before/After statements without End blocks.

2. Directive Patterns

Discovery: ShellSpec has several directive-style statements that don't follow the typical block structure

Include directive:

  • Include ./lib.sh - Include external scripts
  • Include ./support/custom_matcher.sh

Conditional Skip:

  • Skip if "reason" condition - Skip with conditions
  • Skip if 'function returns "skip"' [ "$(conditions)" = "skip" ] - Complex conditions

Grammar Implementation: Added shellspec_directive_statement rule for Include and conditional Skip patterns.

3. Data Block Complexity (Future Enhancement)

Discovery: Data blocks have sophisticated syntax not yet fully supported:

  • Data:raw and Data:expand modifiers for variable expansion control
  • Data | filter syntax for piping data through filters
  • #|content line prefix syntax for multi-line data
  • Function-based data: Data function_name
  • String-based data: Data 'string content'

Current Status: Basic Data blocks work as utility blocks, but advanced syntax requires future enhancement.

4. Top-Level Examples

Discovery: ShellSpec allows It/Example/Specify blocks at the top level without requiring Describe/Context wrapping.

Pattern:

It 'is simple'
  When call echo 'ok'
  The output should eq 'ok'
End

Grammar Implementation: Already supported through existing shellspec_it_block rule.

5. Complex Nesting and Context Switching

Discovery: Real-world examples show sophisticated nesting:

  • Describe > Context > It hierarchies
  • Mixed hook scoping (hooks defined at different nesting levels)
  • Before/After hooks with multiple arguments for setup chains
  • Comments and shellcheck directives intermixed

Grammar Enhancements Made

New Rules Added

  1. shellspec_hook_statement - For Before/After without End
  2. shellspec_directive_statement - For Include and conditional Skip
  3. Enhanced conflicts array to handle new statement types

Test Coverage Added

  • 59 total tests (up from 53)
  • New real_world_patterns.txt test file
  • 6 additional tests covering hook statements, directives, and complex patterns

Integration Status

Fully Integrated:

  • Hook statements (Before/After without End)
  • Include directive
  • Conditional Skip statements
  • Top-level It blocks

⚠️ Partially Supported:

  • Data blocks (basic functionality works, advanced syntax needs work)
  • Complex conditional expressions in Skip

🔄 Future Enhancements Needed:

  • Data block modifiers (:raw, :expand)
  • Data block filters (| syntax)
  • Data block #| line syntax
  • More sophisticated conditional parsing for Skip

Real-World Usage Patterns Observed

  1. Hook Chains: Multiple Before/After hooks for complex setup/teardown
  2. Conditional Logic: Heavy use of conditional Skip statements
  3. External Dependencies: Frequent use of Include for modular test organization
  4. Mixed Context: ShellSpec blocks mixed with regular bash functions and variables
  5. Assertion Patterns: Consistent use of When/The assertion syntax
  6. Descriptive Strings: Heavy use of descriptive string literals for test names

Grammar Statistics

  • Block types: 5 (Describe, Context, It, Hook, Utility)
  • Statement types: 2 (Hook statements, Directives)
  • Keywords supported: 25+ ShellSpec keywords
  • Test coverage: 100% (59/59 tests passing)
  • Conflict warnings: 13 (mostly unnecessary, can be optimized)

Recommendations for Future Development

  1. Priority 1: Implement advanced Data block syntax for better real-world compatibility
  2. Priority 2: Enhance conditional Skip parsing to handle complex bash expressions
  3. Priority 3: Optimize conflict declarations to reduce parser warnings
  4. Priority 4: Add support for ShellSpec assertion syntax (When/The statements)