The static code analysis and linting tool ESLint is the de-facto standard for linting JavaScript projects. In this lesson we’ll see how to install, run, and configure it for your preferences.

Install:

npm i -D eslint

Run:

npx eslint src

Create: .eslintrc file

{
"parserOptions": {
"ecmaVersion": "2018"
},
"extends": [
"eslint:recommended"
],
"rules": {
"no-console": "off"
},
"env": {
"browser": true,
"node": true,
"es6": true,
"jest": true
}
}

The code formatting tool prettier can help you avoid a lot of useless time spent formatting code and arguing about code formatting with your co-workers. It can also help you catch subtle issues with your code that you may not notice otherwise. In this lesson we’ll learn how to install and run prettier.

Install:

npm i -D prettier

Run:

"format": "prettier --write \"**/*.+(js|jsx|json|yml|yaml|css|less|scss|ts|tsx|md|graphql|mdx)\""
npm run format

In VSCode: Settings.json

{
"editor.formatOnSave": true
}

Prettier is a pretty opinionated tool, but it does allow for some customization. In this lesson we’ll check out the prettier playground and see what options we want to enable in our project’s .prettierrc file.

After adding in our custom configuration, we’ll create a .prettierignore file so that you can avoid formatting any files generated within the project such as node_modules or a build folder.

.prettierrc:

{
"arrowParens": "avoid",
"bracketSpacing": false,
"insertPragma": false,
"jsxBracketSameLine": true,
"parser": "babylon",
"printWidth": 80,
"proseWrap": "always",
"requirePragma": false,
"semi": true,
"singleQuote": false,
"tabWidth": 2,
"trailingComma": "all",
"useTabs": false
}

.prettierignore:

node_modules
coverage
dist
build
.build
.vscode
package.json
package-lock.json
debug.log

Because prettier can automatically fix a lot of stylistic issues in our codebase, it’s not necessary to have eslint check for those and it can actually be kind of annoying if it does. So let’s see how we can use eslint-config-prettier to disable all rules that are made irrelevant thanks to prettier.

Install:

npm i -D eslint-config-prettier

.eslintrc:

{
"parserOptions": {
"ecmaVersion": "2018"
},
"extends": [
"eslint:recommended",
"eslint-config-prettier"
],
"rules": {
"no-console": "off"
},
"env": {
"browser": true,
"node": true,
"es6": true,
"jest": true
}
}

You can’t force everyone on your project to use the prettier integration for their editor, so let’s add a validation script to verify that prettier has been run on all project files.

Fortunately for us, Prettier accepts a --list-different flag that you can use that will throw an error when code is not up to the standard of your project.

  "scripts": {
"test": "node --require ./setup-global.js src/index.js",
"lint": "eslint src",
"format": "npm run prettier -- --write",
"prettier": "prettier \"**/*.+(js|jsx|json|yml|yaml|css|less|scss|ts|tsx|md|graphql|mdx)\"",
"validate": "npm run lint && npm run prettier -- --list-different"
},

We can run:

npm run validate

to check whether there is any files which is not formatted.

If it is, then will throw error, we can run:

npm run format

to format the code.

ESLint can check for a lot of things, but it’s not a great tool for checking the types of variables that flow through your application. For this you’ll need a type-checking tool like Flow or TypeScript. Let’s see how we can configure our project to work with Flow.

Install:

npm i -D flow-bin

Add script:

"flow": "flow",

Run:

npm run flow

For the first time, we run 'npm run flow' will tell us to run:

npm run flow init

it create a .flowconfig file.

Create a exmaple js file to pratice flow:

//@flow

function add(a: number, b: number): number {
return a + b
}
type User = {
name: {
first: string,
middle: string,
last: string,
},
}
function getFullName(user: User): string {
const {
name: {first, middle, last},
} = user
return [first, middle, last].filter(Boolean).join('')
}
add(1,2) getFullName({name: {first: 'Joe', middle: 'Bud', last: 'Matthews'}})

Run:

npm run flow

We won't see any error for this, if everything is correct type checking.

We have a 'validate' script setup previously:

"validate": "npm run lint && npm run prettier -- --list-different"

we can also add "flow" into it:

"validate": "npm run lint && npm run prettier -- --list-different && npm run flow"

In order to make 'validate' script works, we still need to install 'babel-eslint' for eslint to understand flow syntax:

Install:

npm i -D babel-eslint

.eslintrc:

{
"parser": "babel-eslint",
"parserOptions": {
"ecmaVersion": "2018"
},
"extends": [
"eslint:recommended",
"eslint-config-prettier"
],
"rules": {
"no-console": "off"
},
"env": {
"browser": true,
"node": true,
"es6": true,
"jest": true
}
}

Then run:

npm run validate

it will checks eslint, prettier and flow.

We have a few checks we’ll run in continuous integration when someone opens a pull request, but it’d be even better if we could run some of those checks before they even commit their code so they can fix it right away rather than waiting for CI to run. Let’s use husky’s precommit script to run our validation.

Install:

npm i -D husky

Hook with 'precommit' with valdiation:

"precommit": "npm run validate"

If you don't want this precommit hook and commit you changes anyway you can do:

