UNPKG

31.8 kBMarkdownView Raw
1<h1 align="center">
2 <a href="https://standardjs.com"><img src="https://cdn.rawgit.com/standard/standard/master/sticker.svg" alt="Standard - JavaScript Style Guide" width="200"></a>
3 <br>
4 JavaScript Standard Style
5 <br>
6 <br>
7</h1>
8
9<p align="center">
10 <a href="https://travis-ci.org/standard/standard"><img src="https://img.shields.io/travis/standard/standard/master.svg" alt="travis"></a>
11 <a href="https://www.npmjs.com/package/standard"><img src="https://img.shields.io/npm/v/standard.svg" alt="npm version"></a>
12 <a href="https://www.npmjs.com/package/eslint-config-standard"><img src="https://img.shields.io/npm/dm/eslint-config-standard.svg" alt="npm downloads"></a>
13 <a href="https://standardjs.com"><img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard - JavaScript Style Guide"></a>
14</p>
15
16<p align="center">
17 <a href="/docs/README-en.md">English</a>
18 <a href="/docs/README-esla.md">Español (Latinoamérica)</a>
19 <a href="/docs/README-fr.md">Français</a>
20 <a href="/docs/README-iteu.md">Italiano (Italian)</a>
21 <a href="/docs/README-ja.md">日本語 (Japanese)</a>
22 <a href="/docs/README-kokr.md">한국어 (Korean)</a>
23 <a href="/docs/README-ptbr.md">Português (Brasil)</a>
24 <a href="/docs/README-zhcn.md">简体中文 (Simplified Chinese)</a>
25 <a href="/docs/README-zhtw.md">繁體中文 (Taiwanese Mandarin)</a>
26</p>
27
28## JavaScript style guide, linter, and formatter
29
30This module saves you (and others!) time in three ways:
31
32- **No configuration.** The easiest way to enforce consistent style in your
33 project. Just drop it in.
34- **Automatically format code.** Just run `standard --fix` and say goodbye to
35 messy or inconsistent code.
36- **Catch style issues & programmer errors early.** Save precious code review
37 time by eliminating back-and-forth between reviewer & contributor.
38
39No decisions to make. No `.eslintrc`, `.jshintrc`, or `.jscsrc` files to manage. It just
40works.
41
42Install with:
43
44```
45npm install standard --save-dev
46```
47
48### Open Source Supporters
49
50<a href="https://tidelift.com/subscription/pkg/npm-standard?utm_source=npm-standard&utm_medium=readme" target='_blank'><img src='https://feross.org/images/supporters/tidelift.png' width=250></a>
51
52[Become a supporter!](https://feross.org/thanks/)
53
54## StandardJS — The Rules
55
56- **2 spaces** – for indentation
57- **Single quotes for strings** – except to avoid escaping
58- **No unused variables** – this one catches *tons* of bugs!
59- **No semicolons** – [It's][1] [fine.][2] [Really!][3]
60 - [More details][4]
61- **Space after keywords** `if (condition) { ... }`
62- **Space after function name** `function name (arg) { ... }`
63- Always use `===` instead of `==` – but `obj == null` is allowed to check `null || undefined`.
64- Always handle the node.js `err` function parameter
65- Declare browser globals with `/* global */` comment at top of file
66 - Prevents accidental use of vaguely-named browser globals like `open`, `length`,
67 `event`, and `name`.
68 - Example: `/* global alert, prompt */`
69 - Exceptions are: `window`, `document`, and `navigator`
70- **And [more goodness][5]***give `standard` a try today!*
71
72[1]: http://blog.izs.me/post/2353458699/an-open-letter-to-javascript-leaders-regarding
73[2]: http://inimino.org/~inimino/blog/javascript_semicolons
74[3]: https://www.youtube.com/watch?v=gsfbh17Ax9I
75[4]: /RULES.md#semicolons
76[5]: /RULES.md#javascript-standard-style
77
78To get a better idea, take a look at
79[a sample file](https://github.com/expressjs/body-parser/blob/master/index.js) written
80in JavaScript Standard Style. Or, check out one of the
81[thousands of projects](https://raw.githubusercontent.com/standard/standard-packages/master/all.json)
82that use `standard`!
83
84## Table of Contents
85
86- Quick start
87 - [Install](#install)
88 - [Usage](#usage)
89 - [What you might do if you're clever](#what-you-might-do-if-youre-clever)
90- FAQ
91 - [Why should I use JavaScript Standard Style?](#why-should-i-use-javascript-standard-style)
92 - [Who uses JavaScript Standard Style?](#who-uses-javascript-standard-style)
93 - [Are there text editor plugins?](#are-there-text-editor-plugins)
94 - [Is there a readme badge?](#is-there-a-readme-badge)
95 - [I disagree with rule X, can you change it?](#i-disagree-with-rule-x-can-you-change-it)
96 - [But this isn't a real web standard!](#but-this-isnt-a-real-web-standard)
97 - [Is there an automatic formatter?](#is-there-an-automatic-formatter)
98 - [How do I ignore files?](#how-do-i-ignore-files)
99 - [How do I hide a certain warning?](#how-do-i-hide-a-certain-warning)
100 - [I use a library that pollutes the global namespace. How do I prevent "variable is not defined" errors?](#i-use-a-library-that-pollutes-the-global-namespace-how-do-i-prevent-variable-is-not-defined-errors)
101 - [How do I use experimental JavaScript (ES Next) features?](#how-do-i-use-experimental-javascript-es-next-features)
102 - [Can I use a JavaScript language variant, like Flow or TypeScript?](#can-i-use-a-javascript-language-variant-like-flow-or-typescript)
103 - [What about Mocha, Jest, Jasmine, QUnit, etc?](#what-about-mocha-jest-jasmine-qunit-etc)
104 - [What about Web Workers and Service Workers?](#what-about-web-workers-and-service-workers)
105 - [Can I check code inside of Markdown or HTML files?](#can-i-check-code-inside-of-markdown-or-html-files)
106 - [Is there a Git `pre-commit` hook?](#is-there-a-git-pre-commit-hook)
107 - [How do I make the output all colorful and pretty?](#how-do-i-make-the-output-all-colorful-and-pretty)
108 - [Is there a Node.js API?](#is-there-a-nodejs-api)
109 - [How do I contribute to StandardJS?](#how-do-i-contribute-to-standardjs)
110- [License](#license)
111
112## Install
113
114The easiest way to use JavaScript Standard Style is to install it globally as a
115Node command line program. Run the following command in Terminal:
116
117```bash
118$ npm install standard --global
119```
120
121Or, you can install `standard` locally, for use in a single project:
122
123```bash
124$ npm install standard --save-dev
125```
126
127*Note: To run the preceding commands, [Node.js](http://nodejs.org) and [npm](https://npmjs.com) must be installed.*
128
129## Usage
130
131After you've installed `standard`, you should be able to use the `standard` program. The
132simplest use case would be checking the style of all JavaScript files in the
133current working directory:
134
135```bash
136$ standard
137Error: Use JavaScript Standard Style
138 lib/torrent.js:950:11: Expected '===' and instead saw '=='.
139```
140
141If you've installed `standard` locally, run with `npx` instead:
142
143```bash
144$ npx standard
145```
146
147You can optionally pass in a directory (or directories) using the glob pattern. Be
148sure to quote paths containing glob patterns so that they are expanded by
149`standard` instead of your shell:
150
151```bash
152$ standard "src/util/**/*.js" "test/**/*.js"
153```
154
155**Note:** by default `standard` will look for all files matching the patterns:
156`**/*.js`, `**/*.jsx`.
157
158## What you might do if you're clever
159
1601. Add it to `package.json`
161
162 ```json
163 {
164 "name": "my-cool-package",
165 "devDependencies": {
166 "standard": "*"
167 },
168 "scripts": {
169 "test": "standard && node my-tests.js"
170 }
171 }
172 ```
173
1742. Style is checked automatically when you run `npm test`
175
176 ```bash
177 $ npm test
178 Error: Use JavaScript Standard Style
179 lib/torrent.js:950:11: Expected '===' and instead saw '=='.
180 ```
181
1823. Never give style feedback on a pull request again!
183
184## Why should I use JavaScript Standard Style?
185
186The beauty of JavaScript Standard Style is that it's simple. No one wants to
187maintain multiple hundred-line style configuration files for every module/project
188they work on. Enough of this madness!
189
190This module saves you (and others!) time in three ways:
191
192- **No configuration.** The easiest way to enforce consistent style in your
193 project. Just drop it in.
194- **Automatically format code.** Just run `standard --fix` and say goodbye to
195 messy or inconsistent code.
196- **Catch style issues & programmer errors early.** Save precious code review
197 time by eliminating back-and-forth between reviewer & contributor.
198
199Adopting `standard` style means ranking the importance of code clarity and
200community conventions higher than personal style. This might not make sense for
201100% of projects and development cultures, however open source can be a hostile
202place for newbies. Setting up clear, automated contributor expectations makes a
203project healthier.
204
205For more info, see the conference talk ["Write Perfect Code with Standard and
206ESLint"](https://www.youtube.com/watch?v=kuHfMw8j4xk). In this talk, you'll learn
207about linting, when to use `standard` versus `eslint`, and how `prettier` compares
208to `standard`.
209
210## Who uses JavaScript Standard Style?
211
212Lots of folks!
213
214[<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/nodejs.png>](https://nodejs.org) | [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/npm.png>](https://www.npmjs.com) | [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/github.png>](https://github.com) | [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/elastic.png>](https://www.elastic.co) |
215|---|---|---|---|
216
217[<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/express.png>](http://expressjs.com) | [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/electron.png>](http://electron.atom.io) | [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/nuxtjs.png>](https://nuxtjs.org/) | [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/atom.png>](https://atom.io) |
218|---|---|---|---|
219
220| [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/mongodb.jpg>](https://www.mongodb.com) | [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/zendesk.png>](https://www.zendesk.com) | [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/brave.png>](https://www.brave.com) | [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/zeit.png>](https://zeit.co) |
221|---|---|---|---|
222
223| [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/nodesource.png>](https://nodesource.com) | [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/nearform.png>](http://www.nearform.com) | [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/typeform.png>](https://www.typeform.com) | [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/gov-uk.png>](https://gds.blog.gov.uk) |
224|---|---|---|---|
225
226| [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/webtorrent.png>](https://webtorrent.io) | [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/ipfs.png>](https://ipfs.io) | [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/dat.png>](https://datproject.org) | [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/bitcoinjs.png>](https://bitcoinjs.org) |
227|---|---|---|---|
228
229| [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/voltra.png>](https://voltra.co) | [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/treasuredata.png>](https://www.treasuredata.com) | [<img alt="Free MIDIs, MIDI file downloads" width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/bitmidi.png>](https://bitmidi.com) | [<img width=190 alt="College essays, AP notes" src=https://cdn.rawgit.com/standard/standard/master/docs/logos/studynotes.jpg>](https://www.apstudynotes.org) |
230|---|---|---|---|
231
232| [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/optiopay.png>](https://www.optiopay.com) | [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/jaguar-landrover.png>](https://www.jlrtechincubator.com/jlrti/) | [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/bustle.jpg>](https://www.bustle.com) | [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/zentrick.png>](https://www.zentrick.com) |
233|---|---|---|---|
234
235| [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/greenkeeper.png>](https://greenkeeper.io) | [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/karma.png>](https://karma-runner.github.io) | [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/taser.png>](https://www.taser.com) | [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/neo4j.png>](https://www.neo4j.com) |
236|---|---|---|---|
237
238| [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/rentograph.png>](https://rentograph.com) | [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/eaze.png>](https://www.eaze.com) | [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/ctrl-alt-deseat.png>](https://www.ctrlaltdeseat.com) | [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/clevertech.png>](https://clevertech.biz) |
239|---|---|---|---|
240
241| [<img width=190 src=https://cdn.rawgit.com/standard/standard/master/docs/logos/flowsent.png>](https://www.flowsent.com) | Your logo here | Your logo here | Your logo here
242|---|---|---|---|
243
244In addition to companies, many community members use `standard` on packages that
245are [too numerous](https://raw.githubusercontent.com/standard/standard-packages/master/all.json)
246to list here.
247
248`standard` is also the top-starred linter in GitHub's
249[Clean Code Linter](https://github.com/showcases/clean-code-linters) showcase.
250
251## Are there text editor plugins?
252
253First, install `standard`. Then, install the appropriate plugin for your editor:
254
255### Sublime Text
256
257Using **[Package Control][sublime-1]**, install **[SublimeLinter][sublime-2]** and
258**[SublimeLinter-contrib-standard][sublime-3]**.
259
260For automatic formatting on save, install **[StandardFormat][sublime-4]**.
261
262[sublime-1]: https://packagecontrol.io/
263[sublime-2]: http://www.sublimelinter.com/en/latest/
264[sublime-3]: https://packagecontrol.io/packages/SublimeLinter-contrib-standard
265[sublime-4]: https://packagecontrol.io/packages/StandardFormat
266
267### Atom
268
269Install **[linter-js-standard][atom-1]**.
270
271Alternatively, you can install **[linter-js-standard-engine][atom-4]**. Instead of
272bundling a version of `standard` it will automatically use the version installed
273in your current project. It will also work out of the box with other linters based
274on **[standard-engine][atom-5]**.
275
276For automatic formatting, install **[standard-formatter][atom-2]**. For snippets,
277install **[standardjs-snippets][atom-3]**.
278
279[atom-1]: https://atom.io/packages/linter-js-standard
280[atom-2]: https://atom.io/packages/standard-formatter
281[atom-3]: https://atom.io/packages/standardjs-snippets
282[atom-4]: https://atom.io/packages/linter-js-standard-engine
283[atom-5]: https://github.com/Flet/standard-engine
284
285### Visual Studio Code
286
287Install **[vscode-standardjs][vscode-1]**. (Includes support for automatic formatting.)
288
289For JS snippets, install: **[vscode-standardjs-snippets][vscode-2]**. For React snippets, install **[vscode-react-standard][vscode-3]**.
290
291[vscode-1]: https://marketplace.visualstudio.com/items/chenxsan.vscode-standardjs
292[vscode-2]: https://marketplace.visualstudio.com/items?itemName=capaj.vscode-standardjs-snippets
293[vscode-3]: https://marketplace.visualstudio.com/items/TimonVS.ReactSnippetsStandard
294
295### Vim
296
297Install **[ale][vim-1]**. And add these lines to your `.vimrc` file.
298
299```vim
300let g:ale_linters = {
301\ 'javascript': ['standard'],
302\}
303let g:ale_fixers = {'javascript': ['standard']}
304```
305
306This sets standard as your only linter and fixer for javascript files and so prevents conflicts with eslint. For linting and automatic fixing on save, add these lines to `.vimrc`:
307```vim
308let g:ale_lint_on_save = 1
309let g:ale_fix_on_save = 1
310```
311
312
313Alternative plugins to consider include [neomake][vim-2] and [syntastic][vim-3], both of which have built-in support for `standard` (though configuration may be necessary).
314
315[vim-1]: https://github.com/w0rp/ale
316[vim-2]: https://github.com/neomake/neomake
317[vim-3]: https://github.com/vim-syntastic/syntastic
318
319### Emacs
320
321Install **[Flycheck][emacs-1]** and check out the **[manual][emacs-2]** to learn
322how to enable it in your projects.
323
324[emacs-1]: http://www.flycheck.org
325[emacs-2]: http://www.flycheck.org/en/latest/user/installation.html
326
327### Brackets
328
329Search the extension registry for **["Standard Code Style"][brackets-1]** and click "Install".
330
331[brackets-1]: https://github.com/ishamf/brackets-standard/
332
333### WebStorm (PhpStorm, IntelliJ, RubyMine, JetBrains, etc.)
334
335WebStorm [recently announced native support](https://blog.jetbrains.com/webstorm/2017/01/webstorm-2017-1-eap-171-2272/)
336for `standard` directly in the IDE.
337
338If you still prefer to configure `standard` manually, [follow this guide][webstorm-1]. This applies to all JetBrains products, including PhpStorm, IntelliJ, RubyMine, etc.
339
340[webstorm-1]: docs/webstorm.md
341
342## Is there a readme badge?
343
344Yes! If you use `standard` in your project, you can include one of these badges in
345your readme to let people know that your code is using the standard style.
346
347[![JavaScript Style Guide](https://cdn.rawgit.com/standard/standard/master/badge.svg)](https://github.com/standard/standard)
348
349```md
350[![JavaScript Style Guide](https://cdn.rawgit.com/standard/standard/master/badge.svg)](https://github.com/standard/standard)
351```
352
353[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
354
355```md
356[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
357```
358
359## I disagree with rule X, can you change it?
360
361No. The whole point of `standard` is to save you time by avoiding
362[bikeshedding][bikeshedding] about code style. There are lots of debates online about
363tabs vs. spaces, etc. that will never be resolved. These debates just distract from
364getting stuff done. At the end of the day you have to 'just pick something', and
365that's the whole philosophy of `standard` -- its a bunch of sensible 'just pick
366something' opinions. Hopefully, users see the value in that over defending their
367own opinions.
368
369There are a couple of similar packages for anyone who does not want to completely accept `standard`:
370- [semistandard](https://github.com/Flet/semistandard) - standard, with semicolons
371- [standardx](https://github.com/standard/standardx) - standard, with custom tweaks
372
373If you really want to configure hundreds of ESLint rules individually, you can
374always use `eslint` directly with
375[eslint-config-standard](https://github.com/standard/eslint-config-standard) to
376layer your changes on top.
377[`standard-eject`](https://github.com/josephfrazier/standard-eject) can help
378you migrate from `standard` to `eslint` and `eslint-config-standard`.
379
380Pro tip: Just use `standard` and move on. There are actual real problems that you
381could spend your time solving! :P
382
383[bikeshedding]: https://www.freebsd.org/doc/en/books/faq/misc.html#bikeshed-painting
384
385## But this isn't a real web standard!
386
387Of course it's not! The style laid out here is not affiliated with any official web
388standards groups, which is why this repo is called `standard/standard` and not
389`ECMA/standard`.
390
391The word "standard" has more meanings than just "web standard" :-) For example:
392
393- This module helps hold our code to a high *standard of quality*.
394- This module ensures that new contributors follow some basic *style standards*.
395
396## Is there an automatic formatter?
397
398Yes! You can use `standard --fix` to fix most issues automatically.
399
400`standard --fix` is built into `standard` for maximum convenience. Most problems
401are fixable, but some errors (like forgetting to handle errors) must be fixed
402manually.
403
404To save you time, `standard` outputs the message "`Run standard --fix to
405automatically fix some problems`" when it detects problems that can be fixed
406automatically.
407
408## How do I ignore files?
409
410Certain paths (`node_modules/`, `coverage/`, `vendor/`, `*.min.js`, `bundle.js`,
411and files/folders that begin with `.` like `.git/`) are automatically ignored.
412
413Paths in a project's root `.gitignore` file are also automatically ignored.
414
415Sometimes you need to ignore additional folders or specific minified files. To do
416that, add a `standard.ignore` property to `package.json`:
417
418```json
419"standard": {
420 "ignore": [
421 "**/out/",
422 "/lib/select2/",
423 "/lib/ckeditor/",
424 "tmp.js"
425 ]
426}
427```
428
429## How do I hide a certain warning?
430
431In rare cases, you'll need to break a rule and hide the warning generated by
432`standard`.
433
434JavaScript Standard Style uses [ESLint](http://eslint.org/) under-the-hood and
435you can hide warnings as you normally would if you used ESLint directly.
436
437To get verbose output (so you can find the particular rule name to ignore), run:
438
439```bash
440$ standard --verbose
441Error: Use JavaScript Standard Style
442 routes/error.js:20:36: 'file' was used before it was defined. (no-use-before-define)
443```
444
445Disable **all rules** on a specific line:
446
447```js
448file = 'I know what I am doing' // eslint-disable-line
449```
450
451Or, disable **only** the `"no-use-before-define"` rule:
452
453```js
454file = 'I know what I am doing' // eslint-disable-line no-use-before-define
455```
456
457Or, disable the `"no-use-before-define"` rule for **multiple lines**:
458
459```js
460/* eslint-disable no-use-before-define */
461console.log('offending code goes here...')
462console.log('offending code goes here...')
463console.log('offending code goes here...')
464/* eslint-enable no-use-before-define */
465```
466
467## I use a library that pollutes the global namespace. How do I prevent "variable is not defined" errors?
468
469Some packages (e.g. `mocha`) put their functions (e.g. `describe`, `it`) on the
470global object (poor form!). Since these functions are not defined or `require`'d
471anywhere in your code, `standard` will warn that you're using a variable that is
472not defined (usually, this rule is really useful for catching typos!). But we want
473to disable it for these global variables.
474
475To let `standard` (as well as humans reading your code) know that certain variables
476are global in your code, add this to the top of your file:
477
478```js
479/* global myVar1, myVar2 */
480```
481
482If you have hundreds of files, it may be desirable to avoid adding comments to
483every file. In this case, run:
484
485```bash
486$ standard --global myVar1 --global myVar2
487```
488
489Or, add this to `package.json`:
490
491```json
492{
493 "standard": {
494 "globals": [ "myVar1", "myVar2" ]
495 }
496}
497```
498
499*Note: `global` and `globals` are equivalent.*
500
501## How do I use experimental JavaScript (ES Next) features?
502
503`standard` supports the latest ECMAScript features, ES8 (ES2017), including
504language feature proposals that are in "Stage 4" of the proposal process.
505
506To support experimental language features, `standard` supports specifying a
507custom JavaScript parser. Before using a custom parser, consider whether the added
508complexity is worth it.
509
510To use a custom parser, first install it from npm:
511
512```bash
513npm install babel-eslint --save-dev
514```
515
516Then run:
517
518```bash
519$ standard --parser babel-eslint
520```
521
522Or, add this to `package.json`:
523
524```json
525{
526 "standard": {
527 "parser": "babel-eslint"
528 }
529}
530```
531
532## Can I use a JavaScript language variant, like Flow or TypeScript?
533
534`standard` supports the latest ECMAScript features. However, Flow and TypeScript add new
535syntax to the language, so they are not supported out-of-the-box.
536
537To support JavaScript language variants, `standard` supports specifying a custom JavaScript
538parser as well as an ESLint plugin to handle the changed syntax. Before using a JavaScript
539language variant, consider whether the added complexity is worth it.
540
541### Flow
542
543To use Flow, you need to run `standard` with `babel-eslint` as the parser and
544`eslint-plugin-flowtype` as a plugin.
545
546```bash
547npm install babel-eslint eslint-plugin-flowtype --save-dev
548```
549
550Then run:
551
552```bash
553$ standard --parser babel-eslint --plugin flowtype
554```
555
556Or, add this to `package.json`:
557
558```json
559{
560 "standard": {
561 "parser": "babel-eslint",
562 "plugins": [ "flowtype" ]
563 }
564}
565```
566
567*Note: `plugin` and `plugins` are equivalent.*
568
569### TypeScript
570
571To use TypeScript, you need to run `standard` with `@typescript-eslint/parser` as the parser,
572`@typescript-eslint/eslint-plugin` as a plugin, and tell standard to lint `*.ts` files (since it
573doesn't by default).
574
575```bash
576npm install @typescript-eslint/parser @typescript-eslint/eslint-plugin --save-dev
577```
578
579Then run:
580
581```bash
582$ standard --parser @typescript-eslint/parser --plugin @typescript-eslint/eslint-plugin *.ts
583```
584
585Or, add this to `package.json`:
586
587```json
588{
589 "standard": {
590 "parser": "@typescript-eslint/parser",
591 "plugins": [ "@typescript-eslint/eslint-plugin" ]
592 }
593}
594```
595
596With that in `package.json`, you can run:
597
598```bash
599standard *.ts
600```
601
602## What about Mocha, Jest, Jasmine, QUnit, etc?
603
604To support mocha in test files, add this to the top of the test files:
605
606```js
607/* eslint-env mocha */
608```
609
610Or, run:
611
612```bash
613$ standard --env mocha
614```
615
616Where `mocha` can be one of `jest`, `jasmine`, `qunit`, `phantomjs`, and so on. To see a
617full list, check ESLint's
618[specifying environments](http://eslint.org/docs/user-guide/configuring.html#specifying-environments)
619documentation. For a list of what globals are available for these environments,
620check the
621[globals](https://github.com/sindresorhus/globals/blob/master/globals.json) npm
622module.
623
624*Note: `env` and `envs` are equivalent.*
625
626## What about Web Workers and Service Workers?
627
628Add this to the top of web worker files:
629
630```js
631/* eslint-env worker */
632```
633
634This lets `standard` (as well as humans reading the code) know that `self` is a
635global in web worker code.
636
637For Service workers, add this instead:
638
639```js
640/* eslint-env serviceworker */
641```
642
643## Can I check code inside of Markdown or HTML files?
644
645To check code inside Markdown files, use [`standard-markdown`](https://www.npmjs.com/package/standard-markdown).
646
647Alternatively, there are ESLint plugins that can check code inside Markdown, HTML,
648and many other types of language files:
649
650To check code inside Markdown files, use an ESLint plugin:
651
652```bash
653$ npm install eslint-plugin-markdown
654```
655
656Then, to check JS that appears inside code blocks, run:
657
658```bash
659$ standard --plugin markdown '**/*.md'
660```
661
662To check code inside HTML files, use an ESLint plugin:
663
664```bash
665$ npm install eslint-plugin-html
666```
667
668Then, to check JS that appears inside `<script>` tags, run:
669
670```bash
671$ standard --plugin html '**/*.html'
672```
673
674## Is there a Git `pre-commit` hook?
675
676Funny you should ask!
677
678```bash
679#!/bin/bash
680
681# Ensure all JavaScript files staged for commit pass standard code style
682function xargs-r() {
683 # Portable version of "xargs -r". The -r flag is a GNU extension that
684 # prevents xargs from running if there are no input files.
685 if IFS= read -r -d $'\n' path; then
686 { echo "$path"; cat; } | xargs $@
687 fi
688}
689git diff --name-only --cached --relative | grep '\.jsx\?$' | sed 's/[^[:alnum:]]/\\&/g' | xargs-r -E '' -t standard
690if [[ $? -ne 0 ]]; then
691 echo 'JavaScript Standard Style errors were detected. Aborting commit.'
692 exit 1
693fi
694```
695
696## How do I make the output all colorful and pretty?
697
698The built-in output is simple and straightforward, but if you like shiny things,
699install [snazzy](https://www.npmjs.com/package/snazzy):
700
701```bash
702$ npm install snazzy
703```
704
705And run:
706
707```bash
708$ standard --verbose | snazzy
709```
710
711There's also [standard-tap](https://www.npmjs.com/package/standard-tap),
712[standard-json](https://www.npmjs.com/package/standard-json),
713[standard-reporter](https://www.npmjs.com/package/standard-reporter), and
714[standard-summary](https://www.npmjs.com/package/standard-summary).
715
716## Is there a Node.js API?
717
718Yes!
719
720### `standard.lintText(text, [opts], callback)`
721
722Lint the provided source `text`. An `opts` object may be provided:
723
724```js
725{
726 cwd: '', // current working directory (default: process.cwd())
727 filename: '', // path of the file containing the text being linted (optional, though some eslint plugins require it)
728 fix: false, // automatically fix problems
729 globals: [], // custom global variables to declare
730 plugins: [], // custom eslint plugins
731 envs: [], // custom eslint environment
732 parser: '' // custom js parser (e.g. babel-eslint)
733}
734```
735
736Additional options may be loaded from a `package.json` if it's found for the
737current working directory.
738
739The `callback` will be called with an `Error` and `results` object.
740
741The `results` object will contain the following properties:
742
743```js
744var results = {
745 results: [
746 {
747 filePath: '',
748 messages: [
749 { ruleId: '', message: '', line: 0, column: 0 }
750 ],
751 errorCount: 0,
752 warningCount: 0,
753 output: '' // fixed source code (only present with {fix: true} option)
754 }
755 ],
756 errorCount: 0,
757 warningCount: 0
758}
759```
760
761### `results = standard.lintTextSync(text, [opts])`
762
763Synchronous version of `standard.lintText()`. If an error occurs, an exception is
764thrown. Otherwise, a `results` object is returned.
765
766### `standard.lintFiles(files, [opts], callback)`
767
768Lint the provided `files` globs. An `opts` object may be provided:
769
770```js
771var opts = {
772 ignore: [], // file globs to ignore (has sane defaults)
773 cwd: '', // current working directory (default: process.cwd())
774 fix: false, // automatically fix problems
775 globals: [], // global variables to declare
776 plugins: [], // eslint plugins
777 envs: [], // eslint environment
778 parser: '' // js parser (e.g. babel-eslint)
779}
780```
781
782The `callback` will be called with an `Error` and `results` object (same as above).
783
784## How do I contribute to StandardJS?
785
786Contributions are welcome! Check out the [issues](https://github.com/standard/standard/issues) or the [PRs](https://github.com/standard/standard/pulls), and make your own if you want something that you don't see there.
787
788Want to chat? Join contributors on IRC in the `#standard` channel on freenode.
789
790Here are some important packages in the `standard` ecosystem:
791
792- **[standard](https://github.com/standard/standard)** - this repo
793 - **[standard-engine](https://github.com/flet/standard-engine)** - cli engine for arbitrary eslint rules
794 - **[eslint-config-standard](https://github.com/standard/eslint-config-standard)** - eslint rules for standard
795 - **[eslint-config-standard-jsx](https://github.com/standard/eslint-config-standard-jsx)** - eslint rules for standard (JSX)
796 - **[eslint-plugin-standard](https://github.com/xjamundx/eslint-plugin-standard)** - custom eslint rules for standard (not part of eslint core)
797 - **[eslint](https://github.com/eslint/eslint)** - the linter that powers standard
798- **[snazzy](https://github.com/standard/snazzy)** - pretty terminal output for standard
799- **[standard-www](https://github.com/standard/standard-www)** - code for https://standardjs.com
800- **[semistandard](https://github.com/Flet/semistandard)** - standard, with semicolons (if you must)
801- **[standardx](https://github.com/standard/standardx)** - standard, with custom tweaks
802
803There are also many **[editor plugins](#are-there-text-editor-plugins)**, a list of
804**[npm packages that use `standard`](https://github.com/standard/standard-packages)**,
805and an awesome list of
806**[packages in the `standard` ecosystem](https://github.com/standard/awesome-standard)**.
807
808## Security Policies and Procedures
809
810The `standard` team and community take all security bugs in `standard` seriously. Please see our [security policies and procedures](https://github.com/standard/standard/blob/master/SECURITY.md) document to learn how to report issues.
811
812## License
813
814[MIT](LICENSE). Copyright (c) [Feross Aboukhadijeh](https://feross.org).