Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
Update: Allow parser to be relative to config (fixes eslint#4985)
Browse files Browse the repository at this point in the history
  • Loading branch information
nzakas committed Jan 22, 2016
1 parent cf3a511 commit 505f1a6
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
node_modules/
/node_modules
test.js
coverage/
build/
Expand All @@ -11,4 +11,4 @@ versions.json
*.iml
.eslintcache
.cache
**/node_modules
/packages/**/node_modules
2 changes: 1 addition & 1 deletion docs/developer-guide/working-with-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ The `context` object contains additional functionality that is helpful for rules
* `id` - the rule ID.
* `options` - an array of rule options.
* `settings` - the `settings` from configuration.
* `parserName` - the `parser` from configuration.
* `parserPath` - the full path to the `parser` from configuration.

Additionally, the `context` object has the following methods:

Expand Down
9 changes: 9 additions & 0 deletions lib/config/config-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var debug = require("debug"),
ConfigOps = require("./config-ops"),
validator = require("./config-validator"),
Plugins = require("./plugins"),
resolveModule = require("resolve"),
stripComments = require("strip-json-comments"),
isAbsolutePath = require("path-is-absolute");

Expand Down Expand Up @@ -395,6 +396,14 @@ function load(filePath, applyEnvironments) {
Plugins.loadAll(config.plugins);
}

// include full path of parser if present
if (config.parser) {
config.parser = resolveModule.sync(config.parser, {
// be careful of https://github.com/substack/node-resolve/issues/78
basedir: path.dirname(path.resolve(filePath))
});
}

// validate the configuration before continuing
validator.validate(config, filePath);

Expand Down
6 changes: 3 additions & 3 deletions lib/rule-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@ var PASSTHROUGHS = [
* @param {Array} options The configuration information to be added to the rule.
* @param {Object} settings The configuration settings passed from the config file.
* @param {Object} parserOptions The parserOptions settings passed from the config file.
* @param {Object} parserName The parser setting passed from the config file.
* @param {Object} parserPath The parser setting passed from the config file.
* @param {Object} meta The metadata of the rule
*/
function RuleContext(ruleId, eslint, severity, options, settings, parserOptions, parserName, meta) {
function RuleContext(ruleId, eslint, severity, options, settings, parserOptions, parserPath, meta) {
// public.
this.id = ruleId;
this.options = options;
this.settings = settings;
this.parserOptions = parserOptions;
this.parserName = parserName;
this.parserPath = parserPath;
this.meta = meta;

// private.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"optionator": "^0.8.1",
"path-is-absolute": "^1.0.0",
"path-is-inside": "^1.0.1",
"resolve": "^1.1.6",
"shelljs": "^0.5.3",
"strip-json-comments": "~1.0.1",
"text-table": "~0.2.0",
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/config-file/js/.eslintrc.parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
parser: "foo",
rules: {
semi: [2, "always"]
}
};
6 changes: 6 additions & 0 deletions tests/fixtures/config-file/js/.eslintrc.parser2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
parser: "./not-a-config",
rules: {
semi: [2, "always"]
}
};
1 change: 1 addition & 0 deletions tests/fixtures/config-file/js/node_modules/foo/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/fixtures/config-file/js/not-a-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"not a config";
26 changes: 26 additions & 0 deletions tests/lib/config/config-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,32 @@ describe("ConfigFile", function() {
}, /Cannot read config file/);
});

it("should interpret parser module name when present in a JavaScript file", function() {
var config = ConfigFile.load(getFixturePath("js/.eslintrc.parser.js"));
assert.deepEqual(config, {
parser: path.resolve(getFixturePath("js/node_modules/foo/index.js")),
parserOptions: {},
env: {},
globals: {},
rules: {
semi: [2, "always"]
}
});
});

it("should interpret parser path when present in a JavaScript file", function() {
var config = ConfigFile.load(getFixturePath("js/.eslintrc.parser2.js"));
assert.deepEqual(config, {
parser: path.resolve(getFixturePath("js/not-a-config.js")),
parserOptions: {},
env: {},
globals: {},
rules: {
semi: [2, "always"]
}
});
});

it("should load information from a JSON file", function() {
var config = ConfigFile.load(getFixturePath("json/.eslintrc.json"));
assert.deepEqual(config, {
Expand Down
8 changes: 4 additions & 4 deletions tests/lib/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -1087,13 +1087,13 @@ describe("eslint", function() {

// custom parser unsupported in browser, only test in Node
if (typeof window === "undefined") {
it("should pass parser as parserName to all rules when provided on config", function() {
it("should pass parser as parserPath to all rules when provided on config", function() {

var alternateParser = "esprima-fb";

eslint.reset();
eslint.defineRule("test-rule", sandbox.mock().withArgs(
sinon.match({parserName: alternateParser})
sinon.match({parserPath: alternateParser})
).returns({}));

var config = { rules: { "test-rule": 2 }, parser: alternateParser };
Expand All @@ -1102,13 +1102,13 @@ describe("eslint", function() {
});
}

it("should pass parser as parserName to all rules when default parser is used", function() {
it("should pass parser as parserPath to all rules when default parser is used", function() {

var DEFAULT_PARSER = eslint.defaults().parser;

eslint.reset();
eslint.defineRule("test-rule", sandbox.mock().withArgs(
sinon.match({parserName: DEFAULT_PARSER})
sinon.match({parserPath: DEFAULT_PARSER})
).returns({}));

var config = { rules: { "test-rule": 2 } };
Expand Down

0 comments on commit 505f1a6

Please sign in to comment.