-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
On my project, this makes ESLint as a whole roughly 8% faster, and it seems to result in consistently faster benchmarks. Note a behavior change - member expressions used to find the first matching rule, while they now favor the more general rule if relevant.
- Loading branch information
Showing
4 changed files
with
100 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { AstMetadataApiWithTargetsResolver } from "./types"; | ||
|
||
// https://stackoverflow.com/q/49752151/25507 | ||
type KeysOfType<T, TProp> = keyof T & | ||
{ [P in keyof T]: T[P] extends TProp ? P : never }[keyof T]; | ||
|
||
export type RulesLookup = Map< | ||
string | undefined, | ||
AstMetadataApiWithTargetsResolver | ||
>; | ||
|
||
export function lookupKey(...args: Array<string | null | undefined>) { | ||
return args.map((i) => (i == null ? null : i)).join("\0"); | ||
} | ||
|
||
export function makeLookup( | ||
rules: AstMetadataApiWithTargetsResolver[], | ||
...keys: Array< | ||
KeysOfType<Required<AstMetadataApiWithTargetsResolver>, string> | ||
> | ||
) { | ||
const lookup = new Map< | ||
string | undefined, | ||
AstMetadataApiWithTargetsResolver | ||
>(); | ||
// Iterate in inverse order to favor earlier rules in case of conflict. | ||
for (let i = rules.length - 1; i >= 0; i--) { | ||
const key = | ||
keys.length === 1 | ||
? rules[i][keys[0]] | ||
: lookupKey(...keys.map((k) => rules[i][k])); | ||
lookup.set(key, rules[i]); | ||
} | ||
return lookup; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters