These lines import the VS Code API and ESLint library needed for the extension.
Activation
The activate function runs when your extension starts:
javascript
function activate(context) {
console.log('ESLint extension is now active');
// Command registration...
}
Command Registration
javascript
let disposable = vscode.commands.registerCommand('eslint-extension.lintCurrentFile', async function () {
// Command code...
});
context.subscriptions.push(disposable);
This registers a command called eslint-extension.lintCurrentFile that users can execute from the command palette.
Linting Process
The command does several things:
Checks if a file is open and is JavaScript/TypeScript
Gets the file content
Initializes ESLint and lints the text
Creates VS Code diagnostics from ESLint results
Displays squiggly lines under code issues
The most important part is converting ESLint messages to VS Code diagnostics:
javascript
const diagnostics = results[0].messages.map(message => {
// Create a range that pinpoints the exact location of the issue
const range = new vscode.Range(/* ... */);
// Set severity (warning or error)
const severity = message.severity === 1
? vscode.DiagnosticSeverity.Warning
: vscode.DiagnosticSeverity.Error;
return new vscode.Diagnostic(range, message.message, severity);
});
package.json Breakdown
This file defines:
Basic extension information (name, version, etc.)
When the extension activates (activationEvents)
The command users can run to trigger linting
Dependencies, including ESLint itself
The contributes section is particularly important as it defines what your extension adds to VS Code:
This basic extension provides manual linting with a command. A more advanced version could automatically lint files on save and provide code actions to fix issues.