This document outlines the coding guidelines for contributing to Visual Studio Code. Following these standards ensures consistency across the codebase and makes it easier for everyone to read and maintain the code.
All contributors are expected to follow these guidelines. Pull requests that don’t adhere to these standards may be asked to make revisions.
The project uses EditorConfig to maintain consistent coding styles:
Copy
Ask AI
# Tab indentation for all files[*]indent_style = tabtrim_trailing_whitespace = true# Exception: YAML and package.json use 2 spaces[{*.yml,*.yaml,package.json}]indent_style = spaceindent_size = 2
Why Tabs?
Tabs allow developers to choose their preferred visual width while maintaining consistency in the actual file content. This is a deliberate choice for the VS Code project.
Do not introduce new types or values to the global namespace unless absolutely necessary.
Copy
Ask AI
// Good - scoped exportexport function parse(content: string): ParsedContent { // implementation}// Avoid - polluting global scopedeclare global { function parse(content: string): ParsedContent;}
Use JSDoc style comments for functions, interfaces, enums, and classes:
Copy
Ask AI
/** * Represents a text document, such as a source file. * Text documents have lines and knowledge about an underlying resource. */interface TextDocument { /** * The associated URI for this document. */ readonly uri: URI; /** * Get the text of this document. * @param range The range to get text for. If omitted, returns full document text. * @returns The text in the specified range. */ getText(range?: Range): string;}/** * Validates a URI string and returns a normalized URI object. * @param uriString The URI string to validate * @returns A normalized URI object * @throws {Error} If the URI string is invalid */function validateUri(uriString: string): URI { // implementation}
When to Write Comments
Public APIs: Always document with JSDoc
Complex logic: Explain the “why” not the “what”
Non-obvious behavior: Document surprising or unintuitive code
Workarounds: Explain why a workaround is necessary
Avoid comments that simply restate what the code does:
Copy
Ask AI
// Bad - comment adds no value// Increment countercounter++;// Good - explains why// Skip BOM character at start of UTF-8 filesif (content.charCodeAt(0) === 0xFEFF) { content = content.substring(1);}
Only surround arrow function parameters when necessary:
Copy
Ask AI
// Good - single parameter, no parens neededx => x + x// Good - multiple parameters need parens(x, y) => x + y// Good - type parameters need parens<T>(x: T, y: T) => x === y// Wrong - unnecessary parens(x) => x + x
In top-level scopes, prefer export function x() {} over export const x = () => {}.
Using the function keyword provides better stack traces when debugging:
Copy
Ask AI
// Good - shows function name in stack traceexport function validateInput(input: string): boolean { // implementation}// Less ideal - may show as anonymous in stack tracesexport const validateInput = (input: string): boolean => { // implementation};
Do NOT register a disposable to the containing class if the object is created within a method that is called repeatedly. Instead, return an IDisposable and let the caller register it.
Never duplicate imports. Always reuse existing imports if they are present.
Copy
Ask AI
// Goodimport { URI } from 'vs/base/common/uri';import { Event } from 'vs/base/common/event';// Wrong - duplicate importsimport { URI } from 'vs/base/common/uri';// ... 100 lines laterimport { URI } from 'vs/base/common/uri';
Don’t add tests to the wrong test suite (e.g., adding to end of file instead of inside relevant suite).
Look for existing test patterns before creating new structures:
Copy
Ask AI
// Good - organized test suitesuite('TextDocument', () => { test('should get line count', () => { // test implementation }); test('should get line at position', () => { // test implementation });});
All files must include the Microsoft copyright header at the top.
Copy
Ask AI
/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/