Skip to content

Add syntax error detection and basic linting #135

@DhruvParikh1

Description

@DhruvParikh1

Describe the feature

Thanks for creating this useful extension! The syntax highlighting has been a great help for working with VBA code in VS Code.

Feature Request

I'd like to request adding syntax error detection and basic linting/validation capabilities to the extension. Currently, the extension provides excellent syntax highlighting, but doesn't catch common VBA syntax errors until the code is imported back into Excel.

Use Case

When editing VBA modules externally in VS Code (a common workflow for version control and using modern editor features), it would be extremely helpful to catch basic errors before re-importing the code into Excel. This would save significant time in the edit-test cycle.

Examples of Errors That Could Be Caught

  • Missing End Sub, End Function, End If, Next, etc.
  • Mismatched brackets/parentheses
  • Invalid variable declarations (e.g., Dim x As InvalidType)
  • Typos in keywords (Dimm instead of Dim, Thenn instead of Then)
  • Missing line continuation characters when needed
  • Incorrect statement syntax (e.g., If x = 1 without Then)

Proposed Solution

Proposed Solutions

Option 1: Basic Regex-Based Validator (Easiest)

  • Implement simple pattern matching for common syntax errors
  • Use VSCode's Diagnostics API to display errors
  • Check for:
    • Mismatched block endings (count Sub/End Sub, If/End If, etc.)
    • Missing Then after If statements
    • Unclosed quotes or parentheses
    • Basic keyword typos using a whitelist
  • Pros: Quick to implement, low overhead
  • Cons: Limited accuracy, won't catch complex syntax issues

Option 2: VBA Parser + Language Server Protocol (Comprehensive)

  • Implement a proper VBA parser (or leverage existing ones like vba-blocks parser components)
  • Create a Language Server Protocol (LSP) implementation for VBA
  • Use VSCode's language client to connect to the language server
  • Pros: Proper syntax tree analysis, extensible to IntelliSense later
  • Cons: Significantly more complex, requires maintaining a parser

Option 3: Hybrid Approach (Balanced)

  • Start with regex-based validation for quick wins
  • Gradually build toward a simple recursive descent parser for statement-level validation
  • Use VSCode's diagnostic collection API for error reporting
  • Validate on document change with debouncing to avoid performance issues
  • Pros: Iterative improvement, immediate value while building toward better solution
  • Cons: May need refactoring as parser capabilities grow

Option 4: External Validator Integration

  • Shell out to Excel's VBA compiler (if available via COM automation) or other existing VBA validators
  • Parse compiler output and map to VSCode diagnostics
  • Pros: Leverages existing, accurate validation
  • Cons: Requires Excel installation, slower, platform-dependent

I'd personally recommend Option 3 as it provides immediate value while allowing for future enhancement.

Additional Information

Implementation Considerations

I understand VBA's syntax can be complex, but even basic validation would be valuable. This could potentially be implemented as:

  • Real-time diagnostics (red squiggles)
  • Problems panel integration
  • Optional linting on save

Even a subset of error detection would significantly improve the VBA editing experience in VS Code.

Thanks again for your work on this extension!

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions