Blog

Blog

PHODAL

nodejs jslint——为NodeJS项目添加语法检查

在给iot coap添加了pre-commit,测试之后,现在也加上了jslint。

Nodejs Pre-Commit

pre commit 的意思就是在我们提交代码之前,做一些额外的事。因为本地没有CI,但是也要跑一些简单的测试,总的来说,每次在commit之前或者之后跑一下测试都是比较麻烦的事。于是,之前就找来了这个库,做一些简单的测试。

添加pre-commit

1.在我们的package.json中的devDependencies添加pre-commit

"devDependencies": {
    "pre-commit": "0.0.9"
 }

2.安装

 npm install

3.配置简单的任务

  "pre-commit": [
    "test"
  ],
  "scripts": {
    "test": "ls"
  }

于是我们就可以试试了

git commit

就可以看到效果了

jslint

JSLint是一个JavaScript验证工具(非开源),可以扫描JavaScript源代码来查找问题。如果JSLint发现一 个问题,JSLint就会显示描述这个问题的消息,并指出错误在源代码中的大致位置。有些编码风格约定可能导致未预见的行为或错误,JSLint除了能指 出这些不合理的约定,还能标志出结构方面的问题。尽管JSLint不能保证逻辑一定正确,但确实有助于发现错误,这些错误很可能导致浏览器的 JavaScript引擎抛出错误。

Nodejs jslint

接着我们要做的便是把jslint添加到我们的项目中。

1.添加jslint库,同样是添加到devDependencies

"devDependencies": {
    "pre-commit": "0.0.9",
    "jslint": "0.6.4"
}

2.安装

npm install

3.配置

  "pre-commit": [
    "test",
    "jslint"
  ],
  "scripts": {
    "test": "./node_modules/.bin/mocha --bail --reporter spec 2>&1",
    "jslint": "jslint 'lib/*.js' 'index.js' "
  }

Nodejs pre-commit jslint

看看最后的效果

git commit

> iot-coap@0.2.0 test /Users/fdhuang/learing/iot-coap
> mocha --bail --reporter spec 2>&1



coap listening at coap://0.0.0.0:5683
restify listening at http://0.0.0.0:8848

  coap function test
    ✓ should return 20 when coap get id = 1
    ✓ should return 12 when post a data and get data
    ✓ should no return when delete id

  rest function test
    ✓ should return 200 when start rest server
    ✓ should return id value when get id = 1  (1012ms)
    ✓ should return sensors 20 when post data & id = 10

  query data status code test
    ✓ should return 4.04 when result empty
    ✓ should return 2.05 when result empty
    ✓ should return 2.05 when delete result
    ✓ should return 2.05 when save result


  10 passing (1s)


> iot-coap@0.2.0 jslint /Users/fdhuang/learing/iot-coap
> jslint 'lib/*.js' 'index.js'


lib/coap_query_helper.js is OK.

lib/coap_request_handler.js is OK.

lib/coap_result_helper.js is OK.

lib/coap_server.js is OK.

lib/db_helper.js is OK.

lib/rest_helper.js is OK.

lib/rest_server.js is OK.

index.js
 #1 Expected exactly one space between ')' and '{'.
    db.serialize(function (){ // Line 11, Pos 25

就这样华丽丽的挂了,也commit不了,这就是我们要的效果。

关于我

Github: @phodal     微博:@phodal     知乎:@phodal    

微信公众号(Phodal)

围观我的Github Idea墙, 也许,你会遇到心仪的项目

QQ技术交流群: 321689806
comment

Feeds

RSS / Atom

最近文章

关于作者

Phodal Huang

Engineer, Consultant, Writer, Designer

ThoughtWorks 技术专家

工程师 / 咨询师 / 作家 / 设计学徒

开源深度爱好者

出版有《前端架构:从入门到微前端》、《自己动手设计物联网》、《全栈应用开发:精益实践》

联系我: h@phodal.com

微信公众号: 最新技术分享

标签