Skip to content

Commit

Permalink
New: add --parser-options to CLI (fixes #5495)
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Mar 10, 2016
1 parent 13c440f commit 13ef1c7
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 1 deletion.
10 changes: 10 additions & 0 deletions docs/user-guide/command-line-interface.md
Expand Up @@ -31,6 +31,7 @@ Basic configuration:
--ext [String] Specify JavaScript file extensions - default: .js
--global [String] Define global variables
--parser String Specify the parser to be used - default: espree
--parser-options Object Specify parser options
Caching:
--cache Only check changed files - default: false
Expand Down Expand Up @@ -147,6 +148,15 @@ Examples:

This option allows you to specify a parser to be used by eslint. By default, `espree` will be used.

#### `--parser-options`

This option allows you to specify parser options to be used by eslint.

Examples:

echo '3 ** 4' | eslint --stdin --parser-options=ecmaVersion:6 # will fail with a parsing error
echo '3 ** 4' | eslint --stdin --parser-options=ecmaVersion:7 # succeeds, yay!

### Caching

#### `--cache`
Expand Down
1 change: 1 addition & 0 deletions lib/cli.js
Expand Up @@ -51,6 +51,7 @@ function translateOptions(cliOptions) {
rulePaths: cliOptions.rulesdir,
useEslintrc: cliOptions.eslintrc,
parser: cliOptions.parser,
parserOptions: cliOptions.parserOptions,
cache: cliOptions.cache,
cacheFile: cliOptions.cacheFile,
cacheLocation: cliOptions.cacheLocation,
Expand Down
3 changes: 2 additions & 1 deletion lib/config.js
Expand Up @@ -166,6 +166,7 @@ function Config(options) {
this.ignorePath = options.ignorePath;
this.cache = {};
this.parser = options.parser;
this.parserOptions = options.parserOptions || {};

this.baseConfig = options.baseConfig ? loadConfig(options.baseConfig) : { rules: {} };

Expand Down Expand Up @@ -226,7 +227,7 @@ Config.prototype.getConfig = function(filePath) {
}

// Step 2: Create a copy of the baseConfig
config = ConfigOps.merge({parser: this.parser}, this.baseConfig);
config = ConfigOps.merge({parser: this.parser, parserOptions: this.parserOptions}, this.baseConfig);

// Step 3: Merge in the user-specified configuration from .eslintrc and package.json
config = ConfigOps.merge(config, userConfig);
Expand Down
5 changes: 5 additions & 0 deletions lib/options.js
Expand Up @@ -60,6 +60,11 @@ module.exports = optionator({
default: "espree",
description: "Specify the parser to be used"
},
{
option: "parser-options",
type: "Object",
description: "Specify parser options"
},
{
heading: "Caching"
},
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/passing-es7.js
@@ -0,0 +1,4 @@
var foo = "bar";
if (foo) {
foo = 3 ** 4;
}
30 changes: 30 additions & 0 deletions tests/lib/cli.js
Expand Up @@ -612,6 +612,36 @@ describe("cli", function() {
});
});

describe("when given parser options", function() {
it("should exit with error if parser options are invalid", function() {
var filePath = getFixturePath("passing.js");
var exit = cli.execute("--no-ignore --parser-options test111 " + filePath);

assert.equal(exit, 1);
});

it("should exit with no error if parser is valid", function() {
var filePath = getFixturePath("passing.js");
var exit = cli.execute("--no-ignore --parser-options=ecmaVersion:6 " + filePath);

assert.equal(exit, 0);
});

it("should exit with an error on ecmaVersion 7 feature in ecmaVersion 6", function() {
var filePath = getFixturePath("passing-es7.js");
var exit = cli.execute("--no-ignore --parser-options=ecmaVersion:6 " + filePath);

assert.equal(exit, 1);
});

it("should exit with no error on ecmaVersion 7 feature in ecmaVersion 7", function() {
var filePath = getFixturePath("passing-es7.js");
var exit = cli.execute("--no-ignore --parser-options=ecmaVersion:7 " + filePath);

assert.equal(exit, 0);
});
});

describe("when given the max-warnings flag", function() {
it("should not change exit code if warning count under threshold", function() {
var filePath = getFixturePath("max-warnings"),
Expand Down

0 comments on commit 13ef1c7

Please sign in to comment.