Skip to content

Commit e3cf562

Browse files
committed
Add "globals" option in package.json (fix #177)
1 parent 8123d95 commit e3cf562

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,36 @@ Or, disable the `"no-use-before-define"` rule for **multiple lines**:
263263
/*eslint-enable no-use-before-define */
264264
```
265265

266+
### I use a library that pollutes the global namespace. How do I prevent "variable is not defined" errors?
267+
268+
Some packages (e.g. `mocha`) put their functions (e.g. `describe`, `it`) on the global
269+
object (poor form!). Since these functions are not defined or `require`d anywhere in your
270+
code, `standard` will warn that you're using a variable that is not defined (usually, this
271+
rule is really useful for catching typos!). But we want to disable it for these global
272+
variables.
273+
274+
To let `standard` (as well as humans reading your code) know that certain variables are
275+
global in your code, add this to the top of your file:
276+
277+
```
278+
/* global myVar1, myVar2 */
279+
```
280+
281+
If you have hundreds of files, adding comments to every file can be tedious. In these
282+
cases, you can add this to `package.json`:
283+
284+
```js
285+
{
286+
"standard": {
287+
"global": [ "myVar1", "myVar2" ]
288+
}
289+
}
290+
```
291+
266292
### Can I use a custom JS parser for bleeding-edge ES6 or ES7 support?
267293

268294
`standard` supports custom JS parsers. To use a custom parser, install it from npm
269-
(example: `npm install babel-eslint`) and add this to your package.json:
295+
(example: `npm install babel-eslint`) and add this to your `package.json`:
270296

271297
```js
272298
{

index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,17 @@ function parseOpts (opts) {
150150
var packageOpts = pkgConfig('standard', { root: false, cwd: opts.cwd })
151151

152152
if (packageOpts) {
153-
// Use ignore patterns from package.json
153+
// Use ignore patterns from package.json ("standard.ignore" property)
154154
if (packageOpts.ignore) addIgnorePattern(packageOpts.ignore)
155155

156-
// Use custom js parser from package.json
156+
// Use globals from package.json ("standard.globals" property)
157+
if (packageOpts.globals) {
158+
opts._config.globals = Array.isArray(packageOpts.globals)
159+
? packageOpts.globals
160+
: [ packageOpts.globals ]
161+
}
162+
163+
// Use custom js parser from package.json ("standard.parser" property)
157164
if (!opts.parser && packageOpts.parser) useCustomParser(packageOpts.parser)
158165
}
159166

0 commit comments

Comments
 (0)