Skip to content

Commit 9ca9fbb

Browse files
committed
Simplify normalizer
1 parent e2ddfc4 commit 9ca9fbb

File tree

1 file changed

+24
-30
lines changed

1 file changed

+24
-30
lines changed

lib/src/normalizer.ts

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import { globalLogger } from "./logger";
88
import { Matcher, basicMatcher, loadMatcher } from "./matcher";
9-
import { InbuiltMergeStrategies, Config, RuleTree, StrategyFn } from "./types";
9+
import { InbuiltMergeStrategies, Config, RuleTree } from "./types";
1010

1111
export interface StrategyItem {
1212
name: string;
@@ -26,15 +26,11 @@ export interface NormalizedRules {
2626
default: StrategyItem[];
2727
}
2828

29-
export interface NormalizedConfig {
29+
export interface NormalizedConfig<T extends string = InbuiltMergeStrategies, TContext = unknown>
30+
extends Omit<Config<T, TContext>, "byStrategy" | "rules" | "defaultStrategy"> {
3031
rules: NormalizedRules;
3132
matcher: Matcher;
3233
fileFilter: (filepath: string) => boolean;
33-
customStrategies: Record<string, StrategyFn>;
34-
includeNonConflicted: boolean;
35-
debug: boolean;
36-
strictArrays: boolean;
37-
writeConflictSidecar: boolean;
3834
}
3935

4036
/** Defaults */
@@ -53,29 +49,30 @@ export const DEFAULT_CONFIG = {
5349
export const normalizeConfig = async <T extends string = InbuiltMergeStrategies>(
5450
config: Config<T>,
5551
): Promise<NormalizedConfig> => {
56-
const userConfig = { ...DEFAULT_CONFIG, ...config };
52+
const { rules, byStrategy, defaultStrategy, matcher, ...userConfig } = {
53+
...DEFAULT_CONFIG,
54+
...config,
55+
};
5756

58-
const rules: NormalizedRules = {
57+
const normalizedRules: NormalizedRules = {
5958
exact: Object.create(null),
6059
exactFields: Object.create(null),
6160
patterns: Object.create(null),
62-
default: normalizeDefault(userConfig.defaultStrategy),
61+
default: normalizeDefault(defaultStrategy),
6362
};
6463

65-
const matcher =
66-
typeof userConfig.matcher === "string"
67-
? await loadMatcher(userConfig.matcher)
68-
: (userConfig.matcher ?? basicMatcher);
64+
const resolvedMatcher =
65+
typeof matcher === "string" ? await loadMatcher(matcher) : (matcher ?? basicMatcher);
6966

7067
let order = 0;
7168

72-
if (userConfig.byStrategy) {
73-
for (const [rawStrategy, fields] of Object.entries(userConfig.byStrategy)) {
69+
if (byStrategy) {
70+
for (const [rawStrategy, fields] of Object.entries(byStrategy)) {
7471
if (!fields) continue;
7572
const { name, important: strategyImportant } = parseStrategyName(rawStrategy);
7673
for (const raw of fields as string[]) {
7774
const { key, important: keyImportant } = parseImportance(raw);
78-
addRule(rules, key, {
75+
addRule(normalizedRules, key, {
7976
strategies: [{ name, important: strategyImportant || keyImportant }],
8077
order: order++,
8178
source: key,
@@ -84,14 +81,14 @@ export const normalizeConfig = async <T extends string = InbuiltMergeStrategies>
8481
}
8582
}
8683

87-
if (userConfig.rules) {
88-
expandRuleTree(userConfig.rules, [], (pathKey, strategyNames) => {
84+
if (rules) {
85+
expandRuleTree(rules, [], (pathKey, strategyNames) => {
8986
const { key, important: keyImportant } = parseImportance(pathKey);
9087
const strategies = strategyNames.map(s => {
9188
const { name, important } = parseStrategyName(s);
9289
return { name, important: important || keyImportant };
9390
});
94-
addRule(rules, key, {
91+
addRule(normalizedRules, key, {
9592
strategies,
9693
order: order++,
9794
source: key,
@@ -103,20 +100,17 @@ export const normalizeConfig = async <T extends string = InbuiltMergeStrategies>
103100
if (config.debug) {
104101
globalLogger.info("all", `[normalizer] Filtering ${filepath}`);
105102
}
106-
if (!matcher.isMatch(filepath, userConfig.include)) return false;
107-
if (matcher.isMatch(filepath, [...userConfig.exclude, "**/node_modules/**"])) return false;
103+
if (!resolvedMatcher.isMatch(filepath, userConfig.include)) return false;
104+
if (resolvedMatcher.isMatch(filepath, [...userConfig.exclude, "**/node_modules/**"]))
105+
return false;
108106
return true;
109107
};
110108

111109
return {
112-
rules,
113-
matcher,
110+
...userConfig,
111+
rules: normalizedRules,
112+
matcher: resolvedMatcher,
114113
fileFilter,
115-
customStrategies: userConfig.customStrategies ?? {},
116-
includeNonConflicted: userConfig.includeNonConflicted ?? false,
117-
debug: userConfig.debug ?? false,
118-
strictArrays: userConfig.strictArrays ?? false,
119-
writeConflictSidecar: userConfig.writeConflictSidecar ?? false,
120114
};
121115
};
122116

@@ -175,7 +169,7 @@ const addRule = (target: NormalizedRules, key: string, entry: StrategyList) => {
175169
return;
176170
}
177171

178-
const hasWildcards = /[*?\[\]]/.test(key);
172+
const hasWildcards = /[*?[\]]/.test(key);
179173
if (hasWildcards) {
180174
push(target.patterns, key, entry);
181175
return;

0 commit comments

Comments
 (0)