Skip to content

Commit f355382

Browse files
committed
feat(worker): remove, log invalid rules set via settings.json#commitlint.config.extend.rules
fixes joshbolduc#793 This gracefully handles invalid configurations set via the extension's configuration. Previously, commitlint's Lint function would throw an error if our extendRules contained rules that do not satisfy commitlint's `AnyRuleConfig<RuleConfigQuality.Qualified>`.
1 parent 0eee374 commit f355382

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/worker/index.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,57 @@ if (parentPort) {
5858
}:\n${JSON.stringify(config.rules)}`,
5959
);
6060

61+
if (typeof message.extendsRules === 'object' && message.extendsRules !== null) {
62+
/** @template T The type of the rule value. If valid, it will be `QualifiedRules | undefined` */
63+
type AnyInvalidRuleValue<T> = T extends typeof message.extendsRules[string]
64+
? never
65+
: T;
66+
const invalidRules: Record<string, AnyInvalidRuleValue<unknown>> = {};
67+
const isInvalidRule = (entry: [string, unknown]): entry is [string, AnyInvalidRuleValue<unknown>] => {
68+
if (!Array.isArray(entry[1])) {
69+
return true;
70+
}
71+
72+
const isInvalid = true,
73+
isValid = false,
74+
maybeRuleValue: readonly unknown[] = Object.freeze(entry[1]);
75+
76+
/* disabled*/
77+
if (maybeRuleValue.length === 1 && maybeRuleValue[0] === 0) {
78+
return isValid;
79+
}
80+
81+
/* warning or error w/ optional value */
82+
if (maybeRuleValue.length === 2 || maybeRuleValue.length === 3
83+
&& (maybeRuleValue[0] === 1 || maybeRuleValue[0] === 2)
84+
&& (maybeRuleValue[1] === 'always' || maybeRuleValue[1] === 'never')
85+
) {
86+
// Type cast because typescript still thinks it's unknown[] despite narrowing.
87+
// Reminder: this line will be reduced to `(maybeRuleValue);` after build.
88+
(maybeRuleValue as [(1 | 2), ('always' | 'never')] | [(1 | 2), ('always' | 'never'), unknown]) satisfies typeof message.extendsRules[string];
89+
return isValid;
90+
}
91+
92+
invalidRules[entry[0]] = maybeRuleValue;
93+
return isInvalid;
94+
}
95+
96+
// remove some invalid rules from client's rules (non-thorough). The
97+
// rules of a bad config (e.g. in settings.json) may contain non-rule
98+
// values. This should be _loudly_ reported to the user. Although
99+
// commitlint would report it, it won't denote the config the invalid
100+
// rules came from. We must do it, instead.
101+
message.extendsRules = Object.fromEntries(
102+
Object.entries(message.extendsRules)
103+
.filter((entry) => !isInvalidRule(entry))
104+
) satisfies typeof message.extendsRules;
105+
106+
if (Object.keys(invalidRules).length > 0) {
107+
// TODO: change to warn(...) after implementing multi-LogLevel logging in IPC API. See https://www.npmjs.com/package/@vscode-logging/logger
108+
log(`One or more rules configured in user- or workspace-scoped settings.json#commitlint.config.extend.rules are invalid!\n${JSON.stringify(invalidRules, undefined, 2)}`);
109+
}
110+
}
111+
61112
return {
62113
...config,
63114
rules: { ...message.extendsRules, ...config.rules },

0 commit comments

Comments
 (0)