JavaScript and Typescript
General rules and advice
Nextcloud uses Vue.js for its interface, for a consistent user interface we recommend apps to also use Vue with :ref:`provided components <js-library_nextcloud-vue>`. Yet also vanilla JavaScript and HTML can be used.
We recommend using Typescript for its type checking and improved static code analysis features.
Do not create global variables, instead if needed use global namespace objects like OCA.YourApp.…
Use JavaScript strict mode (automatically the case when using JavaScript modules)
ESLint config
There is a shared configuration for eslint that you can use to automatically format your Nextcloud apps's JavaScript and Typescript code. It consists of two parts: a config package that contains the formatting preferences and a plugin to detect deprecated and removed APIs in your code. See their readmes for instructions.
Filesystem structure
For vanilla JavaScript we recommend the following structure:
- appid/: Root of the app
- js/: JavaScript files
appid.js: Your app entry point
- css/: Location for all CSS files
appid.css
When using a bundler to compile Typescript (or JavaScript) we recommend the following structure:
- appid/: Root of the app
js/: Compiled JavaScript files
css/: Compiled CSS output
- src/: Root of all source files
components/: Location of Vue components
composables/: Location of Vue composables
services/: Location for service files like API abstractions
stores/: Location of Pinia stores
views/: Location of views
main.ts: Main entry point of your app
Filenames
We do not have strict rules for filenames, either kebab case or camel case will work.
Yet we strongly recommend for Vue apps to follow the Vue recommendations and use the same filename as the component name. E.g. if your component is called AppRoot then the file should be called AppRoot.vue.
Code style
General
Naming and casing
Use camelCase for
functions
methods
properties
variables
Use PascalCase for
classes
enums
types
interfaces
Vue components
For readability only capitalize the first letter of abbreviations like callHttpApi() instead of callHTTPAPI().
Sub-components should be prefixed. E.g. splitting a component like FileListEntry into smaller components called FileListEntryName, FileListEntryIcon …
Components should not have single-word names, this could conflict with current or future native HTML tags as these are always single-word. E.g. if you have a settings view, do not call it Settings but SettingsView or UserSettings etc.
✅ Do |
❌ Don't |
|---|---|
|
|
✅ Do |
❌ Don't |
|---|---|
|
|
Indentation
Use tabs instead of spaces for indenting - tab width is 4 spaces.
You can align e.g. comments using spaces if needed.
Semicolons
✅ Do |
❌ Don't |
|---|---|
|
|
|
Strings
✅ Do |
❌ Don't |
|---|---|
|
|
✅ Do |
❌ Don't |
|---|---|
|
|
Arrays
✅ Do |
❌ Don't |
|---|---|
|
|
✅ Do |
❌ Don't |
|---|---|
|
|
|
|
Functions
No spaces between function name and parameters.
Braces on same line as the definition.
Use consistent new lines in parameters (either all on one line, or one parameter per line).
For top-level functions, prefer regular functions over arrow functions. In Javascript functions defined with the function keyword will be hoisted, thus can even be used in other functions above their definition. Also using the function keyword makes the definition more explicit for readability. For callbacks anonymous arrow functions are often better suited as they do not create their own this binding.
Always use parenthesis for arrow functions. This helps for readability and prevents issues if parameters are added.
When using implicit return values in arrow functions with multi-line body use parenthesis around the body.
✅ Do |
❌ Don't |
|---|---|
|
|
✅ Do |
❌ Don't |
|---|---|
|
|
|
|
|
|
✅ Do |
❌ Don't |
|---|---|
|
|
|
|
✅ Do |
❌ Don't |
|---|---|
|
|
|
|
✅ Do |
❌ Don't |
|---|---|
|
|
|
✅ Do |
❌ Don't |
|---|---|
|
|
|
Objects
✅ Do |
❌ Don't |
|---|---|
|
|
✅ Do |
❌ Don't |
|---|---|
|
|
✅ Do |
❌ Don't |
|---|---|
|
|
✅ Do |
❌ Don't |
|---|---|
|
|
✅ Do |
❌ Don't |
|---|---|
|
|
|
|
Operators
Always use === and !== instead of == and !=
Prefer explicit comparisons
Here's why:
'' == '0' // false
0 == '' // true
0 == '0' // true
false == 'false' // false
false == '0' // true
false == undefined // false
false == null // false
null == undefined // true
' \t\r\n ' == 0 // true
✅ Do |
❌ Don't |
|---|---|
|
|
|
Control structures
Always use braces, also for one line ifs
Split long ifs into multiple lines
Always use break in switch statements and prevent a default block with warnings if it shouldn't be accessed
✅ Do |
❌ Don't |
|---|---|
|
|
|
|
✅ Do |
❌ Don't |
|---|---|
|
|