编码风格
🌐 Coding Style
这些是 Electron 编码的风格指南。
🌐 These are the style guidelines for coding in Electron.
你可以运行 npm run lint 来显示 cpplint 和 eslint 检测到的任何样式问题。
🌐 You can run npm run lint to show any style issues detected by cpplint and
eslint.
通用代码
🌐 General Code
- 以换行符结束文件。
- 地点要求按以下顺序:
- 内置于 Node 模块(例如
path) - 内置的 Electron 模块(例如
ipc、app) - 本地模块(使用相对路径)
- 内置于 Node 模块(例如
- 按以下顺序放置类属性:
- 类方法和属性(以
@开头的方法) - 实例方法和属性
- 类方法和属性(以
- 避免依赖平台的代码:
- 使用
path.join()来连接文件名。 - 当你需要引用临时目录时,请使用
os.tmpdir()而不是/tmp。
- 使用
- 在函数末尾显式返回时使用普通的
return。- 不是
return null、return undefined、null或undefined
- 不是
C++ 和 Python
🌐 C++ and Python
对于 C++ 和 Python,我们遵循 Chromium 的Coding Style。
还有一个脚本 script/cpplint.py 用于检查所有文件是否符合规范。
🌐 For C++ and Python, we follow Chromium's
Coding Style.
There is also a script script/cpplint.py to check whether all files conform.
我们现在使用的 Python 版本是 Python 3.9。
🌐 The Python version we are using now is Python 3.9.
这段 C++ 代码使用了许多 Chromium 的抽象和类型,因此建议先熟悉它们。一个好的起点是 Chromium 的重要抽象和数据结构文档。该文档提到了一些特殊类型、作用域类型(在超出作用域时会自动释放内存)、日志机制等内容。
🌐 The C++ code uses a lot of Chromium's abstractions and types, so it's recommended to get acquainted with them. A good place to start is Chromium's Important Abstractions and Data Structures document. The document mentions some special types, scoped types (that automatically release their memory when going out of scope), logging mechanisms etc.
文档
🌐 Documentation
- 用 remark Markdown 风格书写。
你可以运行 npm run lint:docs 来确保你的文档更改格式正确。
🌐 You can run npm run lint:docs to ensure that your documentation changes are
formatted correctly.
JavaScript
- 使用标准的 JavaScript 风格编写。
- 文件名应使用
-进行连接,而不是_,例如file-name.js而不是file_name.js,因为在 atom/atom 模块中,名称通常采用module-name形式。此规则仅适用于.js文件。 - 在适当的情况下使用较新的 ES6/ES2015 语法
命名事物
🌐 Naming Things
Electron API 使用与 Node.js 相同的大小写方案:
🌐 Electron APIs uses the same capitalization scheme as Node.js:
- 当模块本身是像
BrowserWindow这样的类时,使用PascalCase。 - 当模块是一组 API 时,例如
globalShortcut,使用camelCase。 - 当 API 是对象的一个属性,并且它足够复杂,需要在像
win.webContents这样的单独章节中描述时,使用mixedCase。 - 对于其他非模块 API,使用自然的标题,例如
<webview> Tag或Process Object。
在创建新的 API 时,建议使用 getter 和 setter 而不是 jQuery 的单函数风格。例如,.getText() 和 .setText(text) 比 .text([text]) 更受推荐。对此有一篇讨论。
🌐 When creating a new API, it is preferred to use getters and setters instead of
jQuery's one-function style. For example, .getText() and .setText(text)
are preferred to .text([text]). There is a
discussion on this.