Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3a431a2

Browse files
committed
feat: config ordering
1 parent d8967e1 commit 3a431a2

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

packages/eslint-plugin/src/rules/naming-convention.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ type Selector<TType extends Selectors> = FormatOptions &
6666
type NormalizedSelector<TType extends Selectors> = FormatOptions &
6767
SelectorBase<TType> & {
6868
filter: RegExp | null;
69+
// calculated ordering weight based on modifiers
70+
weight: number;
6971
};
7072

7173
// Note that this intentionally does not strictly type the modifiers/types properties.
@@ -617,6 +619,9 @@ function createValidator(
617619
context: Context,
618620
configs: Config[],
619621
): (node: TSESTree.Identifier | TSESTree.Literal) => void {
622+
// make sure the "highest priority" configs are checked first
623+
configs = [...configs].sort((a, b) => b.weight - a.weight);
624+
620625
return (
621626
node: TSESTree.Identifier | TSESTree.Literal,
622627
modifiers: Set<Modifiers> = new Set<Modifiers>(),
@@ -663,7 +668,8 @@ function createValidator(
663668
return;
664669
}
665670

666-
// it's valid for this config!
671+
// it's valid for this config, so we don't need to check any more configs
672+
return;
667673
}
668674
};
669675

@@ -946,12 +952,41 @@ function selectorTypeToMessageString(selectorType: Selectors): string {
946952
return notCamelCase.charAt(0).toUpperCase() + notCamelCase.slice(1);
947953
}
948954

955+
const ModifierWeight = ((): Readonly<
956+
Record<Modifiers | TypeModifiers, number>
957+
> => {
958+
let i = 0;
959+
return {
960+
// Modifiers
961+
readonly: 1 << i++,
962+
static: 1 << i++,
963+
public: 1 << i++,
964+
protected: 1 << i++,
965+
private: 1 << i++,
966+
abstract: 1 << i++,
967+
// TypeModifiers
968+
boolean: 1 << i++,
969+
string: 1 << i++,
970+
number: 1 << i++,
971+
function: 1 << i++,
972+
array: 1 << i++,
973+
};
974+
})();
949975
function normalizeOption<TType extends Selectors>(
950976
option: Selector<TType>,
951977
): NormalizedSelector<TType> {
978+
let weight = 0;
979+
option.modifiers?.forEach(mod => {
980+
weight |= ModifierWeight[mod];
981+
});
982+
option.types?.forEach(mod => {
983+
weight |= ModifierWeight[mod];
984+
});
985+
952986
return {
953987
...option,
954988
filter: option.filter !== undefined ? new RegExp(option.filter) : null,
989+
weight,
955990
};
956991
}
957992

0 commit comments

Comments
 (0)