This extension enables TypeScript language server features for code embedded in YAML files using a virtual document approach.
- Open the extension project folder in VS Code
- Press
F5(or Run > Start Debugging) - This will open a new "Extension Development Host" window
- In the new window, open
example.yaml - You should see TypeScript errors highlighted in the YAML file:
- Line with
const num: number = "not a number"should show a type error - The incomplete
Userobject should show a missing property error - The invalid status value should show an error
- Line with
# Package the extension
pnpm run package
# Install the .vsix file in VS Code
# Go to Extensions > ... > Install from VSIXThe extension registers a custom URI scheme yamlscript-ts:// that provides content for virtual TypeScript documents.
vscode.workspace.registerTextDocumentContentProvider('yamlscript-ts', provider)The extension uses two methods to extract TypeScript code:
Method A: YAML Parsing (Primary)
- Uses the
yamllibrary to parse the YAML structure - Walks the AST looking for string values that contain TypeScript code
- Extracts position information from the YAML AST
Method B: Regex Fallback
- Falls back to regex patterns if YAML parsing fails
- Looks for patterns like
code: |,script: >, etc. - Uses heuristics to detect TypeScript syntax
Code is identified as TypeScript if it contains:
- Keywords:
const,let,var,function,class,interface,type,enum - Type annotations:
: string,: number, etc. - Arrow functions:
=> - Import/export statements
- Generic syntax:
<T>,<Type>
When the TypeScript language server produces diagnostics:
- Extension listens to
onDidChangeDiagnosticsevents - Checks if the diagnostic is for a virtual document (URI starts with
yamlscript-ts:) - Maps line/column numbers from virtual document to original YAML file
- Creates new diagnostics with adjusted positions
- Displays them in the original YAML file
// Virtual document line -> YAML file line
yamlLine = codeBlock.startLine + virtualLinehandler:
code: |
const x = 1;transform:
script: >
function process() {
return true;
}# typescript:
utils: |
interface Data {
id: number;
}components:
button:
onClick: |
const handleClick = (e: Event) => {
console.log(e);
};YAML File
↓
[Extract Code Blocks]
↓
Virtual TS Documents (yamlscript-ts://...)
↓
[TypeScript Language Server]
↓
Diagnostics
↓
[Map Back to YAML]
↓
Show Errors in YAML File
vscode.workspace.registerTextDocumentContentProvider()- Register virtual document providervscode.workspace.onDidOpenTextDocument()- Listen for document opensvscode.workspace.onDidChangeTextDocument()- Listen for document changesvscode.workspace.openTextDocument()- Open virtual documents
vscode.languages.createDiagnosticCollection()- Create diagnostic collectionvscode.languages.onDidChangeDiagnostics()- Listen for diagnostic changesvscode.languages.getDiagnostics()- Get diagnostics for a documentdiagnosticCollection.set()- Set diagnostics for a document
vscode.Uri.parse()- Create URIs for virtual documents- Custom URI scheme:
yamlscript-ts:
Add this to your launch.json:
{
"type": "extensionHost",
"request": "launch",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}",
"--log=debug"
]
}In the Extension Development Host:
- View > Output
- Select "Extension Host" from the dropdown
You can open virtual documents directly:
Command Palette > Open: yamlscript-ts://path/to/file.yaml.block0.ts
Solution: Make sure TypeScript language features are enabled in VS Code
Solution: Check that the YAML parser is correctly identifying code block positions
Solution: Ensure the code contains TypeScript-specific syntax (types, interfaces, etc.)
- IntelliSense Support: Provide completions, hover info within YAML
- Go to Definition: Navigate from YAML to definitions
- Refactoring: Support rename, extract function, etc.
- Configuration: Allow users to customize detection patterns
- Multi-language Support: Support JavaScript, JSX, TSX
- Import Resolution: Resolve imports relative to YAML file location
- Workspace Configuration: Respect tsconfig.json from workspace
- Virtual documents are created only for open YAML files
- Documents are cleaned up when YAML files are closed
- Code extraction is done on-demand, not for entire workspace
- Diagnostics are updated incrementally as you type
- Extension activates when YAML file is opened
- TypeScript errors appear in YAML file
- Errors update as you type
- Multiple code blocks in same file work
- Nested YAML structures handled correctly
- Extension doesn't impact non-YAML files
- No performance issues with large YAML files
- Virtual documents cleaned up on file close
- VS Code Extension API
- Virtual Documents in VS Code
- TypeScript Language Server Protocol
- YAML Specification
Happy coding! 🎉