This document provides detailed information about the internal API and architecture of the Slim Lint VS Code extension.
The extension follows a modular architecture with clear separation of concerns:
src/
├── extension.ts # Extension entry point and lifecycle management
├── linter.ts # Core linting engine
└── test/
├── fixtures/ # Test files for integration testing
├── runTest.ts # Test runner configuration
└── suite/ # Test suites
- Document Events: VS Code triggers events for document changes
- Extension Activation: Extension activates and sets up event listeners
- Linting Process: Linter executes slim-lint and parses output
- Diagnostic Updates: Results are displayed in VS Code
The main entry point that handles extension lifecycle and event management.
activate(context): Extension activationdeactivate(): Extension cleanupvalidateConfiguration(): Configuration validation- Event handlers for document changes
// Document save events
vscode.workspace.onDidSaveTextDocument(updateDiagnostics);
// Document open events
vscode.workspace.onDidOpenTextDocument(updateDiagnostics);
// Document close events
vscode.workspace.onDidCloseTextDocument(clearDiagnostics);
// Document change events
vscode.workspace.onDidChangeTextDocument(handleDocumentChange);The core linting engine that manages slim-lint execution and diagnostic creation.
Main linter class that implements vscode.Disposable.
constructor(outputChannel: vscode.OutputChannel)Creates a new linter instance with an output channel for logging.
Runs the linter on a document. This is the main entry point for linting.
Parameters:
document: The VS Code text document to lint
Behavior:
- Validates the document should be linted
- Executes slim-lint command
- Parses output and creates diagnostics
- Updates VS Code diagnostic collection
Clears diagnostics for a document.
Parameters:
document: The VS Code text document to clear diagnostics for
Disposes of the linter and cleans up resources.
Determines if a document should be linted.
Returns: true if the document is a Slim file and is a file document.
Retrieves and validates slim-lint configuration from VS Code settings.
Returns: Configuration object with executable and config paths.
Throws: Error if configuration is invalid.
Resolves the configuration file path relative to the workspace.
Parameters:
configPath: Configuration path from settings
Returns: Absolute path to configuration file.
Builds the command arguments for slim-lint execution.
Parameters:
config: Slim-lint configurationdocumentPath: Path to document to lint
Returns: Array of command arguments.
Executes the slim-lint command.
Parameters:
commandArgs: Command arguments array
Returns: Promise resolving to slim-lint output.
Throws: Error if execution fails.
Parses slim-lint output and creates VS Code diagnostics.
Parameters:
output: Raw slim-lint outputdocument: VS Code text document
Returns: Array of VS Code diagnostics.
Creates a single diagnostic from a regex match.
Parameters:
match: Regex match from slim-lint outputdocument: VS Code text document
Returns: VS Code diagnostic or null if invalid.
Updates VS Code diagnostic collection for a document.
Parameters:
document: VS Code text documentdiagnostics: Array of diagnostics to set
Main linting method that orchestrates the entire process.
Parameters:
document: VS Code text document to lint
interface SlimLintConfig {
executablePath: string;
configurationPath: string;
}Configuration object containing slim-lint executable and configuration file paths.
interface SlimLintOutput {
stdout: string;
stderr: string;
failed?: boolean;
code?: string;
}Output from slim-lint execution.
const SLIM_LANGUAGE_ID = 'slim';
const DIAGNOSTIC_COLLECTION_NAME = 'slim-lint';
const DEFAULT_CONFIG_FILE = '.slim-lint.yml';
const SLIM_LINT_OUTPUT_REGEX = /.+?:(\d+) \[(W|E)] (\w+): (.+)/g;
const LINT_TIMEOUT = 30000; // 30 secondsThe extension implements comprehensive error handling:
- Missing executable path
- Invalid configuration file
- Permission issues
- File size warnings
- Command not found
- Timeout errors
- Permission denied
- Invalid output format
- Error messages in VS Code
- Warning notifications
- Output channel logging
- Debug information
- 30-second timeout for slim-lint execution
- Prevents hanging on large files
- Configurable timeout value
- Diagnostic collection management
- Efficient document change handling
- Resource cleanup on disposal
- Proper disposal of resources
- Diagnostic collection cleanup
- Output channel management
The extension uses VS Code's configuration system:
{
"slimLint.executablePath": "slim-lint",
"slimLint.configurationPath": ".slim-lint.yml"
}The extension validates configuration on activation:
- Executable Path: Must be non-empty and contain valid command
- Configuration Path: Must be non-empty
- File Accessibility: Configuration file must be readable
- File Size: Warns if configuration file is very large
- File Extension: Validates YAML file extensions
Configuration paths are resolved relative to the workspace root:
// Relative paths are resolved against workspace root
const resolvedPath = path.join(workspaceRoot, configPath);
// Absolute paths are used as-is
const resolvedPath = path.normalize(configPath);src/test/
├── fixtures/ # Test files
│ ├── complex-test.slim
│ ├── tab-test.slim
│ ├── valid-test.slim
│ └── test-file.js
├── runTest.ts # Test runner
└── suite/
└── linter.test.ts # Main test suite
- Configuration validation
- Output parsing
- Diagnostic creation
- Error handling
- Real slim-lint execution
- File system operations
- VS Code integration
- Performance testing
complex-test.slim: Tests multiple linting rulestab-test.slim: Tests tab-related issuesvalid-test.slim: Tests clean filestest-file.js: Tests non-Slim files
Creates mock VS Code text documents for testing.
Runs the linter on fixture files and returns diagnostics.
Resolves paths to test fixture files.
{
"languages": [
{
"id": "slim",
"aliases": ["Slim", "slim"],
"extensions": [".slim", ".html.slim"]
}
]
}{
"configuration": {
"type": "object",
"title": "Slim Lint Configuration",
"properties": {
"slimLint.executablePath": {
"type": "string",
"default": "slim-lint",
"description": "Path to slim-lint executable"
},
"slimLint.configurationPath": {
"type": "string",
"default": ".slim-lint.yml",
"description": "Path to slim-lint configuration file"
}
}
}
}{
"activationEvents": ["onLanguage:slim"]
}The extension creates and manages a diagnostic collection:
const collection = languages.createDiagnosticCollection('slim-lint');Provides logging and debugging information:
const outputChannel = window.createOutputChannel('Slim Lint');- Use TypeScript strict mode
- Follow ESLint configuration
- Use Prettier for formatting
- Add JSDoc comments for public methods
- Always check disposal state
- Provide meaningful error messages
- Log errors to output channel
- Show user-friendly notifications
- Use timeouts for external commands
- Implement proper cleanup
- Avoid blocking operations
- Cache expensive operations
- Write unit tests for all public methods
- Include integration tests
- Use fixture files for complex scenarios
- Test error conditions
- Check VS Code version compatibility
- Verify slim-lint installation
- Review configuration settings
- Validate slim-lint executable path
- Check configuration file permissions
- Review output channel for errors
- Check configuration file size
- Review slim-lint rules
- Monitor timeout settings
Enable debug logging by checking the output channel:
- Open Command Palette
- Select "Developer: Show Output"
- Choose "Slim Lint" from dropdown
- Review log messages
- Info: Normal operation messages
- Warning: Non-critical issues
- Error: Critical failures
- slim-lint: The core linting tool that powers this extension
- Shopify vscode-shopify-ruby: Comprehensive Ruby development extension pack
- Ruby LSP: Language Server Protocol for Ruby
This API documentation provides comprehensive information for developers working with or extending the Slim Lint VS Code extension.