feat: refactoring project
This commit is contained in:
63
node_modules/webpack/lib/rules/RuleSetCompiler.js
generated
vendored
63
node_modules/webpack/lib/rules/RuleSetCompiler.js
generated
vendored
@@ -7,7 +7,10 @@
|
||||
|
||||
const { SyncHook } = require("tapable");
|
||||
|
||||
/** @typedef {function(string): boolean} RuleConditionFunction */
|
||||
/** @typedef {import("../../declarations/WebpackOptions").RuleSetRule} RuleSetRule */
|
||||
/** @typedef {import("../../declarations/WebpackOptions").RuleSetRules} RuleSetRules */
|
||||
|
||||
/** @typedef {function(string | EffectData): boolean} RuleConditionFunction */
|
||||
|
||||
/**
|
||||
* @typedef {object} RuleCondition
|
||||
@@ -22,10 +25,14 @@ const { SyncHook } = require("tapable");
|
||||
* @property {RuleConditionFunction} fn
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Record<string, TODO>} EffectData
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {object} CompiledRule
|
||||
* @property {RuleCondition[]} conditions
|
||||
* @property {(Effect|function(object): Effect[])[]} effects
|
||||
* @property {(Effect|function(EffectData): Effect[])[]} effects
|
||||
* @property {CompiledRule[]=} rules
|
||||
* @property {CompiledRule[]=} oneOf
|
||||
*/
|
||||
@@ -39,13 +46,18 @@ const { SyncHook } = require("tapable");
|
||||
/**
|
||||
* @typedef {object} RuleSet
|
||||
* @property {Map<string, any>} references map of references in the rule set (may grow over time)
|
||||
* @property {function(object): Effect[]} exec execute the rule set
|
||||
* @property {function(EffectData): Effect[]} exec execute the rule set
|
||||
*/
|
||||
|
||||
/** @typedef {{ apply: (function(RuleSetCompiler): void) }} RuleSetPlugin */
|
||||
|
||||
class RuleSetCompiler {
|
||||
/**
|
||||
* @param {RuleSetPlugin[]} plugins plugins
|
||||
*/
|
||||
constructor(plugins) {
|
||||
this.hooks = Object.freeze({
|
||||
/** @type {SyncHook<[string, object, Set<string>, CompiledRule, Map<string, any>]>} */
|
||||
/** @type {SyncHook<[string, RuleSetRule, Set<string>, CompiledRule, Map<string | undefined, any>]>} */
|
||||
rule: new SyncHook([
|
||||
"path",
|
||||
"rule",
|
||||
@@ -62,7 +74,7 @@ class RuleSetCompiler {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {object[]} ruleSet raw user provided rules
|
||||
* @param {TODO[]} ruleSet raw user provided rules
|
||||
* @returns {RuleSet} compiled RuleSet
|
||||
*/
|
||||
compile(ruleSet) {
|
||||
@@ -70,7 +82,7 @@ class RuleSetCompiler {
|
||||
const rules = this.compileRules("ruleSet", ruleSet, refs);
|
||||
|
||||
/**
|
||||
* @param {object} data data passed in
|
||||
* @param {EffectData} data data passed in
|
||||
* @param {CompiledRule} rule the compiled rule
|
||||
* @param {Effect[]} effects an array where effects are pushed to
|
||||
* @returns {boolean} true, if the rule has matched
|
||||
@@ -79,6 +91,7 @@ class RuleSetCompiler {
|
||||
for (const condition of rule.conditions) {
|
||||
const p = condition.property;
|
||||
if (Array.isArray(p)) {
|
||||
/** @type {EffectData | string | undefined} */
|
||||
let current = data;
|
||||
for (const subProperty of p) {
|
||||
if (
|
||||
@@ -147,25 +160,33 @@ class RuleSetCompiler {
|
||||
|
||||
/**
|
||||
* @param {string} path current path
|
||||
* @param {object[]} rules the raw rules provided by user
|
||||
* @param {RuleSetRules} rules the raw rules provided by user
|
||||
* @param {Map<string, any>} refs references
|
||||
* @returns {CompiledRule[]} rules
|
||||
*/
|
||||
compileRules(path, rules, refs) {
|
||||
return rules
|
||||
.filter(Boolean)
|
||||
.map((rule, i) => this.compileRule(`${path}[${i}]`, rule, refs));
|
||||
.map((rule, i) =>
|
||||
this.compileRule(
|
||||
`${path}[${i}]`,
|
||||
/** @type {RuleSetRule} */ (rule),
|
||||
refs
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} path current path
|
||||
* @param {object} rule the raw rule provided by user
|
||||
* @param {RuleSetRule} rule the raw rule provided by user
|
||||
* @param {Map<string, any>} refs references
|
||||
* @returns {CompiledRule} normalized and compiled rule for processing
|
||||
*/
|
||||
compileRule(path, rule, refs) {
|
||||
const unhandledProperties = new Set(
|
||||
Object.keys(rule).filter(key => rule[key] !== undefined)
|
||||
Object.keys(rule).filter(
|
||||
key => rule[/** @type {keyof RuleSetRule} */ (key)] !== undefined
|
||||
)
|
||||
);
|
||||
|
||||
/** @type {CompiledRule} */
|
||||
@@ -236,7 +257,7 @@ class RuleSetCompiler {
|
||||
matchWhenEmpty: condition(""),
|
||||
fn: condition
|
||||
};
|
||||
} catch (err) {
|
||||
} catch (_err) {
|
||||
throw this.error(
|
||||
path,
|
||||
condition,
|
||||
@@ -303,7 +324,7 @@ class RuleSetCompiler {
|
||||
const fn = matcher.fn;
|
||||
conditions.push({
|
||||
matchWhenEmpty: !matcher.matchWhenEmpty,
|
||||
fn: v => !fn(v)
|
||||
fn: /** @type {RuleConditionFunction} */ (v => !fn(v))
|
||||
});
|
||||
}
|
||||
break;
|
||||
@@ -337,12 +358,11 @@ class RuleSetCompiler {
|
||||
};
|
||||
} else if (conditions.length === 1) {
|
||||
return conditions[0];
|
||||
} else {
|
||||
return {
|
||||
matchWhenEmpty: conditions.some(c => c.matchWhenEmpty),
|
||||
fn: v => conditions.some(c => c.fn(v))
|
||||
};
|
||||
}
|
||||
return {
|
||||
matchWhenEmpty: conditions.some(c => c.matchWhenEmpty),
|
||||
fn: v => conditions.some(c => c.fn(v))
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -357,12 +377,11 @@ class RuleSetCompiler {
|
||||
};
|
||||
} else if (conditions.length === 1) {
|
||||
return conditions[0];
|
||||
} else {
|
||||
return {
|
||||
matchWhenEmpty: conditions.every(c => c.matchWhenEmpty),
|
||||
fn: v => conditions.every(c => c.fn(v))
|
||||
};
|
||||
}
|
||||
return {
|
||||
matchWhenEmpty: conditions.every(c => c.matchWhenEmpty),
|
||||
fn: v => conditions.every(c => c.fn(v))
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user