git commit -m "bad stuff" --no-verify

Rather than failing when a developer has failed to format their files or run linting on all the files in the project on every commit, it would be even better to just automatically format the files on commit and only check the relevant files with eslint. Let’s use lint-staged to run scripts on the files that are going to be committed as part of our precommit hook.

Install:

npm i -D lint-staged

package.json:

"precommit": "lint-staged && npm run flow"

.lintstagedrc:

{
"linters": {
"*.js": [
"eslint"
],
"**/*.+(js|jsx|json|yml|yaml|css|less|scss|ts|tsx|md|graphql|mdx)": [
"prettier --write",
"git add"
]
}
}

Now when you do commit, it will automatcilly format all the code and readded to git and commit it.

Full code here.

 

[Testing] Static Analysis Testing JavaScript Applications的更多相关文章

  1. Unit Testing, Integration Testing and Functional Testing

    转载自:https://codeutopia.net/blog/2015/04/11/what-are-unit-testing-integration-testing-and-functional- ...

  2. Difference Between Performance Testing, Load Testing and Stress Testing

    http://www.softwaretestinghelp.com/what-is-performance-testing-load-testing-stress-testing/ Differen ...

  3. WWDC: Thread Sanitizer and Static Analysis

    Thread Sanitizer 过程 编译过程中链接了一个新的库.  也可以通过命令行来操作: $ clang -fsanitize=thread source.c -o executable $ ...

  4. Go testing 库 testing.T 和 testing.B 简介

    testing.T 判定失败接口 Fail 失败继续 FailNow 失败终止 打印信息接口 Log 数据流 (cout 类似) Logf format (printf 类似) SkipNow 跳过当 ...

  5. [Unit Testing] Fundamentals of Testing in Javascript

    In this lesson, we’ll get the most fundamental understanding of what an automated test is in JavaScr ...

  6. Penetration Testing、Security Testing、Automation Testing

    相关学习资料 http://www.cnblogs.com/LittleHann/p/3823513.html http://www.cnblogs.com/LittleHann/p/3828927. ...

  7. [Unit Testing] AngularJS Unit Testing - Karma

    Install Karam: npm install -g karma npm install -g karma-cli Init Karam: karma init First test: 1. A ...

  8. 测试理论--branch testing and boundary testing

    1 branch testing 分支测试 测试代码的所有分支 2 boundary testing 测试 程序的限制条件

  9. [React & Testing] Simulate Event testing

    Here we want to test a toggle button component, when the button was click, state should change, styl ...

随机推荐

  1. windows下升级pip失败,重新安装pip最新版本

    环境: python3.6.5 32bit,后改为python3.4.3 32bit pycharm2018旗舰版 问题: pycharm里的pip一直无法升级到10.0.1版本,在cmd中使用升级命 ...

  2. I2C驱动框架(二)

    参考:I2C子系统之I2C bus初始化——I2C_init() 在linux内核启动的时候最先执行的和I2C子系统相关的函数应该是driver/i2c/i2c-core.c文件中的i2c_init( ...

  3. 交换机VLAN的定义、意义以及划分方式

    什么是VLAN 虚拟网技术(VLAN,Virtual Local Area Network)的诞生主要源于广播.广播在网络中起着非常重要的作用,如发现新设备.调整网络路径.IP地址租赁等等,许多网络协 ...

  4. 《C#高级编程》笔记系列第三弹

    我们在开发WinForm时,经常会看到partial关键字,比如,我们新建一个Windows Form时,后台代码自动添加如下: 1 public partial class Form1 : Form ...

  5. IOS 自动布局-UIStackPanel和UIGridPanel(五)

    试想这样的一个需求场合,一个button靠右显示,并且距离superView的顶部和右边间距分别为10和5.如下图所示: 要实现这样的需求,如果不用自动布局技术,那么我们能想到的就是老老实实的使用绝对 ...

  6. 【JavaScript 13—应用总结】:锁屏遮罩

    导读:上次说了,当弹出登录框时,由于背景色和弹出框时一样的,这样子,其实比较难聚焦到底该操作哪一块.所以,如果,有了颜色的区分,那么通过屏幕遮罩的效果,就可以将我们希望要被处理的东西突出显示.也就达到 ...

  7. 九度oj 题目1367:二叉搜索树的后序遍历序列

    题目描述: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 输入: 每个测试案例包括2行: 第一行为1个整数 ...

  8. iOS学习笔记21-推送证书与秘钥申请

    ---恢复内容开始--- 一.iOS证书 iOS常用的证书包括开发证书和发布证书,无论是真机调试还是最终发布应用到App Store这两个证书都是必须的,它是iOS开发的基本证书.Xcode7以后真机 ...

  9. [POJ1143]Number Game

    [POJ1143]Number Game 试题描述 Christine and Matt are playing an exciting game they just invented: the Nu ...

  10. BZOJ 3282 Tree ——Link-Cut Tree

    [题目分析] 明显的LCT维护连通性的题目. access的操作是比较巧妙的,可以把结点到根变成偏爱路径,而且保证了该点是链上深度最深的点. 而且需边的思想也很巧妙,保证了复杂度. 但是只能用于修改路 ...