|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
-
-
- "use strict";
-
-
-
-
-
- const lodash = require("lodash");
- const loadRules = require("./load-rules");
- const ruleReplacements = require("../conf/replacements").rules;
-
-
-
-
-
-
- const createMissingRule = lodash.memoize(ruleId => {
- const message = Object.prototype.hasOwnProperty.call(ruleReplacements, ruleId)
- ? `Rule '${ruleId}' was removed and replaced by: ${ruleReplacements[ruleId].join(", ")}`
- : `Definition for rule '${ruleId}' was not found`;
-
- return {
- create: context => ({
- Program() {
- context.report({
- loc: { line: 1, column: 0 },
- message
- });
- }
- })
- };
- });
-
-
- function normalizeRule(rule) {
- return typeof rule === "function" ? Object.assign({ create: rule }, rule) : rule;
- }
-
-
-
-
-
- class Rules {
- constructor() {
- this._rules = Object.create(null);
-
- this.load();
- }
-
-
-
- define(ruleId, ruleModule) {
- this._rules[ruleId] = normalizeRule(ruleModule);
- }
-
-
-
- load(rulesDir, cwd) {
- const newRules = loadRules(rulesDir, cwd);
-
- Object.keys(newRules).forEach(ruleId => {
- this.define(ruleId, newRules[ruleId]);
- });
- }
-
-
-
- importPlugin(plugin, pluginName) {
- if (plugin.rules) {
- Object.keys(plugin.rules).forEach(ruleId => {
- const qualifiedRuleId = `${pluginName}/${ruleId}`,
- rule = plugin.rules[ruleId];
-
- this.define(qualifiedRuleId, rule);
- });
- }
- }
-
-
-
- get(ruleId) {
- if (!Object.prototype.hasOwnProperty.call(this._rules, ruleId)) {
- return createMissingRule(ruleId);
- }
- if (typeof this._rules[ruleId] === "string") {
- return normalizeRule(require(this._rules[ruleId]));
- }
- return this._rules[ruleId];
-
- }
-
-
-
- getAllLoadedRules() {
- const allRules = new Map();
-
- Object.keys(this._rules).forEach(name => {
- const rule = this.get(name);
-
- allRules.set(name, rule);
- });
- return allRules;
- }
- }
-
- module.exports = Rules;
|