source: imaps-frontend/node_modules/eslint/lib/linter/rules.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 2.3 KB
Line 
1/**
2 * @fileoverview Defines a storage for rules.
3 * @author Nicholas C. Zakas
4 * @author aladdin-add
5 */
6
7"use strict";
8
9//------------------------------------------------------------------------------
10// Requirements
11//------------------------------------------------------------------------------
12
13const builtInRules = require("../rules");
14
15//------------------------------------------------------------------------------
16// Helpers
17//------------------------------------------------------------------------------
18
19/**
20 * Normalizes a rule module to the new-style API
21 * @param {(Function|{create: Function})} rule A rule object, which can either be a function
22 * ("old-style") or an object with a `create` method ("new-style")
23 * @returns {{create: Function}} A new-style rule.
24 */
25function normalizeRule(rule) {
26 return typeof rule === "function" ? Object.assign({ create: rule }, rule) : rule;
27}
28
29//------------------------------------------------------------------------------
30// Public Interface
31//------------------------------------------------------------------------------
32
33/**
34 * A storage for rules.
35 */
36class Rules {
37 constructor() {
38 this._rules = Object.create(null);
39 }
40
41 /**
42 * Registers a rule module for rule id in storage.
43 * @param {string} ruleId Rule id (file name).
44 * @param {Function} ruleModule Rule handler.
45 * @returns {void}
46 */
47 define(ruleId, ruleModule) {
48 this._rules[ruleId] = normalizeRule(ruleModule);
49 }
50
51 /**
52 * Access rule handler by id (file name).
53 * @param {string} ruleId Rule id (file name).
54 * @returns {{create: Function, schema: JsonSchema[]}}
55 * A rule. This is normalized to always have the new-style shape with a `create` method.
56 */
57 get(ruleId) {
58 if (typeof this._rules[ruleId] === "string") {
59 this.define(ruleId, require(this._rules[ruleId]));
60 }
61 if (this._rules[ruleId]) {
62 return this._rules[ruleId];
63 }
64 if (builtInRules.has(ruleId)) {
65 return builtInRules.get(ruleId);
66 }
67
68 return null;
69 }
70
71 *[Symbol.iterator]() {
72 yield* builtInRules;
73
74 for (const ruleId of Object.keys(this._rules)) {
75 yield [ruleId, this.get(ruleId)];
76 }
77 }
78}
79
80module.exports = Rules;
Note: See TracBrowser for help on using the repository browser.