Skip to content
Permalink
Browse files

Update: Allow parser to be relative to config (fixes #4985)

  • Loading branch information...
nzakas committed Jan 21, 2016
1 parent cf3a511 commit 505f1a6234099ed8d10f73d377888f1482fe8418
@@ -1,4 +1,4 @@
node_modules/
/node_modules
test.js
coverage/
build/
@@ -11,4 +11,4 @@ versions.json
*.iml
.eslintcache
.cache
**/node_modules
/packages/**/node_modules
@@ -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:

@@ -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");

@@ -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);

@@ -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.
@@ -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",
@@ -0,0 +1,6 @@
module.exports = {
parser: "foo",
rules: {
semi: [2, "always"]
}
};
@@ -0,0 +1,6 @@
module.exports = {
parser: "./not-a-config",
rules: {
semi: [2, "always"]
}
};

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1 @@
"not a config";
@@ -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, {
@@ -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 };
@@ -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 } };

0 comments on commit 505f1a6

Please sign in to comment.
You can’t perform that action at this time.