* Applies the plugin by registering its hooks on the compiler. * @param {RuleSetCompiler} ruleSetCompiler the rule set compiler * @returns {void}
(ruleSetCompiler)
| 28 | * @returns {void} |
| 29 | */ |
| 30 | apply(ruleSetCompiler) { |
| 31 | ruleSetCompiler.hooks.rule.tap( |
| 32 | PLUGIN_NAME, |
| 33 | (path, rule, unhandledProperties, result, references) => { |
| 34 | /** |
| 35 | * Processes the provided property. |
| 36 | * @param {keyof RuleSetRule} property property |
| 37 | * @param {string} correctProperty correct property |
| 38 | */ |
| 39 | const conflictWith = (property, correctProperty) => { |
| 40 | if (unhandledProperties.has(property)) { |
| 41 | throw ruleSetCompiler.error( |
| 42 | `${path}.${property}`, |
| 43 | rule[property], |
| 44 | `A Rule must not have a '${property}' property when it has a '${correctProperty}' property` |
| 45 | ); |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | if (unhandledProperties.has("use")) { |
| 50 | unhandledProperties.delete("use"); |
| 51 | unhandledProperties.delete("enforce"); |
| 52 | |
| 53 | conflictWith("loader", "use"); |
| 54 | conflictWith("options", "use"); |
| 55 | |
| 56 | const use = /** @type {RuleSetUse} */ (rule.use); |
| 57 | const enforce = rule.enforce; |
| 58 | |
| 59 | const type = |
| 60 | /** @type {EffectUseType} */ |
| 61 | (enforce ? `use-${enforce}` : "use"); |
| 62 | |
| 63 | /** |
| 64 | * Returns effect. |
| 65 | * @param {string} path options path |
| 66 | * @param {string} defaultIdent default ident when none is provided |
| 67 | * @param {RuleSetUseItem} item user provided use value |
| 68 | * @returns {(Effect | ((effectData: EffectData) => Effect[]))} effect |
| 69 | */ |
| 70 | const useToEffect = (path, defaultIdent, item) => { |
| 71 | if (typeof item === "function") { |
| 72 | return (data) => |
| 73 | useToEffectsWithoutIdent( |
| 74 | path, |
| 75 | /** @type {RuleSetUseItem | RuleSetUseItem[]} */ |
| 76 | (item(data)) |
| 77 | ); |
| 78 | } |
| 79 | return useToEffectRaw(path, defaultIdent, item); |
| 80 | }; |
| 81 | |
| 82 | /** |
| 83 | * Returns effect. |
| 84 | * @param {string} path options path |
| 85 | * @param {string} defaultIdent default ident when none is provided |
| 86 | * @param {Exclude<NonNullable<RuleSetUseItem>, RuleSetUseFunction>} item user provided use value |
| 87 | * @returns {Effect} effect |