3.0.5 • Published 9 years ago

check-style v3.0.5

Weekly downloads
3
License
MIT
Repository
github
Last release
9 years ago

JavaScript 编码规范

每个人或者每个公司都应该有一个 JavaScript 的编码规范,而且这个规范一般是不会变的, 我们可以要求每人在项目目录或者个人目录下放上 .jshintrc 或 .jscsrc 或 .eslintrc, 但你不觉得这样很麻烦吗?

此工具是基于我的个人编码习惯,同时结合了公司(沪江)里的一些规范,将 jshint, jscs, eslint 三者组装起来的一个工具,它不需要在项目下包含任何的相关 配置文件,只要运行一下 check-style 或缩写 cs;就会自动检查 js 文件中不符合规范的地方。 另外,也可以检查 jsx 文件的编码风格!

对于其他想利用此工具的用户:

  1. 如果你的编码风格和下面说的一致的话,那么恭喜你,你直接全局安装此工具,直接使用就行。
  2. 如果你的编码风格和下面的不一样,那么建议你 fork 此项目,修改其中的规范, 然后换个名称发布你的项目,这样你也可以使用你自己风格的此工具了。
  3. 如果你嫌第 2 步太麻烦了,你可以执行 check-style write 将配置文件写入你的项目目录, 手动修改写入后的配置文件即可

灵感来自于 standard,只是它强制要求你使用它的规范,没有自定义的可能,当然也不支持 jsx 文件

使用

  1. 先全局安装 check-style 工具

    npm --global install check-style
  2. 在项目目录下运行以下命令:

    自动检查项目目录下的所有 js 文件

    cs
    # 或者使用长命令:check-style

    也可以检查指定的 js 文件:

    cs file1 file2

    如果你是 jsx 文件,需要检查它的语法,可以这样用:

    cs --jsx --ext jsx # 假设你的 jsx 文件是以 .jsx 为后缀命名的
  1. 如果想查看 .jscsrc.jshintrc.eslint 中某个字段的意思,直接运行:

    cs manual jscs  disallowMultipleSpaces
  2. 更多命令用 cs -h 查看

jscs 相关规范

我推荐的规范

"esnext": true,
"validateJSDoc": { "checkParamNames": true, "requireParamTypes": true },
"disallowMultipleSpaces": true,
"requireCommaBeforeLineBreak": true,
// "requireSemicolons": true, // jshint 中已经有此判断了
"requireSpaceBetweenArguments": true,
"validateParameterSeparator": ", ",
"requireSpacesInConditionalExpression": true,
"requireSpacesInForStatement": true,
"disallowSpacesInFunctionDeclaration": { "beforeOpeningRoundBrace": true },
"requireSpacesInFunction": { "beforeOpeningCurlyBrace": true },
// "requireYodaConditions": true,
"disallowKeywords": ["with", "eval"],
"disallowKeywordsOnNewLine": ["else"],
"disallowTrailingWhitespace": true,
"requireLineFeedAtFileEnd": true,
"requireCurlyBraces": ["try", "catch"],

空格

  • 除括号外,所有运算符的前后都需要有空格
  • 某些关键字之后需要有空格,包括 if, else, try, finally
  • block 语句块之前要有空格
  • 行注释 // 后需要有空格
  • 对象初始化({ ... })的每个属性名的:后面要有空格
  • 所有逗号,后, 但除了逗号在行尾的情况
  • 单行的对象初始化({ ... }),在{后面和}前面要有空格
"disallowSpaceBeforeBinaryOperators": [","],
"disallowSpaceAfterBinaryOperators": ["!"],
"requireSpaceBeforeBinaryOperators": ["+", "-", "/", "*", "=", "==", "===", "!=", "!==", ">", ">=", "<", "<="],
"requireSpaceAfterBinaryOperators": ["+", "-", "/", "*", "=", "==", "===", "!=", "!==", ">", ">=", "<", "<="],
"requireSpaceBeforeBlockStatements": true,
"requireSpaceAfterKeywords": [
  "do",
  "for",
  "if",
  "else",
  "switch",
  "case",
  "try",
  "catch",
  "finally",
  "void",
  "while",
  "return",
  "typeof",
  "function"
],
"requireSpaceBeforeKeywords": ["else", "catch", "finally"],
"requireSpaceAfterLineComment": true,
"requireSpaceBeforeObjectValues": true,

对齐和缩进

  • 必须采用 2 个空格,不得使用 TAB
  • 未结束的语句在换行后必须多一次缩进
"validateIndentation": 2,
"disallowMixedSpacesAndTabs": true,

换行

  • if, do, for, while 等关键字前
  • 运算符处换行时,运算符必须在新行的行首
"requireKeywordsOnNewLine": ["while", "do"],
"disallowOperatorBeforeLineBreak": ["+", "-", "/", "*", "=", "==", "===", "!=", "!==", ">", ">=", "<", "<="],
"requireOperatorBeforeLineBreak": [","],

命名

  • 构造函数首字母大写
"requireCapitalizedConstructors": true,
3.0.5

9 years ago

3.0.4

9 years ago

3.0.3

10 years ago

3.0.2

10 years ago

3.0.1

10 years ago

3.0.0

10 years ago

2.0.1

10 years ago

2.0.0

10 years ago

1.0.4

10 years ago

1.0.3

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago