当前位置: 首页 > 文档资料 > Electron 中文文档 >

代码规范

优质
小牛编辑
123浏览
2023-12-01

这些是 Electron 编码风格指南。

您可以运行 npm run lint 来显示 cpplinteslint 检测到的任何样式问题。

Coding Style

These are the style guidelines for coding in Electron.

You can run npm run lint to show any style issues detected by cpplint and eslint.

通用代码

  • 用换行符结束文件。
  • 按照如下顺序排列 node 模块的 require 代码

    • 内置Node模块(如 path
    • 内置Electron模块(如 ipcapp
    • 本地模块(使用相对路径)
  • 按照如下顺序排列类的属性

    • Class methods and properties (methods starting with a @)
    • Instance methods and properties
  • 避免与平台相关的代码:

    • 使用 path.join() 来组织文件路径。
    • 请使用os.tmpdir()而不是/tmp来引用临时目录。
  • 使用 return 来明确的结束一个函数

    • 不是return nullreturn undefinednullundefined

General Code

  • End files with a newline.
  • Place requires in the following order:

    • Built in Node Modules (such as path)
    • Built in Electron Modules (such as ipc, app)
    • Local Modules (using relative paths)
  • Place class properties in the following order:

    • Class methods and properties (methods starting with a @)
    • Instance methods and properties
  • Avoid platform-dependent code:

    • Use path.join() to concatenate filenames.
    • Use os.tmpdir() rather than /tmp when you need to reference the temporary directory.
  • Using a plain return when returning explicitly at the end of a function.

    • Not return null, return undefined, null or undefined

C + + 和 Python

对于 C++ 和 Python, 我们遵循 Chromium 的编码风格. 你可以使用 clang-format 来自动格式化 C++ 代码. 你可以使用 script/cpplint.py 来检验文件是否符合要求。

我们现在使用的 Python 版本是 Python 2.7。

C++ 代码使用了大量 Chromium 的抽象和类型,因此建议您熟悉它们。 一个起步的好地方是 Chromium 的 重要的抽象概念和数据库结构 文档. 该文档提到一些特殊类型,范围类型(超出范围时自动释放其内存), 记录机制等。

C++ and Python

For C++ and Python, we follow Chromium's Coding Style. You can use clang-format to format the C++ code automatically. There is also a script script/cpplint.py to check whether all files conform.

The Python version we are using now is Python 2.7.

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.

文档

  • 使用remark Markdown样式.

你可以运行 npm run lint-docs 来保证你修改的文档格式正确。

Documentation

  • Write remark markdown style.

You can run npm run lint-docs to ensure that your documentation changes are formatted correctly.

JavaScript

  • 书写 标准 JavaScript 样式
  • 文件名应使用 - 连接而不是 _, 例如. file-name.js 而不是 file_name.js, 因为在 github/atom中模块名通常是 module-name 形式. 此规则仅适用于 .js 文件。
  • 酌情使用更新的 ES6 / ES2015 语法

    • const 用于需要的和其他的常数
    • let 用于定义变量
    • Arrow functions 代替 function () { }
    • Template literals 而不是使用字符串连接符 +

JavaScript

  • Write standard JavaScript style.
  • File names should be concatenated with - instead of _, e.g. file-name.js rather than file_name.js, because in github/atom module names are usually in the module-name form. This rule only applies to .js files.
  • Use newer ES6/ES2015 syntax where appropriate

    • const for requires and other constants
    • let for defining variables
    • Arrow functions instead of function () { }
    • Template literals instead of string concatenation using +

命名相关

Electron API 使用与 Node.js 相同的大小写方案:

  • 当模块本身是class时, 比如 BrowserWindow, 使用 CamelCase.
  • 当模块是一组 API 时, 比如 globalShortcut时,使用 mixedCase
  • 当 API 是对象的属性时, 并且它复杂到足以成为一个单独的块, 比如 win.webContents, 使用 mixedCase.
  • 对于其他非模块API, 使用自然标题, 比如 <webview> TagProcess Object.

当创建新的 API 时, 最好使用 getter 和 setter 而不是 jQuery 的一次性函数。 举个例子, .getText().setText(text) 优于 .text([text]). 这是一些相关的 讨论

Naming Things

Electron APIs uses the same capitalization scheme as Node.js:

  • When the module itself is a class like BrowserWindow, use PascalCase.
  • When the module is a set of APIs, like globalShortcut, use camelCase.
  • When the API is a property of object, and it is complex enough to be in a separate chapter like win.webContents, use mixedCase.
  • For other non-module APIs, use natural titles, like <webview> Tag or Process Object.

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.