Style guide
Stay organized with collections
Save and categorize content based on your preferences.
Follow the Google TypeScript style
guide.
Migration to TypeScript & ES6
Blockly was originally written in ES5.1 in compliance with an older,
then-current version of the Google JavaScript style
guide. Newly-written
code should comply with the current style guide and use ES6 language features
like let
, const
, class
, destructuring assignment where applicable.
Existing code may be updated or may be left out of compliance. The Blockly team
tries to make the best decision taking into account code consistency and the
experience for users of the library - for example, we may opt not to rename
public functions that no longer comply with the style guide.
Do
- Use linting and formatting tools.
- We use eslint and have an
eslint.config.mjs
file
set up with rules for our preferred style.
- We use prettier for automatic formatting.
- Run
npm run lint
to run the linter and npm run format
to run the
formatter.
- Indent with spaces, not tabs.
- Use semicolons.
- Use
camelCase
for variables and functions.
- Use
TitleCase
for classes.
- Use
ALL_CAPS
for constants.
- Use braces
for all control structures.
- Exception: You may omit the braces for single-line
if
statements.
- Use single quotes (except when writing JSON).
- Redeclare variables in
for
loops. That is, always write for (const i = 0;
...)
instead of for (i = 0; ...)
.
- Not doing so raises the risk that after a refactor higher up in the
function the variable will be orphaned and become a surprise global.
- Start comments with capital letters and end them with periods.
- Create GitHub issues with TODOs and link them using
TODO(#issueNumber)
.
- Annotate everything with TSDoc.
Don't
- Indent with tabs.
- Use underlines at the ends of variable or function names.
- Some earlier code uses underscores for private or internal properties or
functions. While these may continue to exist, no new code should be
added following this convention.
- Use
snake_case
.
- Use double quotes (except when writing JSON).
- Use malformed TSDoc.
- Our TSDoc is automatically published as part of our documentation.
- Write
TODO (username)
.
- Instead create GitHub issues with TODOs and link them using
TODO(#issueNumber)
.
- Use
string.startsWith
. Use Blockly.utils.string.startsWith
instead.
TSDoc
The Blockly team uses TSDoc to annotate our code and
generate documentation. We expect TSDoc for all public properties of classes,
and for all exported functions.
TSDoc comments must start with /**
and end with */
to be parsed correctly.
Types
Types are omitted from TSDoc because that information is in the TypeScript code
directly. If you are editing one of the few remaining JavaScript files, include
type annotations according to the Closure Compiler
documentation.
Visibility
Functions or properties that should only be accessed within the Blockly library
should be annotated with @internal
. This prevents these properties from
appearing in the public documentation. Other visibility
modifiers should be placed in
the TypeScript code directly, not in the TSDoc.
Properties
TSDoc for properties should include a description of the property. The
description may be omitted for self-explanatory properties.
/**
* The location of the top left of this block (in workspace coordinates)
* relative to either its parent block, or the workspace origin if it has no
* parent.
*
* @internal
*/
relativeCoords = new Coordinate(0, 0);
Functions
Annotations for functions should include
- A description of the function
- One
@param
tag per parameter,
including
- A
@returns
tag if the function
will return a value, with a description of the returned value.
Descriptions may omitted for functions, parameters, or return values if they are
self-explanatory.
For example:
/**
* Find the workspace with the specified ID.
*
* @param id ID of workspace to find.
* @returns The sought after workspace or null if not found.
*/
export function getWorkspaceById(id: string): Workspace | null {
return WorkspaceDB_[id] || null;
}
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-06-16 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-06-16 UTC."],[[["\u003cp\u003eBlockly code must follow the Google TypeScript style guide, using ES6 features and adhering to specific formatting rules like using camelCase for variables and functions.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers are required to use linting and formatting tools such as ESLint and Prettier to maintain code consistency and quality.\u003c/p\u003e\n"],["\u003cp\u003eAll code should be thoroughly documented using TSDoc, providing descriptions, parameters, and return values for public properties and functions.\u003c/p\u003e\n"],["\u003cp\u003eInternal properties and functions should be marked with \u003ccode\u003e@internal\u003c/code\u003e to exclude them from public documentation and ensure proper visibility.\u003c/p\u003e\n"],["\u003cp\u003eWhen migrating existing ES5.1 code, prioritize code consistency and user experience, potentially leaving some older code untouched for backward compatibility.\u003c/p\u003e\n"]]],["New code should follow the Google TypeScript style guide, using ES6 features. Use linting (eslint) and formatting (prettier) tools, indent with spaces, use semicolons, `camelCase`, `TitleCase`, and `ALL_CAPS` for variables, classes, and constants respectively. Redeclare variables in `for` loops and use braces for control structures. Employ single quotes (except for JSON) and write `TODO(#issueNumber)` for issues. Annotate with TSDoc for all public properties and exported functions, following specific formatting for descriptions, `@param`, and `@returns` tags.\n"],null,["# Style guide\n\nFollow the [Google TypeScript style\nguide](https://google.github.io/styleguide/tsguide.html).\n\nMigration to TypeScript \\& ES6\n------------------------------\n\nBlockly was originally written in ES5.1 in compliance with an [older,\nthen-current version of the Google JavaScript style\nguide](https://google.github.io/styleguide/javascriptguide.xml). Newly-written\ncode should comply with the current style guide and use ES6 language features\nlike `let`, `const`, `class`, destructuring assignment where applicable.\nExisting code may be updated or may be left out of compliance. The Blockly team\ntries to make the best decision taking into account code consistency and the\nexperience for users of the library - for example, we may opt not to rename\npublic functions that no longer comply with the style guide.\n\nDo\n---\n\n- Use linting and formatting tools.\n - We use [eslint](http://eslint.org/) and have an [`eslint.config.mjs`\n file](https://github.com/google/blockly/blob/master/eslint.config.mjs) set up with rules for our preferred style.\n - We use [prettier](https://prettier.io/) for automatic formatting.\n - Run `npm run lint` to run the linter and `npm run format` to run the formatter.\n- Indent with spaces, not tabs.\n- Use [semicolons](https://google.github.io/styleguide/jsguide.html#formatting-semicolons-are-required).\n- Use `camelCase` for variables and functions.\n- Use `TitleCase` for classes.\n- Use `ALL_CAPS` for constants.\n- [Use braces](https://google.github.io/styleguide/jsguide.html#formatting-braces-all) for all control structures.\n - Exception: You may omit the braces for single-line `if` statements.\n- Use single quotes (except when writing JSON).\n- Redeclare variables in `for` loops. That is, always write `for (const i = 0;\n ...)` instead of `for (i = 0; ...)`.\n - Not doing so raises the risk that after a refactor higher up in the function the variable will be orphaned and become a surprise global.\n- Start comments with capital letters and end them with periods.\n- Create GitHub issues with TODOs and link them using `TODO(#issueNumber)`.\n- Annotate everything with [TSDoc](#tsdoc).\n\nDon't\n-----\n\n- Indent with tabs.\n- Use underlines at the ends of variable or function names.\n - Some earlier code uses underscores for private or internal properties or functions. While these may continue to exist, no new code should be added following this convention.\n- Use `snake_case`.\n- Use double quotes (except when writing JSON).\n- Use malformed TSDoc.\n - Our TSDoc is automatically published as part of our documentation.\n- Write `TODO (username)`.\n - Instead create GitHub issues with TODOs and link them using `TODO(#issueNumber)`.\n- Use `string.startsWith`. Use `Blockly.utils.string.startsWith` instead.\n\nTSDoc\n-----\n\nThe Blockly team uses [TSDoc](https://tsdoc.org/) to annotate our code and\ngenerate documentation. We expect TSDoc for all public properties of classes,\nand for all exported functions.\n\nTSDoc comments must start with `/**` and end with `*/` to be parsed correctly.\n\n### Types\n\nTypes are omitted from TSDoc because that information is in the TypeScript code\ndirectly. If you are editing one of the few remaining JavaScript files, include\ntype annotations according to the [Closure Compiler\ndocumentation](https://github.com/google/closure-compiler/wiki/Annotating-JavaScript-for-the-Closure-Compiler).\n\n### Visibility\n\nFunctions or properties that should only be accessed within the Blockly library\nshould be annotated with `@internal`. This prevents these properties from\nappearing in the public documentation. Other [visibility\nmodifiers](/blockly/guides/programming/using_blockly_apis) should be placed in\nthe TypeScript code directly, not in the TSDoc.\n\n### Properties\n\nTSDoc for properties should include a description of the property. The\ndescription may be omitted for self-explanatory properties. \n\n /**\n * The location of the top left of this block (in workspace coordinates)\n * relative to either its parent block, or the workspace origin if it has no\n * parent.\n *\n * @internal\n */\n relativeCoords = new Coordinate(0, 0);\n\n### Functions\n\nAnnotations for functions should include\n\n- A description of the function\n- One [`@param`](https://tsdoc.org/pages/tags/param/) tag per parameter, including\n - Name\n - Description\n- A [`@returns`](https://tsdoc.org/pages/tags/returns/) tag if the function will return a value, with a description of the returned value.\n\nDescriptions may omitted for functions, parameters, or return values if they are\nself-explanatory.\n\nFor example: \n\n /**\n * Find the workspace with the specified ID.\n *\n * @param id ID of workspace to find.\n * @returns The sought after workspace or null if not found.\n */\n export function getWorkspaceById(id: string): Workspace | null {\n return WorkspaceDB_[id] || null;\n }"]]