npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hj-check-style

v3.0.1

Published

Import jshint and jscs test for your project.

Downloads

3

Readme

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 为后缀命名的
  3. 如果想查看 .jscsrc.jshintrc.eslint 中某个字段的意思,直接运行:

    cs manual jscs  disallowMultipleSpaces
  4. 更多命令用 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,

对齐和缩进

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

换行

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

命名

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