Skip to content

Commit

Permalink
Add globals cli flag (ref eslint#692)
Browse files Browse the repository at this point in the history
Adds a --globals cli flag to define global variables. Takes a comma-
separated list of variable names to define. By default, all varibles
are read-only, but appending `:true` to the name will mark it as
writable.

Example:

    eslint --globals require,exports:true file.js
  • Loading branch information
btmills committed Apr 3, 2014
1 parent 4a02025 commit cc595d7
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs/command-line-interface/README.md
Expand Up @@ -28,6 +28,7 @@ Options:
-f, --format String use a specific output format - default: stylish
--reset set all default rules to off
--eslintrc enable loading .eslintrc configuration. - default: true
--globals String define global variables
-v, --version outputs the version number
```

Expand Down Expand Up @@ -93,6 +94,14 @@ Example

eslint --env browser,node file.js

### `--globals`

This option defines one or more comma-separated global variables so that they will not be flagged as undefined by the `no-undef` rule. All are considered by default to be read-only, but appending `:true` to the name of the variable makes it writable.

Example:

eslint --globals require,exports:true file.js

### `-v`, `--version`

This option outputs the current ESLint version onto the console. All other options are ignored when present.
Expand Down
8 changes: 8 additions & 0 deletions lib/config.js
Expand Up @@ -130,6 +130,13 @@ function Config(options) {
this.baseConfig.format = options.format;
this.useEslintrc = (options.eslintrc !== false);
this.env = options.env;
this.globals = (options.globals || "").split(",").reduce(function (globals, def) {

// Default "foo" to false and handle "foo:false" and "foo:true"
var parts = def.split(":");
globals[parts[0]] = (parts.length > 1 && parts[1] === "true");
return globals;
}, {});
useConfig = options.config;

if (useConfig) {
Expand Down Expand Up @@ -188,6 +195,7 @@ Config.prototype.getConfig = function (filePath) {

config = this.mergeConfigs(Object.create(this.baseConfig), envConfig);
config = this.mergeConfigs(config, userConfig);
config = this.mergeConfigs(config, { globals: this.globals });

this.cache[directory] = config;

Expand Down
4 changes: 4 additions & 0 deletions lib/options.js
Expand Up @@ -57,5 +57,9 @@ module.exports = optionator({
option: "env",
type: "[String]",
description: "Specify one or more comma-separated environments."
}, {
option: "globals",
type: "String",
description: "Define global variables."
}]
});
2 changes: 2 additions & 0 deletions tests/fixtures/undef.js
@@ -0,0 +1,2 @@
var bar = baz;
bat = 42;
16 changes: 16 additions & 0 deletions tests/lib/cli.js
Expand Up @@ -277,4 +277,20 @@ describe("cli", function() {
assert.equal(console.log.args[0][0].split("\n").length, 14);
});
});

describe("when executing with globals flag", function () {
it("should default defined variables to read-only", function () {
var exit = cli.execute("--globals baz,bat ./tests/fixtures/undef.js");

assert.isTrue(console.log.calledOnce);
assert.equal(exit, 1);
});

it("should allow defining writable global variables", function () {
var exit = cli.execute("--globals baz:false,bat:true ./tests/fixtures/undef.js");

assert.isTrue(console.log.notCalled);
assert.equal(exit, 0);
});
});
});

0 comments on commit cc595d7

Please sign in to comment.