LEARNING JAVASCRIPT - Trang 52

• Do you use tabs or space to indent? A

recent StackOverflow poll

showed that the

majority of programmers prefer tabs, but that more experienced programmers

prefer spaces. I will let you choose your own path here….

• Do you prefer single or double quotes for strings? It doesn’t matter what you

answer here…we want to be able to use either equally.

• What line endings do you use (Unix or Windows)? If you’re on Linux or OS X,

choose Unix. If you’re on Windows, choose Windows.

• Do you require semicolons? Yes.
• Are you using ECMAScript 6 (ES6) features? Yes.
• Where will your code run (Node or in the browser)? Ideally, you would use a dif‐

ferent configuration for browser and Node code, but that’s a more advanced con‐

figuration. Go ahead and choose Node.

• Do you want to use JSX? No. (JSX is a XML-based extension to JavaScript that is

used in Facebook’s React UI library. We won’t be using it in this book.)

• What format do you want your config file to be in (JSON or YAML)? Choose

JSON (YAML is a popular data serialization format like JSON, but JSON is more

appropriate for JavaScript development).

After you’ve answered all the questions, you will have a .eslintrc file, and we can start

using ESLint.
There are several ways to run ESLint. You can run it directly (for example,

eslint

es6/test.js

), integrate into your editor, or add it to your Gulpfile. Editor integration

is great, but the instructions differ for every editor and operating system: if you want

editor integration, I recommend Googling the name of your editor with “eslint.”
Whether or not you use editor integration, I recommend adding ESLint to your

Gulpfile. After all, we have to run Gulp when we want to build, so that’s also a great

time to check the quality of our code. First, run:

npm

install

--

save

-

dev

gulp

-

eslint

Then modify gulpfile.js:

const

gulp

=

require

(

'gulp'

);

const

babel

=

require

(

'gulp-babel'

);

const

eslint

=

require

(

'gulp-eslint'

);

gulp

.

task

(

'default'

,

function

() {

// Run ESLint

gulp

.

src

([

"es6/**/*.js"

,

"public/es6/**/*.js"

])

.

pipe

(

eslint

())

.

pipe

(

eslint

.

format

());

// Node source

gulp

.

src

(

"es6/**/*.js"

)

28 | Chapter 2: JavaScript Development Tools