Skip to content
Permalink
Browse files

New: provide config.parser via `parserName` on RuleContext (fixes #3670)

  • Loading branch information...
benmosher committed Dec 16, 2015
1 parent 3b9fe1f commit bbf7f27565f2a9516d4a0851474614e1fa4ae5ca
Showing with 41 additions and 3 deletions.
  1. +1 −0 docs/developer-guide/working-with-rules.md
  2. +1 −1 lib/eslint.js
  3. +3 −1 lib/rule-context.js
  4. +35 −0 tests/lib/eslint.js
  5. +1 −1 tests/lib/rule-context.js
@@ -73,6 +73,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.

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

@@ -728,7 +728,7 @@ module.exports = (function() {
try {
rule = ruleCreator(new RuleContext(
key, api, severity, options,
config.settings, config.parserOptions
config.settings, config.parserOptions, config.parser
));

// add all the node types as listeners
@@ -70,13 +70,15 @@ 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.
*/
function RuleContext(ruleId, eslint, severity, options, settings, parserOptions) {
function RuleContext(ruleId, eslint, severity, options, settings, parserOptions, parserName) {
// public.
this.id = ruleId;
this.options = options;
this.settings = settings;
this.parserOptions = parserOptions;
this.parserName = parserName;

// private.
this.eslint = eslint;
@@ -1083,6 +1083,41 @@ describe("eslint", function() {

});

describe("when config has parser", function() {

// custom parser unsupported in browser, only test in Node
if (typeof window === "undefined") {

This comment has been minimized.

Copy link
@benmosher

benmosher Dec 21, 2015

Author Contributor

Had to copy this trick from the bottom of the file for this test... passes in Node, fails in browser.

Is this actually a bug? Or just a quirk of running in the browser? the existence of the bottom bit seemed to imply the latter.

This comment has been minimized.

Copy link
@nzakas

nzakas Dec 21, 2015

Member

Sorry, I'm not following. What are you asking?

This comment has been minimized.

Copy link
@benmosher

benmosher via email Dec 21, 2015

Author Contributor

This comment has been minimized.

Copy link
@nzakas

nzakas Dec 21, 2015

Member

Ah yes. The browser cannot use custom parsers, so it will fail.

it("should pass parser as parserName 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})
).returns({}));

var config = { rules: { "test-rule": 2 }, parser: alternateParser };

eslint.verify("0", config, filename);
});
}

it("should pass parser as parserName 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})
).returns({}));

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

eslint.verify("0", config, filename);
});

});


describe("when passing in configuration values for rules", function() {
var code = "var answer = 6 * 7";
@@ -26,7 +26,7 @@ describe("RuleContext", function() {

beforeEach(function() {
eslint = leche.fake(realESLint);
ruleContext = new RuleContext("fake-rule", eslint, 2, {}, {}, {});
ruleContext = new RuleContext("fake-rule", eslint, 2, {}, {}, {}, "espree");
});

describe("old-style call with location", function() {

0 comments on commit bbf7f27

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