1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | /** @typedef {import("./RuleSetCompiler")} RuleSetCompiler */
|
---|
9 | /** @typedef {import("./RuleSetCompiler").RuleCondition} RuleCondition */
|
---|
10 |
|
---|
11 | class BasicMatcherRulePlugin {
|
---|
12 | constructor(ruleProperty, dataProperty, invert) {
|
---|
13 | this.ruleProperty = ruleProperty;
|
---|
14 | this.dataProperty = dataProperty || ruleProperty;
|
---|
15 | this.invert = invert || false;
|
---|
16 | }
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * @param {RuleSetCompiler} ruleSetCompiler the rule set compiler
|
---|
20 | * @returns {void}
|
---|
21 | */
|
---|
22 | apply(ruleSetCompiler) {
|
---|
23 | ruleSetCompiler.hooks.rule.tap(
|
---|
24 | "BasicMatcherRulePlugin",
|
---|
25 | (path, rule, unhandledProperties, result) => {
|
---|
26 | if (unhandledProperties.has(this.ruleProperty)) {
|
---|
27 | unhandledProperties.delete(this.ruleProperty);
|
---|
28 | const value = rule[this.ruleProperty];
|
---|
29 | const condition = ruleSetCompiler.compileCondition(
|
---|
30 | `${path}.${this.ruleProperty}`,
|
---|
31 | value
|
---|
32 | );
|
---|
33 | const fn = condition.fn;
|
---|
34 | result.conditions.push({
|
---|
35 | property: this.dataProperty,
|
---|
36 | matchWhenEmpty: this.invert
|
---|
37 | ? !condition.matchWhenEmpty
|
---|
38 | : condition.matchWhenEmpty,
|
---|
39 | fn: this.invert ? v => !fn(v) : fn
|
---|
40 | });
|
---|
41 | }
|
---|
42 | }
|
---|
43 | );
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | module.exports = BasicMatcherRulePlugin;
|
---|