Skip to content

Commit

Permalink
Chore: always normalize rules to new API in rules.js (#9236)
Browse files Browse the repository at this point in the history
  • Loading branch information
not-an-aardvark committed Sep 5, 2017
1 parent c5f4227 commit 3c41a05
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 24 deletions.
17 changes: 8 additions & 9 deletions lib/linter.js
Expand Up @@ -877,7 +877,7 @@ class Linter {
return;
}

const ruleCreator = this.rules.get(ruleId);
const rule = this.rules.get(ruleId);
let reportTranslator = null;
const ruleContext = Object.freeze(
Object.assign(
Expand All @@ -902,7 +902,7 @@ class Linter {
}
const problem = reportTranslator.apply(null, arguments);

if (problem.fix && ruleCreator.meta && !ruleCreator.meta.fixable) {
if (problem.fix && rule.meta && !rule.meta.fixable) {
throw new Error("Fixable rules should export a `meta.fixable` property.");
}
problems.push(problem);
Expand Down Expand Up @@ -930,16 +930,15 @@ class Linter {
);

try {
const rule = ruleCreator.create
? ruleCreator.create(ruleContext)
: ruleCreator(ruleContext);
const ruleListeners = rule.create(ruleContext);

// add all the selectors from the rule as listeners
Object.keys(rule).forEach(selector => {
Object.keys(ruleListeners).forEach(selector => {
emitter.on(
selector, timing.enabled
? timing.time(ruleId, rule[selector])
: rule[selector]
selector,
timing.enabled
? timing.time(ruleId, ruleListeners[selector])
: ruleListeners[selector]
);
});
} catch (ex) {
Expand Down
17 changes: 14 additions & 3 deletions lib/rules.js
Expand Up @@ -42,6 +42,16 @@ const createMissingRule = lodash.memoize(ruleId => {
};
});

/**
* Normalizes a rule module to the new-style API
* @param {(Function|{create: Function})} rule A rule object, which can either be a function
* ("old-style") or an object with a `create` method ("new-style")
* @returns {{create: Function}} A new-style rule.
*/
function normalizeRule(rule) {
return typeof rule === "function" ? Object.assign({ create: rule }, rule) : rule;
}

//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------
Expand All @@ -60,7 +70,7 @@ class Rules {
* @returns {void}
*/
define(ruleId, ruleModule) {
this._rules[ruleId] = ruleModule;
this._rules[ruleId] = normalizeRule(ruleModule);
}

/**
Expand Down Expand Up @@ -97,14 +107,15 @@ class Rules {
/**
* Access rule handler by id (file name).
* @param {string} ruleId Rule id (file name).
* @returns {Function} Rule handler.
* @returns {{create: Function, schema: JsonSchema[]}}
* A rule. This is normalized to always have the new-style shape with a `create` method.
*/
get(ruleId) {
if (!Object.prototype.hasOwnProperty.call(this._rules, ruleId)) {
return createMissingRule(ruleId);
}
if (typeof this._rules[ruleId] === "string") {
return require(this._rules[ruleId]);
return normalizeRule(require(this._rules[ruleId]));
}
return this._rules[ruleId];

Expand Down
10 changes: 0 additions & 10 deletions lib/testers/rule-tester.js
Expand Up @@ -350,16 +350,6 @@ class RuleTester {
linter.rules.get = function(ruleId) {
const originalRule = originalGet.call(linter.rules, ruleId);

if (typeof originalRule === "function") {
return function(context) {
Object.freeze(context);
freezeDeeply(context.options);
freezeDeeply(context.settings);
freezeDeeply(context.parserOptions);

return originalRule(context);
};
}
return {
meta: originalRule.meta,
create(context) {
Expand Down
23 changes: 21 additions & 2 deletions tests/lib/rules.js
Expand Up @@ -50,6 +50,25 @@ describe("rules", () => {
rules.define(ruleId, {});
assert.ok(rules.get(ruleId));
});

it("should return the rule as an object with a create() method if the rule was defined as a function", () => {

/**
* A rule that does nothing
* @returns {void}
*/
function rule() {}
rule.schema = [];
rules.define("foo", rule);
assert.deepEqual(rules.get("foo"), { create: rule, schema: [] });
});

it("should return the rule as-is if it was defined as an object with a create() method", () => {
const rule = { create() {} };

rules.define("foo", rule);
assert.strictEqual(rules.get("foo"), rule);
});
});

describe("when a rule is not found", () => {
Expand Down Expand Up @@ -101,15 +120,15 @@ describe("rules", () => {
rules.importPlugin(customPlugin, pluginName);

assert.isDefined(rules.get("custom-plugin/custom-rule"));
assert.equal(rules.get("custom-plugin/custom-rule"), customPlugin.rules["custom-rule"]);
assert.equal(rules.get("custom-plugin/custom-rule").create, customPlugin.rules["custom-rule"]);
});

it("should return custom rules as part of getAllLoadedRules", () => {
rules.importPlugin(customPlugin, pluginName);

const allRules = rules.getAllLoadedRules();

assert.equal(allRules.get("custom-plugin/custom-rule"), customPlugin.rules["custom-rule"]);
assert.equal(allRules.get("custom-plugin/custom-rule").create, customPlugin.rules["custom-rule"]);
});
});

Expand Down

0 comments on commit 3c41a05

Please sign in to comment.