2 * @fileoverview `Map` to load rules lazily.
3 * @author Toru Nagashima <https://github.com/mysticatea>
7 const debug = require("debug")("eslint:rules");
9 /** @typedef {import("./types").Rule} Rule */
12 * The `Map` object that loads each rule when it's accessed.
14 * const rules = new LazyLoadingRuleMap([
15 * ["eqeqeq", () => require("eqeqeq")],
16 * ["semi", () => require("semi")],
17 * ["no-unused-vars", () => require("no-unused-vars")],
20 * rules.get("semi") // call `() => require("semi")` here.
22 * @extends {Map<string, () => Rule>}
24 class LazyLoadingRuleMap extends Map {
27 * Initialize this map.
28 * @param {Array<[string, function(): Rule]>} loaders The rule loaders.
30 constructor(loaders) {
31 let remaining = loaders.length;
35 ? loaders.map(([ruleId, load]) => {
42 debug("Loading rule %o (remaining=%d)", ruleId, --remaining);
52 // `super(...iterable)` uses `this.set()`, so disable it here.
53 Object.defineProperty(LazyLoadingRuleMap.prototype, "set", {
61 * Each rule will be loaded on the first access.
62 * @param {string} ruleId The rule ID to get.
63 * @returns {Rule|undefined} The rule.
66 const load = super.get(ruleId);
68 return load && load();
73 * @returns {IterableIterator<Rule>} Rules.
76 for (const load of super.values()) {
83 * @returns {IterableIterator<[string, Rule]>} Rules.
86 for (const [ruleId, load] of super.entries()) {
87 yield [ruleId, load()];
92 * Call a function with each rule.
93 * @param {Function} callbackFn The callback function.
94 * @param {any} [thisArg] The object to pass to `this` of the callback function.
97 forEach(callbackFn, thisArg) {
98 for (const [ruleId, load] of super.entries()) {
99 callbackFn.call(thisArg, load(), ruleId, this);
105 Object.defineProperties(LazyLoadingRuleMap.prototype, {
106 clear: { configurable: true, value: void 0 },
107 delete: { configurable: true, value: void 0 },
111 value: LazyLoadingRuleMap.prototype.entries
115 module.exports = { LazyLoadingRuleMap };